B
    H/\!                 @   sv  d Z ddlmZ ddddddd	gZdd
lZejZddlm	Z	 ddl
mZmZmZ ddlmZmZ e	Z	eZeZeZeZeZeZdd Zdd Zd
aG dd	 d	eZeedrdd Zndd Zdd
lZejd
d dkrG dd dejZed G dd deejZed ejd
d dkrje d ed eZe d eeds\t!d 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
getcurrentc             C   s   t jt| d  d S )N)__threading___activepopr   )g r   /lib/python3.7/site-packages/gevent/threading.py_cleanup8   s    r   c                s    fdd}|S )Nc                s   t j d  d S )N)r   r   r   )Z_r)gidr   r   _<   s    z_make_cleanup_id.<locals>._r   )r   r   r   )r   r   _make_cleanup_id;   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dS )r	   FTr   Nc             C   sz   t d | _| _|   t }t|}| t j|< t|dd }|d k	rP|t	 n&t
d kr`tda
t
|t|}|| _d S )NzDummyThread-%drawlinkweakref)r   Z_newname_nameZ_Thread__nameZ
_set_identr   r   r   getattrr   _weakref
__import__refr   Z_DummyThread__raw_ref)selfr   r   r   r   r   r   r   __init__l   s    

z_DummyThread.__init__c             C   s   d S )Nr   )r    r   r   r   _Thread__stop   s    z_DummyThread._Thread__stopc             O   s   d S )Nr   )r    argskwargsr   r   r   _wait_for_tstate_lock   s    z"_DummyThread._wait_for_tstate_lock)__name__
__module____qualname__Z_Thread__stoppedZ_is_stoppedZ_stoppedZ_Thread__initializedZ_initializedZ_Thread__daemonicZ	_daemonicZ_Thread__argsZ_argsZ_Thread__kwargsZ_kwargsZ_Thread__targetZ_targetZ_Thread_ident_identr   ZEventZ_Thread__startedZ_startedsetZ_tstate_lockr!   r"   _stopr%   r   r   r   r   r	   B   s   main_threadc               C   s   t  S )N)r   r,   r   r   r   r   main_native_thread   s    r-   c              C   s0   dd t j D } t| dks(td| d S )Nc             S   s   g | ]}t |tjr|qS r   )
isinstancer   Z_MainThread).0vr   r   r   
<listcomp>   s    z&main_native_thread.<locals>.<listcomp>   zToo many main threadsr   )r   r   valueslenAssertionError)Zmain_threadsr   r   r   r-      s       )      c                   sF   e Zd ZdZdd ZeZdd Z fddZddd	Zd
d Z	  Z
S )ThreadNc             C   s
   t | jS )N)bool	_greenlet)r    r   r   r   is_alive   s    zThread.is_alivec             C   s   t  | _d S )N)r   r;   )r    r   r   r   _set_tstate_lock   s    zThread._set_tstate_lockc                s(   zt t|   W d d | _|   X d S )N)superr9   runr;   r+   )r    )	__class__r   r   r?      s    z
Thread.runc             C   s2   d| j krtd| jd kr d S | jj|d d S )Nr;   zCannot join an inactive thread)timeout)__dict__RuntimeErrorr;   join)r    rA   r   r   r   rD      s
    

zThread.joinc             O   s
   t  d S )N)NotImplementedError)r    r#   r$   r   r   r   r%      s    zThread._wait_for_tstate_lock)N)r&   r'   r(   r;   r<   ZisAliver=   r?   rD   r%   __classcell__r   r   )r@   r   r9      s   	
r9   c               @   s   e Zd ZdS )TimerN)r&   r'   r(   r   r   r   r   rG      s   rG   )r7   r7   r   _CRLockzUnsupported Python versionc             C   sV   t  }t |kr|d d S t tjkrR|j}tj|= t  |_|_|tjt < 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   Zcurrent_threadr   r   Zidentr)   Z_Thread__ident)Znative_moduleitemswarnr,   Zmain_idr   r   r   _gevent_will_monkey_patch   s    rK   )$__doc__Z
__future__r   Z__implements__Z	threadingr   r	   Z_DummyThread_Zgevent.localr   Zgevent.threadr
   r   r   r   r   r   Z
gevent.hubr   r   r   r   r   r   r   hasattrr-   sysversion_infor9   appendrG   remover5   rH   rK   r   r   r   r   <module>   sT   N







