ó
O'^c           @@  sb   d  Z  d d l m Z d d l m Z d d g Z d e f d     YZ d e f d     YZ d S(	   sV   
kombu.async.semaphore
=====================

Semaphores and concurrency primitives.

i    (   t   absolute_import(   t   dequet	   DummyLockt   LaxBoundedSemaphorec           B@  sS   e  Z d  Z d   Z d   Z d   Z d d  Z d d  Z d   Z d   Z	 RS(	   s`  Asynchronous Bounded Semaphore.

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

    Example:

        >>> from future import print_statement as printf
        # ^ ignore: just fooling stupid pyflakes

        >>> x = LaxBoundedSemaphore(2)

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

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

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

        >>> x.release()
        HELLO 3

    c         C@  s>   | |  _  |  _ t   |  _ |  j j |  _ |  j j |  _ d  S(   N(   t   initial_valuet   valueR   t   _waitingt   appendt   _add_waitert   popleftt   _pop_waiter(   t   selfR   (    (    s7   /tmp/pip-unpacked-wheel-UAnTfW/kombu/async/semaphore.pyt   __init__,   s    c         G@  sT   |  j  } | d k r, |  j | | f  t St | d d  |  _  | |   t Sd S(   sÇ   Acquire semaphore, applying ``callback`` if
        the resource is available.

        :param callback: The callback to apply.
        :param \*partial_args: partial arguments to callback.

        i    i   N(   R   R   t   Falset   maxt   True(   R   t   callbackt   partial_argsR   (    (    s7   /tmp/pip-unpacked-wheel-UAnTfW/kombu/async/semaphore.pyt   acquire2   s    	
c         C@  sT   y |  j    \ } } Wn- t k
 rE t |  j d |  j  |  _ n X| |   d S(   s   Release semaphore.

        If there are any waiters this will apply the first waiter
        that is waiting for the resource (FIFO order).

        i   N(   R
   t
   IndexErrort   minR   R   (   R   t   waitert   args(    (    s7   /tmp/pip-unpacked-wheel-UAnTfW/kombu/async/semaphore.pyt   releaseC   s
     i   c         C@  sE   |  j  | 7_  |  j | 7_ g  t |  D] } |  j   ^ q+ d S(   s6   Change the size of the semaphore to accept more users.N(   R   R   t   rangeR   (   R   t   nt   _(    (    s7   /tmp/pip-unpacked-wheel-UAnTfW/kombu/async/semaphore.pyt   growQ   s    c         C@  s6   t  |  j | d  |  _ t  |  j | d  |  _ d S(   s6   Change the size of the semaphore to accept less users.i    N(   R   R   R   (   R   R   (    (    s7   /tmp/pip-unpacked-wheel-UAnTfW/kombu/async/semaphore.pyt   shrinkW   s    c         C@  s   |  j  j   |  j |  _ d S(   s@   Reset the semaphore, which also wipes out any waiting callbacks.N(   R   t   clearR   R   (   R   (    (    s7   /tmp/pip-unpacked-wheel-UAnTfW/kombu/async/semaphore.pyR   \   s    c         C@  s.   d j  |  j j t |   |  j t |  j   S(   Ns%   <{0} at {1:#x} value:{2} waiting:{3}>(   t   formatt	   __class__t   __name__t   idR   t   lenR   (   R   (    (    s7   /tmp/pip-unpacked-wheel-UAnTfW/kombu/async/semaphore.pyt   __repr__a   s    (
   R    t
   __module__t   __doc__R   R   R   R   R   R   R#   (    (    (    s7   /tmp/pip-unpacked-wheel-UAnTfW/kombu/async/semaphore.pyR      s   				c           B@  s    e  Z d  Z d   Z d   Z RS(   s   Pretending to be a lock.c         C@  s   |  S(   N(    (   R   (    (    s7   /tmp/pip-unpacked-wheel-UAnTfW/kombu/async/semaphore.pyt	   __enter__j   s    c         G@  s   d  S(   N(    (   R   t   exc_info(    (    s7   /tmp/pip-unpacked-wheel-UAnTfW/kombu/async/semaphore.pyt   __exit__m   s    (   R    R$   R%   R&   R(   (    (    (    s7   /tmp/pip-unpacked-wheel-UAnTfW/kombu/async/semaphore.pyR   g   s   	N(	   R%   t
   __future__R    t   collectionsR   t   __all__t   objectR   R   (    (    (    s7   /tmp/pip-unpacked-wheel-UAnTfW/kombu/async/semaphore.pyt   <module>   s
   W