o
    €¨Êh¢2  ã                   @   sr  d dl Z d dlZd dl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	m
Z
 d dlmZ g d¢Ze
ƒ  eed	ƒƒ\ZZe	jjZze	jjZW n eyf   zd d
lmZ W n eyc   dZY nw Y nw dd„ ZG dd„ dƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ dƒZG dd„ dƒZeedƒr°G dd„ dej ƒZ dS G dd„ dƒZ dS )é    Né   )Úcontext)Úprocess)Úutil)Ú	_billiardÚensure_SemLock)Ú	monotonic)ÚLockÚRLockÚ	SemaphoreÚBoundedSemaphoreÚ	ConditionÚEventé   )Ú
sem_unlinkc                 C   s   z| j W S  ty   Y d S w ©N)ÚnameÚAttributeError)Úsl© r   úF/var/www/html/env/lib/python3.10/site-packages/billiard/synchronize.pyÚ_semname4   s
   ÿr   c                   @   s^   e Zd Ze ¡ Zddd„Zedd„ ƒZdd„ Z	dd	„ Z
d
d„ Zdd„ Zdd„ Zedd„ ƒZdS )ÚSemLockNc                 C   sL  |d u r	t j ¡ }| ¡ }tjdkp|dk}trZtdƒD ]2}zt 	||||  
¡ |¡ }| _W n ttfyL }	 zt|	dd ƒtjkrB‚ W Y d }	~	qd }	~	ww  n
tdƒ}	tj|	_|	‚n
t 	|||¡ }| _t d|j¡ |  ¡  tr¢tjdkr€dd„ }
t | |
¡ t| jƒd ur¤d	d
lm} || jjƒ tj| t	j| jjfdd� d S d S d S )NÚwin32Úforkéd   Úerrnozcannot find file for semaphorezcreated semlock with handle %sc                 S   s   | j  ¡  d S r   )Ú_semlockÚ_after_fork)Úobjr   r   r   r   [   s   z%SemLock.__init__.<locals>._after_forkr   )Úregisterr   )Úexitpriority)r   Ú_default_contextÚget_contextÚget_start_methodÚsysÚplatformr   Úranger   r   Ú
_make_namer   ÚOSErrorÚIOErrorÚgetattrr   ÚEEXISTr   ÚdebugÚhandleÚ_make_methodsÚregister_after_forkr   Úsemaphore_trackerr    r   ÚFinalizeÚ_cleanup)ÚselfÚkindÚvalueÚmaxvalueÚctxr   Ú
unlink_nowÚir   Úexcr   r    r   r   r   Ú__init__>   sH   
ÿÿ€ÿü

ÿózSemLock.__init__c                 C   s    ddl m} t| ƒ || ƒ d S )Nr   )Ú
unregister)r1   r=   r   )r   r=   r   r   r   r3   h   s   zSemLock._cleanupc                 C   ó   | j j| _| j j| _d S r   )r   ÚacquireÚrelease©r4   r   r   r   r/   n   ó   
zSemLock._make_methodsc                 C   ó
   | j  ¡ S r   )r   Ú	__enter__rA   r   r   r   rD   r   ó   
zSemLock.__enter__c                 G   ó   | j j|Ž S r   )r   Ú__exit__©r4   Úargsr   r   r   rG   u   ó   zSemLock.__exit__c                 C   sh   t  | ¡ | j}tjdkrt  ¡  |j¡}n|j}||j|j	f}z	||j
f7 }W |S  ty3   Y |S w )Nr   )r   Úassert_spawningr   r%   r&   Úget_spawning_popenÚduplicate_for_childr.   r5   r7   r   r   )r4   r   ÚhÚstater   r   r   Ú__getstate__x   s   

þþzSemLock.__getstate__c                 C   s*   t jj|Ž | _t d|d ¡ |  ¡  d S )Nz recreated blocker with handle %rr   )r   r   Ú_rebuildr   r   r-   r/   ©r4   rO   r   r   r   Ú__setstate__†   s   zSemLock.__setstate__c                   C   s   dt  ¡ jd ttjƒf S )Nz%s-%sÚ	semprefix)r   Úcurrent_processÚ_configÚnextr   Ú_randr   r   r   r   r(   ‹   s   ÿzSemLock._make_namer   )Ú__name__Ú
__module__Ú__qualname__ÚtempfileÚ_RandomNameSequencerX   r<   Ústaticmethodr3   r/   rD   rG   rP   rS   r(   r   r   r   r   r   ;   s    
*
r   c                   @   s&   e Zd Zd	dd„Zdd„ Zdd„ ZdS )
r   r   Nc                 C   s   t j| t|t|d� d S ©N©r8   )r   r<   Ú	SEMAPHOREÚSEM_VALUE_MAX©r4   r6   r8   r   r   r   r<   “   ó   zSemaphore.__init__c                 C   rC   r   )r   Ú
_get_valuerA   r   r   r   Ú	get_value–   rE   zSemaphore.get_valuec                 C   s6   z| j  ¡ }W n ty   d}Y nw d| jj|f S )NÚunknownz<%s(value=%s)>)r   re   Ú	ExceptionÚ	__class__rY   ©r4   r6   r   r   r   Ú__repr__™   s   ÿzSemaphore.__repr__©r   N)rY   rZ   r[   r<   rf   rk   r   r   r   r   r   ‘   s    
r   c                   @   s   e Zd Zddd„Zdd„ ZdS )r   r   Nc                 C   s   t j| t|||d� d S r_   ©r   r<   ra   rc   r   r   r   r<   £   rd   zBoundedSemaphore.__init__c                 C   s<   z| j  ¡ }W n ty   d}Y nw d| jj|| j jf S )Nrg   z<%s(value=%s, maxvalue=%s)>)r   re   rh   ri   rY   r7   rj   r   r   r   rk   ¦   s   ÿÿzBoundedSemaphore.__repr__rl   )rY   rZ   r[   r<   rk   r   r   r   r   r   ¡   s    
r   c                   @   ó"   e Zd ZdZddd„Zdd„ ZdS )r	   z
    Non-recursive lock.
    Nc                 C   ó   t j| tdd|d� d S ©Nr   r`   rm   ©r4   r8   r   r   r   r<   ´   rd   zLock.__init__c                 C   sŽ   z3| j  ¡ rt ¡ j}t ¡ jdkr|dt ¡ j 7 }n| j  ¡ dkr&d}n| j  ¡ dkr0d}nd}W n t	y>   d}Y nw d	| j
j|f S )
NÚ
MainThreadú|r   ÚNoner   ÚSomeOtherThreadÚSomeOtherProcessrg   z<%s(owner=%s)>)r   Ú_is_miner   rU   r   Ú	threadingÚcurrent_threadre   Ú_countrh   ri   rY   )r4   r   r   r   r   rk   ·   s    

€€ÿzLock.__repr__r   ©rY   rZ   r[   Ú__doc__r<   rk   r   r   r   r   r	   ¯   ó    
r	   c                   @   rn   )r
   z
    Recursive lock
    Nc                 C   ro   rp   )r   r<   ÚRECURSIVE_MUTEXrq   r   r   r   r<   Í   rd   zRLock.__init__c                 C   sª   z>| j  ¡ r!t ¡ j}t ¡ jdkr|dt ¡ j 7 }| j  ¡ }n| j  ¡ dkr-d\}}n| j  ¡ dkr9d\}}nd\}}W n t	yK   d\}}Y nw d	| j
j||f S )
Nrr   rs   r   )rt   r   r   )ru   Únonzero)rv   r   )rg   rg   ú<%s(%s, %s)>)r   rw   r   rU   r   rx   ry   rz   re   rh   ri   rY   )r4   r   Úcountr   r   r   rk   Ð   s    



€ÿzRLock.__repr__r   r{   r   r   r   r   r
   È   r}   r
   c                   @   sn   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
ddd„Zdd„ Zdd„ Zddd„ZdS )r   z
    Condition variable
    Nc                 C   sF   |sJ ‚|p	|  ¡ | _| d¡| _| d¡| _| d¡| _|  ¡  d S ©Nr   )r
   Ú_lockr   Ú_sleeping_countÚ_woken_countÚ_wait_semaphorer/   )r4   Úlockr8   r   r   r   r<   ç   s   zCondition.__init__c                 C   s   t  | ¡ | j| j| j| jfS r   )r   rK   rƒ   r„   r…   r†   rA   r   r   r   rP   ï   s   
ÿzCondition.__getstate__c                 C   s    |\| _ | _| _| _|  ¡  d S r   )rƒ   r„   r…   r†   r/   rR   r   r   r   rS   ô   s   
ÿzCondition.__setstate__c                 C   rC   r   )rƒ   rD   rA   r   r   r   rD   ù   rE   zCondition.__enter__c                 G   rF   r   )rƒ   rG   rH   r   r   r   rG   ü   rJ   zCondition.__exit__c                 C   r>   r   )rƒ   r?   r@   rA   r   r   r   r/   ÿ   rB   zCondition._make_methodsc                 C   sH   z| j j ¡ | jj ¡  }W n ty   d}Y nw d| jj| j|f S )Nrg   r€   )r„   r   re   r…   rh   ri   rY   rƒ   )r4   Únum_waitersr   r   r   rk     s   

ÿÿÿzCondition.__repr__c              	   C   sš   | j j ¡ s
J dƒ‚| j ¡  | j j ¡ }t|ƒD ]}| j  ¡  qz| j d|¡W | j	 ¡  t|ƒD ]}| j  ¡  q2S | j	 ¡  t|ƒD ]}| j  ¡  qDw )Nz,must acquire() condition before using wait()T)
rƒ   r   rw   r„   r@   rz   r'   r†   r?   r…   )r4   Útimeoutr�   r:   r   r   r   Úwait  s"   ÿ

ÿ
ýÿzCondition.waitc                 C   s„   | j j ¡ s
J dƒ‚| j d¡rJ ‚| j d¡r(| j d¡}|s"J ‚| j d¡s| j d¡r@| j ¡  | j ¡  | j d¡ d S d S )Núlock is not ownedF)rƒ   r   rw   r†   r?   r…   r„   r@   )r4   Úresr   r   r   Únotify#  s   þ

ûzCondition.notifyc                 C   sÀ   | j j ¡ s
J dƒ‚| j d¡rJ ‚| j d¡r(| j d¡}|s"J ‚| j d¡sd}| j d¡r?| j ¡  |d7 }| j d¡s0|r\t|ƒD ]}| j ¡  qE| j d¡r^	 | j d¡sSd S d S d S )Nr‹   Fr   r   )	rƒ   r   rw   r†   r?   r…   r„   r@   r'   )r4   rŒ   Úsleepersr:   r   r   r   Ú
notify_all4  s&   þ
þÿûzCondition.notify_allc                 C   sf   |ƒ }|r|S |d urt ƒ | }nd }d }|s1|d ur'|t ƒ  }|dkr'	 |S |  |¡ |ƒ }|r|S r‚   )r   rŠ   )r4   Ú	predicater‰   ÚresultÚendtimeÚwaittimer   r   r   Úwait_forK  s"   

þúzCondition.wait_for)NNr   )rY   rZ   r[   r|   r<   rP   rS   rD   rG   r/   rk   rŠ   r�   r�   r”   r   r   r   r   r   â   s    

	r   c                   @   s8   e Zd Zddd„Zdd„ Zdd„ Zdd	„ Zdd
d„ZdS )r   Nc                 C   s(   |sJ ‚|  | ¡ ¡| _| d¡| _d S r‚   )r   r	   Ú_condr   Ú_flagrq   r   r   r   r<   `  s   zEvent.__init__c                 C   sV   | j � | j d¡r| j ¡  	 W d   ƒ dS 	 W d   ƒ dS 1 s$w   Y  d S ©NFT)r•   r–   r?   r@   rA   r   r   r   Úis_sete  s   
ý$üzEvent.is_setc                 C   sL   | j � | j d¡ | j ¡  | j  ¡  W d   ƒ d S 1 sw   Y  d S ©NF)r•   r–   r?   r@   r�   rA   r   r   r   Úsetl  s
   
"ýz	Event.setc                 C   s8   | j � | j d¡ W d   ƒ d S 1 sw   Y  d S r™   )r•   r–   r?   rA   r   r   r   Úclearr  s   "ÿzEvent.clearc                 C   sz   | j �0 | j d¡r| j ¡  n| j  |¡ | j d¡r*| j ¡  	 W d   ƒ dS 	 W d   ƒ dS 1 s6w   Y  d S r—   )r•   r–   r?   r@   rŠ   )r4   r‰   r   r   r   rŠ   v  s   
ø	$÷z
Event.waitr   )rY   rZ   r[   r<   r˜   rš   r›   rŠ   r   r   r   r   r   ^  s    
r   ÚBarrierc                   @   sZ   e Zd Zddd„Zdd„ Zdd„ Zedd	„ ƒZejd
d	„ ƒZedd„ ƒZ	e	jdd„ ƒZ	dS )rœ   Nc           	      C   sZ   |sJ ‚dd l }ddlm} || d¡d ƒ}| ¡ }|  |||||f¡ d| _d| _d S )Nr   r   )ÚBufferWrapperr:   r   )ÚstructÚheapr�   Úcalcsizer   rS   Ú_staterz   )	r4   ÚpartiesÚactionr‰   r8   rž   r�   ÚwrapperÚcondr   r   r   r<   ‹  s   
úBarrier.__init__c                 C   s.   |\| _ | _| _| _| _| j ¡  d¡| _d S )Nr:   )Ú_partiesÚ_actionÚ_timeoutr•   Ú_wrapperÚcreate_memoryviewÚcastÚ_arrayrR   r   r   r   rS   •  s   ÿzBarrier.__setstate__c                 C   s   | j | j| j| j| jfS r   )r§   r¨   r©   r•   rª   rA   r   r   r   rP   š  s   ÿzBarrier.__getstate__c                 C   ó
   | j d S r‚   ©r­   rA   r   r   r   r¡   ž  ó   
zBarrier._statec                 C   ó   || j d< d S r‚   r¯   rj   r   r   r   r¡   ¢  ó   c                 C   r®   ©Nr   r¯   rA   r   r   r   rz   ¦  r°   zBarrier._countc                 C   r±   r³   r¯   rj   r   r   r   rz   ª  r²   )NNN)
rY   rZ   r[   r<   rS   rP   Úpropertyr¡   Úsetterrz   r   r   r   r   rœ   ‰  s    




c                   @   s   e Zd Zdd„ ZdS )rœ   c                 O   s   t dƒ‚)NzBarrier only supported on Py3)ÚNotImplementedError)r4   rI   Úkwargsr   r   r   r<   ³  s   r¦   N)rY   rZ   r[   r<   r   r   r   r   rœ   ±  s    )!r   r%   r\   rx   Ú r   r   r   Ú_extr   r   Útimer   Ú__all__Úlistr'   r~   ra   r   rb   r   r   Ú_multiprocessingÚImportErrorr   r   r   r	   r
   r   r   Úhasattrrœ   r   r   r   r   Ú<module>   sD   	ÿ€üV|
)(