U
    ~fh|                      @  s   U d dl mZ d dl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	m
Z
mZmZ eedZe Zded< edZdd	d
dZdd	ddZdddddZG dd dZdddddZG dd dZdS )    )annotationsN)AnyCallableOptionalTypeVarregister_at_forkzweakref.WeakSet[threading.Lock]_forkable_locks_Tthreading.Lockreturnc                  C  s   t  } trt|  | S )zpRepresents a lock that is tracked upon instantiation using a WeakSet and
    reset by pymongo upon forking.
    )	threadingLock_HAS_REGISTER_AT_FORKr   addlock r   0/tmp/pip-unpacked-wheel-36gvocj8/pymongo/lock.py_create_lock    s    
r   Nonec                  C  s   t D ]} |  r|   qd S N)r   lockedreleaser   r   r   r   _release_locks*   s    r   r   r   c                 C  s   | S r   r   r   r   r   r   _Lock2   s    r   c                   @  s   e Zd ZdZdddddZdd	d
d	dddZdd	d
d	dddZddddZd dddZd dddZ	dddddddZ
dddddddZdS ) _ALock_lockr
   r   r   c                 C  s
   || _ d S r   r   )selfr   r   r   r   __init__9   s    z_ALock.__init__Tboolfloatblockingtimeoutr   c                 C  s   | j j||dS )N)r&   r'   r   acquire)r    r&   r'   r   r   r   r)   <   s    z_ALock.acquirec                   s`   |dkrt  }| jjdd}|r&dS |dkrBt  | |krBdS |sJdS tdI d H  qd S Nr   F)r&   T)time	monotonicr   r)   asynciosleepr    r&   r'   ZtstartZacquiredr   r   r   	a_acquire?   s    z_ALock.a_acquirer   c                 C  s   | j   d S r   )r   r   r    r   r   r   r   L   s    z_ALock.releasec                   s   |   I d H  | S r   )r0   r1   r   r   r   
__aenter__O   s    z_ALock.__aenter__c                 C  s   | j   | S r   r(   r1   r   r   r   	__enter__S   s    
z_ALock.__enter__r   exc_typeexctbr   c                 C  s   |    d S r   r   r    r5   r6   r7   r   r   r   __exit__W   s    z_ALock.__exit__c                   s   |    d S r   r8   r9   r   r   r   	__aexit__Z   s    z_ALock.__aexit__N)Tr"   )Tr"   )__name__
__module____qualname__	__slots__r!   r)   r0   r   r2   r3   r:   r;   r   r   r   r   r   6   s   r   zasyncio.Future)futr   c                 C  s   |   s| d d S )NF)doneZ
set_result)r@   r   r   r   _safe_set_result^   s    rB   c                   @  s   e Zd ZdZdddddZd.d	d
d	dddZd/dd	dddZdddddZd0dddddZddddZ	d	dd d!Z
ddd"d#Zd dd$d%Zd dd&d'Zd(d(d(dd)d*d+Zd(d(d(dd)d,d-ZdS )1_ACondition)
_condition_waiterszthreading.Conditionr   )	conditionr   c                 C  s   || _ t | _d S r   )rD   collectionsdequerE   )r    rF   r   r   r   r!   g   s    z_ACondition.__init__Tr"   r#   r$   r%   c                   s`   |dkrt  }| jjdd}|r&dS |dkrBt  | |krBdS |sJdS tdI d H  qd S r*   )r+   r,   rD   r)   r-   r.   r/   r   r   r   r)   k   s    z_ACondition.acquireNzOptional[float])r'   r   c                   s
  t  }| }| j||f |   zzBzt 
||I dH  W W (W dS  t jk
rh   Y W W dS X W 5 d}z|  I dH  W qW qr t jjk
r } z|}W 5 d}~X Y qrX qr| j	||f |dk	rz|W 5 d}X X W n" tk
r   | d  Y nX dS )a&  Wait until notified.

        If the calling task has not acquired the lock when this
        method is called, a RuntimeError is raised.

        This method releases the underlying lock, and then blocks
        until it is awakened by a notify() or notify_all() call for
        the same condition variable in another task.  Once
        awakened, it re-acquires the lock and returns True.

        This method may return spuriously,
        which is why the caller should always
        re-check the state and be prepared to wait() again.
        NTF   )r-   Zget_running_loopZcreate_futurerE   appendr   r)   
exceptionsZCancelledErrorremovewait_forTimeoutErrorBaseExceptionnotify)r    r'   loopr@   errer   r   r   waitx   s2    
z_ACondition.waitzCallable[[], _T]r	   )	predicater   c                   s$   | }|s |   I dH  | }q|S )a  Wait until a predicate becomes true.

        The predicate should be a callable whose result will be
        interpreted as a boolean value.  The method will repeatedly
        wait() until it evaluates to true.  The final predicate value is
        the return value.
        N)rT   )r    rU   resultr   r   r   rM      s
    z_ACondition.wait_forrI   int)nr   c              	   C  s   d}g }| j D ]^\}}||kr" qn| r,qz|t| W n& tk
rb   |||f Y qY nX |d7 }q|D ]}| j | qrdS )a  By default, wake up one coroutine waiting on this condition, if any.
        If the calling coroutine has not acquired the lock when this method
        is called, a RuntimeError is raised.

        This method wakes up at most n of the coroutines waiting for the
        condition variable; it is a no-op if no coroutines are waiting.

        Note: an awakened coroutine does not actually return from its
        wait() call until it can reacquire the lock. Since notify() does
        not release the lock, its caller should.
        r   rI   N)rE   rA   Zcall_soon_threadsaferB   RuntimeErrorrJ   rL   )r    rX   idxZ	to_removerQ   r@   waiterr   r   r   rP      s    

z_ACondition.notifyr   c                 C  s   |  t| j dS )a  Wake up all threads waiting on this condition. This method acts
        like notify(), but wakes up all waiting threads instead of one. If the
        calling thread has not acquired the lock when this method is called,
        a RuntimeError is raised.
        N)rP   lenrE   r1   r   r   r   
notify_all   s    z_ACondition.notify_allc                 C  s   | j j S )z$Only needed for tests in test_locks.)rD   r   r   r1   r   r   r   r      s    z_ACondition.lockedc                 C  s   | j   d S r   )rD   r   r1   r   r   r   r      s    z_ACondition.releasec                   s   |   I d H  | S r   )r)   r1   r   r   r   r2      s    z_ACondition.__aenter__c                 C  s   | j   | S r   )rD   r)   r1   r   r   r   r3      s    
z_ACondition.__enter__r   r4   c                   s   |    d S r   r8   r9   r   r   r   r;      s    z_ACondition.__aexit__c                 C  s   |    d S r   r8   r9   r   r   r   r:      s    z_ACondition.__exit__)Tr"   )N)rI   )r<   r=   r>   r?   r!   r)   rT   rM   rP   r]   r   r   r2   r3   r;   r:   r   r   r   r   rC   d   s   5!rC   )
__future__r   r-   rG   osr   r+   weakreftypingr   r   r   r   hasattrr   WeakSetr   __annotations__r	   r   r   r   r   rB   rC   r   r   r   r   <module>   s"    

(