o
    Á¿iñ  ã                   @   sF  U d dl mZ d dlZd dlmZ d dlmZ d dlm	Z	 d dl
mZ eeƒZzd dlmZ W n ey>   d dlmZ Y nw zd d	lmZ W n eyV   d d	lmZ Y nw g d
¢Zdaeƒ aG dd„ dejƒZg Zee ed< G dd„ deƒZeƒ aee ed< G dd„ deƒZejdd„ ƒZ ej!ddd„ƒZ"ej#ddd„ƒZ$dS )é    )Úmonotonic_nsN)Úforksafe)ÚPeriodicThread)Úperiodic_threads)Ú
get_logger)Úallocate_lock)ÚLock)ÚRLock)r   r   r	   Fc                   @   s"   e Zd ZU ejed< ddd„ZdS )ÚBoundMethodÚ__self__ÚreturnNc                 C   s   d S ©N© ©Úselfr   r   úL/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/internal/threads.pyÚ__call__.   s    zBoundMethod.__call__©r   N)Ú__name__Ú
__module__Ú__qualname__ÚtÚAnyÚ__annotations__r   r   r   r   r   r
   +   s   
 
r
   Ú_threads_to_start_after_forkc                       s&   e Zd ZdZdZd‡ fdd„Z‡  ZS )r   zA fork-safe periodic thread.Tr   Nc                    s`   t $ tstƒ  ¡  nt t ttƒ j¡¡ W d   ƒ d S W d   ƒ d S 1 s)w   Y  d S r   )	Ú_forking_lockÚ_forkingÚsuperÚstartr   Úappendr   Úcastr
   r   ©Ú	__class__r   r   r   ;   s   ù"ûzPeriodicThread.startr   )r   r   r   Ú__doc__Ú__autorestart__r   Ú__classcell__r   r   r!   r   r   6   s    r   Ú_threads_to_restart_after_forkc                       sj   e Zd ZU edƒZdZejd  ed< dZ	‡ fdd„Z
ddd	„Zed
d„ ƒZedd„ ƒZedd„ ƒZ‡  ZS )ÚThreadRestartTimerg    „×—ANÚ	_instancer   c                    s,   t ƒ j| jd | jt› d| jj› d d S )Ng    eÍÍAÚ.)Úname)r   Ú__init__Ú__timeout__Ú_restart_threadsr   r"   r   r!   r   r   r+   U   s   ,zThreadRestartTimer.__init__r   c                 C   sÂ   t U tr	 W d   ƒ d S tƒ | jkrOt ¡ D ]}|| u rqt d|j¡ | 	¡  qt 
¡  tD ]}t d|jj¡ |ƒ  q1t 
¡  |  
¡  W d   ƒ d S W d   ƒ d S 1 sZw   Y  d S )NzRestarting thread %s after forkzStarting thread %s after fork)r   r   r   Ú
_timestampr&   ÚcopyÚlogÚdebugr*   Ú_after_forkÚclearr   r   )r   ÚthreadÚthread_startr   r   r   r-   X   s&   ý

é"øz#ThreadRestartTimer._restart_threadsc                 C   s"   | j dur| j  ¡  d| _ dS dS )z-Clear the timer and stop it if it is running.N)r(   Ústop©Úclsr   r   r   r3   s   s   


þzThreadRestartTimer.clearc                 C   s   t ƒ | j | _dS )z*Set the new expiration time for the timer.N)r   r,   r.   r7   r   r   r   Útouchz   s   zThreadRestartTimer.touchc                 C   s.   | j du r| ƒ | _ | j  ¡  dS | j  ¡  dS )z2Set the timer to restart the threads after a fork.N)r(   r   r2   r7   r   r   r   Úset   s   
zThreadRestartTimer.setr   )r   r   r   Úintr,   r(   r   ÚOptionalr   r.   r+   r-   Úclassmethodr3   r9   r:   r%   r   r   r!   r   r'   O   s   
 


r'   c                  C   sr   da t ¡ D ]} t| tƒr| jsqt d| j¡ |  	¡  qt 
¡  t ¡ D ]}t d|jj¡ |ƒ  q%t 
¡  d S )NFz(Restarting thread %s after fork in childz&Starting thread %s after fork in child)r   r&   r/   Ú
isinstancer   r$   r0   r1   r*   r2   r3   r   r   )r4   r5   r   r   r   Ú_after_fork_child‹   s   
r?   r   c                   C   s   da tstrt ¡  d S d S )NF)r   r&   r   r'   r:   r   r   r   r   Ú_after_fork_parent    s   ÿr@   c                  C   s‚   t  ¡  t
 daW d   ƒ n1 sw   Y  t t ¡ ¡ tD ]} t 	d| j
¡ |  ¡  q!tD ]} t 	d| j
¡ |  ¡  q1d S )NTzStopping thread %s before forkzJoining thread %s before fork)r'   r9   r   r   r&   Úupdater   Úvaluesr0   r1   r*   Ú_before_forkÚjoin)r4   r   r   r   rC   ª   s   ÿ

þrC   r   )%Útimer   Útypingr   Úddtrace.internalr   Úddtrace.internal._threadsr   Ú_PeriodicThreadr   Úddtrace.internal.loggerr   r   r0   Ú_threadr   r   ÚImportErrorÚ	threadingr	   Ú__all__r   r   ÚProtocolr
   r   Úlistr   r:   r&   r'   Úregisterr?   Úregister_after_parentr@   Úregister_before_forkrC   r   r   r   r   Ú<module>   s>    ÿÿ
<
	