f2J`dZdZddlZddlZddlmZddlmZGddZGd d eejZ Gd d ejZ Gd deejZ GddeejZ Gdde Z GddejZGddejZy)zSynchronization primitives.)LockEvent Condition SemaphoreBoundedSemaphoreBarrierN) exceptions)mixinsceZdZdZdZy)_ContextManagerMixinc@K|jd{y7wN)acquireselfs $/usr/lib/python3.12/asyncio/locks.py __aenter__z_ContextManagerMixin.__aenter__ slln s c,K|jywr)release)rexc_typeexctbs r __aexit__z_ContextManagerMixin.__aexit__s sN)__name__ __module__ __qualname__rrrr r s  rr c@eZdZdZdZfdZdZdZdZdZ xZ 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 ... c d|_d|_yNF)_waiters_lockedrs r__init__z Lock.__init__Ms  rct|}|jrdnd}|jr|dt |j}d|ddd|dS Nlockedunlocked , waiters:)super__repr__r$r#lenrresextra __class__s rr0z Lock.__repr__QsYg  LLj ==gZDMM(:';zLock.acquire..cs9aAKKM9sT) r$r#all collectionsdeque _get_loop create_futureappendremover CancelledError_wake_up_firstrfuts rrz Lock.acquire\s  $--"794==99DL == '--/DMnn,,. S!   *  $$S)   $$S)(( <<##%  sBBD#C$C %C)C/D# CC,,C//1D  D#c`|jrd|_|jytd)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$rG RuntimeErrorrs rrz Lock.release|s* << DL    !67 7rc|jsy tt|j}|j s|j dyy#t$rYywxYw)z*Wake up the first waiter if it isn't done.NT)r#nextiter StopIterationdone set_resultrHs rrGzLock._wake_up_firstsT}}  tDMM*+Cxxz NN4     sA AA) rrr__doc__r%r0r(rrrG __classcell__r5s@rrrs(3j*@8" !rrc@eZdZdZdZfdZdZdZdZdZ xZ S)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. cDtj|_d|_yr")r@rAr#_valuers rr%zEvent.__init__s#))+  rct|}|jrdnd}|jr|dt |j}d|ddd|dS) Nsetunsetr*r+r r,r-r.)r/r0rWr#r1r2s rr0zEvent.__repr__sYg ' ==gZDMM(:';= 0) ValueErrorr#rW)rvalues rr%zSemaphore.__init__Ys# 19CD D  rct|}|jrdnd|j}|jr|dt |j}d|ddd|dS) Nr(zunlocked, value:r*r+r r,r-r.)r/r0r(rWr#r1r2s rr0zSemaphore.__repr___sgg  KKM1A$++/O ==gZDMM(:';K|]}|j ywrr9r;s rr>z#Semaphore.locked..isAaAKKM!Asr)rWanyr#rs rr(zSemaphore.lockedfs1{{aC ADMM,?RA A CrcK|js|xjdzc_y|jtj|_|j j }|jj| |d{|jj| |jdkDr|jy7@#|jj|wxYw#tj$r7|js%|xjdz c_|jwxYww)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. r TNr) r(rWr#r@rArBrCrDrEr rFr: _wake_up_nextrHs rrzSemaphore.acquireks{{} KK1 K == '--/DMnn,,. S!  *  $$S) ;;?     $$S)(( ==? q ""$   sCBD> CCCC1.!D>CC..C11A D;;D>cN|xjdz c_|jy)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. r N)rWr~rs rrzSemaphore.releases q  rc|jsy|jD]:}|jr|xjdzc_|jdyy)z)Wake up the first waiter that isn't done.Nr T)r#rPrWrQrHs rr~zSemaphore._wake_up_nextsE}} == C88: q t$  rrt) rrrrRr%r0r(rrr~rSrTs@rrrJs(  *C "H rrc.eZdZdZdfd ZfdZxZS)rzA bounded semaphore implementation. This raises ValueError in release() if it would increase the value above the initial value. c2||_t| |yr) _bound_valuer/r%)rrxr5s rr%zBoundedSemaphore.__init__s! rcj|j|jk\r tdt|y)Nz(BoundedSemaphore released too many times)rWrrwr/r)rr5s rrzBoundedSemaphore.releases+ ;;$++ +GH H rrt)rrrrRr%rrSrTs@rrrs  rrceZdZdZdZdZdZy) _BarrierStatefillingdraining resettingbrokenN)rrrFILLINGDRAINING RESETTINGBROKENrrrrrsGHI FrrceZdZdZdZfdZdZdZdZdZ dZ d Z d Z d Z d Zed ZedZedZxZS)ra Asyncio equivalent to threading.Barrier Implements a Barrier primitive. Useful for synchronizing a fixed number of tasks at known synchronization points. Tasks block on 'wait()' and are simultaneously awoken once they have all made their call. c|dkr tdt|_||_tj |_d|_y)z1Create a barrier, initialised to 'parties' tasks.r zparties must be > 0rN)rwr_cond_partiesrr_state_count)rpartiess rr%zBarrier.__init__s9 Q;23 3[  #++  rct|}|jj}|js|d|j d|j z }d|ddd|dS)Nr*/r+r r,r-r.)r/r0rrxr n_waitingrr2s rr0zBarrier.__repr__sdg ;;$$%{{ z$..!14<<.A AE3q9+Rwb))rc>K|jd{S7wrrjrs rrzBarrier.__aenter__sYY[   s c Kywrr)rargss rrzBarrier.__aexit__s  sc0K|j4d{|jd{ |j}|xjdz c_|dz|jk(r|j d{n|j d{||xjdzc_|j cdddd{S777Y7B7 #|xjdzc_|j wxYw#1d{7swYyxYww)zWait for the barrier. When the specified number of tasks have started waiting, they are all simultaneously awoken. Returns an unique and individual index number from 0 to 'parties-1'. Nr )r_blockrr_release_wait_exit)rindexs rrbz Barrier.waits::  ++-     q 19 ---/))**,&& q       *&  q     sDC DDCDAC7C8CCC%D; DCDDCCD'C>>DDD DDcKjjfdd{jtjurt j dy76w)Nc\jtjtjfvSr)rrrrrsrz Barrier._block..s&DKK&& (?(?(rzBarrier aborted)rrmrrrr BrokenBarrierErrorrs`rrzBarrier._blocksZ jj!!     ;;-.. .//0AB B / s"AA7AcjKtj|_|jj ywr)rrrrrsrs rrzBarrier._releases% $,,  s13cKjjfdd{jtjtj fvrt jdy7Fw)Nc<jtjuSr)rrrrsrrzBarrier._wait..s$++]=R=R*RrzAbort or reset of barrier)rrmrrrrr rrs`rrz Barrier._waits] jj!!"RSSS ;;=//1H1HI I//0KL L J Ts"A.A,AA.c|jdk(r\|jtjtjfvrtj |_|j jyy)Nr)rrrrrrrrsrs rrz Barrier._exitsO ;;! {{}66 8N8NOO+33 JJ ! ! # rchK|j4d{|jdkDr2|jtjur+tj|_ntj |_|jj dddd{y77#1d{7swYyxYww)zReset the barrier to the initial state. Any tasks currently waiting will get the BrokenBarrier exception raised. Nr)rrrrrrrsrs rresetz Barrier.reset"s :: $ ${{Q;;m&=&=="/"9"9DK+33 JJ ! ! # $ $ $ $ $ $ $sEB2BB2A1B B2BB2B2B/#B& $B/+B2cK|j4d{tj|_|jj dddd{y7D7#1d{7swYyxYww)zPlace the barrier into a 'broken' state. Useful in case of error. Any currently waiting tasks and tasks attempting to 'wait()' will have BrokenBarrierError raised. N)rrrrrsrs rabortz Barrier.abort1s[ :: $ $'..DK JJ ! ! # $ $ $ $ $ $ $sDA1AA10A A1AA1A1A."A% #A.*A1c|jS)z8Return the number of tasks required to trip the barrier.)rrs rrzBarrier.parties;s}}rcT|jtjur |jSy)zrs! * C! !7!7C!L:&F " ":&zm($f&<&<m(`W$f&<&<Wty$DIIM3f$$M3r