o
    éT•jZ-  ã                   @   s  g d ¢Z ddl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	 z
ddlm
Z
mZ W n ey?   edƒ‚w eed	ƒƒ\ZZej
j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G dd„ dejƒZdS ))ÚLockÚRLockÚ	SemaphoreÚBoundedSemaphoreÚ	ConditionÚEventé    Né   )Úcontext)Úprocess)Úutil)ÚSemLockÚ
sem_unlinkz—This platform lacks a functioning sem_open implementation, therefore, the required synchronization primitives needed will not function, see issue 3770.é   c                   @   s\   e Zd Ze ¡ Z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 )r   c             	   C   sö   |d u r	t j ¡ }| ¡ }tjdkp|dk}tdƒD ]}zt ||||  	¡ |¡ }| _
W n	 ty5   Y qw  ntdƒ‚t d|j ¡ |  ¡  tjdkrWdd„ }	t | |	¡ | j
jd urydd	lm}
 |
| j
jd
ƒ tj| tj| j
jfdd� d S d S )NÚwin32Úforkéd   zcannot find name for semaphorezcreated semlock with handle %sc                 S   s   | j  ¡  d S ©N)Ú_semlockÚ_after_fork)Úobj© r   ú2/usr/lib/python3.10/multiprocessing/synchronize.pyr   G   s   z%SemLock.__init__.<locals>._after_forkr   )ÚregisterÚ	semaphorer   )Úexitpriority)r	   Ú_default_contextÚget_contextÚget_start_methodÚsysÚplatformÚrangeÚ_multiprocessingr   Ú
_make_namer   ÚFileExistsErrorr   ÚdebugÚhandleÚ_make_methodsÚregister_after_forkÚnameÚresource_trackerr   ÚFinalizeÚ_cleanup)ÚselfÚkindÚvalueÚmaxvalueÚctxr(   Ú
unlink_nowÚiÚslr   r   r   r   r   Ú__init__2   s6   
þÿ

ÿúzSemLock.__init__c                 C   s"   ddl m} t| ƒ || dƒ d S )Nr   )Ú
unregisterr   )r)   r5   r   )r(   r5   r   r   r   r+   T   s   zSemLock._cleanupc                 C   ó   | j j| _| j j| _d S r   )r   ÚacquireÚrelease©r,   r   r   r   r&   Z   ó   
zSemLock._make_methodsc                 C   ó
   | j  ¡ S r   )r   Ú	__enter__r9   r   r   r   r<   ^   ó   
zSemLock.__enter__c                 G   ó   | j j|Ž S r   )r   Ú__exit__©r,   Úargsr   r   r   r?   a   ó   zSemLock.__exit__c                 C   sD   t  | ¡ | j}tjdkrt  ¡  |j¡}n|j}||j|j	|j
fS )Nr   )r	   Úassert_spawningr   r   r   Úget_spawning_popenÚduplicate_for_childr%   r-   r/   r(   )r,   r3   Úhr   r   r   Ú__getstate__d   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&   ©r,   Ústater   r   r   Ú__setstate__m   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"   r   s   ÿzSemLock._make_nameN)Ú__name__Ú
__module__Ú__qualname__ÚtempfileÚ_RandomNameSequencerP   r4   Ústaticmethodr+   r&   r<   r?   rG   rK   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   c                C   s   t j| t|t|d� d S ©N©r0   )r   r4   Ú	SEMAPHOREÚSEM_VALUE_MAX©r,   r.   r0   r   r   r   r4   }   ó   zSemaphore.__init__c                 C   r;   r   )r   Ú
_get_valuer9   r   r   r   Ú	get_value€   r=   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   r]   Ú	ExceptionÚ	__class__rQ   ©r,   r.   r   r   r   Ú__repr__ƒ   s   ÿzSemaphore.__repr__N©r   )rQ   rR   rS   r4   r^   rc   r   r   r   r   r   {   s    
r   c                   @   s   e Zd Zddd„Zdd„ ZdS )r   r   c                C   s   t j| t|||d� d S rW   ©r   r4   rY   r[   r   r   r   r4   �   r\   zBoundedSemaphore.__init__c                 C   s<   z| j  ¡ }W n ty   d}Y nw d| jj|| j jf S )Nr_   z<%s(value=%s, maxvalue=%s)>)r   r]   r`   ra   rQ   r/   rb   r   r   r   rc   “   s   ÿÿzBoundedSemaphore.__repr__Nrd   ©rQ   rR   rS   r4   rc   r   r   r   r   r   Ž   s    
r   c                   @   ó   e Zd Zdd„ Zdd„ ZdS )r   c                C   ó   t j| tdd|d� d S ©Nr   rX   re   ©r,   r0   r   r   r   r4   ¡   r\   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ÚSomeOtherProcessr_   z<%s(owner=%s)>)r   Ú_is_miner
   rM   r(   Ú	threadingÚcurrent_threadr]   Ú_countr`   ra   rQ   )r,   r(   r   r   r   rc   ¤   s    

€€ÿzLock.__repr__Nrf   r   r   r   r   r   Ÿ   ó    r   c                   @   rg   )r   c                C   rh   ri   )r   r4   ÚRECURSIVE_MUTEXrj   r   r   r   r4   º   r\   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 )
Nrk   rl   r   )rm   r   r   )rn   Únonzero)ro   rv   )r_   r_   ú<%s(%s, %s)>)r   rp   r
   rM   r(   rq   rr   rs   r]   r`   ra   rQ   )r,   r(   Úcountr   r   r   rc   ½   s    



€ÿzRLock.__repr__Nrf   r   r   r   r   r   ¸   rt   r   c                   @   sl   e 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d„Zdd„ Zddd„ZdS )r   Nc                C   s>   |p|  ¡ | _| d¡| _| d¡| _| d¡| _|  ¡  d S ©Nr   )r   Ú_lockr   Ú_sleeping_countÚ_woken_countÚ_wait_semaphorer&   )r,   Úlockr0   r   r   r   r4   Ô   s
   zCondition.__init__c                 C   s   t  | ¡ | j| j| j| jfS r   )r	   rC   rz   r{   r|   r}   r9   r   r   r   rG   Û   s   
ÿzCondition.__getstate__c                 C   s    |\| _ | _| _| _|  ¡  d S r   )rz   r{   r|   r}   r&   rI   r   r   r   rK   à   s   
ÿzCondition.__setstate__c                 C   r;   r   )rz   r<   r9   r   r   r   r<   å   r=   zCondition.__enter__c                 G   r>   r   )rz   r?   r@   r   r   r   r?   è   rB   zCondition.__exit__c                 C   r6   r   )rz   r7   r8   r9   r   r   r   r&   ë   r:   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 )Nr_   rw   )r{   r   r]   r|   r`   ra   rQ   rz   )r,   Únum_waitersr   r   r   rc   ï   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)
rz   r   rp   r{   r8   rs   r    r}   r7   r|   )r,   Útimeoutrx   r2   r   r   r   Úwait÷   s"   ÿ

ÿ
ýÿzCondition.waitr   c                 C   sØ   | j j ¡ s
J dƒ‚| j d¡rJ dƒ‚| j d¡r,| j d¡}|s&J dƒ‚| j d¡sd}||k rK| j d¡rK| j ¡  |d7 }||k rK| j d¡s8|rht|ƒD ]}| j ¡  qQ| j d¡rj	 | j d¡s_d S d S d S )Nzlock is not ownedFz<notify: Should not have been able to acquire _wait_semaphorez>notify: Bug in sleeping_count.acquire- res should not be Falser   r   )	rz   r   rp   r}   r7   r|   r{   r8   r    )r,   ÚnÚresÚsleepersr2   r   r   r   Únotify  s.   ÿÿþ
þÿûzCondition.notifyc                 C   s   | j tjd� d S )N)r‚   )r…   r   Úmaxsizer9   r   r   r   Ú
notify_all(  s   zCondition.notify_allc                 C   sj   |ƒ }|r|S |d urt  ¡ | }nd }d }|s3|d ur)|t  ¡  }|dkr)	 |S |  |¡ |ƒ }|r|S ry   )ÚtimeÚ	monotonicr�   )r,   Ú	predicater€   ÚresultÚendtimeÚwaittimer   r   r   Úwait_for+  s"   
þúzCondition.wait_forr   rd   )rQ   rR   rS   r4   rG   rK   r<   r?   r&   rc   r�   r…   r‡   rŽ   r   r   r   r   r   Ò   s    


r   c                   @   s6   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zdd
d„Zd	S )r   c                C   s    |  | ¡ ¡| _| d¡| _d S ry   )r   r   Ú_condr   Ú_flagrj   r   r   r   r4   C  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�   r7   r8   r9   r   r   r   Úis_setG  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�   r7   r8   r‡   r9   r   r   r   ÚsetN  s
   
"ýz	Event.setc                 C   s8   | j � | j d¡ W d   ƒ d S 1 sw   Y  d S r“   )r�   r�   r7   r9   r   r   r   ÚclearT  s   "ÿzEvent.clearNc                 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�   r7   r8   r�   )r,   r€   r   r   r   r�   X  s   
ø	$÷z
Event.waitr   )rQ   rR   rS   r4   r’   r”   r•   r�   r   r   r   r   r   A  s    r   c                   @   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 )ÚBarrierNc          	      C   sR   dd l }ddlm} || d¡d ƒ}| ¡ }|  |||||f¡ d| _d| _d S )Nr   r   )ÚBufferWrapperr2   r   )ÚstructÚheapr—   Úcalcsizer   rK   Ú_staters   )	r,   ÚpartiesÚactionr€   r0   r˜   r—   ÚwrapperÚcondr   r   r   r4   j  s   
zBarrier.__init__c                 C   s.   |\| _ | _| _| _| _| j ¡  d¡| _d S )Nr2   )Ú_partiesÚ_actionÚ_timeoutr�   Ú_wrapperÚcreate_memoryviewÚcastÚ_arrayrI   r   r   r   rK   s  s   ÿzBarrier.__setstate__c                 C   s   | j | j| j| j| jfS r   )r    r¡   r¢   r�   r£   r9   r   r   r   rG   x  s   ÿzBarrier.__getstate__c                 C   ó
   | j d S ry   ©r¦   r9   r   r   r   r›   |  ó   
zBarrier._statec                 C   ó   || j d< d S ry   r¨   rb   r   r   r   r›   €  ó   c                 C   r§   ©Nr   r¨   r9   r   r   r   rs   „  r©   zBarrier._countc                 C   rª   r¬   r¨   rb   r   r   r   rs   ˆ  r«   )NN)
rQ   rR   rS   r4   rK   rG   Úpropertyr›   Úsetterrs   r   r   r   r   r–   h  s    
	


r–   )Ú__all__rq   r   rT   r!   rˆ   Ú r	   r
   r   r   r   ÚImportErrorÚlistr    ru   rY   rZ   Úobjectr   r   r   r   r   r   r–   r   r   r   r   Ú<module>   s0   	ÿ
Mo'