3
Z_ð]  ã               @   s,   d dl mZ d dlmZ G dd„ deƒZdS )é    )Úprint_function)Úeventc               @   s4   e Zd ZdZdd„ Zddd„Zddd„Zd	d
„ ZdS )Ú	metaphorea  This is sort of an inverse semaphore: a counter that starts at 0 and
    waits only if nonzero. It's used to implement a "wait for all" scenario.

    >>> from eventlet import coros, spawn_n
    >>> count = coros.metaphore()
    >>> count.wait()
    >>> def decrementer(count, id):
    ...     print("{0} decrementing".format(id))
    ...     count.dec()
    ...
    >>> _ = spawn_n(decrementer, count, 'A')
    >>> _ = spawn_n(decrementer, count, 'B')
    >>> count.inc(2)
    >>> count.wait()
    A decrementing
    B decrementing
    c             C   s   d| _ tjƒ | _| jjƒ  d S )Nr   )ÚcounterÚ_eventÚEventr   Úsend)Úself© r
   ú2/tmp/pip-build-yz_zf6az/eventlet/eventlet/coros.pyÚ__init__   s    
zmetaphore.__init__é   c             C   s2   |dkst ‚|  j|7  _| j|kr.| jjƒ  dS )z‰Increment our counter. If this transitions the counter from zero to
        nonzero, make any subsequent :meth:`wait` call wait.
        r   N)ÚAssertionErrorr   r   Úreset)r	   Úbyr
   r
   r   Úinc   s    
zmetaphore.incc             C   s8   |dkst ‚|  j|8  _| jdkr4d| _| jjƒ  dS )z–Decrement our counter. If this transitions the counter from nonzero
        to zero, a current or subsequent wait() call need no longer wait.
        r   N)r   r   r   r   )r	   r   r
   r
   r   Údec,   s
    
zmetaphore.decc             C   s   | j jƒ  dS )zŠSuspend the caller only if our count is nonzero. In that case,
        resume the caller once the count decrements to zero again.
        N)r   Úwait)r	   r
   r
   r   r   9   s    zmetaphore.waitN)r   )r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r
   r
   r
   r   r      s
   

r   N)Ú
__future__r   Zeventletr   r   Úobjectr   r
   r
   r
   r   Ú<module>   s   