o ?Og*7@sdZdZddlZddlmZddlmZddlmZGdd d ZGd d d eejZ Gd d d ejZ GdddeejZ GdddeejZ Gddde Z dS)zSynchronization primitives.)LockEvent Condition SemaphoreBoundedSemaphoreN) exceptions)mixins)tasksc@seZdZddZddZdS)_ContextManagerMixincs|IdHdSN)acquireselfr4/opt/alt/python310/lib64/python3.10/asyncio/locks.py __aenter__ sz_ContextManagerMixin.__aenter__cs|dSr )release)rexc_typeexctbrrr __aexit__s z_ContextManagerMixin.__aexit__N)__name__ __module__ __qualname__rrrrrrr s r cTeZdZdZejdfdd ZfddZddZd d Z d d Z d dZ Z S)raPrimitive lock objects. A primitive lock is a synchronization primitive that is not owned by a particular coroutine when locked. A primitive lock is in one of two states, 'locked' or 'unlocked'. It is created in the unlocked state. It has two basic methods, acquire() and release(). When the state is unlocked, acquire() changes the state to locked and returns immediately. When the state is locked, acquire() blocks until a call to release() in another coroutine changes it to unlocked, then the acquire() call resets it to locked and returns. The release() method should only be called in the locked state; it changes the state to unlocked and returns immediately. If an attempt is made to release an unlocked lock, a RuntimeError will be raised. When more than one coroutine is blocked in acquire() waiting for the state to turn to unlocked, only one coroutine proceeds when a release() call resets the state to unlocked; first coroutine which is blocked in acquire() is being processed. acquire() is a coroutine and should be called with 'await'. Locks also support the asynchronous context management protocol. 'async with lock' statement should be used. Usage: lock = Lock() ... await lock.acquire() try: ... finally: lock.release() Context manager usage: lock = Lock() ... async with lock: ... Lock objects can be tested for locking state: if not lock.locked(): await lock.acquire() else: # lock is acquired ... loopcstj|dd|_d|_dSNrF)super__init___waiters_lockedrr __class__rrr Ms z Lock.__init__cLt}|jr dnd}|jr|dt|j}d|ddd|dS NlockedZunlocked , waiters:)r__repr__r"r!lenrresZextrar$rrr.R z Lock.__repr__cC|jS)z Return True if lock is acquired.)r"rrrrr(Yz Lock.lockedc s|js|jdustdd|jDrd|_dS|jdur"t|_|}|j|zz |IdHW|j|n|j|wWnt j yU|jsT| wd|_dS)zAcquire a lock. This method blocks until the lock is unlocked, then sets it to locked and returns True. Ncss|]}|VqdSr  cancelled.0wrrr dszLock.acquire..T) r"r!all collectionsdeque _get_loop create_futureappendremoverCancelledError_wake_up_firstrfutrrrr ]s*      z Lock.acquirecCs |jr d|_|dStd)aGRelease a lock. When the lock is locked, reset it to unlocked, and return. If any other coroutines are blocked waiting for the lock to become unlocked, allow exactly one of them to proceed. When invoked on an unlocked lock, a RuntimeError is raised. There is no return value. FzLock is not acquired.N)r"rC RuntimeErrorrrrrr}s  z Lock.releasecCsL|jsdSz tt|j}Wn tyYdSw|s$|ddSdS)z*Wake up the first waiter if it isn't done.NT)r!nextiter StopIterationdone set_resultrDrrrrCs zLock._wake_up_first) rrr__doc__r _markerr r.r(r rrC __classcell__rrr$rrs5  rcr)ra#Asynchronous equivalent to threading.Event. Class implementing event objects. An event manages a flag that can be set to true with the set() method and reset to false with the clear() method. The wait() method blocks until the flag is true. The flag is initially false. rcs"tj|dt|_d|_dSr)rr r<r=r!_valuer#r$rrr s  zEvent.__init__cr&) NsetZunsetr)r*rr+r,r-)rr.rOr!r/r0r$rrr.r2zEvent.__repr__cCr3)z5Return True if and only if the internal flag is true.rOrrrris_setr4z Event.is_setcCs2|jsd|_|jD] }|s|dq dSdS)zSet the internal flag to true. All coroutines waiting for it to become true are awakened. Coroutine that call wait() once the flag is true will not block at all. TN)rOr!rJrKrDrrrrPs  z Event.setcCs d|_dS)zReset the internal flag to false. Subsequently, coroutines calling wait() will block until set() is called to set the internal flag to true again.FNrQrrrrclears z Event.clearc sP|jrdS|}|j|z|IdHW|j|dS|j|w)zBlock until the internal flag is true. If the internal flag is true on entry, return True immediately. Otherwise, block until another coroutine calls set() to set the flag to true, then return True. TN)rOr>r?r!r@rArDrrrwaits   z Event.wait) rrrrLr rMr r.rRrPrSrTrNrrr$rrs  rcsXeZdZdZdejdfdd ZfddZdd Zd d Z dd dZ ddZ Z S)raAsynchronous equivalent to threading.Condition. This class implements condition variable objects. A condition variable allows one or more coroutines to wait until they are notified by another coroutine. A new Lock object is created and used as the underlying lock. NrcsHtj|d|durt}||_|j|_|j|_|j|_t|_ dSNr) rr r_lockr(r rr<r=r!)rlockrr$rrr szCondition.__init__csNt}|r dnd}|jr|dt|j}d|ddd|dSr')rr.r(r!r/r0r$rrr.s zCondition.__repr__cs|s td|zB|}|j|z.|IdHW|j|Wd} z |IdHWn t j y@d}Ynwq+|rGt j dS|j|wd} z |IdHWn t j yhd}YnwqS|rot j w)aWait until notified. If the calling coroutine has not acquired the lock when this method is called, a RuntimeError is raised. This method releases the underlying lock, and then blocks until it is awakened by a notify() or notify_all() call for the same condition variable in another coroutine. Once awakened, it re-acquires the lock and returns True. zcannot wait on un-acquired lockNFT) r(rFrr>r?r!r@rAr rrB)rrEr6rrrrTsF    zCondition.waitcs(|}|s|IdH|}|r|S)zWait until a predicate becomes true. The predicate should be a callable which result will be interpreted as a boolean value. The final predicate value is the return value. N)rT)rZ predicateresultrrrwait_forszCondition.wait_forrcCsL|stdd}|jD]}||krdS|s#|d7}|dq dS)aBy default, wake up one coroutine waiting on this condition, if any. If the calling coroutine has not acquired the lock when this method is called, a RuntimeError is raised. This method wakes up at most n of the coroutines waiting for the condition variable; it is a no-op if no coroutines are waiting. Note: an awakened coroutine does not actually return from its wait() call until it can reacquire the lock. Since notify() does not release the lock, its caller should. z!cannot notify on un-acquired lockrrFN)r(rFr!rJrK)rnidxrErrrnotify,s   zCondition.notifycCs|t|jdS)aWake up all threads waiting on this condition. This method acts like notify(), but wakes up all waiting threads instead of one. If the calling thread has not acquired the lock when this method is called, a RuntimeError is raised. N)r\r/r!rrrr notify_allDszCondition.notify_allr r) rrrrLr rMr r.rTrYr\r]rNrrr$rrs % rcsVeZdZdZdejdfdd ZfddZdd Zd d Z d d Z ddZ Z S)raA Semaphore implementation. A semaphore manages an internal counter which is decremented by each acquire() call and incremented by each release() call. The counter can never go below zero; when acquire() finds that it is zero, it blocks, waiting until some other thread calls release(). Semaphores also support the context management protocol. The optional argument gives the initial value for the internal counter; it defaults to 1. If the value given is less than 0, ValueError is raised. rrcs.tj|d|dkrtdd|_||_dS)Nrrz$Semaphore initial value must be >= 0)rr ValueErrorr!rOrvaluerr$rrr \s  zSemaphore.__init__csVt}|r dnd|j}|jr|dt|j}d|ddd|dS) Nr(zunlocked, value:r)r*rr+r,r-)rr.r(rOr!r/r0r$rrr.cs zSemaphore.__repr__cCs"|jdkptdd|jp dDS)z9Returns True if semaphore cannot be acquired immediately.rcss|]}| VqdSr r5r7rrrr:msz#Semaphore.locked..r)rOanyr!rrrrr(js zSemaphore.lockedc s|s|jd8_dS|jdurt|_|}|j|zz |IdHW|j|n|j|wWnt j yS| sR|jd7_| w|jdkr]| dS)a5Acquire a semaphore. If the internal counter is larger than zero on entry, decrement it by one and return True immediately. If it is zero on entry, block, waiting until some other coroutine has called release() to make it larger than 0, and then return True. rTNr) r(rOr!r<r=r>r?r@rArrBr6 _wake_up_nextrDrrrr os*        zSemaphore.acquirecCs|jd7_|dS)zRelease a semaphore, incrementing the internal counter by one. When it was zero on entry and another coroutine is waiting for it to become larger than zero again, wake up that coroutine. rN)rOrcrrrrrs zSemaphore.releasecCs@|jsdS|jD]}|s|jd8_|ddSqdS)z)Wake up the first waiter that isn't done.NrT)r!rJrOrKrDrrrrcs  zSemaphore._wake_up_nextr^) rrrrLr rMr r.r(r rrcrNrrr$rrMs $ rcs6eZdZdZdejdfdd ZfddZZS) rzA bounded semaphore implementation. This raises ValueError in release() if it would increase the value above the initial value. rrcs||_tj||ddSrU) _bound_valuerr r`r$rrr szBoundedSemaphore.__init__cs"|j|jkr tdtdS)Nz(BoundedSemaphore released too many times)rOrdr_rrrr$rrrs zBoundedSemaphore.releaser^) rrrrLr rMr rrNrrr$rrsr)rL__all__r<rr r r Z_LoopBoundMixinrrrrrrrrrs    >q[