3
O6bu                 @   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   R/var/www/html/sandeepIITI/myenv/lib/python3.6/site-packages/pymongo/thread_util.py__init__    s    zSemaphore.__init__TNc          	   C   s   | r|d k	rt dd}d }| jh x`| jdkrv|s8P |d k	rh|d krTt | }n|t  }|dkrhP | jj| q(W | jd | _d}W d Q R X |S )Nz.can't specify timeout for non-blocking acquireFr   r   T)r   r   r   _timewait)r   blockingtimeoutrcendtimer   r   r   acquire&   s$    
zSemaphore.acquirec          	   C   s,   | j  | jd | _| j j  W d Q R X d S )Nr   )r   r   notify)r   r   r   r   release>   s    zSemaphore.releasec             C   s   | j   d S )N)r   )r   tvtbr   r   r   __exit__C   s    zSemaphore.__exit__c             C   s   | j S )N)r   )r   r   r   r   counterF   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   M   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   Q   s    zBoundedSemaphore.releaseN)r   )r    r!   r"   __doc__r   r   r   r   r   r   r%   K   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   Y   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   _   s    zDummySemaphore.release)N)TN)r    r!   r"   r   r   r   r   r   r   r   r(   X   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   d   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   h   s
    z"MaxWaitersBoundedSemaphore.acquirec             C   s   t | j|S )N)getattrr+   )r   namer   r   r   __getattr__p   s    z&MaxWaitersBoundedSemaphore.__getattr__)r   r   )TN)r    r!   r"   r   r   r/   r   r   r   r   r)   c   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   u   s    z)MaxWaitersBoundedSemaphoreThread.__init__N)r   r   )r    r!   r"   r   r   r   r   r   r0   t   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_semaphorez   s
    r2   )r'   r   r   r   r   ImportErrorZpymongo.monotonicZpymongo.errorsr   r   r%   objectr(   r)   r0   r2   r   r   r   r   <module>   s   /