o
    'h(                     @  s   d Z ddlmZ ddlZddlZddlmZmZ ddlm	Z	m
Z
mZ e ZG dd dZG dd	 d	ZG d
d deeZG dd deeZdS )zLock and Condition classes vendored from https://github.com/python/cpython/blob/main/Lib/asyncio/locks.py
to port 3.13 fixes to older versions of Python.
Can be removed once we drop Python 3.12 support.    )annotationsN)events
exceptions)Any	CoroutineOptionalc                   @  s   e Zd ZdZdddZdS )_LoopBoundMixinNreturnr   c                 C  sb   t  }| jd u r#t | jd u r|| _W d    n1 sw   Y  || jur/t| d|S )Nz# is bound to a different event loop)r   _get_running_loop_loop_global_lockRuntimeError)selfloop r   Q/var/www/html/olx_land/venv/lib/python3.10/site-packages/pymongo/_asyncio_lock.py	_get_loop   s   


z_LoopBoundMixin._get_loop)r	   r   )__name__
__module____qualname__r   r   r   r   r   r   r      s    r   c                   @  s    e Zd ZdddZdd	d
ZdS )_ContextManagerMixinr	   Nonec                   s   |   I d H  d S N)acquirer   r   r   r   
__aenter__!   s   z_ContextManagerMixin.__aenter__exc_typer   exctbc                   s   |    d S r   )release)r   r   r   r   r   r   r   	__aexit__'   s   z_ContextManagerMixin.__aexit__Nr	   r   )r   r   r   r   r   r   r	   r   )r   r   r   r   r    r   r   r   r   r       s    
r   c                      sT   e Zd ZdZdddZd fddZdd
dZdddZdddZdddZ	  Z
S )Locka5  Primitive lock objects.

    A primitive lock is a synchronization primitive that is not owned
    by a particular task when locked.  A primitive lock is in one
    of two states, 'locked' or 'unlocked'.

    It is created in the unlocked state.  It has two basic methods,
    acquire() and release().  When the state is unlocked, acquire()
    changes the state to locked and returns immediately.  When the
    state is locked, acquire() blocks until a call to release() in
    another task changes it to unlocked, then the acquire() call
    resets it to locked and returns.  The release() method should only
    be called in the locked state; it changes the state to unlocked
    and returns immediately.  If an attempt is made to release an
    unlocked lock, a RuntimeError will be raised.

    When more than one task is blocked in acquire() waiting for
    the state to turn to unlocked, only one task proceeds when a
    release() call resets the state to unlocked; successive release()
    calls will unblock tasks in FIFO order.

    Locks also support the asynchronous context management protocol.
    'async with lock' statement should be used.

    Usage:

        lock = Lock()
        ...
        await lock.acquire()
        try:
            ...
        finally:
            lock.release()

    Context manager usage:

        lock = Lock()
        ...
        async with lock:
             ...

    Lock objects can be tested for locking state:

        if not lock.locked():
           await lock.acquire()
        else:
           # lock is acquired
           ...

    r	   r   c                 C  s   d | _ d| _d S )NF)_waiters_lockedr   r   r   r   __init___   s   
zLock.__init__strc                   sL   t   }| jr
dnd}| jr| dt| j }d|dd  d| dS 	Nlockedunlockedz
, waiters:<   z [z]>)super__repr__r$   r#   lenr   resextra	__class__r   r   r.   c   s
   
zLock.__repr__boolc                 C  s   | j S )z Return True if lock is acquired.)r$   r   r   r   r   r(   j   s   zLock.lockedc              	     s   | j s| jdu stdd | jD rd| _ dS | jdu r"t | _|   }| j| zz|I dH  W | j| n| j| w W n t	j
yU   | j sT|    w d| _ dS )zAcquire a lock.

        This method blocks until the lock is unlocked, then sets it to
        locked and returns True.
        Nc                 s  s    | ]}|  V  qd S r   )	cancelled).0wr   r   r   	<genexpr>w   s    zLock.acquire.<locals>.<genexpr>T)r$   r#   allcollectionsdequer   create_futureappendremover   CancelledError_wake_up_firstr   futr   r   r   r   n   s(   

 zLock.acquirec                 C  s    | j rd| _ |   dS td)aB  Release a lock.

        When the lock is locked, reset it to unlocked, and return.
        If any other tasks are blocked waiting for the lock to become
        unlocked, allow exactly one of them to proceed.

        When invoked on an unlocked lock, a RuntimeError is raised.

        There is no return value.
        FzLock is not acquiredN)r$   rA   r   r   r   r   r   r      s   zLock.releasec                 C  sL   | j sdS z	tt| j }W n
 ty   Y dS w | s$|d dS dS )z*Ensure that the first waiter will wake up.NT)r#   nextiterStopIterationdone
set_resultrB   r   r   r   rA      s   zLock._wake_up_firstr!   r	   r&   r	   r5   )r   r   r   __doc__r%   r.   r(   r   r   rA   __classcell__r   r   r3   r   r"   +   s    
3


&r"   c                      sb   e Zd ZdZddddZd fd
dZd ddZd!ddZd"d#ddZd#ddZ	d$ddZ
  ZS )%	Conditiona  Asynchronous equivalent to threading.Condition.

    This class implements condition variable objects. A condition variable
    allows one or more tasks to wait until they are notified by another
    task.

    A new Lock object is created and used as the underlying lock.
    NlockOptional[Lock]r	   r   c                 C  s:   |d u rt  }|| _|j| _|j| _|j| _t | _d S r   )r"   _lockr(   r   r   r;   r<   r#   )r   rN   r   r   r   r%      s   zCondition.__init__r&   c                   sN   t   }|  rdnd}| jr| dt| j }d|dd  d| dS r'   )r-   r.   r(   r#   r/   r0   r3   r   r   r.      s
   
zCondition.__repr__r5   c                   s6  |   s	td|   }|   zzzK| j| z=|I dH  W | j| W d}	 z	|  I dH  W n t	j
yJ } z|}W Y d}~nd}~ww q,|durVz|d}w W dS | j| w d}	 z	|  I dH  W n t	j
y } z|}W Y d}~nd}~ww qc|durz|d}w w  ty   | d  w )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.
        zcannot wait on un-acquired lockNTr+   )r(   r   r   r=   r   r#   r>   r?   r   r   r@   BaseException_notify)r   rC   errer   r   r   wait   s\   

zCondition.wait	predicater   r   c                   s(   | }|s|   I dH  | }|r|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)rU   )r   rV   resultr   r   r   wait_for  s   zCondition.wait_forr+   nintc                 C  s   |   std| | dS )a  By default, wake up one task waiting on this condition, if any.
        If the calling task has not acquired the lock when this method
        is called, a RuntimeError is raised.

        This method wakes up n of the tasks waiting for the condition
         variable; if fewer than n are waiting, they are all awoken.

        Note: an awakened task does not actually return from its
        wait() call until it can reacquire the lock. Since notify() does
        not release the lock, its caller should.
        z!cannot notify on un-acquired lockN)r(   r   rR   )r   rY   r   r   r   notify  s   zCondition.notifyc                 C  s<   d}| j D ]}||kr d S | s|d7 }|d qd S )Nr   r+   F)r#   rG   rH   )r   rY   idxrC   r   r   r   rR   %  s   

zCondition._notifyc                 C  s   |  t| j dS )a   Wake up all tasks waiting on this condition. This method acts
        like notify(), but wakes up all waiting tasks instead of one. If the
        calling task has not acquired the lock when this method is called,
        a RuntimeError is raised.
        N)r[   r/   r#   r   r   r   r   
notify_all/  s   zCondition.notify_allr   )rN   rO   r	   r   rI   rJ   )rV   r   r	   r   )r+   )rY   rZ   r	   r   r!   )r   r   r   rK   r%   r.   rU   rX   r[   rR   r]   rL   r   r   r3   r   rM      s    	

7

rM   )rK   
__future__r   r;   	threadingasyncior   r   typingr   r   r   r"   r   r   r   rM   r   r   r   r   <module>   s    	