o
    0׾g,                     @   s  d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlm	Z	 ddl
mZ ddl
mZ g d	Ze	d
d\ZZdd ZG dd deZG dd deZG dd deeZG dd deeZdd Ze  [erreZde_eZde_G dd deZG dd deZdS )a'  
Locking primitives.

These include semaphores with arbitrary bounds (:class:`Semaphore` and
its safer subclass :class:`BoundedSemaphore`) and a semaphore with
infinite bounds (:class:`DummySemaphore`), along with a reentrant lock
(:class:`RLock`) with the same API as :class:`threading.RLock`.
    )absolute_import)print_function)
getcurrent)PURE_PYTHON)monkey)	Semaphore)BoundedSemaphore)r   r   DummySemaphoreRLock)_threadthread)allocate_lock	get_identc                    s    fdd}|S )Nc                    s:   | j   | g|R  W  d    S 1 sw   Y  d S N)_atomic)selfargsmeth U/var/www/html/backend_erp/backend_erp_env/lib/python3.10/site-packages/gevent/lock.pym0      $zatomic.<locals>.mr   )r   r   r   r   r   atomic/   s   r   c                   @   sH   e Zd ZdZdd Zedd Zedd Zdd	 Zd
d Z	dd Z
dS )_GILLock)_owned_thread_id_gilr   _recursion_depthc                 C   s    d | _ t | _t | _d| _d S Nr   )r   _allocate_lockr   r   r   r   r   r   r   __init__D   s   
z_GILLock.__init__c                 C   sr   t  }| j|kr|  jd7  _dS 	 | j  z| j  W | j  n| j  w | jd u r0nq|| _d| _dS )N   T)
_get_identr   r   r   releaser   acquirer   current_tidr   r   r   r%   J   s   


	z_GILLock.acquirec                 C   sT   t  }|| jkrtd| || jpdf |  jd8  _| js(d | _| j  d S d S )Nz;%s: Releasing lock not owned by you. You: 0x%x; Owner: 0x%xr   r"   )r#   r   RuntimeErrorr   r   r$   r&   r   r   r   r$   _   s   

z_GILLock.releasec                 C      |    d S r   r%   r    r   r   r   	__enter__n      z_GILLock.__enter__c                 C   r)   r   r$   )r   tvtbr   r   r   __exit__q   r,   z_GILLock.__exit__c                 C   s
   | j  S r   )r   lockedr    r   r   r   r2   t   s   
z_GILLock.lockedN)__name__
__module____qualname__	__slots__r!   r   r%   r$   r+   r1   r2   r   r   r   r   r   6   s    

r   c                       sh   e Zd ZdZ fddZdd Zdd Z fdd	Z fd
dZd fdd	Z	e	Z
d fdd	Z  ZS )_AtomicSemaphoreMixinr   c                    s"   t  | _tt| j|i | d S r   )r   
_lock_locksuperr7   r!   )r   r   kwargs	__class__r   r   r!      s   z_AtomicSemaphoreMixin.__init__c                 C      | j   d S r   )r8   r%   r    r   r   r   _acquire_lock_for_switch_in      z1_AtomicSemaphoreMixin._acquire_lock_for_switch_inc                 C   r=   r   )r8   r$   r    r   r   r   _drop_lock_for_switch_out   r?   z/_AtomicSemaphoreMixin._drop_lock_for_switch_outc                    :   | j  tt| |W  d    S 1 sw   Y  d S r   )r8   r9   r7   _notify_links)r   arrived_while_waitingr;   r   r   rB      r   z#_AtomicSemaphoreMixin._notify_linksc                    s8   | j  tt|  W  d    S 1 sw   Y  d S r   )r8   r9   r7   r$   r    r;   r   r   r$      s   $z_AtomicSemaphoreMixin.releaseTNc                    s<   | j  tt| ||W  d    S 1 sw   Y  d S r   )r8   r9   r7   r%   r   blockingtimeoutr;   r   r   r%      s   $z_AtomicSemaphoreMixin.acquirec                    rA   r   )r8   r9   r7   waitr   rF   r;   r   r   rG      r   z_AtomicSemaphoreMixin.waitTNr   )r3   r4   r5   r6   r!   r>   r@   rB   r$   r%   _py3k_acquirerG   __classcell__r   r   r;   r   r7   w   s    	r7   c                   @   s   e Zd ZejZdZdS )_AtomicSemaphorer8   N)r3   r4   r5   r   __doc__r6   r   r   r   r   rL      s    rL   c                       s&   e Zd ZejZdZ fddZ  ZS )_AtomicBoundedSemaphorerM   c                    s   t t|  S r   )r9   rO   r$   r    r;   r   r   r$      s   z_AtomicBoundedSemaphore.release)r3   r4   r5   r   rN   r6   r$   rK   r   r   r;   r   rO      s    rO   c                  C   sl   t tfD ]/} | jd }|jdrd|jvsJ | j|jks J dD ]}t| |}t||}|j|_q"qd S )N   r   Atomic)r%   r$   rG   )rL   rO   __mro__r3   endswithrN   getattr)cbr   c_methb_methr   r   r   _fixup_docstrings   s   



rY   r   r   c                   @   sn   e Zd ZdZdddZdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dddZdddZdd Zdd ZdS )r	   ac  
    DummySemaphore(value=None) -> DummySemaphore

    An object with the same API as :class:`Semaphore`,
    initialized with "infinite" initial value. None of its
    methods ever block.

    This can be used to parameterize on whether or not to actually
    guard access to a potentially limited resource. If the resource is
    actually limited, such as a fixed-size thread pool, use a real
    :class:`Semaphore`, but if the resource is unbounded, use an
    instance of this class. In that way none of the supporting code
    needs to change.

    Similarly, it can be used to parameterize on whether or not to
    enforce mutual exclusion to some underlying object. If the
    underlying object is known to be thread-safe itself mutual
    exclusion is not needed and a ``DummySemaphore`` can be used, but
    if that's not true, use a real ``Semaphore``.
    Nc                 C      dS )z
        .. versionchanged:: 1.1rc3
            Accept and ignore a *value* argument for compatibility with Semaphore.
        Nr   )r   valuer   r   r   r!          zDummySemaphore.__init__c                 C   s   d| j j S )Nz<%s>)r<   r3   r    r   r   r   __str__   r,   zDummySemaphore.__str__c                 C   rZ   )z>A DummySemaphore is never locked so this always returns False.Fr   r    r   r   r   r2         zDummySemaphore.lockedc                 C   rZ   )z=A DummySemaphore is never locked so this always returns True.Tr   r    r   r   r   ready   r^   zDummySemaphore.readyc                 C   rZ   )z)Releasing a dummy semaphore does nothing.Nr   r    r   r   r   r$      r\   zDummySemaphore.releasec                 C      d S r   r   r   callbackr   r   r   rawlink   r^   zDummySemaphore.rawlinkc                 C   r`   r   r   ra   r   r   r   unlink      zDummySemaphore.unlinkc                 C   rZ   )z1Waiting for a DummySemaphore returns immediately.r"   r   rH   r   r   r   rG      r^   zDummySemaphore.waitTc                 C   rZ   )z
        A DummySemaphore can always be acquired immediately so this always
        returns True and ignores its arguments.

        .. versionchanged:: 1.1a1
           Always return *true*.
        Tr   rD   r   r   r   r%     s   	zDummySemaphore.acquirec                 C   r`   r   r   r    r   r   r   r+     re   zDummySemaphore.__enter__c                 C   r`   r   r   )r   typvalr0   r   r   r   r1     re   zDummySemaphore.__exit__r   rI   )r3   r4   r5   rN   r!   r]   r2   r_   r$   rc   rd   rG   r%   r+   r1   r   r   r   r   r	      s    


r	   c                   @   s`   e Zd ZdZdZdddZdd Zdd	d
Zdd Zdd Z	dd Z
dd Zdd Zdd ZdS )r
   a  
    A mutex that can be acquired more than once by the same greenlet.

    A mutex can only be locked by one greenlet at a time. A single greenlet
    can `acquire` the mutex as many times as desired, though. Each call to
    `acquire` must be paired with a matching call to `release`.

    It is an error for a greenlet that has not acquired the mutex
    to release it.

    Instances are context managers.
    )_block_owner_count__weakref__Nc                 C   s   t d|| _d| _d| _dS )zQ
        .. versionchanged:: 20.5.1
           Add the ``hub`` argument.
        r"   Nr   )r   rh   ri   rj   )r   hubr   r   r   r!   (  s   
zRLock.__init__c                 C   s    d| j jt| | j| j| jf S )Nz+<%s at 0x%x _block=%s _count=%r _owner=%r)>)r<   r3   idrh   rj   ri   r    r   r   r   __repr__1  s   zRLock.__repr__Tc                 C   sD   t  }| j|u r|  jd7  _dS | j||}|r || _d| _|S )z
        Acquire the mutex, blocking if *blocking* is true, for up to
        *timeout* seconds.

        .. versionchanged:: 1.5a4
           Added the *timeout* parameter.

        :return: A boolean indicating whether the mutex was acquired.
        r"   )r   ri   rj   rh   r%   )r   rE   rF   mercr   r   r   r%   9  s   

zRLock.acquirec                 C   s   |   S r   r*   r    r   r   r   r+   M  s   zRLock.__enter__c                 C   sL   | j t urtd| j t f | jd  | _}|s$d| _ | j  dS dS )zz
        Release the mutex.

        Only the greenlet that originally acquired the mutex can
        release it.
        z6cannot release un-acquired lock. Owner: %r Current: %rr"   N)ri   r   r(   rj   rh   r$   )r   countr   r   r   r$   P  s   zRLock.releasec                 C   r)   r   r-   )r   rf   r[   r0   r   r   r   r1   `  r,   zRLock.__exit__c                 C   s"   |\}}| j   || _|| _d S r   )rh   r%   rj   ri   )r   count_ownerrq   ownerr   r   r   _acquire_restoree  s   

zRLock._acquire_restorec                 C   s*   | j }d| _ | j}d | _| j  ||fS r   )rj   ri   rh   r$   )r   rq   rs   r   r   r   _release_savek  s   
zRLock._release_savec                 C   s   | j t u S r   )ri   r   r    r   r   r   	_is_owneds  r,   zRLock._is_ownedr   rI   )r3   r4   r5   rN   r6   r!   rn   r%   r+   r$   r1   rt   ru   rv   r   r   r   r   r
     s    
	
r
   N)rN   
__future__r   r   
gevent.hubr   gevent._compatr   geventr   gevent._semaphorer   r   __all__get_originalr   r#   r   objectr   r7   rL   rO   rY   r3   r	   r
   r   r   r   r   <module>   s8   A&
N