3 \<@sdddddgZddlZddlmZdd lmZdd lmZdd lmZGd d d ZGdddZ Gddde Z GdddZ Gddde Z Gddde Z Gddde ZdS)LockEvent Condition SemaphoreBoundedSemaphoreN)compat)events)futures) coroutinec@s$eZdZddZddZddZdS)_ContextManagercCs ||_dS)N)_lock)selflockr%/usr/lib64/python3.6/asyncio/locks.py__init__sz_ContextManager.__init__cCsdS)Nr)rrrr __enter__sz_ContextManager.__enter__c Gsz|jjWdd|_XdS)N)r release)rargsrrr__exit__$sz_ContextManager.__exit__N)__name__ __module__ __qualname__rrrrrrrr sr c@sNeZdZddZddZeddZejrJddZ ed d Z ed d Z d S)_ContextManagerMixincCs tddS)Nz9"yield from" should be used as context manager expression) RuntimeError)rrrrr,sz_ContextManagerMixin.__enter__cGsdS)Nr)rrrrrr0sz_ContextManagerMixin.__exit__ccs|jEdHt|S)N)acquirer )rrrr__iter__5sz_ContextManagerMixin.__iter__ccs|jEdHt|S)N)rr )rrrr __await__Hsz_ContextManagerMixin.__await__ccs|jEdHdS)N)r)rrrr __aenter__Msz_ContextManagerMixin.__aenter__cCs |jdS)N)r)rexc_typeexctbrrr __aexit__Tsz_ContextManagerMixin.__aexit__N) rrrrrr rrZPY35rrr#rrrrr+s  rcsNeZdZddddZfddZddZed d Zd d Zd dZ Z S)rN)loopcCs.tj|_d|_|dk r ||_n tj|_dS)NF) collectionsdeque_waiters_locked_loopr get_event_loop)rr$rrrrs  z Lock.__init__csDtj}|jrdnd}|jr0dj|t|j}dj|dd|S)Nlockedunlockedz {},waiters:{}z <{} [{}]>r)super__repr__r(r'formatlen)rresextra) __class__rrr/s  z Lock.__repr__cCs|jS)N)r()rrrrr+sz Lock.lockedccs|j r&tdd|jDr&d|_dS|jj}|jj|y"z|EdHWd|jj|XWn&tjk r|js~|j YnXd|_dS)Ncss|]}|jVqdS)N) cancelled).0wrrr szLock.acquire..T) r(allr'r) create_futureappendremover CancelledError_wake_up_first)rfutrrrrs  z Lock.acquirecCs"|jrd|_|jntddS)NFzLock is not acquired.)r(r>r)rrrrrs  z Lock.releasec Cs>ytt|j}Wntk r&dSX|js:|jddS)NT)nextiterr' StopIterationdone set_result)rr?rrrr>s zLock._wake_up_first) rrrrr/r+r rrr> __classcell__rr)r4rrYs 6  csNeZdZddddZfddZddZd d Zd d Zed dZ Z S)rN)r$cCs.tj|_d|_|dk r ||_n tj|_dS)NF)r%r&r'_valuer)r r*)rr$rrrrs  zEvent.__init__csDtj}|jrdnd}|jr0dj|t|j}dj|dd|S)NsetZunsetz {},waiters:{}z <{} [{}]>rr-)r.r/rFr'r0r1)rr2r3)r4rrr/s  zEvent.__repr__cCs|jS)N)rF)rrrris_setsz Event.is_setcCs2|js.d|_x |jD]}|js|jdqWdS)NT)rFr'rCrD)rr?rrrrGs  z Event.setcCs d|_dS)NF)rF)rrrrclearsz Event.clearc csB|jr dS|jj}|jj|z|EdHdS|jj|XdS)NT)rFr)r:r'r;r<)rr?rrrwait s   z Event.wait) rrrrr/rHrGrIr rJrErr)r4rrs    csVeZdZdddddZfddZeddZed d Zdd d ZddZ Z S)rN)r$cCsp|dk r||_n tj|_|dkr0t|jd}n|j|jk rDtd||_|j|_|j|_|j|_t j |_ dS)N)r$z"loop argument must agree with lock) r)r r*r ValueErrorr r+rrr%r&r')rrr$rrrr+s  zCondition.__init__csFtj}|jrdnd}|jr2dj|t|j}dj|dd|S)Nr+r,z {},waiters:{}z <{} [{}]>rr-)r.r/r+r'r0r1)rr2r3)r4rrr/>s  zCondition.__repr__ccs|jstd|jz8|jj}|jj|z|EdHdS|jj|XWdd}x4y|jEdHPWqXt j k rd}YqXXqXW|rt j XdS)Nzcannot wait on un-acquired lockTF) r+rrr)r:r'r;r<rr r=)rr?r5rrrrJEs&    zCondition.waitccs(|}x|s"|jEdH|}qW|S)N)rJ)rZ predicateresultrrrwait_forks  zCondition.wait_forrcCsL|jstdd}x2|jD](}||kr*P|js|d7}|jdqWdS)Nz!cannot notify on un-acquired lockrrF)r+rr'rCrD)rnidxr?rrrnotifyys  zCondition.notifycCs|jt|jdS)N)rPr1r')rrrr notify_allszCondition.notify_all)N)r) rrrrr/r rJrMrPrQrErr)r4rr!s    &  csPeZdZdddddZfddZdd Zd d Zed d ZddZ Z S)rrN)r$cCs>|dkrtd||_tj|_|dk r0||_n tj|_dS)Nrz$Semaphore initial value must be >= 0)rKrFr%r&r'r)r r*)rvaluer$rrrrs zSemaphore.__init__csNtj}|jrdn dj|j}|jr:dj|t|j}dj|dd|S)Nr+zunlocked,value:{}z {},waiters:{}z <{} [{}]>rr-)r.r/r+r0rFr'r1)rr2r3)r4rrr/s  zSemaphore.__repr__cCs0x*|jr*|jj}|js|jddSqWdS)N)r'popleftrCrD)rZwaiterrrr _wake_up_nexts   zSemaphore._wake_up_nextcCs |jdkS)Nr)rF)rrrrr+szSemaphore.lockedc cszxf|jdkrf|jj}|jj|y|EdHWq|j|jdkr\|j r\|jYqXqW|jd8_dS)NrrT)rFr)r:r'r;Zcancelr5rT)rr?rrrrs    zSemaphore.acquirecCs|jd7_|jdS)Nr)rFrT)rrrrrszSemaphore.release)r) rrrrr/rTr+r rrrErr)r4rrs   cs0eZdZdddfdd ZfddZZS) rrN)r$cs||_tj||ddS)N)r$) _bound_valuer.r)rrRr$)r4rrrszBoundedSemaphore.__init__cs"|j|jkrtdtjdS)Nz(BoundedSemaphore released too many times)rFrUrKr.r)r)r4rrrs zBoundedSemaphore.release)r)rrrrrrErr)r4rrs)__all__r%rr r Z coroutinesr r rrrrrrrrrrs    .ByM