3 ^c@sxddlZddlZddlmZddlmZddl m Z ddl m Z mZyddlmZWn ek rtddlmZYnXddd d d d d ddddddddddddddgZejZejZejZejZejZy ejZWne k rdZYnXej!Z![da"da#ddZ$ddZ%eZ&ddZGdd d Z'e'Z(Gd!d d Z)Gd"ddZ*Gd#dde*Z+Gd$ddZ,Gd%ddZ-Gd&dde.Z/ej0Z1e1d?d(d)Z2ea3iZ4iZ5e Z6Gd*ddZ7Gd+dde7Z8Gd,d-d-e7Z9Gd.d/d/e7Z:d0d Z;e;Zd2d3Z?d4d Z@dd5lmAZAe9aBd6d7ZCd8d9ZDd:d ZEydd;lmFZGWn"ek rjddZIdS)@N) monotonic) format_exc)WeakSet)islicecount)deque get_ident active_count Conditioncurrent_thread enumerate main_thread TIMEOUT_MAXEventLockRLock SemaphoreBoundedSemaphoreThreadBarrierBrokenBarrierErrorTimer ThreadError setprofilesettracelocal stack_sizecC|adSN) _profile_hookZfuncr !/usr/lib64/python3.6/threading.pyr2cCrr) _trace_hookrr r r!r<r"cOstdkrt||St||Sr)_CRLock_PyRLock)argskwargsr r r!rJs  c@sReZdZddZddZdddZeZd d Zd d Zd dZ ddZ ddZ dS)_RLockcCst|_d|_d|_dSNr)_allocate_lock_block_owner_countselfr r r!__init__asz_RLock.__init__cCs^|j}yt|j}Wntk r(YnXd|jjr:dnd|jj|jj||j t t |fS)Nz)<%s %s.%s object owner=%r count=%d at %s>lockedZunlocked) r,_activenameKeyErrorr+r1 __class__ __module__ __qualname__r-ZhexZid)r/ownerr r r!__repr__fsz_RLock.__repr__TcCsDt}|j|kr"|jd7_dS|jj||}|r@||_d|_|SNr:)rr,r-r+acquire)r/blockingtimeoutZmercr r r!r<us z_RLock.acquirecCs<|jtkrtd|jd|_}|s8d|_|jjdS)Ncannot release un-acquired lockr:)r,r RuntimeErrorr-r+release)r/rr r r!rBs  z_RLock.releasecC |jdSrrBr/tZvZtbr r r!__exit__z_RLock.__exit__cCs|jj|\|_|_dSr)r+r<r-r,)r/Zstater r r!_acquire_restores z_RLock._acquire_restorecCs<|jdkrtd|j}d|_|j}d|_|jj||fS)Nrr@)r-rAr,r+rB)r/rr8r r r! _release_saves  z_RLock._release_savecCs |jtkSr)r,rr.r r r! _is_ownedrHz_RLock._is_ownedNTrL) __name__r6r7r0r9r< __enter__rBrGrIrJrKr r r r!r(Ws  $ r(c@speZdZdddZddZddZdd Zd d Zd d ZddZ dddZ dddZ dddZ ddZ e ZdS)r NcCs|dkrt}||_|j|_|j|_y |j|_Wntk rDYnXy |j|_Wntk rfYnXy |j|_Wntk rYnXt|_ dSr) r_lockr<rBrJAttributeErrorrIrK_deque_waitersr/lockr r r!r0s$   zCondition.__init__cCs |jjSr)rPrOr.r r r!rOrHzCondition.__enter__cGs |jj|Sr)rPrG)r/r&r r r!rGrHzCondition.__exit__cCsd|jt|jfS)Nz)rPlenrSr.r r r!r9rHzCondition.__repr__cC|jjdSr)rPrBr.r r r!rJrHzCondition._release_savecCrWr)rPr<)r/Zxr r r!rIrHzCondition._acquire_restorecCs"|jjdr|jjdSdSdS)NrFT)rPr<rBr.r r r!rKs  zCondition._is_ownedcCs|jstdt}|j|jj||j}d}z:|dkrN|jd}n |dkrd|jd|}n |jd}|S|j||sy|jj|Wnt k rYnXXdS)Nzcannot wait on un-acquired lockFTr) rKrAr*r<rSZappendrJrIremove ValueError)r/r>waiterZ saved_stateZgotitr r r!waits*   zCondition.waitcCs\d}|}|}xH|sV|dk rD|dkr0t|}n|t}|dkrDP|j||}qW|Sr))_timer[)r/Z predicater>endtimeZwaittimeZresultr r r!wait_for7s    zCondition.wait_forr:c Csj|jstd|j}tt||}|s,dSx8|D]0}|jy|j|Wq2tk r`Yq2Xq2WdS)Nz!cannot notify on un-acquired lock)rKrArSrR_islicerBrXrY)r/ZnZ all_waitersZwaiters_to_notifyrZr r r!notifyNs  zCondition.notifycCs|jt|jdSr)r`rVrSr.r r r! notify_alleszCondition.notify_allrrrr:)rNr6r7r0rOrGr9rJrIrKr[r^r`raZ notifyAllr r r r!r s  0   c@s4eZdZd ddZd ddZeZdd Zd d ZdS)rr:cCs&|dkrtdtt|_||_dS)Nrz$semaphore initial value must be >= 0)rYr r_cond_valuer/Zvaluer r r!r0}s zSemaphore.__init__TNc Cs| r|dk rtdd}d}|jjxb|jdkrv|s8P|dk rh|dkrTt|}n|t}|dkrhP|jj|q(W|jd8_d}WdQRX|S)Nz.can't specify timeout for non-blocking acquireFrr:T)rYrcrdr\r[)r/r=r>r?r]r r r!r<s$   zSemaphore.acquirec Cs.|j|jd7_|jjWdQRXdSr;)rcrdr`r.r r r!rBszSemaphore.releasecCrCrrDrEr r r!rGrHzSemaphore.__exit__rb)TN)rNr6r7r0r<rOrBrGr r r r!rqs   - c@seZdZdddZddZdS)rr:cCstj||||_dSr)rr0_initial_valuerer r r!r0 zBoundedSemaphore.__init__c CsB|j2|j|jkrtd|jd7_|jjWdQRXdS)Nz!Semaphore released too many timesr:)rcrdrfrYr`r.r r r!rBs  zBoundedSemaphore.releaseNrb)rNr6r7r0rBr r r r!rs c@sBeZdZddZddZddZeZddZd d Zdd d Z d S)rcCstt|_d|_dSNF)r rrc_flagr.r r r!r0rgzEvent.__init__cCs|jjtdSr)rcr0rr.r r r!_reset_internal_lockszEvent._reset_internal_lockscC|jSr)rir.r r r!is_setrkz Event.is_setc Cs&|jd|_|jjWdQRXdSNT)rcrirar.r r r!setsz Event.setc Cs|j d|_WdQRXdSrh)rcrir.r r r!clear z Event.clearNc Cs,|j|j}|s|jj|}|SQRXdSr)rcrir[)r/r>Zsignaledr r r!r[s  z Event.waitr) rNr6r7r0rjrmZisSetrorpr[r r r r!rs   c@s|eZdZdddZdddZddZdd Zd d Zd d ZddZ ddZ ddZ e ddZ e ddZe ddZdS)rNcCs.tt|_||_||_||_d|_d|_dSr))r rrc_action_timeout_parties_stater-)r/partiesZactionr>r r r!r0?s zBarrier.__init__cCs|dkr|j}|jb|j|j}|jd7_z&|d|jkrL|jn |j||S|jd8_|jXWdQRXdSr;)rsrc_enterr-rt_release_wait_exit)r/r>Zindexr r r!r[Os   z Barrier.waitcCs,x|jdkr|jjqW|jdkr(tdS)Nr:rrLrLr:)rurcr[rr.r r r!rwms   zBarrier._enterc Cs>y"|jr|jd|_|jjWn|jYnXdSr;)rrrurcra_breakr.r r r!rxxszBarrier._releasecs4jjfdd|s"jtjdkr0tdS)Ncs jdkSr)rur r.r r!ZszBarrier._wait..r)rcr^r|rrur/r>r r.r!rys  z Barrier._waitcCs(|jdkr$|jdkr$d|_|jjdS)Nrr:rLr{)r-rurcrar.r r r!rzs  z Barrier._exitc CsT|jD|jdkr6|jdkr$d|_q<|jdkr||_n tj|_d|_ d|_ t |_ d|_ d|_tj|_tj|dS)NFT)_targetstrr_name_args_kwargs _daemonicr r_ident _tstate_lockr_started _is_stopped _initialized_sysstderr_stderr _danglingZadd)r/ZgroupZtargetr3r&r'rr r r!r0s  zThread.__init__cCs(|jj|r|jn d|_d|_dSrn)rrj_set_tstate_lockrr)r/is_aliver r r!rj"s   zThread._reset_internal_lockscCs^d}|jjrd}|j|jr$d}|jr2|d7}|jdk rJ|d|j7}d|jj|j|fS)NZinitialZstartedZstoppedz daemonz %sz <%s(%s, %s)>) rrmrrrrr5rNr)r/Zstatusr r r!r9.s  zThread.__repr__cCs|jstd|jjr tdt|t|<WdQRXyt|jfWn,tk rtt t|=WdQRXYnX|jj dS)Nzthread.__init__() not calledz threads can only be started once) rrArrm_active_limbo_lock_limbo_start_new_thread _bootstrapZ Exceptionr[r.r r r!start<s  z Thread.startc Cs.z|jr|j|j|jWd|`|`|`XdSr)rrrr.r r r!runUs z Thread.runc Cs2y |jWn |jr&tdkr&dSYnXdSr)_bootstrap_innerrrr.r r r!rfs  zThread._bootstrapcCs t|_dSr)rrr.r r r! _set_identzrHzThread._set_identcCst|_|jjdSr) _set_sentinelrr<r.r r r!r}szThread._set_tstate_lockc2CszT|j|j|jjt|t|j<t|=WdQRXtrLt j tt rZt j t zy |j Wntk r|Ynt rt jdk rtd|jtft jdn|jdk rH|j\}}}zxtd|jd|jdtd|jdx6|r$td|jjj|j|jjjf|jd|j}qWtd||f|jdWd~~~XYnXWdXWdt y tt=Wn YnXWdQRXXdS)NzException in thread %s: %s)ZfilezException in thread z2 (most likely raised during interpreter shutdown):z"Traceback (most recent call last):z File "%s", line %s, in %sz%s: %s)rrrrorr2rrr#rrrrrZ SystemExitrZprintr3 _format_excr _exc_infoZtb_frameZf_codeZ co_filenameZ tb_linenoZco_nameZtb_nextr)r/Zexc_typeZ exc_valueZexc_tbr r r!rsT         zThread._bootstrap_innercCs|j}|dk rd|_d|_dSrn)rrrTr r r!_stopsz Thread._stopcCsByttt=WdQRXWn tk r<dtjkr8YnXdS)NZdummy_threading)rr2rr4rZmodulesr.r r r!_deletes  zThread._deletecCsZ|jstd|jjs td|tkr2td|dkrD|jn|jt|dddS)NThread.__init__() not calledz'cannot join thread before it is startedzcannot join current threadr)r>)rrArrmr _wait_for_tstate_lockZmaxr~r r r!joins   z Thread.joinTr:cCs0|j}|dkrn|j||r,|j|jdSr)rr<rBr)r/Zblockr>rUr r r!r&s  zThread._wait_for_tstate_lockcCrlr)rr.r r r!r34s z Thread.namecCst||_dSr)rrr/r3r r r!r3?rcCrlr)rr.r r r!identDs z Thread.identcCs(|js|jj rdS|jd|j Srh)rrrmrr.r r r!rPs  zThread.is_alivecCrlr)rr.r r r!r`sz Thread.daemoncCs*|jstd|jjr td||_dS)Nrz)cannot set daemon status of active thread)rrArrmrr/Zdaemonicr r r!rps  cCrlrrr.r r r!isDaemonxrHzThread.isDaemoncC ||_dSrrrr r r! setDaemon{rHzThread.setDaemoncCrlrr3r.r r r!getName~rHzThread.getNamecCrrrrr r r!setNamerHzThread.setNamerrLrM)rNr6r7rrZexc_inforr0rjr9rrrrrrrrrrrr3ZsetterrrZisAliverrrrrr r r r!rs6 , A# &    c@s&eZdZdddZddZddZdS) rNcCsFtj|||_||_|dk r"|ng|_|dk r4|ni|_t|_dSr)rr0intervalfunctionr&r'rfinished)r/rrr&r'r r r!r0s  zTimer.__init__cCrWr)rror.r r r!cancelrkz Timer.cancelcCs6|jj|j|jjs(|j|j|j|jjdSr)rr[rrmrr&r'ror.r r r!rs z Timer.runr)rNr6r7r0rrr r r r!rs c@seZdZddZdS) _MainThreadc CsHtj|ddd|j|jj|jt|t|j<WdQRXdS)NZ MainThreadFr3r) rr0rrrorrr2rr.r r r!r0s  z_MainThread.__init__N)rNr6r7r0r r r r!rsrc@s.eZdZddZddZddZd dd ZdS) _DummyThreadc CsDtj|tddd|jj|jt|t|j<WdQRXdS)NzDummy-%dTr) rr0rrrorrr2rr.r r r!r0s  z_DummyThread.__init__cCdSrr r.r r r!rrHz_DummyThread._stopcCsdSrnr r.r r r!rrkz_DummyThread.is_aliveNcCrrr r~r r r!rrHz_DummyThread.joinr)rNr6r7r0rrrr r r r!rsrc Cs&y ttStk r tSXdSr)r2rr4rr r r r!r s c Cs tttttSQRXdSr)rrVr2rr r r r!rscCsttjttjSr)listr2valuesrr r r r! _enumeraterkrc Cs(tttjttjSQRXdSr)rrr2rrr r r r!r s)rcCs8tj}|jtjt}x|r2|jt}qWdSr) _main_threadrrBr_pickSomeNonDaemonThreadr)ZtlockrFr r r! _shutdownsrcCs(x"tD]}|j r|jr|SqWdSr)r rr)rFr r r!rs rcCstSr)rr r r r!r s)_local)rc Cstai}t}|at|tt}|jtxF|D]>}||kr`|jdt }||_ |||<q4|jd|j q4Wt j tj tj|WdQRXdS)NTF)r*rr rrorZupdaterrjrrrrrpr2)Z new_activeZcurrentZthreadsZthreadrr r r! _after_fork(s&        r)r)JZsysrZ_threadZtimerr\Z tracebackrrZ _weakrefsetrZ itertoolsrr_rr-Z _collectionsrrRZ ImportErrorZ collectionsZ__all__Zstart_new_threadrZ allocate_lockr*rrZerrorrrr$rQr rr#rrrr(r%r rrrrrArZ__next__rrrr2rrrrrrr Z currentThreadrZ activeCountrr rrrrr rrZ_threading_localrr r r r!Zs           q'P&O '