MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / acquire

Method acquire

tools/python-3.11.9-amd64/Lib/threading.py:440–483  ·  view source on GitHub ↗

Acquire a semaphore, decrementing the internal counter by one. When invoked without arguments: 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 rele

(self, blocking=True, timeout=None)

Source from the content-addressed store, hash-verified

438 f" value={self._value}>")
439
440 def acquire(self, blocking=True, timeout=None):
441 """Acquire a semaphore, decrementing the internal counter by one.
442
443 When invoked without arguments: if the internal counter is larger than
444 zero on entry, decrement it by one and return immediately. If it is zero
445 on entry, block, waiting until some other thread has called release() to
446 make it larger than zero. This is done with proper interlocking so that
447 if multiple acquire() calls are blocked, release() will wake exactly one
448 of them up. The implementation may pick one at random, so the order in
449 which blocked threads are awakened should not be relied on. There is no
450 return value in this case.
451
452 When invoked with blocking set to true, do the same thing as when called
453 without arguments, and return true.
454
455 When invoked with blocking set to false, do not block. If a call without
456 an argument would block, return false immediately; otherwise, do the
457 same thing as when called without arguments, and return true.
458
459 When invoked with a timeout other than None, it will block for at
460 most timeout seconds. If acquire does not complete successfully in
461 that interval, return false. Return true otherwise.
462
463 """
464 if not blocking and timeout is not None:
465 raise ValueError("can't specify timeout for non-blocking acquire")
466 rc = False
467 endtime = None
468 with self._cond:
469 while self._value == 0:
470 if not blocking:
471 break
472 if timeout is not None:
473 if endtime is None:
474 endtime = _time() + timeout
475 else:
476 timeout = endtime - _time()
477 if timeout <= 0:
478 break
479 self._cond.wait(timeout)
480 else:
481 self._value -= 1
482 rc = True
483 return rc
484
485 __enter__ = acquire
486

Callers

nothing calls this directly

Calls 1

waitMethod · 0.45

Tested by

no test coverage detected