
    h                         S r SSKrSSKrSSKrSSKJr  SSKJr  SSKJ	r	J
r
Jr  SSKr\" SSS/5      r " S	 S
\5      r " S S5      r\" 5       rg)zcGarbage collection thread for representing zmq refcount of Python objects
used in zero-copy sends.
    N)
namedtuple)getpid)EventLockThreadgcrefobjeventc                   2   ^  \ rS rSrSrU 4S jrS rSrU =r$ )GarbageCollectorThread   z4Thread in which garbage collection actually happens.c                 x   > [         TU ]  5         Xl        SU l        [	        5       U l        [        5       U l        g )NT)super__init__gcdaemonr   pidr   ready)selfr   	__class__s     dC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\zmq/utils/garbage.pyr   GarbageCollectorThread.__init__   s,    8W
    c                 H   [         b  [        5       U R                  :w  a  U R                  R                  5         g  U R                  R
                  R                  [        R                  5      nSUl	        UR                  U R                  R                  5        U R                  R                  5          [         b  [        5       U R                  :w  a  g UR                  5       nUS:X  a  O[        U5      S:X  a  SOSn[        R                  " X25      S   nU R                  R                   R#                  US 5      nU(       a+  UR$                  (       a  UR$                  R                  5         AM  UR'                  5         g ! U R                  R                  5         f = f)Nr      DIE   LQ)r   r   r   setr   contextsocketzmqPULLlingerbindurlrecvlenstructunpackrefspopr
   close)r   smsgfmtkeytups         r   runGarbageCollectorThread.run   s   >VX1JJNN	&&sxx0AAHFF477;;JJNN~TXX!5&&(Cf}SQ#CC--)!,C'',,""3-Csyy		  	
	 JJNNs   AF F!)r   r   r   r   )	__name__
__module____qualname____firstlineno____doc__r   r3   __static_attributes____classcell__r   s   @r   r   r      s    > r   r   c                      ^  \ rS rSrSrSrSrSrSrSU 4S jjr	\
S 5       r\R                  S 5       rS rS	 rS
 rS r\
S 5       rS rS rSS jrS rSrU =r$ )GarbageCollector:   a  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.
Nzinproc://pyzmq.gc.01c                    > [         TU ]  5         0 U l        S U l        S U l        Xl        [        5       U l        SU l        S U l	        S U l
        [        R                  " U R                  5        g )NF)r   r   r+   r   thread_contextr   _lock
_stay_down_push_push_mutexatexitregister_atexit)r   r    r   s     r   r   GarbageCollector.__init__N   sW    	V

%r   c                     U R                   ce  [        R                  R                  S5      (       a'  SSKJn  UR                  5       U l         U R                   $ [        R                  " 5       U l         U R                   $ )Ngeventr   )green)rB   r   r6   
startswithr"   rM   Context)r   rM   s     r   r    GarbageCollector.contextZ   sX    ==   ++H55% % }} !$}}r   c                     U R                  5       (       a<  U R                  (       a  [        R                  " S[        5        U R                  5         Xl        g )Nz(Replacing gc context while gc is running)is_aliver+   warningswarnRuntimeWarningstoprB   )r   ctxs     r   r    rP   f   s5    ==??yy> IIKr   c                 2    SU l         U R                  5         g)zhatexit callback

sets _stay_down flag so that gc doesn't try to start up again in other atexit handlers
TN)rD   rV   r   s    r   rI   GarbageCollector._atexitp   s    
 		r   c                 P    U R                  5       (       d  gU R                  5         g)z"stop the garbage-collection threadN)rR   _stoprY   s    r   rV   GarbageCollector.stopx   s    }}

r   c                 p    SU l         SU l        SU l        U R                  R	                  5         SU l        g)zCClear state

called after stop or when setting up a new subprocess
N)rE   rF   rA   r+   clearr    rY   s    r   _clearGarbageCollector._clear~   s/    
 
		r   c                    U R                   R                  [        R                  5      nUR	                  U R
                  5        UR                  S5        UR                  5         U R                  (       a  U R                  R                  5         U R                  R                  5         U R                   R                  5         U R                  5         g )Nr   )r    r!   r"   PUSHconnectr&   sendr-   rE   rA   jointermr`   )r   pushs     r   r\   GarbageCollector._stop   s    ||""388,TXX		&

::JJr   c                 >   [        U SS5      (       a  [        S5      eU R                  5       (       a  U R                  cS  U R                  R                  [        R                  5      U l        U R                  R                  U R                  5        U R                  $ )z?The PUSH socket for use in the zmq message destructor callback.rD   Fz'zmq gc socket requested during shutdown)
getattrRuntimeErrorrR   rE   r    r!   r"   rc   rd   r&   rY   s    r   _push_socketGarbageCollector._push_socket   sm     4u--HII}}$**"4,,SXX6DJJJtxx(zzr   c                 6   U R                   b(  U R                  [        5       :w  a  U R                  5         [        5       U l        0 U l        [        U 5      U l         U R                   R                  5         U R                   R                  R                  5         g)zStart 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)	rA   r   r   r`   r+   r   startr   waitrY   s    r   rp   GarbageCollector.start   sh     ;;"txx68'; KKM8	,T2 r   c                     [         bD  [        5       U R                  :w  d,  U R                  b  U R                  R                  5       (       d  gg)zcIs the garbage collection thread currently running?

Includes checks for process shutdown or fork.
FT)r   r   rA   rR   rY   s    r   rR   GarbageCollector.is_alive   s:     Nx488#{{";;''))r   c                 2   U R                  5       (       dL  U R                  (       a  gU R                     U R                  5       (       d  U R                  5         SSS5        [	        X5      n[        U5      nX0R                  U'   U$ ! , (       d  f       N4= f)z4store an object and (optionally) event for zero-copyr   N)rR   rD   rC   rp   r   idr+   )r   r	   r
   r2   theids        r   storeGarbageCollector.store   sg    }} }}JJL  C3		% s   &B
Bc                 z    U R                  5       (       d  g  U R                  5         g ! [         a  nUeS nAff = fN)rR   rV   	Exception)r   es     r   __del__GarbageCollector.__del__   s3    }}	IIK 	I	s   ) 
:5:)	rB   rC   rE   rF   rD   r    r   r+   rA   r{   )r5   r6   r7   r8   r9   r+   rB   rC   r&   r   propertyr    setterrI   rV   r`   r\   rm   rp   rR   rx   r~   r:   r;   r<   s   @r   r>   r>   :   s     DHE
 C
& 	 	 ^^ 		  !   r   r>   )r9   rG   r)   rS   collectionsr   osr   	threadingr   r   r   r"   r   r   r>   r    r   r   <module>r      sY       "  ) ) 
7UG,-#V #LX Xv r   