Exemplo n.º 1
0
    def join(self, timeout=None):
        if not self.__initialized:
            raise RuntimeError("Thread.__init__() not called")
        if not self.__started:
            raise RuntimeError("cannot join thread before it is started")
        if self is currentThread():
            raise RuntimeError("cannot join current thread")

        if __debug__:
            if not self.__stopped:
                self._note("%s.join(): waiting until thread stops", self)
        self.__block.acquire()
        try:
            if timeout is None:
                while not self.__stopped:
                    self.__block.wait()
                if __debug__:
                    self._note("%s.join(): thread stopped", self)
            else:
                deadline = _time() + timeout
                while not self.__stopped:
                    delay = deadline - _time()
                    if delay <= 0:
                        if __debug__:
                            self._note("%s.join(): timed out", self)
                        break
                    self.__block.wait(delay)
                else:
                    if __debug__:
                        self._note("%s.join(): thread stopped", self)
        finally:
            self.__block.release()
Exemplo n.º 2
0
    def join(self, timeout=None):
        if not self.__initialized:
            raise RuntimeError("Thread.__init__() not called")
        if not self.__started:
            raise RuntimeError("cannot join thread before it is started")
        if self is currentThread():
            raise RuntimeError("cannot join current thread")

        if __debug__:
            if not self.__stopped:
                self._note("%s.join(): waiting until thread stops", self)
        self.__block.acquire()
        try:
            if timeout is None:
                while not self.__stopped:
                    self.__block.wait()
                if __debug__:
                    self._note("%s.join(): thread stopped", self)
            else:
                deadline = _time() + timeout
                while not self.__stopped:
                    delay = deadline - _time()
                    if delay <= 0:
                        if __debug__:
                            self._note("%s.join(): timed out", self)
                        break
                    self.__block.wait(delay)
                else:
                    if __debug__:
                        self._note("%s.join(): thread stopped", self)
        finally:
            self.__block.release()
Exemplo n.º 3
0
 def wait(self, timeout=None):
     if not self._is_owned():
         raise RuntimeError("cannot wait on un-aquired lock")
     waiter = _allocate_lock()
     waiter.acquire()
     self.__waiters.append(waiter)
     saved_state = self._release_save()
     try:    # restore state no matter what (e.g., KeyboardInterrupt)
         if timeout is None:
             waiter.acquire()
             if __debug__:
                 self._note("%s.wait(): got it", self)
         else:
             # Balancing act:  We can't afford a pure busy loop, so we
             # have to sleep; but if we sleep the whole timeout time,
             # we'll be unresponsive.  The scheme here sleeps very
             # little at first, longer as time goes on, but never longer
             # than 20 times per second (or the timeout time remaining).
             endtime = _time() + timeout
             delay = 0.0005 # 500 us -> initial delay of 1 ms
             while True:
                 gotit = waiter.acquire(0)
                 if gotit:
                     break
                 remaining = endtime - _time()
                 if remaining <= 0:
                     break
                 delay = min(delay * 2, remaining, .05)
                 _sleep(delay)
             if not gotit:
                 if __debug__:
                     self._note("%s.wait(%s): timed out", self, timeout)
                 try:
                     self.__waiters.remove(waiter)
                 except ValueError:
                     pass
             else:
                 if __debug__:
                     self._note("%s.wait(%s): got it", self, timeout)
     finally:
         self._acquire_restore(saved_state)
Exemplo n.º 4
0
 def wait(self, timeout=None):
     if not self._is_owned():
         raise RuntimeError("cannot wait on un-aquired lock")
     waiter = _allocate_lock()
     waiter.acquire()
     self.__waiters.append(waiter)
     saved_state = self._release_save()
     try:  # restore state no matter what (e.g., KeyboardInterrupt)
         if timeout is None:
             waiter.acquire()
             if __debug__:
                 self._note("%s.wait(): got it", self)
         else:
             # Balancing act:  We can't afford a pure busy loop, so we
             # have to sleep; but if we sleep the whole timeout time,
             # we'll be unresponsive.  The scheme here sleeps very
             # little at first, longer as time goes on, but never longer
             # than 20 times per second (or the timeout time remaining).
             endtime = _time() + timeout
             delay = 0.0005  # 500 us -> initial delay of 1 ms
             while True:
                 gotit = waiter.acquire(0)
                 if gotit:
                     break
                 remaining = endtime - _time()
                 if remaining <= 0:
                     break
                 delay = min(delay * 2, remaining, .05)
                 _sleep(delay)
             if not gotit:
                 if __debug__:
                     self._note("%s.wait(%s): timed out", self, timeout)
                 try:
                     self.__waiters.remove(waiter)
                 except ValueError:
                     pass
             else:
                 if __debug__:
                     self._note("%s.wait(%s): got it", self, timeout)
     finally:
         self._acquire_restore(saved_state)