
    tf0                         d Z ddlmZ ddlmZ ddlmZ ddlmZ  e	            Z
 ee           G d d                      Z ee           G d	 d
                      ZdS )zE
Implementation of an L{IWorker} based on native threads and queues.
    )Callable)implementer   )Quit)IExclusiveWorkerc                   >    e Zd ZdZd Zdeg df         ddfdZ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.
    c                 \    | _         t                      | _        fd} ||           dS )a  
        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.
        @type startThread: callable taking a 0-argument callable and returning
            nothing.

        @param queue: A L{Queue} to use to give tasks to the thread created by
            C{startThread}.
        @type queue: L{Queue}
        c                  V    t          j        t                    D ]}  |              d S )N)iterget_stop)taskqueues    ]/var/www/surfInsights/venv3-11/lib/python3.11/site-packages/twisted/_threads/_threadworker.pyworkz#ThreadWorker.__init__.<locals>.work/   s5    UY..       N)_qr   _hasQuit)selfstartThreadr   r   s     ` r   __init__zThreadWorker.__init__   sH     	 	 	 	 	 	Dr   r   Nreturnc                 l    | 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)r   checkr   put)r   r   s     r   dozThreadWorker.do5   s2     	Dr   c                 v    | j                                          | j                            t                     dS )zT
        Reject all future work and stop the thread started by C{__init__}.
        N)r   setr   r   r   r   s    r   quitzThreadWorker.quit>   s2     	Er   __name__
__module____qualname____doc__r   r   r   r!    r   r   r	   r	      sg           .xD) d        r   r	   c                   >    e Zd ZdZd Zdeg df         ddfdZd ZdS )
LockWorkerzE
    An L{IWorker} implemented based on a mutual-exclusion lock.
    c                 H    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   locklocals      r   r   zLockWorker.__init__N   s!     VV

r   r   Nr   c                    | j         }| j        }| j                                         t	          |dd          }|g x}|_        |                    |           |                                 	 |r |                    d                       ||	                                 d|_        dS # |	                                 d|_        w xY 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.
        workingNr   )
r,   r-   r+   r   getattrr1   appendacquirepoprelease)r   r   r.   r/   r1   s        r   r   zLockWorker.do[   s     z
%D11?&((GemNN4   LLNNN% %"GKKNN$$$  %  $  $$$$$NN4     s   .!B, ,C	c                 F    | j                                          d| _        dS )z*
        Quit this L{LockWorker}.
        N)r+   r   r,   r    s    r   r!   zLockWorker.quitt   s!     	



r   r"   r'   r   r   r)   r)   H   sg           !xD) !d ! ! ! !2    r   r)   N)r&   typingr   zope.interfacer   _conveniencer   	_ithreadsr   objectr   r	   r)   r'   r   r   <module>r=      s   
 
       & & & & & &       ' ' ' ' ' ' 0 0 0 0 0 0 0 0f 0 0 0 0 0 0 0 0 0 0r   