U
    Íø.eÁ  ã                   @  s˜   d Z ddlmZ ddlZddlmZ ddlmZmZm	Z	 ej
dk rPddlmZ nddlmZ erlddlmZ d	Zed
ƒZG dd„ dƒZG dd„ dƒZdS )z&Semaphores and concurrency primitives.é    )ÚannotationsN)Údeque)ÚTYPE_CHECKINGÚCallableÚDeque)é   é
   )Ú	ParamSpec)ÚTracebackType)Ú	DummyLockÚLaxBoundedSemaphoreÚPc                   @  s‚   e Zd ZdZdddœdd„Zddd	d
dœdd„Zddœdd„Zddddœdd„Zddddœdd„Zddœdd„Z	ddœdd„Z
dS )r   aû  Asynchronous Bounded Semaphore.

    Lax means that the value will stay within the specified
    range even if released more times than it was acquired.

    Example:
    -------
        >>> x = LaxBoundedSemaphore(2)

        >>> x.acquire(print, 'HELLO 1')
        HELLO 1

        >>> x.acquire(print, 'HELLO 2')
        HELLO 2

        >>> x.acquire(print, 'HELLO 3')
        >>> x._waiters   # private, do not access directly
        [print, ('HELLO 3',)]

        >>> x.release()
        HELLO 3
    ÚintÚNone)ÚvalueÚreturnc                 C  s,   | | _ | _tƒ | _| jj| _| jj| _d S ©N)Úinitial_valuer   r   Ú_waitingÚappendÚ_add_waiterÚpopleftÚ_pop_waiter)Úselfr   © r   ú@/tmp/pip-unpacked-wheel-48hrr5dg/kombu/asynchronous/semaphore.pyÚ__init__.   s    
zLaxBoundedSemaphore.__init__zCallable[P, None]zP.argszP.kwargsÚbool)ÚcallbackÚpartial_argsÚpartial_kwargsr   c                 O  sD   | j }|dkr"|  |||f¡ dS t|d dƒ| _ |||Ž dS dS )a^  Acquire semaphore.

        This will immediately apply ``callback`` if
        the resource is available, otherwise the callback is suspended
        until the semaphore is released.

        Arguments:
        ---------
            callback (Callable): The callback to apply.
            *partial_args (Any): partial arguments to callback.
        r   Fé   TN)r   r   Úmax)r   r   r   r    r   r   r   r   Úacquire4   s    
zLaxBoundedSemaphore.acquire©r   c                 C  sJ   z|   ¡ \}}}W n( tk
r:   t| jd | jƒ| _Y nX |||Ž dS )z¸Release semaphore.

        Note:
        ----
            If there are any waiters this will apply the first waiter
            that is waiting for the resource (FIFO order).
        r!   N)r   Ú
IndexErrorÚminr   r   )r   ÚwaiterÚargsÚkwargsr   r   r   ÚreleaseN   s
    zLaxBoundedSemaphore.releaser!   )Únr   c                 C  s6   |  j |7  _ |  j|7  _t|ƒD ]}|  ¡  q$dS )z6Change the size of the semaphore to accept more users.N)r   r   Úranger*   )r   r+   Ú_r   r   r   Úgrow]   s    zLaxBoundedSemaphore.growc                 C  s(   t | j| dƒ| _t | j| dƒ| _dS )z6Change the size of the semaphore to accept less users.r   N)r"   r   r   )r   r+   r   r   r   Úshrinkd   s    zLaxBoundedSemaphore.shrinkc                 C  s   | j  ¡  | j| _dS )z@Reset the semaphore, which also wipes out any waiting callbacks.N)r   Úclearr   r   ©r   r   r   r   r0   i   s    
zLaxBoundedSemaphore.clearÚstrc                 C  s    d  | jjt| ƒ| jt| jƒ¡S )Nz!<{} at {:#x} value:{} waiting:{}>)ÚformatÚ	__class__Ú__name__Úidr   Úlenr   r1   r   r   r   Ú__repr__n   s       ÿzLaxBoundedSemaphore.__repr__N)r!   )r!   )r5   Ú
__module__Ú__qualname__Ú__doc__r   r#   r*   r.   r/   r0   r8   r   r   r   r   r      s   r   c                   @  s2   e Zd ZdZd dœdd„Zddddd	œd
d„ZdS )r   zPretending to be a lock.r$   c                 C  s   | S r   r   r1   r   r   r   Ú	__enter__w   s    zDummyLock.__enter__ztype[BaseException] | NonezBaseException | NonezTracebackType | Noner   )Úexc_typeÚexc_valÚexc_tbr   c                 C  s   d S r   r   )r   r=   r>   r?   r   r   r   Ú__exit__z   s    zDummyLock.__exit__N)r5   r9   r:   r;   r<   r@   r   r   r   r   r   t   s   r   )r;   Ú
__future__r   ÚsysÚcollectionsr   Útypingr   r   r   Úversion_infoZtyping_extensionsr	   Útypesr
   Ú__all__r   r   r   r   r   r   r   Ú<module>   s   
^