o
    ‡eM"  ã                   @   sl  d Z ddlmZ g d¢ZddlZej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mZ dd
lmZ ddlmZ e	Z	eZeZeZeZeZeZeZdd„ Zdd„ ZdZG dd„ deƒZeedƒrkdd„ Zndd„ ZG dd„ dejƒZe  d¡ G dd„ deej!ƒZ!e  d¡ eZ"e  d¡ e #d¡ e  d¡ eZe #d¡ eedƒr°dZ$e  d¡ dd „ Z%dS )!aÆ  
Implementation of the standard :mod:`threading` using greenlets.

.. note::

    This module is a helper for :mod:`gevent.monkey` and is not
    intended to be used directly. For spawning greenlets in your
    applications, prefer higher level constructs like
    :class:`gevent.Greenlet` class or :func:`gevent.spawn`. Attributes
    in this module like ``__threading__`` are implementation artifacts subject
    to change at any time.

.. versionchanged:: 1.2.3

   Defer adjusting the stdlib's list of active threads until we are
   monkey patched. Previously this was done at import time. We are
   documented to only be used as a helper for monkey patching, so this should
   functionally be the same, but some applications ignore the documentation and
   directly import this module anyway.

   A positive consequence is that ``import gevent.threading,
   threading; threading.current_thread()`` will no longer return a DummyThread
   before monkey-patching.
é    )Úabsolute_import)ÚlocalÚ_start_new_threadÚ_allocate_lockÚLockÚ
_get_identÚ_sleepÚ_DummyThreadN)r   )Ústart_new_thread)Úallocate_lock)Ú	get_ident)ÚsleepÚ
getcurrent)ÚRLock)ÚLazyOnClassc                 C   s   t j t| ƒd ¡ d S ©N)Ú__threading__Ú_activeÚpopr   )Úg© r   ú@D:\Projects\ConvertPro\env\Lib\site-packages\gevent/threading.pyÚ_cleanupE   s   r   c                    s   ‡ fdd„}|S )Nc                    s   t j ˆ d ¡ d S r   )r   r   r   )Ú_r©Úgidr   r   Ú_I   s   z_make_cleanup_id.<locals>._r   )r   r   r   r   r   Ú_make_cleanup_idH   s   r   c                   @   sˆ   e Zd Zd Z ZZd ZZd ZZ	d Z
Zd ZZd ZZd ZZe ¡  ZZe ¡  dZdd„ Zdd„ ZeZd	d
„ Zedd„ ƒZdS )r	   FTr   Nc                 C   s~   t  d¡ | _| _|  ¡  tƒ }t|ƒ}| t j|< t|dd ƒ}|d ur)|t	ƒ d S | j
}||t|ƒƒ}|| _| j|u s=J ‚d S )NzDummy-%dÚrawlink)r   Ú_newnameÚ_nameZ_Thread__nameÚ
_set_identr   r   r   Úgetattrr   Ú_DummyThread__weakref_refr   Z_DummyThread__raw_ref)Úselfr   r   r   Úrefr   r   r   Ú__init__y   s   
z_DummyThread.__init__c                 C   ó   d S r   r   ©r$   r   r   r   Ú_Thread__stop“   ó   z_DummyThread._Thread__stopc                 O   r'   r   r   )r$   ÚargsÚkwargsr   r   r   Ú_wait_for_tstate_lock˜   r*   z"_DummyThread._wait_for_tstate_lockc                 C   s
   t dƒjS )NÚweakref)Ú
__import__r%   r(   r   r   r   Z__weakref_ref›   s   
z_DummyThread.__weakref_ref)Ú__name__Ú
__module__Ú__qualname__Z_Thread__stoppedÚ_is_stoppedZ_stoppedZ_Thread__initializedÚ_initializedZ_Thread__daemonicÚ	_daemonicZ_Thread__argsÚ_argsZ_Thread__kwargsÚ_kwargsZ_Thread__targetÚ_targetZ_Thread_identÚ_identr   ÚEventZ_Thread__startedÚ_startedÚsetÚ_tstate_lockr&   r)   Ú_stopr-   r   r#   r   r   r   r   r	   O   s"    r	   Úmain_threadc                   C   s   t  ¡ S r   )r   r?   r   r   r   r   Úmain_native_thread    s   r@   c                  C   s0   dd„ t j ¡ D ƒ} t| ƒdksJ dƒ‚| d S )Nc                 S   s   g | ]
}t |tjƒr|‘qS r   )Ú
isinstancer   Ú_MainThread)Ú.0Úvr   r   r   Ú
<listcomp>¤   s    

ÿz&main_native_thread.<locals>.<listcomp>é   zToo many main threadsr   )r   r   ÚvaluesÚlen)Zmain_threadsr   r   r   r@   £   s   c                       s$   e Zd Z‡ fdd„Zdd„ Z‡  ZS )ÚThreadc                    s$   t t| ƒ ¡  tƒ }| | j¡ d S r   )ÚsuperrI   Ú_set_tstate_lockr   r   Ú_Thread__greenlet_finished)r$   Zgreenlet©Ú	__class__r   r   rK   ·   s   zThread._set_tstate_lockc                 C   s    | j r| j  ¡  |  ¡  d S d S r   )r=   Úreleaser>   )r$   r   r   r   r   Z__greenlet_finished¼   s   
þzThread.__greenlet_finished)r0   r1   r2   rK   rL   Ú__classcell__r   r   rM   r   rI   µ   s    rI   c                   @   s   e Zd ZdS )ÚTimerN)r0   r1   r2   r   r   r   r   rQ   Ã   s    rQ   Ú_set_sentinelr   r   r   Ú_CRLockc                 C   sZ   t ƒ }t ¡ |kr|dƒ d S tƒ tjvr+|j}tj|= tƒ  |_|_|tjtƒ < d S d S )Nz}Monkey-patching outside the main native thread. Some APIs will not be available. Expect a KeyError to be printed at shutdown.)r@   r   Úcurrent_threadr   r   Úidentr9   Z_Thread__ident)Znative_moduleÚitemsÚwarnr?   Zmain_idr   r   r   Ú_gevent_will_monkey_patchß   s   ürX   )&Ú__doc__Ú
__future__r   Z__implements__Ú	threadingr   r	   Z_DummyThread_Zgevent.localr   Zgevent.threadr
   r   r   r   r   r   Z
gevent.hubr   r   r   Zgevent.lockr   Zgevent._utilr   r   r   r   Ú_weakrefÚhasattrr@   rI   ÚappendrQ   rR   ÚremoverS   rX   r   r   r   r   Ú<module>   sR    
P









