
    VhZ                    X   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e      Zej*                  Z ed      Z ed      Z G d dee         Zded<   eZ ee       G d d             Z G d de      Z ee       G d d             Zy)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                      e Zd Z e       Zy)StopN)__name__
__module____qualname__r   Thread     N/home/dcms/DCMS/lib/python3.12/site-packages/twisted/_threads/_threadworker.pyr   r      s	    VFr   r   TUc                      e Zd ZddZddZy)SimpleQueuec                     y Nr   )selfitems     r   putzSimpleQueue.put!       r   c                     y r   r   r   s    r   getzSimpleQueue.get$   r"   r   N)r    r   returnNone)r&   r   )r   r   r   r!   r%   r   r   r   r   r           r   r   z/Callable[[Callable[[], T | U], U], Iterator[T]]	smartiterc                  0    e Zd ZdZ	 	 	 	 ddZddZddZy)	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.
    c                N    | _         t               | _        dfd} ||       y)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}.
        c                 R    t        j                  t              D ]	  }  |          y r   )r)   r%   
StopThread)taskqueues    r   workz#ThreadWorker.__init__.<locals>.workI   s"    !%))Z8 r   Nr&   r'   )_qr   _hasQuit)r   startThreadr0   r1   s     ` r   __init__zThreadWorker.__init__7   s$     	 	Dr   c                n    | j                   j                          | j                  j                  |       y)z
        Perform the given task on the thread owned by this L{ThreadWorker}.

        @param task: the function to call on a thread.
        N)r4   checkr3   r!   )r   r/   s     r   dozThreadWorker.doO   s$     	Dr   c                v    | j                   j                          | j                  j                  t               y)zT
        Reject all future work and stop the thread started by C{__init__}.
        N)r4   setr3   r!   r.   r$   s    r   quitzThreadWorker.quitX   s$     	Jr   N)r5   z(Callable[[Callable[[], object]], object]r0   z8SimpleQueue[Callable[[], object] | Literal[Stop.Thread]])r/   Callable[[], None]r&   r'   r2   r   r   r   __doc__r6   r9   r<   r   r   r   r+   r+   -   s)    = H0 r   r+   c                      e Zd ZddZddZy)
SimpleLockc                     y r   r   r$   s    r   acquirezSimpleLock.acquirec   r"   r   c                     y r   r   r$   s    r   releasezSimpleLock.releasef   r"   r   N)r&   boolr2   )r   r   r   rC   rE   r   r   r   rA   rA   b   r(   r   rA   c                  (    e Zd ZdZddZddZddZy)	
LockWorkerzE
    An L{IWorker} implemented based on a mutual-exclusion lock.
    c                >    t               | _        || _        || _        y)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   locklocals      r   r6   zLockWorker.__init__p   s     V
(,
r   c                   | j                   }| j                  }| j                  j                          t	        |dd      }|g|J d       g x}|_        |j                  |       |j                          	 |r |j                  d              |r|j                          d|_        y|j                  |       y# |j                          d|_        w xY w)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   )
rK   rL   rJ   r8   getattrrP   appendrC   poprE   )r   r1   rM   rN   rP   s        r   r9   zLockWorker.do}   s     zz

%D1?#C%CC#&((GemNN4 LLN%"GKKN$   $NN4   $s   6B; ;Cc                F    | j                   j                          d| _        y)z*
        Quit this L{LockWorker}.
        N)rJ   r;   rK   r$   s    r   r<   zLockWorker.quit   s     	


r   N)rM   rA   rN   zthreading.local)r1   r=   r&   r'   r2   r>   r   r   r   rH   rH   j   s    !4r   rH   ) 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+   rA   rH   r   r   r   <module>r^      s   
 #  P P &  '4  [[
CLCL(1+  ; :	 1  1  1 h  1 1 1r   