B
    v9a)>                 @   s   d Z ddlZddlZddlmZ ddlmZ ddlmZ G dd dZG d	d
 d
Z	G dd de	Z
G dd dZG dd de	ZG dd de	ZG dd deZdS ))LockEvent	Condition	SemaphoreBoundedSemaphore    N   )events)futures)	coroutinec               @   s$   e Zd Zdd Zdd Zdd ZdS )_ContextManagerc             C   s
   || _ d S )N)_lock)selflock r   /usr/lib/python3.7/locks.py__init__    s    z_ContextManager.__init__c             C   s   d S )Nr   )r   r   r   r   	__enter__#   s    z_ContextManager.__enter__c             G   s   z| j   W d d | _ X d S )N)r   release)r   argsr   r   r   __exit__(   s    z_ContextManager.__exit__N)__name__
__module____qualname__r   r   r   r   r   r   r   r      s   r   c               @   sH   e Zd Zdd Zdd Zedd Zdd Zd	d
 Zdd Z	dd Z
dS )_ContextManagerMixinc             C   s   t dd S )Nz9"yield from" should be used as context manager expression)RuntimeError)r   r   r   r   r   0   s    z_ContextManagerMixin.__enter__c             G   s   d S )Nr   )r   r   r   r   r   r   4   s    z_ContextManagerMixin.__exit__c             c   s&   t jdtdd |  E d H  t| S )NzD'with (yield from lock)' is deprecated use 'async with lock' instead   )
stacklevel)warningswarnDeprecationWarningacquirer   )r   r   r   r   __iter__9   s    
z_ContextManagerMixin.__iter__c                s   |   I d H  t| S )N)r    r   )r   r   r   r   Z__acquire_ctxP   s    z"_ContextManagerMixin.__acquire_ctxc             C   s   t jdtdd |   S )Nz='with await lock' is deprecated use 'async with lock' insteadr   )r   )r   r   r   !_ContextManagerMixin__acquire_ctx	__await__)r   r   r   r   r#   T   s    
z_ContextManagerMixin.__await__c                s   |   I d H  d S )N)r    )r   r   r   r   
__aenter__[   s    z_ContextManagerMixin.__aenter__c                s   |    d S )N)r   )r   exc_typeexctbr   r   r   	__aexit__a   s    z_ContextManagerMixin.__aexit__N)r   r   r   r   r   r
   r!   r"   r#   r$   r(   r   r   r   r   r   /   s   r   c                   sJ   e Zd ZddddZ fddZdd Zd	d
 Zdd Zdd Z  Z	S )r   N)loopc            C   s.   t  | _d| _|d k	r || _n
t | _d S )NF)collectionsdeque_waiters_locked_loopr   get_event_loop)r   r)   r   r   r   r      s
    
zLock.__init__c                sL   t   }| jrdnd}| jr2| dt| j }d|dd  d| dS )	Nlockedunlockedz
, waiters:<r   z [z]>)super__repr__r-   r,   len)r   resextra)	__class__r   r   r5      s
    
zLock.__repr__c             C   s   | j S )N)r-   )r   r   r   r   r0      s    zLock.lockedc          	      s   | j s$tdd | jD r$d| _ dS | j }| j| y"z|I d H  W d | j| X W n& tjk
r   | j s|| 	   Y nX d| _ dS )Nc             s   s   | ]}|  V  qd S )N)	cancelled).0wr   r   r   	<genexpr>   s    zLock.acquire.<locals>.<genexpr>T)
r-   allr,   r.   create_futureappendremover	   CancelledError_wake_up_first)r   futr   r   r   r       s    
zLock.acquirec             C   s"   | j rd| _ |   ntdd S )NFzLock is not acquired.)r-   rC   r   )r   r   r   r   r      s    
zLock.releasec             C   s>   yt t| j}W n tk
r&   d S X | s:|d d S )NT)nextiterr,   StopIterationdone
set_result)r   rD   r   r   r   rC      s    zLock._wake_up_first)
r   r   r   r   r5   r0   r    r   rC   __classcell__r   r   )r9   r   r   e   s   6r   c                   sJ   e Zd ZddddZ fddZdd Zd	d
 Zdd Zdd Z  Z	S )r   N)r)   c            C   s.   t  | _d| _|d k	r || _n
t | _d S )NF)r*   r+   r,   _valuer.   r   r/   )r   r)   r   r   r   r      s
    
zEvent.__init__c                sL   t   }| jrdnd}| jr2| dt| j }d|dd  d| dS )	NsetZunsetz
, waiters:r2   r   r3   z [z]>)r4   r5   rK   r,   r6   )r   r7   r8   )r9   r   r   r5      s
    
zEvent.__repr__c             C   s   | j S )N)rK   )r   r   r   r   is_set  s    zEvent.is_setc             C   s2   | j s.d| _ x | jD ]}| s|d qW d S )NT)rK   r,   rH   rI   )r   rD   r   r   r   rL     s
    z	Event.setc             C   s
   d| _ d S )NF)rK   )r   r   r   r   clear  s    zEvent.clearc          	      sB   | j r
dS | j }| j| z|I d H  dS | j| X d S )NT)rK   r.   r?   r,   r@   rA   )r   rD   r   r   r   wait  s    

z
Event.wait)
r   r   r   r   r5   rM   rL   rN   rO   rJ   r   r   )r9   r   r      s   	r   c                   sN   e Zd ZdddddZ fddZdd Zd	d
 ZdddZdd Z  Z	S )r   N)r)   c            C   sp   |d k	r|| _ n
t | _ |d kr0t| j d}n|j | j k	rDtd|| _|j| _|j| _|j| _t	
 | _d S )N)r)   z"loop argument must agree with lock)r.   r   r/   r   
ValueErrorr   r0   r    r   r*   r+   r,   )r   r   r)   r   r   r   r   5  s    
zCondition.__init__c                sN   t   }|  rdnd}| jr4| dt| j }d|dd  d| dS )	Nr0   r1   z
, waiters:r2   r   r3   z [z]>)r4   r5   r0   r,   r6   )r   r7   r8   )r9   r   r   r5   H  s
    
zCondition.__repr__c                s   |   std|   z8| j }| j| z|I d H  dS | j| X W d d}x4y|  I d H  P W qX t	j
k
r   d}Y qXX qXW |rt	j
X d S )Nzcannot wait on un-acquired lockTF)r0   r   r   r.   r?   r,   r@   rA   r    r	   rB   )r   rD   r:   r   r   r   rO   O  s&    

zCondition.waitc                s(   | }x|s"|   I d H  | }qW |S )N)rO   )r   Z	predicateresultr   r   r   wait_fort  s
    
zCondition.wait_forr   c             C   sL   |   stdd}x2| jD ](}||kr*P | s|d7 }|d qW d S )Nz!cannot notify on un-acquired lockr   r   F)r0   r   r,   rH   rI   )r   nidxrD   r   r   r   notify  s    zCondition.notifyc             C   s   |  t| j d S )N)rU   r6   r,   )r   r   r   r   
notify_all  s    zCondition.notify_all)N)r   )
r   r   r   r   r5   rO   rR   rU   rV   rJ   r   r   )r9   r   r   +  s   
%
r   c                   sL   e Zd ZdddddZ fddZdd	 Zd
d Zdd Zdd Z  Z	S )r   r   N)r)   c            C   s>   |dk rt d|| _t | _|d k	r0|| _n
t | _d S )Nr   z$Semaphore initial value must be >= 0)rP   rK   r*   r+   r,   r.   r   r/   )r   valuer)   r   r   r   r     s    
zSemaphore.__init__c                sV   t   }|  rdn
d| j }| jr<| dt| j }d|dd  d| dS )	Nr0   zunlocked, value:z
, waiters:r2   r   r3   z [z]>)r4   r5   r0   rK   r,   r6   )r   r7   r8   )r9   r   r   r5     s
    
zSemaphore.__repr__c             C   s0   x*| j r*| j  }| s|d  d S qW d S )N)r,   popleftrH   rI   )r   Zwaiterr   r   r   _wake_up_next  s
    

zSemaphore._wake_up_nextc             C   s
   | j dkS )Nr   )rK   )r   r   r   r   r0     s    zSemaphore.lockedc                sx   xd| j dkrd| j }| j| y|I d H  W q   |  | j dkrZ| sZ|    Y qX qW |  j d8  _ dS )Nr   r   T)rK   r.   r?   r,   r@   Zcancelr:   rY   )r   rD   r   r   r   r      s    	
zSemaphore.acquirec             C   s   |  j d7  _ |   d S )Nr   )rK   rY   )r   r   r   r   r     s    zSemaphore.release)r   )
r   r   r   r   r5   rY   r0   r    r   rJ   r   r   )r9   r   r     s   
r   c                   s0   e Zd Zddd fddZ fddZ  ZS )	r   r   N)r)   c               s   || _ t j||d d S )N)r)   )_bound_valuer4   r   )r   rW   r)   )r9   r   r   r     s    zBoundedSemaphore.__init__c                s"   | j | jkrtdt   d S )Nz(BoundedSemaphore released too many times)rK   rZ   rP   r4   r   )r   )r9   r   r   r     s    zBoundedSemaphore.release)r   )r   r   r   r   r   rJ   r   r   )r9   r   r     s   r   )__all__r*   r    r   r	   Z
coroutinesr
   r   r   r   r   r   r   r   r   r   r   r   <module>   s   "6 AwK