Index: Doc/lib/libthreading.tex =================================================================== RCS file: /cvsroot/python/python/dist/src/Doc/lib/libthreading.tex,v retrieving revision 1.18 diff -u -r1.18 libthreading.tex --- Doc/lib/libthreading.tex 30 Jun 2003 21:47:47 -0000 1.18 +++ Doc/lib/libthreading.tex 28 Nov 2003 12:37:58 -0000 @@ -371,26 +371,27 @@ counter; it defaults to \code{1}. \end{classdesc} -\begin{methoddesc}{acquire}{\optional{blocking}} +\begin{methoddesc}{acquire}{\optional{blocking\optional{, timeout}}} Acquire a semaphore. -When invoked without arguments: if the internal counter is larger than +When invoked with \var{blocking} set to true and \var{timeout} set to None +(the default): if the internal counter is larger than zero on entry, decrement it by one and return immediately. If it is zero on entry, block, waiting until some other thread has called \method{release()} to make it larger than zero. This is done with proper interlocking so that if multiple \method{acquire()} calls are blocked, \method{release()} will wake exactly one of them up. The implementation may pick one at random, so the order in which blocked -threads are awakened should not be relied on. There is no return -value in this case. +threads are awakened should not be relied on. Return true. -When invoked with \var{blocking} set to true, do the same thing as -when called without arguments, and return true. +When invoked with \var{blocking} set to false, do not block +(\var{timeout} is ignored in that case). If a call with \var{blocking} +set to true would block, return false immediately; otherwise, do the same thing +as when called with \var{blocking} set to true, and return true. -When invoked with \var{blocking} set to false, do not block. If a -call without an argument would block, return false immediately; -otherwise, do the same thing as when called without arguments, and -return true. +When invoked with \var{blocking} set to true and \var{timeout} set to +a positive number, it blocks at most \var{timeout} seconds and return false +if the internal counter is still zero; otherwise return true. \end{methoddesc} \begin{methoddesc}{release}{} Index: Lib/threading.py =================================================================== RCS file: /cvsroot/python/python/dist/src/Lib/threading.py,v retrieving revision 1.39 diff -u -r1.39 threading.py --- Lib/threading.py 5 Nov 2003 23:02:59 -0000 1.39 +++ Lib/threading.py 28 Nov 2003 12:37:58 -0000 @@ -89,7 +89,7 @@ self.__owner and self.__owner.getName(), self.__count) - def acquire(self, blocking=1): + def acquire(self, blocking=True): me = currentThread() if self.__owner is me: self.__count = self.__count + 1 @@ -268,7 +268,7 @@ self.__cond = Condition(Lock()) self.__value = value - def acquire(self, blocking=1): + def acquire(self, blocking=True, timeout=None): rc = False self.__cond.acquire() while self.__value == 0: @@ -277,7 +277,13 @@ if __debug__: self._note("%s.acquire(%s): blocked waiting, value=%s", self, blocking, self.__value) - self.__cond.wait() + if timeout is not None: + endtime = _time() + timeout + self.__cond.wait(timeout) + if timeout is not None and self.__value == 0: + timeout = endtime - _time() + if timeout <= 0: + break else: self.__value = self.__value - 1 if __debug__: