Ejemplo n.º 1
0
class Shared:
    def __init__(self, n):
        self.N = n
        self.counter = 0
        self.fibonacci = [0, 1] + [0] * n
        self.threads = [0] * (n + 1)
        self.mutex = Mutex()
        self.event = Event()
        self.event.signal()
        for j in range(n + 1):
            self.threads[j] = Semaphore(0)
        self.threads[0].signal(1)

    def fnc_fibonacci_seq(self, pin):
        self.threads[pin].wait()
        self.fibonacci[pin + 2] = self.fibonacci[pin] + self.fibonacci[pin + 1]
        self.threads[pin + 1].signal()

    def fnc_fibonacci_event(self, pin):
        while True:
            self.event.wait()
            self.mutex.lock()
            if self.counter == pin:
                break
            self.mutex.unlock()
        self.mutex.unlock()
        self.mutex.lock()
        self.counter += 1
        self.mutex.unlock()
        self.fibonacci[pin + 2] = self.fibonacci[pin] + self.fibonacci[pin + 1]
        self.event.signal()
Ejemplo n.º 2
0
class Barrier:
    def __init__(self, n):
        self.n = n
        self.count = 0
        self.mutex = Mutex()
        self.event = Event()

    def wait(self):
        self.mutex.lock()
        self.count += 1
        if self.count == self.n:
            self.event.signal()
        self.mutex.unlock()
        self.event.wait()
Ejemplo n.º 3
0
class SimpleBarrier:
    def __init__(self, number_of_threads):
        self.number_of_threads = number_of_threads
        self.mutex = Mutex()
        self.event = Event()
        self.counter = 0

    def wait(self):
        self.mutex.lock()
        self.counter += 1
        if self.counter == self.number_of_threads:
            self.event.signal()
        self.mutex.unlock()
        self.event.wait()
Ejemplo n.º 4
0
class Barrier():
    def __init__(self, n):
        self.mutex = Mutex()
        self.event = Event()
        self.counter = n
        self.n = n

    def wait(self):
        self.mutex.lock()
        self.counter -= 1
        if(self.counter == 0):
            self.counter = self.n
            self.event.signal()
            self.mutex.unlock()
            return
        self.mutex.unlock()
        self.event.wait()
Ejemplo n.º 5
0
class ReusableBarrier:
    def __init__(self, numberOfThreads):
        self.numberOfThreads = numberOfThreads
        self.mutex = Mutex()
        self.event = Event()
        self.counter = 0

    def wait(self):
        self.mutex.lock()
        if self.counter == 0:
            self.event.clear()
        self.counter += 1

        if self.counter == self.numberOfThreads:
            self.event.signal()
            self.counter = 0

        self.mutex.unlock()
        self.event.wait()