o
    >hZ                     @  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   ~/var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/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]queue8SimpleQueue[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
   	threadingzope.interfacer   _conveniencer   	_ithreadsr   r   r   r+   r   r   r   __annotations__iterr$   r&   r;   r?   r   r   r   r   <module>   s*    	4