def do_release_read_lock(self):
        self.condition.acquire()
        try:
            self.async -= 1

            # check if we are the last asynchronous reader thread
            # out the door.
            if self.async == 0:
                # yes. so if a sync operation is waiting, notifyAll to wake
                # it up
                if self.current_sync_operation is not None:
                    self.condition.notifyAll()
            elif self.async < 0:
                raise LockError("Synchronizer error - too many "
                                "release_read_locks called")
    def acquire_write_lock(self, wait=True):
        state = self.state

        if state.reading:
            raise LockError("lock is in reading state")

        if state.reentrantcount == 0:
            x = self.do_acquire_write_lock(wait)
            if (wait or x):
                state.reentrantcount += 1
                state.writing = True
            return x
        elif state.writing:
            state.reentrantcount += 1
            return True
示例#3
0
    def do_release_write_lock(self):
        self.condition.acquire()
        try:
            if self.current_sync_operation is not _threading.currentThread():
                raise LockError("Synchronizer error - current thread doesnt "
                                "have the write lock")

            # reset the current sync operation so
            # another can get it
            self.current_sync_operation = None

            # tell everyone to get ready
            self.condition.notifyAll()
        finally:
            # everyone go !!
            self.condition.release()
示例#4
0
class ConditionSynchronizer(SynchronizerImpl):
    """a synchronizer using a Condition.  
    
    this synchronizer is based on threading.Lock() objects and
    therefore must be shared among threads, so it is also threadsafe.
    the "state" variable referenced by the base SynchronizerImpl class
    is turned into a thread local, and all the do_XXXX methods are synchronized
    on the condition object.
    
    The Synchronizer container will maintain a registry of ConditionSynchronizer
    objects keyed to the name of the synchronizer.
    """
    def __init__(self, identifier):
        self.tlocalstate = util.ThreadLocal(creator=lambda: SyncState())

        # counts how many asynchronous methods are executing
        self. async = 0

        # pointer to thread that is the current sync operation
        self.current_sync_operation = None

        # condition object to lock on
        self.condition = _threading.Condition(_threading.Lock())

    state = property(lambda self: self.tlocalstate())

    def do_acquire_read_lock(self, wait=True):
        self.condition.acquire()

        # see if a synchronous operation is waiting to start
        # or is already running, in which case we wait (or just
        # give up and return)
        if wait:
            while self.current_sync_operation is not None:
                self.condition.wait()
        else:
            if self.current_sync_operation is not None:
                self.condition.release()
                return False

        self. async += 1

        self.condition.release()

        if not wait: return True

    def do_release_read_lock(self):
        self.condition.acquire()

        self. async -= 1

        # check if we are the last asynchronous reader thread
        # out the door.
        if self. async == 0:
            # yes. so if a sync operation is waiting, notifyAll to wake
            # it up
            if self.current_sync_operation is not None:
                self.condition.notifyAll()
        elif self. async < 0:
            raise LockError(
                "Synchronizer error - too many release_read_locks called")