§
    Uä�aÎQ  ã                   ód  — d dl mZmZmZ ddg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	„ Z e¦   «          e¦   «         d
<   [d dlmZ d dlmZ d dlmZ  G d„ de¦  «        Z e¦   «         Z e¦   «         Z G d„ de¦  «        Z G d„ de¦  «        Zd dlmZ  e e¦   «         d¦  «         dS )é    )Úprint_functionÚabsolute_importÚdivisionÚ	SemaphoreÚBoundedSemaphore)Úsleep)Ú	monotonic)ÚInvalidThreadUseError)ÚLoopExit)ÚTimeoutc                  ó8   — t          d¦  «        } | j        j        S )Nzgevent._abstract_linkable)Ú
__import__Ú_abstract_linkableÚAbstractLinkable)Úxs    ú3/usr/lib/python3/dist-packages/gevent/_semaphore.pyÚ_get_linkabler      s   € ÝÐ.Ñ/Ô/€AØÔÔ0Ð0ó    r   )Úget_hub_if_exists)Úget_hub)Ú	spawn_rawc                   ó   — e Zd ZdZd„ Zd„ ZdS )Ú_LockReleaseLink©Úlockc                 ó   — || _         d S ©Nr   )Úselfr   s     r   Ú__init__z_LockReleaseLink.__init__'   s   € ØˆŒ	ˆ	ˆ	r   c                 ó8   — | j                              ¦   «          d S r   )r   Úrelease)r   Ú_s     r   Ú__call__z_LockReleaseLink.__call__*   s   € ØŒ	×ÒÑÔÐÐÐr   N)Ú__name__Ú
__module__Ú__qualname__Ú	__slots__r   r#   © r   r   r   r   "   s9   € € € € € ð€Iðð ð ðð ð ð ð r   r   c                   óœ   ‡ — e Zd ZdZdZdˆ fd„	Zd„ Zd„ Zd„ Zd	„ Z	d
„ Z
d„ Zdd„Zdd„ZeZd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zˆ xZS )r   aî  
    Semaphore(value=1) -> Semaphore

    .. seealso:: :class:`BoundedSemaphore` for a safer version that prevents
       some classes of bugs. If unsure, most users should opt for `BoundedSemaphore`.

    A semaphore manages a counter representing the number of `release`
    calls minus the number of `acquire` calls, plus an initial value.
    The `acquire` method blocks if necessary until it can return
    without making the counter negative. A semaphore does not track ownership
    by greenlets; any greenlet can call `release`, whether or not it has previously
    called `acquire`.

    If not given, ``value`` defaults to 1.

    The semaphore is a context manager and can be used in ``with`` statements.

    This Semaphore's ``__exit__`` method does not call the trace function
    on CPython, but does under PyPy.

    .. versionchanged:: 1.4.0
        Document that the order in which waiters are awakened is not specified. It was not
        specified previously, but due to CPython implementation quirks usually went in FIFO order.
    .. versionchanged:: 1.5a3
       Waiting greenlets are now awakened in the order in which they waited.
    .. versionchanged:: 1.5a3
       The low-level ``rawlink`` method (most users won't use this) now automatically
       unlinks waiters before calling them.
    .. versionchanged:: 20.12.0
       Improved support for multi-threaded usage. When multi-threaded usage is detected,
       instances will no longer create the thread's hub if it's not present.
    )ÚcounterÚ_multithreadedé   Nc                 óÀ   •— || _         | j         dk     rt          d¦  «        ‚t          t          | ¦  «                             |¦  «         d| _        t          | _        d S )Nr   z$semaphore initial value must be >= 0F)r*   Ú
ValueErrorÚsuperr   r   Ú_notify_allÚ_UNSETr+   )r   ÚvalueÚhubÚ	__class__s      €r   r   zSemaphore.__init__\   sZ   ø€ ØˆŒØŒ<˜!ÒÐÝÐCÑDÔDÐDÝ�i˜ÑÔ×'Ò'¨Ñ,Ô,Ð,Ø ˆÔÝ$ˆÔÐÐr   c                 óp   — d| j         j        t          | ¦  «        | j        |                      ¦   «         fz  S )Nz"<%s at 0x%x counter=%s _links[%s]>)r4   r$   Úidr*   Ú	linkcount©r   s    r   Ú__str__zSemaphore.__str__d   s7   € Ø3ØŒNÔ#Ýˆt‰HŒHØŒLØ�NŠNÑÔð	7
ñ 
ð 	
r   c                 ó   — | j         dk    S )zî
        Return a boolean indicating whether the semaphore can be
        acquired (`False` if the semaphore *can* be acquired). Most
        useful with binary semaphores (those with an initial value of 1).

        :rtype: bool
        r   ©r*   r8   s    r   ÚlockedzSemaphore.lockedl   s   € ð Œ|˜qÒ Ð r   c                 óX   — | xj         dz  c_         |                      ¦   «          | j         S )aò  
        Release the semaphore, notifying any waiters if needed. There
        is no return value.

        .. note::

            This can be used to over-release the semaphore.
            (Release more times than it has been acquired or was initially
            created with.)

            This is usually a sign of a bug, but under some circumstances it can be
            used deliberately, for example, to model the arrival of additional
            resources.

        :rtype: None
        r,   )r*   Ú_check_and_notifyr8   s    r   r!   zSemaphore.releasev   s.   € ð" 	ˆŒ˜ÑˆŒØ×ÒÑ Ô Ð ØŒ|Ðr   c                 ó   — | j         dk    S )zœ
        Return a boolean indicating whether the semaphore can be
        acquired (`True` if the semaphore can be acquired).

        :rtype: bool
        r   r;   r8   s    r   ÚreadyzSemaphore.ready‹   s   € ð Œ|˜aÒÐr   c                 ó.   — |                       ¦   «          d S r   )r>   r8   s    r   Ú_start_notifyzSemaphore._start_notify”   s   € Ø×ÒÑ Ô Ð Ð Ð r   c                 ó   — |r|S dS )NTr(   )r   ÚwaitedÚwait_successs      r   Ú_wait_return_valuezSemaphore._wait_return_value—   s   € Øð 	 ØÐð ˆtr   c                 ó^   — | j         dk    r| j         S |                      |¦  «         | j         S )aD  
        Wait until it is possible to acquire this semaphore, or until the optional
        *timeout* elapses.

        .. note:: If this semaphore was initialized with a *value* of 0,
           this method will block forever if no timeout is given.

        :keyword float timeout: If given, specifies the maximum amount of seconds
           this method will block.
        :return: A number indicating how many times the semaphore can be acquired
            before blocking. *This could be 0,* if other waiters acquired
            the semaphore.
        :rtype: int
        r   )r*   Ú_wait)r   Útimeouts     r   ÚwaitzSemaphore.waitŸ   s3   € ð Œ<˜!ÒÐØ”<Ðà�
Š
�7ÑÔÐØŒ|Ðr   Tc                 ó  — | j         t          u r|                      ¦   «         | _         n)| j         |                      ¦   «         k    rt          | _         d}	 |                      d¦  «         nE# t
          $ r8}|j        }d}| j        s|r|                      |||¦  «        cY d}~S Y d}~nd}~ww xY w| j        dk    r| xj        dz  c_        dS |sdS | j         t          ur| j	        €t          ¦   «         | _	        | j	        €/|s-|                      dd|                      ¦   «         df||¦  «        S 	 |                      |¦  «        }nŽ# t          $ r�}|j        }d}| j        rd}ndt          |¦  «        dk    r|d         j        r‚ |                      | j	        t!          ¦   «         |                      ¦   «         df||¦  «        cY d}~S Y d}~nd}~ww xY w|s|€J ‚dS | j        dk    sJ | j        |||f¦   «         ‚| xj        dz  c_        dS )	a"  
        acquire(blocking=True, timeout=None) -> bool

        Acquire the semaphore.

        .. note:: If this semaphore was initialized with a *value* of 0,
           this method will block forever (unless a timeout is given or blocking is
           set to false).

        :keyword bool blocking: If True (the default), this function will block
           until the semaphore is acquired.
        :keyword float timeout: If given, and *blocking* is true,
           specifies the maximum amount of seconds
           this method will block.
        :return: A `bool` indicating whether the semaphore was acquired.
           If ``blocking`` is True and ``timeout`` is None (the default), then
           (so long as this semaphore was initialized with a size greater than 0)
           this will always return True. If a timeout was given, and it expired before
           the semaphore was acquired, False will be returned. (Note that this can still
           raise a ``Timeout`` exception, if some other caller had already started a timer.)
        NFr   r,   TÚNoHubsé   r   )r+   r1   Ú_get_thread_identÚ_MULTIÚ_capture_hubr
   Úargsr*   Ú%_Semaphore__acquire_from_other_threadr3   r   Ú_getcurrentrH   r   ÚlenÚmain_hubr   )r   ÚblockingrI   Úinvalid_thread_useÚeÚsuccessÚexrQ   s           r   ÚacquirezSemaphore.acquire´   sª  € ð0 Ô¥&Ð(Ð(Ø"&×"8Ò"8Ñ":Ô":ˆDÔÐØÔ  D×$:Ò$:Ñ$<Ô$<Ò<Ð<Ý"(ˆDÔð "Ðð		_Ø×Ò˜eÑ$Ô$Ð$Ð$øÝ$ð 	_ð 	_ð 	_ð "#¤ÐØˆAØ”<ð _ Hð _à×7Ò7Ð8JÈHÐV]Ñ^Ô^Ð^Ð^Ð^Ð^Ð^Ð^øøøøøøøøøð	_øøøð Œ<˜!ÒÐØˆLŒL˜AÑˆLŒLØ�4àð 	Ø�5àÔ¥fÐ,Ð,°´Ð1AÝ‘y”yˆDŒHàŒ8ÐÐ$6Ðð ×3Ò3Ø�t˜T×-Ò-Ñ/Ô/°Ð:ØØñô ð ð	Ø—j’j Ñ)Ô)ˆGˆGøÝð 	ð 	ð 	Ø”7ˆDØˆBØŒ|ð 
Ø��õ �t‘9”9 ’>�> d¨1¤gÔ&6�>àØ×7Ò7Ø”XÕ0Ñ2Ô2°D×4DÒ4DÑ4FÔ4FÈ
ÐSØØñô ð ð ð ð ð ð ð ����øøøøð		øøøð ð 	ØÐ&Ð&Ð&à�5ð Œ|˜aÒÐÐ $¤,°¸'À7Ð!LÑÔÐØˆŒ˜ÑˆŒØˆts=   ÁA+ Á+
B-Á5(B(ÂB-Â(B-Ä0E Å
GÅA1GÇGÇGc                 ó.   — |                       ¦   «          d S r   )r[   r8   s    r   Ú	__enter__zSemaphore.__enter__  ó   € Ø�Š‰Œˆˆˆr   c                 ó.   — |                       ¦   «          d S r   )r!   )r   ÚtÚvÚtbs       r   Ú__exit__zSemaphore.__exit__  r^   r   c                 ó:   — | j                              |¦  «         d S r   )Ú_linksÚextend)r   Ú
unswitcheds     r   Ú _handle_unswitched_notificationsz*Semaphore._handle_unswitched_notifications  s    € ð. 	Œ×Ò˜:Ñ&Ô&Ð&Ð&Ð&r   c                 óŒ   — | j         s|                      |¦  «         d S | j         j        d                              |¦  «         d S )Nr   )Ú	_notifierÚrawlinkrQ   Úappend)r   Úlinks     r   Ú
__add_linkzSemaphore.__add_link5  sH   € ØŒ~ð 	0Ø�LŠL˜ÑÔÐÐÐàŒNÔ Ô"×)Ò)¨$Ñ/Ô/Ð/Ð/Ð/r   c                 óÊ   — |sJ ‚|d         }|d         }|d         }|€|€|                       |¦  «        S |€|                      ||¦  «        S |                      |||¦  «        S )Nr   r,   é   )Ú _Semaphore__acquire_without_hubsÚ#_Semaphore__acquire_using_other_hubÚ"_Semaphore__acquire_using_two_hubs)r   Úex_argsrV   rI   Ú
owning_hubÚhub_for_this_threadÚcurrent_greenlets          r   Ú__acquire_from_other_threadz%Semaphore.__acquire_from_other_thread;  s‹   € Øˆˆˆð ˜Q”Zˆ
Ø% aœjÐØ" 1œ:ÐàÐÐ"5Ð"=Ø×.Ò.¨wÑ7Ô7Ð7àÐ&ð ×1Ò1°*¸gÑFÔFÐFð ×,Ò,Ð-@Ø-=Ø-4ñ6ô 6ð 	6r   c                 ó   — |j                              ¦   «         }|j        }|                     |j        | ¦  «         	 t          j        |¦  «        5 }	 	 | j        dk    rq| xj        dz  c_        | j        dk    sJ | f¦   «         ‚	 d d d ¦  «         |                      |¦  «         | 	                    ¦   «          | 
                    ¦   «          dS |                      |¦  «         |                      |¦  «         |                      d¦  «        }|rK|cd d d ¦  «         |                      |¦  «         | 	                    ¦   «          | 
                    ¦   «          S �Œ
# t
          $ rX}||ur‚ Y d }~d d d ¦  «         |                      |¦  «         | 	                    ¦   «          | 
                    ¦   «          dS d }~ww xY w# 1 swxY w Y   	 |                      |¦  «         | 	                    ¦   «          | 
                    ¦   «          d S # |                      |¦  «         | 	                    ¦   «          | 
                    ¦   «          w xY w)Nr,   r   TF)ÚloopÚasync_Úsend_ignoring_argÚstartÚswitchr   Ú_start_new_or_dummyr*   Ú_quiet_unlink_allÚstopÚcloseÚ_Semaphore__add_linkÚ_switch_to_hubr[   )	r   rv   rw   rI   ÚwatcherÚsendÚtimerÚresultÚtexs	            r   Ú__acquire_using_two_hubsz"Semaphore.__acquire_using_two_hubs`  s°  € ð &Ô*×1Ò1Ñ3Ô3ˆØÔ(ˆØ�ŠÐ&Ô-¨tÑ4Ô4Ð4ð	ÝÔ,¨WÑ5Ô5ð !¸ð!ð*Øœ<¨!Ò+Ð+Ø ˜LœL¨AÑ-˜LœLØ#'¤<°1Ò#4Ð#4Ð#4°t°gÑ#4Ô#4Ð#4Ø#'ð!ð !ð !ñ !ô !ð !ð4 ×"Ò" 4Ñ(Ô(Ð(Ø�LŠL‰NŒNˆNØ�MŠM‰OŒOˆOˆOˆOð% Ÿš¨Ñ-Ô-Ð-ð ×+Ò+Ð,?Ñ@Ô@Ð@ð
 "&§¢¨a¡¤˜Ø!ð *Ø#)ð)!ð !ð !ð !ñ !ô !ð !ð4 ×"Ò" 4Ñ(Ô(Ð(Ø�LŠL‰NŒNˆNØ�MŠM‰OŒOˆOˆOñ1*øõ" ð !ð !ð !Ø %Ð'Ð'ØØ ˜5˜5˜5ð1!ð !ð !ñ !ô !ð !ð4 ×"Ò" 4Ñ(Ô(Ð(Ø�LŠL‰NŒNˆNØ�MŠM‰OŒOˆOˆOˆOøøøøð!øøøð+!ð !ð !ð !øøøð !ð !ð !ð !ð !ð4 ×"Ò" 4Ñ(Ô(Ð(Ø�LŠL‰NŒNˆNØ�MŠM‰OŒOˆOˆOˆOøð ×"Ò" 4Ñ(Ô(Ð(Ø�LŠL‰NŒNˆNØ�MŠM‰OŒOˆOˆOøøøsl   ½H ÁGÁ2EÂH ÃAEÄH ÅEÅ
G Å(F;Å-GÅ1H Æ;G Ç GÇGÇH Ç
GÇH È?Ic                 óº   — 	 |                       ||¦  «        }|                     |¦  «         |                     ¦   «          n# |                     ¦   «          w xY w|S r   )r[   rl   r!   )r   ÚresultsrV   rI   Úthread_lockrˆ   s         r   Ú__acquire_from_other_thread_cbz(Semaphore.__acquire_from_other_thread_cbŠ  s^   € ð	"Ø—\’\ (¨GÑ4Ô4ˆFØ�NŠN˜6Ñ"Ô"Ð"à×ÒÑ!Ô!Ð!Ð!øˆK×ÒÑ!Ô!Ð!Ð!øøøØˆs   ‚+A ÁAc                 ó  — |t          ¦   «         usJ ‚|                      ¦   «         }|                     ¦   «          g }|j                             t
          | j        |d||¦  «         |                      |d ¦  «         |d         S )Nr,   r   )r   Ú_allocate_lockr[   rz   Úrun_callback_threadsafer   Ú(_Semaphore__acquire_from_other_thread_cbÚ_Semaphore__spin_on_native_lock)r   ru   rI   r�   rŒ   s        r   Ú__acquire_using_other_hubz#Semaphore.__acquire_using_other_hub’  s“   € ØÕ!2Ñ!4Ô!4Ð4Ð4Ð4Ð4Ø×)Ò)Ñ+Ô+ˆØ×ÒÑÔÐØˆàŒ×/Ò/ÝØÔ/ØØØØñ	ô 	ð 	ð 	×"Ò" ;°Ñ5Ô5Ð5Ø�qŒzÐr   c                 óÈ  — |                       ¦   «         }|                     ¦   «          d}d}|rt          ¦   «         |z   }t          |¦  «        }	 |                      |¦  «         |rt          ¦   «         }|                      ||¦  «        }|                      |¦  «         |r|                      d¦  «        rdS |r(t          ¦   «         }||k    rdS ||z
  }||z  }|dk    rdS Œ”)Nr   r,   TF)r�   r[   r	   r   rƒ   r“   r€   )	r   rI   r�   Úabsolute_expirationÚbeginrm   Ú
got_nativeÚnowÚdurations	            r   Ú__acquire_without_hubsz Semaphore.__acquire_without_hubs¦  s  € Ø×)Ò)Ñ+Ô+ˆØ×ÒÑÔÐØÐØˆØð 	8Ý"+¡+¤+°Ñ"7Ðõ   Ñ,Ô,ˆð	!Ø�OŠO˜DÑ!Ô!Ð!Ø"ð $Ý!™œ�à×3Ò3°KÀÑIÔIˆJØ×"Ò" 4Ñ(Ô(Ð(Øð  Ø—<’< ‘?”?ð  Ø˜4Ø"ð !Ý‘k”k�ØÐ-Ò-Ð-Ø ˜5Ø ™;�Ø˜8Ñ#�Ø˜a’<�<Ø ˜5ð#	!r   c                 ó~  — d}|rt          ¦   «         |z   }|                      ¦   «          	 |                     d¦  «        sO|r)t          ¦   «         |k    r	 |                      ¦   «          dS t	          d¦  «         |                     d¦  «        ¯O	 |                      ¦   «          dS # |                      ¦   «          w xY w)Nr   Fgü©ñÒMbP?T)r	   Ú_drop_lock_for_switch_outr[   Ú_acquire_lock_for_switch_inÚ_native_sleep)r   r�   rI   Ú
expirations       r   Ú__spin_on_native_lockzSemaphore.__spin_on_native_lockÃ  sÜ   € Øˆ
Øð 	/Ý"™œ wÑ.ˆJà×&Ò&Ñ(Ô(Ð(ð	/ð
 "×)Ò)¨!Ñ,Ô,ð %Øð !¥)¡+¤+°Ò";Ð";Ø ð
 ×,Ò,Ñ.Ô.Ð.Ð.Ð.õ ˜eÑ$Ô$Ð$ð	 "×)Ò)¨!Ñ,Ô,ð %ð
 à×,Ò,Ñ.Ô.Ð.Ð.Ð.øˆD×,Ò,Ñ.Ô.Ð.Ð.øøøs   «)B& Á+$B& Â&B<)r,   Nr   )TN)r$   r%   r&   Ú__doc__r'   r   r9   r<   r!   r@   rB   rF   rJ   r[   Ú_py3k_acquirer]   rc   rh   rƒ   rR   rs   r’   rr   rq   r“   Ú__classcell__©r4   s   @r   r   r   0   su  ø€ € € € € ðð ðB€Ið%ð %ð %ð %ð %ð %ð
ð 
ð 
ð!ð !ð !ðð ð ð* ð  ð  ð!ð !ð !ðð ð ðð ð ð ð*^ð ^ð ^ð ^ð@ €Mðð ð ðð ð ð'ð 'ð 'ð20ð 0ð 0ð#6ð #6ð #6ðJ(ð (ð (ðTð ð ðð ð ð(!ð !ð !ð:/ð /ð /ð /ð /ð /ð /r   c                   ó6   ‡ — e Zd ZdZdZeZd„ Zd„ Zˆ fd„Z	ˆ xZ
S )r   a�  
    BoundedSemaphore(value=1) -> BoundedSemaphore

    A bounded semaphore checks to make sure its current value doesn't
    exceed its initial value. If it does, :class:`ValueError` is
    raised. In most situations semaphores are used to guard resources
    with limited capacity. If the semaphore is released too many times
    it's a sign of a bug.

    If not given, *value* defaults to 1.
    )Ú_initial_valuec                 óH   — t          j        | g|¢R i |¤Ž | j        | _        d S r   )r   r   r*   r§   )r   rQ   Úkwargss      r   r   zBoundedSemaphore.__init__ì  s3   € ÝÔ˜4Ð1 $Ð1Ð1Ð1¨&Ð1Ð1Ð1Ø"œlˆÔÐÐr   c                 ó¨   — | j         | j        k    r|                      d¦  «        ‚t                               | ¦  «        }|| j        k    rd| _        |S )z�
        Like :meth:`Semaphore.release`, but raises :class:`ValueError`
        if the semaphore is being over-released.
        z!Semaphore released too many timesN)r*   r§   Ú_OVER_RELEASE_ERRORr   r!   r3   )r   r*   s     r   r!   zBoundedSemaphore.releaseð  sX   € ð
 Œ<˜4Ô.Ò.Ð.Ø×*Ò*Ð+NÑOÔOÐOÝ×#Ò# DÑ)Ô)ˆð �dÔ)Ò)Ð)ØˆDŒHØˆr   c                 ón   •— t          t          | ¦  «                             ¦   «          | j        | _        d S r   )r/   r   Ú_at_fork_reinitr§   r*   )r   r4   s    €r   r­   z BoundedSemaphore._at_fork_reinitÿ  s.   ø€ ÝÕ Ñ%Ô%×5Ò5Ñ7Ô7Ð7ØÔ*ˆŒˆˆr   )r$   r%   r&   r¢   r'   r.   r«   r   r!   r­   r¤   r¥   s   @r   r   r   Ø  so   ø€ € € € € ð
ð 
ð€Ið
 %Ðð+ð +ð +ðð ð ð+ð +ð +ð +ð +ð +ð +ð +ð +r   )Úimport_c_accelzgevent.__semaphoreN) Ú
__future__r   r   r   Ú__all__Útimer   rŸ   Úgevent._compatr	   Úgevent.exceptionsr
   r   Úgevent.timeoutr   r   ÚlocalsÚgevent._hub_localr   r   Ú
gevent.hubr   Úobjectr   r1   rO   r   r   r   Úgevent._utilr®   Úglobalsr(   r   r   ú<module>r»      sÆ  ðð AÐ @Ð @Ð @Ð @Ð @Ð @Ð @Ð @Ð @ð Øð€ð
 (Ð 'Ð 'Ð 'Ð 'Ð 'à $Ð $Ð $Ð $Ð $Ð $Ø 3Ð 3Ð 3Ð 3Ð 3Ð 3Ø &Ð &Ð &Ð &Ð &Ð &Ø "Ð "Ð "Ð "Ð "Ð "ð1ð 1ð 1ð  -˜}™œ€€�„Ð	Ñ Øà /Ð /Ð /Ð /Ð /Ð /Ø %Ð %Ð %Ð %Ð %Ð %Ø  Ð  Ð  Ð  Ð  Ð  ð	ð 	ð 	ð 	ð 	�vñ 	ô 	ð 	ð 
ˆ‰Œ€Ø	ˆ‰Œ€ðe/ð e/ð e/ð e/ð e/Ð ñ e/ô e/ð e/ðP)+ð )+ð )+ð )+ð )+�yñ )+ô )+ð )+ðp (Ð 'Ð 'Ð 'Ð 'Ð 'Ø €ˆwˆw‰yŒyÐ.Ñ /Ô /Ð /Ð /Ð /r   