
    5fv                     X    d dl Z d dlZd dlZd dlZd dlZd dlZd dlZ G d de      Zd Z	y)    Nc                   0    e Zd ZdZd ZddZd Zd Zd Zy)		PipeMutexzMutex using a pipe.

    Works across both greenlets and real threads, even at the same time.

    Class code copied from Swift's swift/common/utils.py
    Related eventlet bug: https://github.com/eventlet/eventlet/issues/432
    c                    t        j                         \  | _        | _        t	        j                  | j                  t        j
                        }t	        j                  | j                  t        j                  |t         j                  z         t        j                  | j                  d       d | _	        d| _
        t        j                  j                  d       y )N   -r   F)ospiperfdwfdfcntlF_GETFLF_SETFL
O_NONBLOCKwriteownerrecursion_deptheventletdebughub_prevent_multiple_readers)selfrflagss     5/usr/lib/python3/dist-packages/oslo_log/pipe_mutex.py__init__zPipeMutex.__init__"   s    WWY$( TXXu}}5DHHemmVbmm-CD
4 
  	33E:    c                    t        t        j                  j                               }| j                  |k(  r| xj
                  dz  c_        y	 	 t        j                  | j                  d       || _        y# t        $ rZ}|j                  t        j                  k7  r |sY d}~yt        j                  j                  | j                  d       Y d}~nd}~ww xY w)ax  Acquire the mutex.

        If called with blocking=False, returns True if the mutex was
        acquired and False if it wasn't. Otherwise, blocks until the mutex
        is acquired and returns True.
        This lock is recursive; the same greenthread may acquire it as many
        times as it wants to, though it must then release it that many times
        too.
           TNF)read)idr   greenthread
getcurrentr   r   r   r   r	   OSErrorerrnoEAGAINhubs
trampoline)r   blockingcurrent_greenthread_iderrs       r   acquirezPipeMutex.acquire>   s     "$H$8$8$C$C$E!F:://  A% > !$3
 
>99, 
 (((==
> s   'A7 7	C  C%+CCc                    t        t        j                  j                               }| j                  |k7  rt        d      | j                  dkD  r| xj                  dz  c_        yd| _        t        j                  | j                  d       y)zRelease the mutex.zcannot release un-acquired lockr   r   N   X)
r   r   r   r   r   RuntimeErrorr   r   r   r
   )r   r&   s     r   releasezPipeMutex.releasea   sp    !#H$8$8$C$C$E!F:://@AA!#  A% 

4 r   c                     | j                   &t        j                  | j                          d| _         | j                  &t        j                  | j                         d| _        d| _        d| _        y)z|Close the mutex.

        This releases its file descriptors.
        You can't use a mutex after it's been closed.
        Nr   )r
   r   closer	   r   r   r   s    r   r.   zPipeMutex.closen   sU     88HHTXXDH88HHTXXDH
 r   c                 $    | j                          y )N)r.   r/   s    r   __del__zPipeMutex.__del__}   s     	

r   N)T)	__name__
__module____qualname____doc__r   r(   r,   r.   r1    r   r   r   r      s!    ;8!>F!!r   r   c                 "    t               | _        y)z2Replacement for logging.Handler.createLock method.N)r   lockr/   s    r   pipe_createLockr9      s    DIr   )
r!   r   r   r   eventlet.debugeventlet.greenthreadeventlet.hubsobjectr   r9   r6   r   r   <module>r>      s0       	    o odr   