def acquire(self, timeout = None):
        """
        Attempts to acquire the lock exclusively within the optional timeout.
        If the timeout is not specified, waits for the lock infinitely.
        Returns True if the lock has been acquired, False otherwise.
        """

        thrCurrent = currentThread()

        self._lock()
        try:

            if self.__log: self._log("acquiring exclusive")
            if self.__debug:
                assert self._invariant(), "SharedLock invariant failed: %s in %s" % \
                                          (self._debug_dump(), trace_string())

            # this thread already has exclusive lock, the count is incremented

            if thrCurrent is self.thrOwner:

                self.intOwnerDepth += 1
                if self.__debug:
                    assert self._invariant(), "SharedLock invariant failed: %s in %s" % \
                                              (self._debug_dump(), trace_string())
                if self.__log: self._log("acquired exclusive")
                return True

            # this thread already has shared lock, this is the most complicated case

            elif thrCurrent in self.dicUsers:
                
                # the thread gets exclusive lock immediately if there is no other threads

                if self.dicUsers.keys() == [thrCurrent] \
                and not self._has_pending_users() and not self._has_pending_owners():
                    
                    self.thrOwner = thrCurrent
                    self.intOwnerDepth = 1
                    if self.__debug:
                        assert self._invariant(), "SharedLock invariant failed: %s in %s" % \
                                                  (self._debug_dump(), trace_string())
                    if self.__log: self._log("acquired exclusive")
                    return True

                # the thread releases its shared lock in hope for the future
                # exclusive one

                intSharedDepth = self.dicUsers.pop(thrCurrent) # that many times it had shared lock

                evtEvent = self._pick_event()
                self.lstOwners.append((thrCurrent, evtEvent, intSharedDepth)) # it will be given them back

                self._release_threads()

            # a thread acquires exclusive lock whenever there is no
            # current owner nor running users

            elif not self._has_owner() and not self._has_users():

                self.thrOwner = thrCurrent
                self.intOwnerDepth = 1
                if self.__debug:
                    assert self._invariant(), "SharedLock invariant failed: %s in %s" % \
                                              (self._debug_dump(), trace_string())
                if self.__log: self._log("acquired exclusive")
                return True

            # otherwise the thread registers itself as a pending owner with no
            # prior record of holding shared lock

            else: 

                evtEvent = self._pick_event()
                self.lstOwners.append((thrCurrent, evtEvent, 0))

            if self.__debug:
                assert self._invariant(), "SharedLock invariant failed: %s in %s" % \
                                          (self._debug_dump(), trace_string())
            if self.__log: self._log("waiting for exclusive")

        finally:
            self._unlock()

        return self._acquire_event(evtEvent, timeout) # the thread waits for a lock release

        Attempts to acquire the lock exclusively within the optional timeout.
        If the timeout is not specified, waits for the lock infinitely.
        Returns True if the lock has been acquired, False otherwise.
        
recipes/Python/465156_Shared_lock_akreaderwriter_lock_timeouts_FIFO/recipe-465156.py