Exemplo n.º 1
0
    def __acquire_without_hubs(self, timeout):
        thread_lock = self._allocate_lock()
        thread_lock.acquire()
        absolute_expiration = 0
        begin = 0
        if timeout:
            absolute_expiration = monotonic() + timeout

        # Cython won't compile a lambda here
        link = _LockReleaseLink(thread_lock)
        while 1:
            self.__add_link(link)
            if absolute_expiration:
                begin = monotonic()

            got_native = self.__spin_on_native_lock(thread_lock, timeout)
            self._quiet_unlink_all(link)
            if got_native:
                if self.acquire(0):
                    return True
            if absolute_expiration:
                now = monotonic()
                if now >= absolute_expiration:
                    return False
                duration = now - begin
                timeout -= duration
                if timeout <= 0:
                    return False
Exemplo n.º 2
0
    def __spin_on_native_lock(self, thread_lock, timeout):
        expiration = 0
        if timeout:
            expiration = monotonic() + timeout

        self._drop_lock_for_switch_out()
        try:
            # TODO: When timeout is given and the lock supports that
            # (Python 3), pass that.
            # Python 2 has terrible behaviour where lock acquires can't
            # be interrupted, so we use a spin loop
            while not thread_lock.acquire(0):
                if expiration and monotonic() >= expiration:
                    return False

                _native_sleep(0.001)
            return True
        finally:
            self._acquire_lock_for_switch_in()