o
    Òó·iZ  ã                   @  sø   U d Z ddlmZ ddlmZmZ ddlmZmZm	Z	m
Z
mZmZ er'ddlZddlmZ ddlmZ dd	lmZ G d
d„ deƒZejZedƒZedƒZG dd„ dee ƒZded< eZeeƒG dd„ dƒƒZG dd„ deƒZeeƒG dd„ dƒƒZdS )zE
Implementation of an L{IWorker} based on native threads and queues.
é    )Úannotations)ÚEnumÚauto)ÚTYPE_CHECKINGÚCallableÚIteratorÚLiteralÚProtocolÚTypeVarN)Úimplementeré   )ÚQuit)ÚIExclusiveWorkerc                   @  s   e Zd Zeƒ ZdS )ÚStopN)Ú__name__Ú
__module__Ú__qualname__r   ÚThread© r   r   údC:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-packages\twisted/_threads/_threadworker.pyr      s    
r   ÚTÚUc                   @  s    e Zd Zd
dd„Zddd„Zd	S )ÚSimpleQueueÚitemr   ÚreturnÚNonec                 C  ó   d S ©Nr   )Úselfr   r   r   r   Úput!   ó   zSimpleQueue.putc                 C  r   r   r   ©r   r   r   r   Úget$   r    zSimpleQueue.getN)r   r   r   r   )r   r   )r   r   r   r   r"   r   r   r   r   r       ó    
r   z/Callable[[Callable[[], T | U], U], Iterator[T]]Ú	smartiterc                   @  ó.   e Zd ZdZddd„Zddd„Zddd„ZdS )ÚThreadWorkerzþ
    An L{IExclusiveWorker} implemented based on a single thread and a queue.

    This worker ensures exclusivity (i.e. it is an L{IExclusiveWorker} and not
    an L{IWorker}) by performing all of the work passed to C{do} on the I{same}
    thread.
    ÚstartThreadú(Callable[[Callable[[], object]], object]Úqueueú8SimpleQueue[Callable[[], object] | Literal[Stop.Thread]]c                   s(   ˆ | _ tƒ | _d‡ fdd„}||ƒ dS )aX  
        Create a L{ThreadWorker} with a function to start a thread and a queue
        to use to communicate with that thread.

        @param startThread: a callable that takes a callable to run in another
            thread.

        @param queue: A L{Queue} to use to give tasks to the thread created by
            C{startThread}.
        r   r   c                    s   t ˆ jtƒD ]} | ƒ  qd S r   )r$   r"   Ú
StopThread)Útask©r)   r   r   ÚworkI   s   ÿz#ThreadWorker.__init__.<locals>.workN©r   r   )Ú_qr   Ú_hasQuit)r   r'   r)   r.   r   r-   r   Ú__init__7   s   zThreadWorker.__init__r,   úCallable[[], None]r   r   c                 C  s   | j  ¡  | j |¡ dS )z�
        Perform the given task on the thread owned by this L{ThreadWorker}.

        @param task: the function to call on a thread.
        N)r1   Úcheckr0   r   )r   r,   r   r   r   ÚdoO   ó   
zThreadWorker.doc                 C  s   | j  ¡  | j t¡ dS )zT
        Reject all future work and stop the thread started by C{__init__}.
        N)r1   Úsetr0   r   r+   r!   r   r   r   ÚquitX   r6   zThreadWorker.quitN)r'   r(   r)   r*   )r,   r3   r   r   r/   ©r   r   r   Ú__doc__r2   r5   r8   r   r   r   r   r&   -   s
    

	r&   c                   @  s    e Zd Zd	dd„Zd
dd„ZdS )Ú
SimpleLockr   Úboolc                 C  r   r   r   r!   r   r   r   Úacquirec   r    zSimpleLock.acquirer   c                 C  r   r   r   r!   r   r   r   Úreleasef   r    zSimpleLock.releaseN)r   r<   r/   )r   r   r   r=   r>   r   r   r   r   r;   b   r#   r;   c                   @  r%   )Ú
LockWorkerzE
    An L{IWorker} implemented based on a mutual-exclusion lock.
    Úlockr;   Úlocalúthreading.localc                 C  s   t ƒ | _|| _|| _dS )zß
        @param lock: A mutual-exclusion lock, with C{acquire} and C{release}
            methods.
        @type lock: L{threading.Lock}

        @param local: Local storage.
        @type local: L{threading.local}
        N)r   Ú_quitÚ_lockÚ_local)r   r@   rA   r   r   r   r2   p   s   	
zLockWorker.__init__r.   r3   r   r   c                 C  s²   | j }| j}| j ¡  t|ddƒ}|du rR|dusJ dƒ‚g  }|_| |¡ | ¡  z|r@| d¡ƒ  |s.W | 	¡  d|_dS W | 	¡  d|_dS | 	¡  d|_w | |¡ dS )zï
        Do the given work on this thread, with the mutex acquired.  If this is
        called re-entrantly, return and wait for the outer invocation to do the
        work.

        @param work: the work to do with the lock held.
        ÚworkingNzLockWorker used after quit()r   )
rD   rE   rC   r4   ÚgetattrrF   Úappendr=   Úpopr>   )r   r.   r@   rA   rF   r   r   r   r5   }   s*   


ÿ
ü
ÿzLockWorker.doc                 C  s   | j  ¡  d| _dS )z*
        Quit this L{LockWorker}.
        N)rC   r7   rD   r!   r   r   r   r8   —   s   

zLockWorker.quitN)r@   r;   rA   rB   )r.   r3   r   r   r/   r9   r   r   r   r   r?   j   s
    

r?   ) r:   Ú
__future__r   Úenumr   r   Útypingr   r   r   r   r	   r
   Ú	threadingZzope.interfacer   Z_conveniencer   Z	_ithreadsr   r   r   r+   r   r   r   Ú__annotations__Úiterr$   r&   r;   r?   r   r   r   r   Ú<module>   s*    	4