σ
ΠH/\c           @@  sσ  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   Z d   Z d
 a d	 e f d     YZ e e d  rϋ d   Z n	 d   Z d d
 l Z e j d  d k rud e j f d     YZ e j d  d e e j  f d     YZ  e j d  n  e j d  d  k rζe j! d  e j d  e Z e j! d  e e d  sΠt" d   d
 Z# e j d  n  d   Z$ d
 S(!   sΖ  
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.
i    (   t   absolute_importt   localt   _start_new_threadt   _allocate_lockt   Lockt
   _get_identt   _sleept   _DummyThreadN(   R   (   t   start_new_threadt   allocate_lockt	   get_ident(   t   sleept
   getcurrentc         C@  s   t  j j t |   d   d  S(   N(   t   __threading__t   _activet   popR   t   None(   t   g(    (    s/   lib/python2.7/site-packages/gevent/threading.pyt   _cleanup8   s    c         @  s     f d   } | S(   Nc         @  s   t  j j   d   d  S(   N(   R   R   R   R   (   t   _r(   t   gid(    s/   lib/python2.7/site-packages/gevent/threading.pyt   _<   s    (    (   R   R   (    (   R   s/   lib/python2.7/site-packages/gevent/threading.pyt   _make_cleanup_id;   s    c           B@  s   e  Z e Z Z Z e Z Z e Z	 Z
 d Z Z d Z Z d Z Z d Z Z e j   Z Z e j   d Z d    Z d   Z e Z d   Z RS(   c         C@  s°   t  j d  |  _ |  _ |  j   t   } t |  } |  t  j | <t | d d   } | d  k	 rp | t
  n< t d  k r t d  a n  t j | t |   } | |  _ d  S(   Ns   DummyThread-%dt   rawlinkt   weakref(   R   t   _newnamet   _namet   _Thread__namet
   _set_identR   R   R   t   getattrR   R   t   _weakreft
   __import__t   refR   t   _DummyThread__raw_ref(   t   selfR   R   R   R    (    (    s/   lib/python2.7/site-packages/gevent/threading.pyt   __init__l   s    
	c         C@  s   d  S(   N(    (   R"   (    (    s/   lib/python2.7/site-packages/gevent/threading.pyt   _Thread__stop   s    c         O@  s   d  S(   N(    (   R"   t   argst   kwargs(    (    s/   lib/python2.7/site-packages/gevent/threading.pyt   _wait_for_tstate_lock   s    (    N(   t   __name__t
   __module__t   Falset   _Thread__stoppedt   _is_stoppedt   _stoppedt   Truet   _Thread__initializedt   _initializedt   _Thread__daemonict	   _daemonict   _Thread__argst   _argsR   t   _Thread__kwargst   _kwargst   _Thread__targett   _targett   _Thread_identt   _identR   t   Eventt   _Thread__startedt   _startedt   sett   _tstate_lockR#   R$   t   _stopR'   (    (    (    s/   lib/python2.7/site-packages/gevent/threading.pyR   B   s   






		t   main_threadc           C@  s
   t  j   S(   N(   R   RA   (    (    (    s/   lib/python2.7/site-packages/gevent/threading.pyt   main_native_thread   s    c          C@  sZ   g  t  j j   D] }  t |  t  j  r |  ^ q } t |  d k sR t d   | d S(   Ni   s   Too many main threadsi    (   R   R   t   valuest
   isinstancet   _MainThreadt   lent   AssertionError(   t   vt   main_threads(    (    s/   lib/python2.7/site-packages/gevent/threading.pyRB      s    i   i   i   t   Threadc           B@  sD   e  Z d Z d    Z e Z d   Z d   Z d d  Z d   Z	 RS(   c         C@  s   t  |  j  S(   N(   t   boolt	   _greenlet(   R"   (    (    s/   lib/python2.7/site-packages/gevent/threading.pyt   is_alive§   s    c         C@  s   t    |  _ d  S(   N(   R   RL   (   R"   (    (    s/   lib/python2.7/site-packages/gevent/threading.pyt   _set_tstate_lock¬   s    c         C@  s2   z t  t |   j   Wd  d  |  _ |  j   Xd  S(   N(   t   superRJ   t   runR   RL   R@   (   R"   (    (    s/   lib/python2.7/site-packages/gevent/threading.pyRP   ―   s    	c         C@  sH   d |  j  k r t d   n  |  j d  k r1 d  S|  j j d |  d  S(   NRL   s   Cannot join an inactive threadt   timeout(   t   __dict__t   RuntimeErrorRL   R   t   join(   R"   RQ   (    (    s/   lib/python2.7/site-packages/gevent/threading.pyRT   Έ   s
    c         O@  s   t     d  S(   N(   t   NotImplementedError(   R"   R%   R&   (    (    s/   lib/python2.7/site-packages/gevent/threading.pyR'   Ώ   s    N(
   R(   R)   R   RL   RM   t   isAliveRN   RP   RT   R'   (    (    (    s/   lib/python2.7/site-packages/gevent/threading.pyRJ   €   s   				t   Timerc           B@  s   e  Z RS(    (   R(   R)   (    (    (    s/   lib/python2.7/site-packages/gevent/threading.pyRW   Ε   s   R
   t   _CRLocks   Unsupported Python versionc         C@  sx   t    } t j   | k r) | d  d  St   t j k rt | j } t j | =t   | _ | _ | t j t   <n  d  S(   Ns}   Monkey-patching outside the main native thread. Some APIs will not be available. Expect a KeyError to be printed at shutdown.(   RB   R   t   current_threadR   R   t   identR:   t   _Thread__ident(   t   native_modulet   itemst   warnRA   t   main_id(    (    s/   lib/python2.7/site-packages/gevent/threading.pyt   _gevent_will_monkey_patchέ   s    	
	
(   i   i   (   i   i   (%   t   __doc__t
   __future__R    t   __implements__t	   threadingR   R   t   _DummyThread_t   gevent.localR   t   gevent.threadR   R   R	   R   R
   R   t
   gevent.hubR   R   R   R   R   R   R   R   t   hasattrRB   t   syst   version_infoRJ   t   appendRW   t   removeRG   RX   R`   (    (    (    s/   lib/python2.7/site-packages/gevent/threading.pyt   <module>   sT   				N	