
    hu                         d Z ddlZ	 ddlmZ ddlmZ ddlmZ  G d d      Z	 G d d	e	      Z
 G d
 de      Z G d de      Z G d de      Zd Zy# e$ r	 ddlmZ Y Sw xY w)z&Utilities for multi-threading support.    N)	monotonic)time)ExceededMaxWaitersc                   <    e Zd ZddZddZeZd Zd Zed        Z	y)		Semaphorec                     |dk  rt        d      t        j                  t        j                               | _        || _        y )Nr   z$semaphore initial value must be >= 0)
ValueError	threading	ConditionLock_cond_valueselfvalues     V/var/www/html/ranktracker/api/venv/lib/python3.12/site-packages/pymongo/thread_util.py__init__zSemaphore.__init__    s5    19CDD(()9:
    Nc                 `   |s|t        d      d}d }| j                  5  | j                  dk(  rS|snf|#|t               |z   }n|t               z
  }|dk  rnA| j                  j	                  |       | j                  dk(  rS| j                  dz
  | _        d}d d d        |S # 1 sw Y   |S xY w)Nz.can't specify timeout for non-blocking acquireFr      T)r	   r   r   _timewait)r   blockingtimeoutrcendtimes        r   acquirezSemaphore.acquire&   s    G/MNNZZ 	++"&"''G"3")EG"3"a<!

( ++" #kkAo	 		 	s   A"B#B##B-c                     | j                   5  | j                  dz   | _        | j                   j                          d d d        y # 1 sw Y   y xY w)Nr   )r   r   notifyr   s    r   releasezSemaphore.release>   s=    ZZ 	 ++/DKJJ	  	  	 s   /AAc                 $    | j                          y N)r!   )r   tvtbs       r   __exit__zSemaphore.__exit__C   s    r   c                     | j                   S r#   )r   r    s    r   counterzSemaphore.counterF   s    {{r   r   TN)
__name__
__module____qualname__r   r   	__enter__r!   r'   propertyr)    r   r   r   r      s1    , I 
  r   r   c                       e Zd ZdZddZd Zy)BoundedSemaphorez6Semaphore that checks that # releases is <= # acquiresc                 >    t         j                  | |       || _        y r#   )r   r   _initial_valuer   s     r   r   zBoundedSemaphore.__init__M   s    4'#r   c                 t    | j                   | j                  k\  rt        d      t        j	                  |       S )Nz!Semaphore released too many times)r   r5   r	   r   r!   r    s    r   r!   zBoundedSemaphore.releaseQ   s1    ;;$---@AA  &&r   Nr*   )r,   r-   r.   __doc__r   r!   r1   r   r   r3   r3   K   s    @$'r   r3   c                   "    e Zd ZddZddZd Zy)DummySemaphoreNc                      y r#   r1   r   s     r   r   zDummySemaphore.__init__Y       r   c                      y)NTr1   r   r   r   s      r   r   zDummySemaphore.acquire\   s    r   c                      y r#   r1   r    s    r   r!   zDummySemaphore.release_   r;   r   r#   r+   )r,   r-   r.   r   r   r!   r1   r   r   r9   r9   X   s    r   r9   c                   "    e Zd ZddZddZd Zy)MaxWaitersBoundedSemaphorec                 8     ||      | _          ||      | _        y r#   )waiter_semaphore	semaphore)r   semaphore_classr   max_waiterss       r   r   z#MaxWaitersBoundedSemaphore.__init__d   s     / <(/r   Nc                     | j                   j                  d      s
t               	 | j                  j                  ||      | j                   j	                          S # | j                   j	                          w xY w)NF)rB   r   r   rC   r!   r=   s      r   r   z"MaxWaitersBoundedSemaphore.acquireh   s]    $$,,U3$&&	,>>))(G<!!))+D!!))+s   A A9c                 .    t        | j                  |      S r#   )getattrrC   )r   names     r   __getattr__z&MaxWaitersBoundedSemaphore.__getattr__p   s    t~~t,,r   r   r   r+   )r,   r-   r.   r   r   rJ   r1   r   r   r@   r@   c   s    0,-r   r@   c                       e Zd ZddZy) MaxWaitersBoundedSemaphoreThreadc                 <    t         j                  | t        ||       y r#   )r@   r   r3   )r   r   rE   s      r   r   z)MaxWaitersBoundedSemaphoreThread.__init__u   s    "++"E;	8r   NrK   )r,   r-   r.   r   r1   r   r   rM   rM   t   s    8r   rM   c                 L    | 
t               S |t        |       S t        | |      S r#   )r9   r3   rM   )max_sizerE   s     r   create_semaphorerQ   z   s/    #H--3HkJJr   )r7   r
   r   r   r   ImportErrorpymongo.monotonicpymongo.errorsr   r   r3   objectr9   r@   rM   rQ   r1   r   r   <module>rV      st    - #' , -, ,^	'y 	'V - -"8'A 8KM  #"#s   A A"!A"