ó
L]c           @   sÀ   d  Z  d d l Z d d l Z d d l m Z d d l m Z d d l m Z m	 Z	 m
 Z
 d d l Z d d l Z e d d d g ƒ Z d	 e f d
 „  ƒ  YZ d e f d „  ƒ  YZ e ƒ  Z d S(   sc   Garbage collection thread for representing zmq refcount of Python objects
used in zero-copy sends.
iÿÿÿÿN(   t   getpid(   t
   namedtuple(   t   Threadt   Eventt   Lockt   gcreft   objt   eventt   GarbageCollectorThreadc           B   s    e  Z d  Z d „  Z d „  Z RS(   s4   Thread in which garbage collection actually happens.c         C   sA   t  t |  ƒ j ƒ  | |  _ t |  _ t ƒ  |  _ t ƒ  |  _	 d  S(   N(
   t   superR   t   __init__t   gct   Truet   daemonR    t   pidR   t   ready(   t   selfR   (    (    s0   lib/python2.7/site-packages/zmq/utils/garbage.pyR
      s
    		c         C   s?  t  d  k s t  ƒ  |  j k r/ |  j j ƒ  d  Sz8 |  j j j t j	 ƒ } d | _
 | j |  j j ƒ Wd  |  j j ƒ  Xx¶ t r0t  d  k sŸ t  ƒ  |  j k r£ d  S| j ƒ  } | d k r¿ Pn  t | ƒ d k r× d n d } t j | | ƒ d } |  j j j | d  ƒ } | r*| j r*| j j ƒ  n  ~ q{ W| j ƒ  d  S(   Ni    t   DIEi   t   Lt   Q(   R    t   NoneR   R   t   setR   t   contextt   sockett   zmqt   PULLt   lingert   bindt   urlR   t   recvt   lent   structt   unpackt   refst   popR   t   close(   R   t   st   msgt   fmtt   keyt   tup(    (    s0   lib/python2.7/site-packages/zmq/utils/garbage.pyt   run   s*    		(   t   __name__t
   __module__t   __doc__R
   R)   (    (    (    s0   lib/python2.7/site-packages/zmq/utils/garbage.pyR      s   	t   GarbageCollectorc           B   s¤   e  Z d  Z d Z d Z d Z d Z d d „ Z e	 d „  ƒ Z
 e
 j d „  ƒ Z
 d „  Z d „  Z d „  Z e	 d „  ƒ Z d	 „  Z d
 „  Z d d „ Z d „  Z RS(   sN  PyZMQ Garbage Collector
    
    Used for representing the reference held by libzmq during zero-copy sends.
    This object holds a dictionary, keyed by Python id,
    of the Python objects whose memory are currently in use by zeromq.
    
    When zeromq is done with the memory, it sends a message on an inproc PUSH socket
    containing the packed size_t (32 or 64-bit unsigned int),
    which is the key in the dict.
    When the PULL socket in the gc thread receives that message,
    the reference is popped from the dict,
    and any tracker events that should be signaled fire.
    s   inproc://pyzmq.gc.01c         C   sr   t  t |  ƒ j ƒ  i  |  _ d  |  _ d  |  _ | |  _ t ƒ  |  _	 t
 |  _ d  |  _ d  |  _ t j |  j ƒ d  S(   N(   R	   R-   R
   R!   R   R   t   threadt   _contextR   t   _lockt   Falset
   _stay_downt   _pusht   _push_mutext   atexitt   registert   _atexit(   R   R   (    (    s0   lib/python2.7/site-packages/zmq/utils/garbage.pyR
   O   s    							c         C   s\   |  j  d  k rU t j j d ƒ rC d d l m } | j ƒ  |  _  qU t j ƒ  |  _  n  |  j  S(   Nt   geventiÿÿÿÿ(   t   green(   R/   R   R   R+   t
   startswithR   R9   t   Context(   R   R9   (    (    s0   lib/python2.7/site-packages/zmq/utils/garbage.pyR   [   s    c         C   sB   |  j  ƒ  r5 |  j r( t j d t ƒ n  |  j ƒ  n  | |  _ d  S(   Ns(   Replacing gc context while gc is running(   t   is_aliveR!   t   warningst   warnt   RuntimeWarningt   stopR/   (   R   t   ctx(    (    s0   lib/python2.7/site-packages/zmq/utils/garbage.pyR   f   s
    	c         C   s   t  |  _ |  j ƒ  d S(   s€   atexit callback
        
        sets _stay_down flag so that gc doesn't try to start up again in other atexit handlers
        N(   R   R2   R@   (   R   (    (    s0   lib/python2.7/site-packages/zmq/utils/garbage.pyR7   n   s    	c         C   s   |  j  ƒ  s d S|  j ƒ  d S(   s"   stop the garbage-collection threadN(   R<   t   _stop(   R   (    (    s0   lib/python2.7/site-packages/zmq/utils/garbage.pyR@   v   s    c         C   s›   |  j  j t j ƒ } | j |  j ƒ | j d ƒ | j ƒ  |  j r^ |  j j ƒ  d  |  _ n  d  |  _
 |  j j ƒ  |  j  j ƒ  |  j j ƒ  d  |  _  d  S(   NR   (   R   R   R   t   PUSHt   connectR   t   sendR#   R3   R   R4   R.   t   joint   termR!   t   clear(   R   t   push(    (    s0   lib/python2.7/site-packages/zmq/utils/garbage.pyRB   |   s    
		c         C   sQ   |  j  ƒ  s |  j d k rJ |  j j t j ƒ |  _ |  j j |  j ƒ n  |  j S(   sH   The PUSH socket for use in the zmq message destructor callback.
        N(	   R<   R3   R   R   R   R   RC   RD   R   (   R   (    (    s0   lib/python2.7/site-packages/zmq/utils/garbage.pyt   _push_socketŠ   s    c         C   ss   |  j  d k	 r. |  j t ƒ  k r. |  j ƒ  n  t ƒ  |  _ i  |  _ t |  ƒ |  _  |  j  j ƒ  |  j  j j	 ƒ  d S(   sÄ   Start a new garbage collection thread.
        
        Creates a new zmq Context used for garbage collection.
        Under most circumstances, this will only be called once per process.
        N(
   R.   R   R   R    RB   R!   R   t   startR   t   wait(   R   (    (    s0   lib/python2.7/site-packages/zmq/utils/garbage.pyRK   “   s    !	c         C   sE   t  d k s= t  ƒ  |  j k s= |  j d k s= |  j j ƒ  rA t St S(   s{   Is the garbage collection thread currently running?
        
        Includes checks for process shutdown or fork.
        N(   R    R   R   R.   R<   R1   R   (   R   (    (    s0   lib/python2.7/site-packages/zmq/utils/garbage.pyR<   £   s    c         C   sq   |  j  ƒ  sE |  j r d S|  j  |  j  ƒ  s< |  j ƒ  n  Wd QXn  t | | ƒ } t | ƒ } | |  j | <| S(   s4   store an object and (optionally) event for zero-copyi    N(   R<   R2   R0   RK   R   t   idR!   (   R   R   R   R(   t   theid(    (    s0   lib/python2.7/site-packages/zmq/utils/garbage.pyt   store°   s    	
c         C   s>   |  j  ƒ  s d  Sy |  j ƒ  Wn t k
 r9 } | ‚ n Xd  S(   N(   R<   R@   t	   Exception(   R   t   e(    (    s0   lib/python2.7/site-packages/zmq/utils/garbage.pyt   __del__À   s    N(   R*   R+   R,   R   R!   R/   R0   R   R
   t   propertyR   t   setterR7   R@   RB   RJ   RK   R<   RO   RR   (    (    (    s0   lib/python2.7/site-packages/zmq/utils/garbage.pyR-   ;   s    						(   R,   R5   R   t   osR    t   collectionsR   t	   threadingR   R   R   R=   R   R   R   t   objectR-   R   (    (    (    s0   lib/python2.7/site-packages/zmq/utils/garbage.pyt   <module>   s   %