o
    ?8¾\w  ã                   @   s®   d Z ddlZzddlmZ W n ey   ddlmZ Y nw ddlmZ ddlmZ G dd„ dƒ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dd„ ZdS )z&Utilities for multi-threading support.é    N)Ú	monotonic)Útime)ÚExceededMaxWaitersc                   @   s@   e Zd Zddd„Zddd„ZeZdd	„ Zd
d„ Zedd„ ƒZ	dS )Ú	Semaphoreé   c                 C   s*   |dk rt dƒ‚t t ¡ ¡| _|| _d S )Nr   z$semaphore initial value must be >= 0)Ú
ValueErrorÚ	threadingÚ	ConditionÚLockÚ_condÚ_value©ÚselfÚvalue© r   úE/var/www/html/env/lib/python3.10/site-packages/pymongo/thread_util.pyÚ__init__    s   
zSemaphore.__init__TNc                 C   sš   |s
|d ur
t dƒ‚d}d }| j ¡  | jdkr>|sn+|d ur3|d u r)tƒ | }n
|tƒ  }|dkr3n| j |¡ | jdks| jd | _d}| j ¡  |S )Nz.can't specify timeout for non-blocking acquireFr   r   T)r   r   Úacquirer   Ú_timeÚwaitÚrelease)r   ÚblockingÚtimeoutÚrcÚendtimer   r   r   r   &   s(   



ö
zSemaphore.acquirec                 C   s.   | j  ¡  | jd | _| j  ¡  | j  ¡  d S )Nr   )r   r   r   Únotifyr   ©r   r   r   r   r   ?   s   

zSemaphore.releasec                 C   s   |   ¡  d S ©N)r   )r   ÚtÚvÚtbr   r   r   Ú__exit__E   ó   zSemaphore.__exit__c                 C   s   | j S r   )r   r   r   r   r   ÚcounterH   s   zSemaphore.counter©r   ©TN)
Ú__name__Ú
__module__Ú__qualname__r   r   Ú	__enter__r   r!   Úpropertyr#   r   r   r   r   r      s    

r   c                   @   s"   e Zd ZdZddd„Zdd„ ZdS )	ÚBoundedSemaphorez6Semaphore that checks that # releases is <= # acquiresr   c                 C   s   t  | |¡ || _d S r   )r   r   Ú_initial_valuer   r   r   r   r   O   s   
zBoundedSemaphore.__init__c                 C   s   | j | jkr
tdƒ‚t | ¡S )Nz!Semaphore released too many times)r   r,   r   r   r   r   r   r   r   r   S   s   
zBoundedSemaphore.releaseNr$   )r&   r'   r(   Ú__doc__r   r   r   r   r   r   r+   M   s    
r+   c                   @   s(   e Zd Zd	dd„Zd
dd„Zdd„ ZdS )ÚDummySemaphoreNc                 C   ó   d S r   r   r   r   r   r   r   [   ó   zDummySemaphore.__init__Tc                 C   s   dS )NTr   ©r   r   r   r   r   r   r   ^   r0   zDummySemaphore.acquirec                 C   r/   r   r   r   r   r   r   r   a   r0   zDummySemaphore.releaser   r%   )r&   r'   r(   r   r   r   r   r   r   r   r.   Z   s    

r.   c                   @   s(   e Zd Zd
dd„Zddd„Zdd	„ ZdS )ÚMaxWaitersBoundedSemaphorer   c                 C   s   ||ƒ| _ ||ƒ| _d S r   )Úwaiter_semaphoreÚ	semaphore)r   Úsemaphore_classr   Úmax_waitersr   r   r   r   f   s   
z#MaxWaitersBoundedSemaphore.__init__TNc                 C   s:   | j  d¡s	tƒ ‚z| j ||¡W | j  ¡  S | j  ¡  w )NF)r3   r   r   r4   r   r1   r   r   r   r   j   s
   z"MaxWaitersBoundedSemaphore.acquirec                 C   s   t | j|ƒS r   )Úgetattrr4   )r   Únamer   r   r   Ú__getattr__r   r"   z&MaxWaitersBoundedSemaphore.__getattr__©r   r   r%   )r&   r'   r(   r   r   r9   r   r   r   r   r2   e   s    

r2   c                   @   s   e Zd Zddd„ZdS )Ú MaxWaitersBoundedSemaphoreThreadr   c                 C   s   t  | t||¡ d S r   )r2   r   r+   )r   r   r6   r   r   r   r   w   s   ÿz)MaxWaitersBoundedSemaphoreThread.__init__Nr:   )r&   r'   r(   r   r   r   r   r   r;   v   s    r;   c                 C   s(   | d u rt ƒ S |d u rt| ƒS t| |ƒS r   )r.   r+   r;   )Úmax_sizer6   r   r   r   Úcreate_semaphore|   s
   
r=   )r-   r   r   r   r   ÚImportErrorÚpymongo.monotonicÚpymongo.errorsr   r   r+   Úobjectr.   r2   r;   r=   r   r   r   r   Ú<module>   s   ÿ1