コード例 #1
0
class SimpleBarrier:
    #### sl 78
    def __init__(self, N):
        self.N = N
        self.count = 0
        self.m = Mutex()
        #self.m = Semaphore(1)   # mutex
        self.b = Event()         # barier
    
    def barrier(self):
        self.m.lock()           #self.m.wait()
        self.count += 1
        self.m.unlock()         #self.m.signal()
        if self.count == self.N:
            self.b.signal()
        self.b.wait()
コード例 #2
0
class Lightswitch:
    def __init__(self):
        self.count = 0
        self.mutex = Mutex()

    def lock(self, semaphore):
        self.mutex.lock()
        self.count += 1
        if self.count == 1:
            semaphore.wait()
        self.mutex.unlock()

    def unlock(self, semaphore):
        self.mutex.lock()
        self.count -= 1
        if self.count == 0:
            semaphore.signal()
        self.mutex.unlock()
コード例 #3
0
class SimpleBarrier:
    """
    ADT, use only barrier()
    """
    def __init__(self, n):
        self.n = n
        self.count = 0
        self.mutex = Mutex()
        self.turnstile = Semaphore(0)

    def barrier(self):
        self.mutex.lock()
        self.count += 1
        if self.count == self.n:
            self.count = 0
            self.turnstile.signal(self.n)
        self.mutex.unlock()
        self.turnstile.wait()
コード例 #4
0
class EventBarrierOptimized:
    """
    ADT, use only barrier()
    """
    def __init__(self, n):
        self.n = n
        self.count = 0
        self.mutex = Mutex()
        self.event = Event()

    def barrier(self):
        self.mutex.lock()
        if self.count == 0:
            self.event.clear()
        self.count += 1
        if self.count == self.n:
            self.count = 0
            self.event.signal()
        self.mutex.unlock()
        self.event.wait()
コード例 #5
0
class Lightswitch:
    def __init__(self):
        self.count = 0
        self.mutex = Mutex()

    def lock(self, semaphore):
        self.mutex.lock()
        self.count += 1
        if self.count == 1:
            semaphore.wait()
        self.mutex.unlock()

    def unlock(self, semaphore):
        self.mutex.lock()
        self.count -= 1
        if self.count == 0:
            semaphore.signal()
        self.mutex.unlock()

    def lock_test(self, semaphore, t_id):
        self.mutex.lock()
        self.count += 1
        if self.count == 1:
            print("First thread walked in and turned on the light!")
            print("thread_id = {:2d}, number of threads inside = {:2d}\n".format(t_id, self.count))
            semaphore.wait()
        else:
            print("A thread walked in, but there already were some threads inside.")
            print("thread_id = {:2d}, number of threads inside = {:2d}\n".format(t_id, self.count))
        self.mutex.unlock()

    def unlock_test(self, semaphore, t_id):
        self.mutex.lock()
        self.count -= 1
        if self.count == 0:
            print("Last thread walked out and turned off the light!")
            print("thread_id = {:2d}, number of threads inside = {:2d}\n".format(t_id, self.count))
            semaphore.signal()
        else:
            print("A thread walked out, but there are still some threads inside.")
            print("thread_id = {:2d}, number of threads inside = {:2d}\n".format(t_id, self.count))
        self.mutex.unlock()