3
]ð]w  ã               @   s°   d Z ddlZyddlmZ W n  ek
r<   ddlmZ Y nX 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jtjƒ ƒ| _|| _d S )Nr   z$semaphore initial value must be >= 0)Ú
ValueErrorÚ	threadingÚ	ConditionÚLockÚ_condÚ_value)ÚselfÚvalue© r   ú6/tmp/pip-build-20mum3z4/pymongo/pymongo/thread_util.pyÚ__init__    s    zSemaphore.__init__TNc             C   s˜   | r|d k	rt dƒ‚d}d }| jjƒ  x`| jdkrx|s:P |d k	rj|d krVtƒ | }n|tƒ  }|dkrjP | jj|ƒ q*W | jd | _d}| j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ƒ  | jd | _| j jƒ  | j jƒ  d S )Nr   )r   r   r   Únotifyr   )r   r   r   r   r   ?   s    

zSemaphore.releasec             C   s   | j ƒ  d S )N)r   )r   ÚtÚvÚtbr   r   r   Ú__exit__E   s    zSemaphore.__exit__c             C   s   | j S )N)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 j| |ƒ || _d S )N)r   r   Ú_initial_value)r   r   r   r   r   r   O   s    zBoundedSemaphore.__init__c             C   s   | j | jkrtdƒ‚tj| ƒS )Nz!Semaphore released too many times)r   r&   r   r   r   )r   r   r   r   r   S   s    zBoundedSemaphore.releaseN)r   )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   s   d S )Nr   )r   r   r   r   r   r   [   s    zDummySemaphore.__init__Tc             C   s   dS )NTr   )r   r   r   r   r   r   r   ^   s    zDummySemaphore.acquirec             C   s   d S )Nr   )r   r   r   r   r   a   s    zDummySemaphore.release)N)TN)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 )N)Úwaiter_semaphoreÚ	semaphore)r   Zsemaphore_classr   Úmax_waitersr   r   r   r   f   s    
z#MaxWaitersBoundedSemaphore.__init__TNc          
   C   s2   | j jdƒstƒ ‚z| jj||ƒS | j jƒ  X d S )NF)r*   r   r   r+   r   )r   r   r   r   r   r   r   j   s
    z"MaxWaitersBoundedSemaphore.acquirec             C   s   t | j|ƒS )N)Úgetattrr+   )r   Únamer   r   r   Ú__getattr__r   s    z&MaxWaitersBoundedSemaphore.__getattr__)r   r   )TN)r    r!   r"   r   r   r/   r   r   r   r   r)   e   s   

r)   c               @   s   e Zd Zddd„ZdS )Ú MaxWaitersBoundedSemaphoreThreadr   c             C   s   t j| t||ƒ d S )N)r)   r   r%   )r   r   r,   r   r   r   r   w   s    z)MaxWaitersBoundedSemaphoreThread.__init__N)r   r   )r    r!   r"   r   r   r   r   r   r0   v   s   r0   c             C   s,   | d krt ƒ S |d krt| ƒS t| |ƒS d S )N)r(   r%   r0   )Úmax_sizer,   r   r   r   Úcreate_semaphore|   s
    r2   )r'   r   r   r   r   ÚImportErrorZpymongo.monotonicZpymongo.errorsr   r   r%   Úobjectr(   r)   r0   r2   r   r   r   r   Ú<module>   s   1