\dFJbdZdZddlZddlZddlmZddlmZddlmZGdd ZGd d eej Z Gd d ej Z Gddeej Z Gddeej Z Gdde ZGddejZGddej ZdS)zSynchronization primitives.)LockEvent Condition SemaphoreBoundedSemaphoreBarrierN) exceptions)mixins)tasksceZdZdZdZdS)_ContextManagerMixinc>K|d{VdSN)acquireselfs ..\python\lib\asyncio\locks.py __aenter__z_ContextManagerMixin.__aenter__s-llnntc2K|dSr)release)rexc_typeexctbs r __aexit__z_ContextManagerMixin.__aexit__s rN)__name__ __module__ __qualname__rrrrrr s2 rrc@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|_dSNF)_waiters_lockedrs r__init__z Lock.__init__Ns  rct}|jrdnd}|jr|dt |j}d|ddd|dS Nlockedunlocked , waiters:)super__repr__r%r$lenrresextra __class__s rr1z Lock.__repr__Rspgg   L8j = =<<DM(:(:<K|]}|VdSr cancelled.0ws r zLock.acquire..ds*99aAKKMM999999rT) r%r$all collectionsdeque _get_loop create_futureappendremover CancelledError_wake_up_firstrfuts rrz Lock.acquire]s-   $-"7994=99999#8DL4 = '-//DMnn,,.. S!!!   *  $$S)))) $$S)))))(   < &##%%%    tsB<!C<CC,Dch|jrd|_|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%rH RuntimeErrorrs rrz Lock.release}s< < 8 DL    ! ! ! ! !677 7rc|jsdS tt|j}n#t$rYdSwxYw|s|ddSdS)z*Wake up the first waiter if it isn't done.NT)r$nextiter StopIterationdone set_resultrIs rrHzLock._wake_up_firsts}  F tDM**++CC    FF  xxzz ! NN4  ! !s !- ;;) rrr__doc__r&r1r)rrrH __classcell__r6s@rrrs33j*****@888" ! ! ! ! ! ! !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|_dSr#)rArBr$_valuers rr&zEvent.__init__s#)++  rct}|jrdnd}|jr|dt |j}d|ddd|dS) Nsetunsetr+r,r r-r.r/)r0r1rXr$r2r3s rr1zEvent.__repr__spgg  1' = =<<DM(:(:<>CC&DDE  D%$E %D96E 8D99E cnK|}|s&|d{V|}|&|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. Nrc)r predicateresults rwait_forzCondition.wait_forsW !))++       Y[[F ! rr c|stdd}|jD]9}||krdS|s|dz }|d: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 lockrr FN)r)rLr$rQrR)rnidxrJs rnotifyzCondition.notify*s{{}} DBCC C= & &Caxx88:: &qu%%%  & &rcT|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)rrr2r$rs r notify_allzCondition.notify_allBs& C &&'''''rrr ) rrrrSr&r1rcrnrrrtrTrUs@rrrs , , , ,*****#0#0#0J   &&&&0(((((((rrcBeZdZdZd dZfdZdZdZdZdZ xZ 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. r cL|dkrtdd|_||_dS)Nrz$Semaphore initial value must be >= 0) ValueErrorr$rX)rvalues rr&zSemaphore.__init__Zs, 199CDD D  rct}|rdn d|j}|jr|dt |j}d|ddd|dS) Nr)zunlocked, value:r+r,r r-r.r/)r0r1r)rXr$r2r3s rr1zSemaphore.__repr__`sgg   KKMMO/O$+/O/O = =<<DM(:(:<.js-AAaAKKMM!AAAAAArr )rXanyr$rs rr)zSemaphore.lockedgs9{aC AADM,?RAAA A A CrctK|s|xjdzc_dS|jtj|_|}|j| |d{V|j|n#|j|wxYwnL#tj $r:| s$|xjdz c_| wxYw|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. r TNr) r)rXr$rArBrCrDrErFr rGr; _wake_up_nextrIs rrzSemaphore.acquirels@{{}}  KK1 KK4 = '-//DMnn,,.. S!!!  *  $$S)))) $$S)))))(   ==?? % q ""$$$    ;??    ts B-C -C  C A DcN|xjdz c_|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. r N)rXrrs rrzSemaphore.releases, q  rc|jsdS|jD]>}|s(|xjdzc_|ddS?dS)z)Wake up the first waiter that isn't done.Nr T)r$rQrXrRrIs rrzSemaphore._wake_up_nextsj}  F=  C88::  q t$$$   rru) rrrrSr&r1r)rrrrTrUs@rrrKs   *****CCC """H       rrc.eZdZdZdfd ZfdZxZS)rzA bounded semaphore implementation. This raises ValueError in release() if it would increase the value above the initial value. r cX||_t|dSr) _bound_valuer0r&)rryr6s rr&zBoundedSemaphore.__init__s)! rc|j|jkrtdtdS)Nz(BoundedSemaphore released too many times)rXrrxr0r)rr6s rrzBoundedSemaphore.releases< ;$+ + +GHH H rru)rrrrSr&rrTrUs@rrrs`       rrceZdZdZdZdZdZdS) _BarrierStatefillingdraining resettingbrokenN)rrrFILLINGDRAINING RESETTINGBROKENr rrrrs"GHI FFFrrceZdZdZdZfdZdZdZdZdZ 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|dkrtdt|_||_tj|_d|_dS)z1Create a barrier, initialised to 'parties' tasks.r zparties must be > 0rN)rxr_cond_partiesrr_state_count)rpartiess rr&zBarrier.__init__sA Q;;233 3[[  #+  rct}|jj}|js|d|jd|jz }d|ddd|dS)Nr+/r,r r-r.r/)r0r1rryr n_waitingrr3s rr1zBarrier.__repr__spgg  ;$&{ B A$.AA4<AA AE)3qt9))))))rc:K|d{VSrrkrs rrzBarrier.__aenter__s(YY[[       rc KdSrr )rargss rrzBarrier.__aexit__s  rcK|j4d{V|d{V |j}|xjdz c_|dz|jkr|d{Vn|d{V||xjdzc_|cdddd{VS#|xjdzc_|wxYw#1d{VswxYwYdS)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 rrcz Barrier.waits:        ++--          q 19 ----//))))))))**,,&&&&&&& q                 q                  s)C'AB>$C'>&C$$C'' C14C1cKjfdd{Vjtjurt jddS)NcBjtjtjfvSr)rrrrrsrz Barrier._block..sDK& (?(rzBarrier aborted)rrnrrrr BrokenBarrierErrorrs`rrzBarrier._blocks j!!              ;-. . ./0ABB B / .rc^Ktj|_|jdSr)rrrrrtrs rrzBarrier._releases, $,  rcKjfdd{Vjtjtjfvrt jddS)Nc*jtjuSr)rrrrsrrzBarrier._wait..s$+]=R*RrzAbort or reset of barrier)rrnrrrrr rrs`rrz Barrier._waitsn j!!"R"R"R"RSSSSSSSSS ;=/1HI I I/0KLL L J Irc|jdkrK|jtjtjfvrtj|_|jdSdS)Nr)rrrrrrrrtrs rrz Barrier._exitsU ;!  {}6 8NOOO+3 J ! ! # # # # #  rc"K|j4d{V|jdkr%|jtjurtj|_ntj|_|jdddd{VdS#1d{VswxYwYdS)zReset the barrier to the initial state. Any tasks currently waiting will get the BrokenBarrier exception raised. Nr)rrrrrrrtrs rresetz Barrier.reset#s : $ $ $ $ $ $ $ ${Q;m&==="/"9DK+3 J ! ! # # # $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $sAA>> B BcK|j4d{Vtj|_|jdddd{VdS#1d{VswxYwYdS)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)rrrrrtrs rabortz Barrier.abort2s : $ $ $ $ $ $ $ $'.DK J ! ! # # # $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $s+A AAc|jS)z8Return the number of tasks required to trip the barrier.)rrs rrzBarrier.parties<s }rc:|jtjur|jSdS)zrs!! * C!C!C!C!C! !7C!C!C!L:&:&:&:&:&F ":&:&:&zm(m(m(m(m($f&<m(m(m(`WWWWW$f&<WWWty$DIM3M3M3M3M3f$M3M3M3M3M3r