o
    svXjñ2  ã                   @   sŠ  d dl mZ 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mZ g d	¢Zeƒ  eed
ƒƒ\ZZejjZzejjZW n eyn   zd dlmZ W n eyk   dZY nw Y nw 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eƒZG dd„ deƒZ G dd„ deƒZ!e"edƒr»G dd„ dej#ƒZ#dS G dd„ deƒZ#dS )é    )Úabsolute_importNé   )Úcontext)Úprocess)Úutil)Ú	_billiardÚensure_SemLock)ÚrangeÚ	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   úQ/var/www/html/myproject/venv/lib/python3.10/site-packages/billiard/synchronize.pyÚ_semname5   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   r	   r   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)r2   r>   r   )r   r>   r   r   r   r4   i   s   zSemLock._cleanupc                 C   ó   | j j| _| j j| _d S r   )r   ÚacquireÚrelease©r5   r   r   r   r0   o   ó   
zSemLock._make_methodsc                 C   ó
   | j  ¡ S r   )r   Ú	__enter__rB   r   r   r   rE   s   ó   
zSemLock.__enter__c                 G   ó   | j j|Ž S r   )r   Ú__exit__©r5   Úargsr   r   r   rH   v   ó   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/   r6   r8   r   r   )r5   r   ÚhÚstater   r   r   Ú__getstate__y   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.   r0   ©r5   rP   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Ú_RandomNameSequencerY   r=   Ústaticmethodr4   r0   rE   rH   rQ   rT   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©r9   )r   r=   Ú	SEMAPHOREÚSEM_VALUE_MAX©r5   r7   r9   r   r   r   r=   ”   ó   zSemaphore.__init__c                 C   rD   r   )r   Ú
_get_valuerB   r   r   r   Ú	get_value—   rF   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   rf   Ú	ExceptionÚ	__class__rZ   ©r5   r7   r   r   r   Ú__repr__š   s   ÿzSemaphore.__repr__©r   N)rZ   r[   r\   r=   rg   rl   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=   rb   rd   r   r   r   r=   ¤   re   zBoundedSemaphore.__init__c                 C   s<   z| j  ¡ }W n ty   d}Y nw d| jj|| j jf S )Nrh   z<%s(value=%s, maxvalue=%s)>)r   rf   ri   rj   rZ   r8   rk   r   r   r   rl   §   s   ÿÿzBoundedSemaphore.__repr__rm   )rZ   r[   r\   r=   rl   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   ra   rn   ©r5   r9   r   r   r   r=   µ   re   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ÚSomeOtherProcessrh   z<%s(owner=%s)>)r   Ú_is_miner   rV   r   Ú	threadingÚcurrent_threadrf   Ú_countri   rj   rZ   )r5   r   r   r   r   rl   ¸   s    

€€ÿzLock.__repr__r   ©rZ   r[   r\   Ú__doc__r=   rl   r   r   r   r   r   °   ó    
r   c                   @   ro   )r   z
    Recursive lock
    Nc                 C   rp   rq   )r   r=   ÚRECURSIVE_MUTEXrr   r   r   r   r=   Î   re   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 )
Nrs   rt   r   )ru   r   r   )rv   Únonzero)rw   r€   )rh   rh   ú<%s(%s, %s)>)r   rx   r   rV   r   ry   rz   r{   rf   ri   rj   rZ   )r5   r   Úcountr   r   r   rl   Ñ   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_semaphorer0   )r5   Úlockr9   r   r   r   r=   è   s   zCondition.__init__c                 C   s   t  | ¡ | j| j| j| jfS r   )r   rL   r„   r…   r†   r‡   rB   r   r   r   rQ   ð   s   
ÿzCondition.__getstate__c                 C   s    |\| _ | _| _| _|  ¡  d S r   )r„   r…   r†   r‡   r0   rS   r   r   r   rT   õ   s   
ÿzCondition.__setstate__c                 C   rD   r   )r„   rE   rB   r   r   r   rE   ú   rF   zCondition.__enter__c                 G   rG   r   )r„   rH   rI   r   r   r   rH   ý   rK   zCondition.__exit__c                 C   r?   r   )r„   r@   rA   rB   r   r   r   r0      rC   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 )Nrh   r�   )r…   r   rf   r†   ri   rj   rZ   r„   )r5   Únum_waitersr   r   r   rl     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   rx   r…   rA   r{   r	   r‡   r@   r†   )r5   Ú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   rx   r‡   r@   r†   r…   rA   )r5   Ú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   rx   r‡   r@   r†   r…   rA   r	   )r5   r�   Úsleepersr;   r   r   r   Ú
notify_all5  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‹   )r5   Ú	predicaterŠ   ÚresultÚendtimeÚwaittimer   r   r   Úwait_forL  s"   

þúzCondition.wait_for)NNr   )rZ   r[   r\   r}   r=   rQ   rT   rE   rH   r0   rl   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   Ú_flagrr   r   r   r   r=   a  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@   rA   rB   r   r   r   Úis_setf  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@   rA   r�   rB   r   r   r   Úsetm  s
   
"ýz	Event.setc                 C   s8   | j � | j d¡ W d   ƒ d S 1 sw   Y  d S rš   )r–   r—   r@   rB   r   r   r   Úclears  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@   rA   r‹   )r5   rŠ   r   r   r   r‹   w  s   
ø	$÷z
Event.waitr   )rZ   r[   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   rT   Ú_stater{   )	r5   ÚpartiesÚactionrŠ   r9   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Ú_arrayrS   r   r   r   rT   –  s   ÿzBarrier.__setstate__c                 C   s   | j | j| j| j| jfS r   )r¨   r©   rª   r–   r«   rB   r   r   r   rQ   ›  s   ÿzBarrier.__getstate__c                 C   ó
   | j d S rƒ   ©r®   rB   r   r   r   r¢   Ÿ  ó   
zBarrier._statec                 C   ó   || j d< d S rƒ   r°   rk   r   r   r   r¢   £  ó   c                 C   r¯   ©Nr   r°   rB   r   r   r   r{   §  r±   zBarrier._countc                 C   r²   r´   r°   rk   r   r   r   r{   «  r³   )NNN)
rZ   r[   r\   r=   rT   rQ   Úpropertyr¢   Úsetterr{   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)r5   rJ   Úkwargsr   r   r   r=   ´  s   r§   N)rZ   r[   r\   r=   r   r   r   r   r�   ²  s    )$Ú
__future__r   r   r'   r]   ry   Ú r   r   r   Ú_extr   r   Úfiver	   r
   Ú__all__Úlistr   rb   r   rc   r   r   Ú_multiprocessingÚImportErrorr   Úobjectr   r   r   r   r   r   Úhasattrr�   r   r   r   r   Ú<module>   sF   ÿ€üV|
)(