3
,6`/                 @   s,  d dddddgZ ddlZddlZddlZddlZddlZdd	lmZ dd
lmZ ddlm	Z	 yddlm
Z
mZ W n ek
r   edY nX eed\ZZej
jZG dd deZ
G dd de
ZG dd deZG dd  d e
ZG dd de
ZG dd deZG dd deZG dd dejZdS )LockRLock	SemaphoreBoundedSemaphore	ConditionEvent    N   )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               @   s\   e Zd Zej Zdd Zedd Zdd Z	dd Z
d	d
 Zdd Zdd Zedd ZdS )r   c            C   s   |d krt jj }|j }tjdkp*|dk}xPtdD ]<}y tj|||| j	 | }| _
W n tk
rn   Y q6X P q6W tdtjd|j  | j  tjdkrdd }	tj| |	 | j
jd k	rdd	lm}
 |
| j
j tj| tj| j
jfd
d d S )Nwin32forkd   zcannot find name for semaphorezcreated semlock with handle %sc             S   s   | j j  d S )N)_semlock_after_fork)obj r   1/usr/lib/python3.6/multiprocessing/synchronize.pyr   G   s    z%SemLock.__init__.<locals>._after_forkr   )registerr   )Zexitpriority)r	   Z_default_contextZget_contextZget_start_methodsysplatformrange_multiprocessingr   
_make_namer   FileExistsErrorr   debughandle_make_methodsZregister_after_forknamesemaphore_trackerr   ZFinalize_cleanup)selfkindvaluemaxvaluectxr!   Z
unlink_nowislr   r   r   r   r   __init__2   s.    

zSemLock.__init__c             C   s    ddl m} t|  ||  d S )Nr   )
unregister)r"   r,   r   )r!   r,   r   r   r   r#   T   s    zSemLock._cleanupc             C   s   | j j| _| j j| _d S )N)r   acquirerelease)r$   r   r   r   r    Z   s    
zSemLock._make_methodsc             C   s
   | j j S )N)r   	__enter__)r$   r   r   r   r/   ^   s    zSemLock.__enter__c             G   s   | j j| S )N)r   __exit__)r$   argsr   r   r   r0   a   s    zSemLock.__exit__c             C   sD   t j|  | j}tjdkr,t j j|j}n|j}||j|j	|j
fS )Nr   )r	   assert_spawningr   r   r   Zget_spawning_popenZduplicate_for_childr   r%   r'   r!   )r$   r*   hr   r   r   __getstate__d   s    

zSemLock.__getstate__c             C   s,   t jj| | _tjd|d   | j  d S )Nz recreated blocker with handle %rr   )r   r   Z_rebuildr   r   r   r    )r$   stater   r   r   __setstate__m   s    zSemLock.__setstate__c               C   s   dt j jd ttjf S )Nz%s-%sZ	semprefix)r
   current_processZ_confignextr   _randr   r   r   r   r   r   s    zSemLock._make_nameN)__name__
__module____qualname__tempfileZ_RandomNameSequencer9   r+   staticmethodr#   r    r/   r0   r4   r6   r   r   r   r   r   r   .   s   "	r   c               @   s&   e Zd Zd	ddZdd Zdd ZdS )
r   r   c            C   s   t j| t|t|d d S )N)r(   )r   r+   	SEMAPHORESEM_VALUE_MAX)r$   r&   r(   r   r   r   r+   }   s    zSemaphore.__init__c             C   s
   | j j S )N)r   
_get_value)r$   r   r   r   	get_value   s    zSemaphore.get_valuec             C   s8   y| j j }W n tk
r&   d}Y nX d| jj|f S )Nunknownz<%s(value=%s)>)r   rA   	Exception	__class__r:   )r$   r&   r   r   r   __repr__   s
    
zSemaphore.__repr__N)r   )r:   r;   r<   r+   rB   rF   r   r   r   r   r   {   s   
c               @   s   e Zd ZdddZdd ZdS )r   r   c            C   s   t j| t|||d d S )N)r(   )r   r+   r?   )r$   r&   r(   r   r   r   r+      s    zBoundedSemaphore.__init__c             C   s>   y| j j }W n tk
r&   d}Y nX d| jj|| j jf S )NrC   z<%s(value=%s, maxvalue=%s)>)r   rA   rD   rE   r:   r'   )r$   r&   r   r   r   rF      s    
zBoundedSemaphore.__repr__N)r   )r:   r;   r<   r+   rF   r   r   r   r   r      s   
c               @   s   e Zd Zdd Zdd ZdS )r   c            C   s   t j| tdd|d d S )Nr   )r(   )r   r+   r?   )r$   r(   r   r   r   r+      s    zLock.__init__c             C   s   yf| j j r8tj j}tj jdkrd|dtj j 7 }n,| j j dkrLd}n| j j dkr`d}nd}W n t	k
r~   d}Y nX d	| j
j|f S )
N
MainThread|r   Noner   SomeOtherThreadSomeOtherProcessrC   z<%s(owner=%s)>)r   _is_miner
   r7   r!   	threadingcurrent_threadrA   _countrD   rE   r:   )r$   r!   r   r   r   rF      s    


zLock.__repr__N)r:   r;   r<   r+   rF   r   r   r   r   r      s   c               @   s   e Zd Zdd Zdd ZdS )r   c            C   s   t j| tdd|d d S )Nr   )r(   )r   r+   RECURSIVE_MUTEX)r$   r(   r   r   r   r+      s    zRLock.__init__c             C   s   y|| j j rBtj j}tj jdkr6|dtj j 7 }| j j }n8| j j dkrZd\}}n | j j dkrrd\}}nd\}}W n t	k
r   d\}}Y nX d
| j
j||f S )NrG   rH   r   rI   r   rJ   nonzerorK   rC   z<%s(%s, %s)>)rI   r   )rJ   rQ   )rK   rQ   )rC   rC   )r   rL   r
   r7   r!   rM   rN   rO   rA   rD   rE   r:   )r$   r!   countr   r   r   rF      s    



zRLock.__repr__N)r:   r;   r<   r+   rF   r   r   r   r   r      s   c               @   sj   e Zd ZdddZdd Zdd Zdd	 Zd
d Zdd Zdd Z	dddZ
dd Zdd ZdddZdS )r   Nc            C   s>   |p
|j  | _|jd| _|jd| _|jd| _| j  d S )Nr   )r   _lockr   _sleeping_count_woken_count_wait_semaphorer    )r$   lockr(   r   r   r   r+      s
    zCondition.__init__c             C   s   t j|  | j| j| j| jfS )N)r	   r2   rS   rT   rU   rV   )r$   r   r   r   r4      s    
zCondition.__getstate__c             C   s    |\| _ | _| _| _| j  d S )N)rS   rT   rU   rV   r    )r$   r5   r   r   r   r6      s    zCondition.__setstate__c             C   s
   | j j S )N)rS   r/   )r$   r   r   r   r/      s    zCondition.__enter__c             G   s   | j j| S )N)rS   r0   )r$   r1   r   r   r   r0      s    zCondition.__exit__c             C   s   | j j| _| j j| _d S )N)rS   r-   r.   )r$   r   r   r   r       s    
zCondition._make_methodsc             C   sJ   y| j jj | jjj  }W n tk
r4   d}Y nX d| jj| j|f S )NrC   z<%s(%s, %s)>)rT   r   rA   rU   rD   rE   r:   rS   )r$   Znum_waitersr   r   r   rF      s    

zCondition.__repr__c             C   s   | j jj std| jj  | j jj }xt|D ]}| j j  q4W z| jj	d|S | j
j  xt|D ]}| j j	  qjW X d S )Nz,must acquire() condition before using wait()T)rS   r   rL   AssertionErrorrT   r.   rO   r   rV   r-   rU   )r$   timeoutrR   r)   r   r   r   wait   s    

zCondition.waitc             C   s|   | j jj std| jjd s&tx$| jjdrJ| jjd}|s(tq(W | jjdrx| jj  | jj  | jjd d S )Nzlock is not ownedF)	rS   r   rL   rX   rV   r-   rU   rT   r.   )r$   resr   r   r   notify  s    

zCondition.notifyc             C   s   | j jj std| jjd s&tx$| jjdrJ| jjd}|s(tq(W d}x"| jjdrr| jj  |d7 }qRW |rxt	|D ]}| jj  qW x| jjdrqW d S )Nzlock is not ownedFr   r   )
rS   r   rL   rX   rV   r-   rU   rT   r.   r   )r$   r[   Zsleepersr)   r   r   r   
notify_all  s    
zCondition.notify_allc             C   sh   | }|r|S |d k	r$t j | }nd }d }x6|sb|d k	rP|t j  }|dkrPP | j| | }q.W |S )Nr   )timeZ	monotonicrZ   )r$   Z	predicaterY   resultZendtimeZwaittimer   r   r   wait_for6  s    

zCondition.wait_for)N)N)N)r:   r;   r<   r+   r4   r6   r/   r0   r    rF   rZ   r\   r]   r`   r   r   r   r   r      s   

c               @   s6   e Zd Zdd Zdd Zdd Zdd Zdd
dZd	S )r   c            C   s    |j |j | _|jd| _d S )Nr   )r   r   _condr   _flag)r$   r(   r   r   r   r+   N  s    zEvent.__init__c          	   C   s0   | j   | jjdr"| jj  dS dS Q R X d S )NFT)ra   rb   r-   r.   )r$   r   r   r   is_setR  s
    
zEvent.is_setc          	   C   s6   | j & | jjd | jj  | j j  W d Q R X d S )NF)ra   rb   r-   r.   r]   )r$   r   r   r   setY  s    
z	Event.setc          	   C   s"   | j  | jjd W d Q R X d S )NF)ra   rb   r-   )r$   r   r   r   clear_  s    zEvent.clearNc          	   C   sT   | j D | jjdr | jj  n| j j| | jjdrF| jj  dS dS Q R X d S )NFT)ra   rb   r-   r.   rZ   )r$   rY   r   r   r   rZ   c  s    
z
Event.wait)N)r:   r;   r<   r+   rc   rd   re   rZ   r   r   r   r   r   L  s
   c               @   sZ   e Zd ZdddZdd Zdd Zedd	 Zejd
d	 Zedd Z	e	jdd Z	dS )BarrierNc      	      C   sR   dd l }ddlm} ||jdd }|j }| j|||||f d| _d| _d S )Nr   r   )BufferWrapperr)   r   )structheaprg   Zcalcsizer   r6   _staterO   )	r$   ZpartiesactionrY   r(   rh   rg   wrapperZcondr   r   r   r+   u  s    zBarrier.__init__c             C   s.   |\| _ | _| _| _| _| jj jd| _d S )Nr)   )_parties_action_timeoutra   _wrapperZcreate_memoryviewcast_array)r$   r5   r   r   r   r6   ~  s    zBarrier.__setstate__c             C   s   | j | j| j| j| jfS )N)rm   rn   ro   ra   rp   )r$   r   r   r   r4     s    zBarrier.__getstate__c             C   s
   | j d S )Nr   )rr   )r$   r   r   r   rj     s    zBarrier._statec             C   s   || j d< d S )Nr   )rr   )r$   r&   r   r   r   rj     s    c             C   s
   | j d S )Nr   )rr   )r$   r   r   r   rO     s    zBarrier._countc             C   s   || j d< d S )Nr   )rr   )r$   r&   r   r   r   rO     s    )NN)
r:   r;   r<   r+   r6   r4   propertyrj   setterrO   r   r   r   r   rf   s  s   
	rf   zRThis platform lacks a functioning sem_open implementation, therefore, the requiredz}This platform lacks a functioning sem_open implementation, therefore, the required synchronization primitives needed will notzThis platform lacks a functioning sem_open implementation, therefore, the required synchronization primitives needed will not function, see issue 3770.)__all__rM   r   r=   r   r^    r	   r
   r   r   r   ImportErrorlistr   rP   r?   r@   objectr   r   r   r   r   r   rf   r   r   r   r   <module>   s0    Mz'