[/ @sddddddgZddlZddlZddlZddlZddlmZd d lmZd d lm Z d d lm Z ydd lm Z m Z Wn*e k re ddddYnXeed\ZZej jZGdddeZ Gddde ZGdddeZGddde ZGddde ZGdddeZGdddeZGdddejZdS)LockRLock SemaphoreBoundedSemaphore ConditionEventN)time)context)process)util)SemLock sem_unlinkz*This platform lacks a functioning sem_openz( implementation, therefore, the requiredz+ synchronization primitives needed will notz function, see issue 3770.c@seZdZejZddZeddZddZ ddZ d d Z d d Z d dZ eddZdS)r c CsT|dkrtjj}n|j}tjdkpB|dk}xbtdD]H}y,tj||||j |}|_ Wnt k rYqRXPqRWt dt j d|j|jtjdkrdd} t j|| n|j jdk rPdd lm} | |j jt j|tj|j jfd d ndS) Nwin32forkdzcannot find name for semaphorezcreated semlock with handle %scSs|jjdS)N)_semlock _after_fork)objr1/usr/lib/python3.4/multiprocessing/synchronize.pyrHsz%SemLock.__init__.._after_forkr )registerZ exitpriorityr)r Z_default_contextZ get_contextZget_start_methodsysplatformrange_multiprocessingr _make_namerFileExistsErrorr debughandle _make_methodsZregister_after_forknamesemaphore_trackerrZFinalize_cleanup) selfkindvaluemaxvaluectxr"Z unlink_nowislrrrrr__init__3s.      zSemLock.__init__cCs(ddlm}t|||dS)Nr ) unregister)r#r-r)r"r-rrrr$Us zSemLock._cleanupcCs"|jj|_|jj|_dS)N)racquirerelease)r%rrrr![szSemLock._make_methodscCs |jjS)N)r __enter__)r%rrrr0_szSemLock.__enter__cGs|jj|S)N)r__exit__)r%argsrrrr1bszSemLock.__exit__cCsbtj||j}tjdkr@tjj|j}n |j}||j|j |j fS)Nr) r assert_spawningrrrZget_spawning_popenZduplicate_for_childr r&r(r")r%r+hrrr __getstate__es    zSemLock.__getstate__cCs8tjj||_tjd|d|jdS)Nz recreated blocker with handle %rr)rr Z_rebuildrr rr!)r%staterrr __setstate__nszSemLock.__setstate__cCs$dtjjdttjfS)Nz%s-%sZ semprefix)r current_processZ_confignextr _randrrrrrsszSemLock._make_nameN)__name__ __module__ __qualname__tempfileZ_RandomNameSequencer:r, staticmethodr$r!r0r1r5r7rrrrrr /s   "    r c@s7eZdZdddZddZddZdS) rr cCs tj|t|td|dS)Nr))r r, SEMAPHORE SEM_VALUE_MAX)r%r'r)rrrr,~szSemaphore.__init__cCs |jjS)N)r _get_value)r%rrr get_valueszSemaphore.get_valuec Cs6y|jj}Wntk r-d}YnXd|S)Nunknownz)rrB Exception)r%r'rrr__repr__s   zSemaphore.__repr__N)r;r<r=r,rCrFrrrrr|s  c@s+eZdZdddZddZdS)rr cCs tj|t||d|dS)Nr))r r,r@)r%r'r)rrrr,szBoundedSemaphore.__init__c CsBy|jj}Wntk r-d}YnXd||jjfS)NrDz))rrBrEr()r%r'rrrrFs   zBoundedSemaphore.__repr__N)r;r<r=r,rFrrrrrs c@s(eZdZddZddZdS)rcCs tj|tddd|dS)Nr r))r r,r@)r%r)rrrr,sz Lock.__init__c Csy|jjrStjj}tjjdkr|dtjj7}qnB|jjdkrqd}n$|jjdkrd}nd}Wnt k rd}YnXd |S) N MainThread|r NonerSomeOtherThreadSomeOtherProcessrDz) r_is_miner r8r" threadingcurrent_threadrB_countrE)r%r"rrrrFs     z Lock.__repr__N)r;r<r=r,rFrrrrrs  c@s(eZdZddZddZdS)rcCs tj|tddd|dS)Nr r))r r,RECURSIVE_MUTEX)r%r)rrrr,szRLock.__init__c Csy|jjrbtjj}tjjdkrP|dtjj7}n|jj}nT|jjdkrd \}}n0|jjdkrd \}}n d \}}Wnt k rd\}}YnXd ||fS)NrGrHr rIrrJnonzerorKrDz)zNoner)zSomeOtherThreadrQ)zSomeOtherProcessrQ)unknownrR) rrLr r8r"rMrNrOrBrE)r%r"countrrrrFs zRLock.__repr__N)r;r<r=r,rFrrrrrs  c@seZdZdddZddZddZdd Zd d Zd d ZddZ dddZ ddZ ddZ dddZ dS)rNcCsY|p|j|_|jd|_|jd|_|jd|_|jdS)Nr)r_lockr_sleeping_count _woken_count_wait_semaphorer!)r%lockr)rrrr,s zCondition.__init__cCs)tj||j|j|j|jfS)N)r r3rTrUrVrW)r%rrrr5s  zCondition.__getstate__cCs,|\|_|_|_|_|jdS)N)rTrUrVrWr!)r%r6rrrr7szCondition.__setstate__cCs |jjS)N)rTr0)r%rrrr0szCondition.__enter__cGs|jj|S)N)rTr1)r%r2rrrr1szCondition.__exit__cCs"|jj|_|jj|_dS)N)rTr.r/)r%rrrr!szCondition._make_methodsc CsRy&|jjj|jjj}Wntk r@d}YnXd|j|fS)NrDz)rUrrBrVrErT)r%Z num_waitersrrrrFs   zCondition.__repr__c Cs|jjjstd|jj|jjj}x!t|D]}|jjqJWz|jj d|SWd|j jx!t|D]}|jj qWXdS)Nz,must acquire() condition before using wait()T) rTrrLAssertionErrorrUr/rOrrWr.rV)r%timeoutrSr*rrrwaits   zCondition.waitcCs|jjjstd|jjd s7tx4|jjdrm|jjd}|s:tq:W|jjdr|jj|jj|jjdndS)Nzlock is not ownedF) rTrrLrYrWr.rVrUr/)r%resrrrnotifys  zCondition.notifycCs|jjjstd|jjd s7tx4|jjdrm|jjd}|s:tq:Wd}x-|jjdr|jj|d7}qwW|rx!t |D]}|jjqWx|jjdrqWndS)Nzlock is not ownedFrr ) rTrrLrYrWr.rVrUr/r)r%r\Zsleepersr*rrr notify_all s zCondition.notify_allcCs|}|r|S|dk r/t|}n d}d}xL|s|dk rp|t}|dkrpPqpn|j||}q>W|S)Nr)_timer[)r%Z predicaterZresultZendtimeZwaittimerrrwait_for7s        zCondition.wait_for)r;r<r=r,r5r7r0r1r!rFr[r]r^rarrrrrs         c@sOeZdZddZddZddZddZd d d Zd S) rcCs.|j|j|_|jd|_dS)Nr)rr_condr_flag)r%r)rrrr,OszEvent.__init__c CsM|jjz+|jjdr3|jjdSdSWd|jjXdS)NFT)rbr.rcr/)r%rrris_setSs  z Event.is_setc CsP|jjz.|jjd|jj|jjWd|jjXdS)NF)rbr.rcr/r^)r%rrrset]s   z Event.setc Cs6|jjz|jjdWd|jjXdS)NF)rbr.rcr/)r%rrrclearfs z Event.clearNc Cs|jjz]|jjdr2|jjn|jj||jjdre|jjdSdSWd|jjXdS)NFT)rbr.rcr/r[)r%rZrrrr[ms  z Event.wait)r;r<r=r,rdrerfr[rrrrrMs   c@seZdZddddZddZddZedd Zejd d Zed d Z e jd d Z dS)BarrierNc Cssddl}ddlm}||jdd}|j}|j|||||fd|_d|_dS)Nrr ) BufferWrapperr*r)structheaprhZcalcsizerr7_staterO) r%ZpartiesactionrZr)rirhwrapperZcondrrrr,s   zBarrier.__init__cCsC|\|_|_|_|_|_|jjjd|_dS)Nr*)_parties_action_timeoutrb_wrapperZcreate_memoryviewcast_array)r%r6rrrr7s$zBarrier.__setstate__cCs"|j|j|j|j|jfS)N)rnrorprbrq)r%rrrr5szBarrier.__getstate__cCs |jdS)Nr)rs)r%rrrrkszBarrier._statecCs||jdrrr_r r r r r ImportErrorlistrrPr@rAobjectrrrrrrrgrrrr s0       Mz3