
    qit(                     <    S r SSKJrJrJr  SSKJr   " S S5      rg)z
ReadWriteLock

Taken from http://code.activestate.com/recipes/502283/

locks.py - Read-Write lock thread lock implementation

See the class documentation for more info.

Copyright (C) 2007, Heiko Wundram.
Released under the BSD-license.
    )	ConditionLockcurrent_thread)timec                   @    \ rS rSrSrS rSS.S jrSS.S jrS rS	r	g)
ReadWriteLock   a  Read-Write lock class. A read-write lock differs from a standard
threading.RLock() by allowing multiple threads to simultaneously hold a
read lock, while allowing only a single thread to hold a write lock at the
same point of time.

When a read lock is requested while a write lock is held, the reader
is blocked; when a write lock is requested while another write lock is
held or there are read locks, the writer is blocked.

Writers are always preferred by this implementation: if there are blocked
threads waiting for a write lock, current readers may request more read
locks (which they eventually should free, as they starve the waiting
writers otherwise), but a new thread requesting a read lock will not
be granted one, and block. This might mean starvation for readers if
two writer threads interweave their calls to acquire_write() without
leaving a window only for readers.

In case a current reader requests a write lock, this can and will be
satisfied without giving up the read locks first, but, only one thread
may perform this kind of lock upgrade, as a deadlock would otherwise
occur. After the write lock has been granted, the thread will hold a
full write lock, and not be downgraded after the upgrading call to
acquire_write() has been match by a corresponding release().
c                 l    [        [        5       5      U l        SU l        SU l        / U l        0 U l        g)z Initialize this read-write lock.Nr   )r   r   _ReadWriteLock__condition_ReadWriteLock__writer"_ReadWriteLock__upgradewritercount_ReadWriteLock__pendingwriters_ReadWriteLock__readers)selfs    D/home/kali/flask_env/lib/python3.13/site-packages/wsgidav/rw_lock.py__init__ReadWriteLock.__init__2   s5    
 %TV, $%! "     N)timeoutc                T   Ub  [        5       U-   n[        5       nU R                  R                  5          U R                  UL a1  U =R
                  S-  sl         U R                  R                  5         g U R                  c  U R                  (       d  U R                  (       aC  X0R                  ;   a3  U R                  U==   S-  ss'    U R                  R                  5         gOHU R                  R                  US5      S-   U R                  U'    U R                  R                  5         gUb:  W[        5       -
  nUS::  a  [        S5      eU R                  R                  U5        OU R                  R                  5         GM  ! U R                  R                  5         f = f)a>  Acquire a read lock for the current thread, waiting at most
timeout seconds or doing a non-blocking check in case timeout is <= 0.

In case timeout is None, the call to acquire_read blocks until the
lock request can be serviced.

In case the timeout expires before the lock could be serviced, a
RuntimeError is thrown.N   r   zAcquiring read lock timed out)r   r   r   acquirer   _ReadWriteLock__writercountreleaser   r   r   getRuntimeErrorwait)r   r   endtimeme	remainings        r   acquire_readReadWriteLock.acquire_readA   sj    fw&G  "	'}}"""a'"6 $$&5 ==(00D4I4I/
 !NN2.!3."  $$&- 0 .2^^-?-?A-F-Jr* $$& & '$& 0I A~*+JKK$$)))4$$))+1 4 $$&s   $F 6AF (-F 1AF F'c                   Ub  [        5       U-   n[        5       SpCU R                  R                  5          U R                  UL a1  U =R
                  S-  sl         U R                  R                  5         gX0R                  ;   a?  U R                  (       a  [        S5      eSnU R                  R                  U5      U l        OU R                  R                  U5         U R                  (       d  U R                  c  U R                  (       aE  U(       a=  X0l        U R                  S-   U l        SU l         U R                  R                  5         gOOU R                  S   UL a=  X0l        SU l        U R                  SS U l         U R                  R                  5         gUb}  W[        5       -
  nUS::  aN  U(       a!  U R                  U R                  U'   SU l        OU R                  R                  U5        [        S5      eU R                  R                  U5        OU R                  R                  5         GM`  ! U R                  R                  5         f = f)a  Acquire a write lock for the current thread, waiting at most
timeout seconds or doing a non-blocking check in case timeout is <= 0.

In case the write lock cannot be serviced due to the deadlock
condition mentioned above, a ValueError is raised.

In case timeout is None, the call to acquire_write blocks until the
lock request can be serviced.

In case the timeout expires before the lock could be serviced, a
RuntimeError is thrown.NFr   z(Inevitable dead lock, denying write lockTr   zAcquiring write lock timed out)r   r   r   r   r   r   r   r   r   
ValueErrorpopr   appendremover   r   )r   r   r   r   upgradewriterr    s         r   acquire_writeReadWriteLock.acquire_writep   s    fw&G*,eM  "B	'}}"""a'"| $$&{ ~~% ,,
 %%OPP $,0NN,>,>r,B) %%,,R0~~$--*?00( -/M151J1JQ1ND.89D5"H $$&U ) ..q1R7
 )+-.*040E0Eab0I-. $$&- & '$& 0I A~( 261J1JDNN2.89D5 !1188<*+KLL$$)))4$$))+Y \ $$&s   $I 7CI 4I $BI Ic                 j   [        5       nU R                  R                  5          U R                  UL aH  U =R                  S-  sl        U R                  (       d!  SU l        U R                  R                  5         O~XR                  ;   ad  U R                  U==   S-  ss'   U R                  U   (       d8  U R                  U	 U R                  (       d  U R                  R                  5         O[        S5      eU R                  R                  5         g! U R                  R                  5         f = f)zcRelease the currently held lock.

In case the current thread holds no lock, a ValueError is thrown.r   NzTrying to release unheld lock)	r   r   r   r   r   
notify_allr   r$   r   )r   r   s     r   r   ReadWriteLock.release   s    
   "	'}}"""a'")) %)DM$$//1~~%r"a'"~~b)r*>> ((335 !@AA$$&D$$&s   CD D2)__condition__pendingwriters	__readers__upgradewritercount__writer__writercount)
__name__
__module____qualname____firstlineno____doc__r   r!   r)   r   __static_attributes__ r   r   r   r      s(    2 '+ -'^ (, S'j'r   r   N)r8   	threadingr   r   r   r   r   r:   r   r   <module>r<      s!     6 5 J' J'r   