
    Ϫf0                         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y)zE
Implementation of an L{IWorker} based on native threads and queues.
    )Callable)implementer   )Quit)IExclusiveWorkerc                   6    e Zd ZdZd Zdeg df   ddfdZ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                 L    | _         t               | _        fd} ||       y)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                  R    t        j                  t              D ]	  }  |          y )N)iterget_stop)taskqueues    @/usr/lib/python3/dist-packages/twisted/_threads/_threadworker.pyworkz#ThreadWorker.__init__.<locals>.work/   s"    UYY.     N)_qr   _hasQuit)selfstartThreadr   r   s     ` r   __init__zThreadWorker.__init__   s$     	 	Dr   r   Nreturnc                 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)r   checkr   put)r   r   s     r   dozThreadWorker.do5   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)r   setr   r   r   r   s    r   quitzThreadWorker.quit>   s$     	Er   __name__
__module____qualname____doc__r   r   r   r!    r   r   r	   r	      s-    .xD) d r   r	   c                   6    e Zd ZdZd Zdeg df   ddfdZ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   r   zLockWorker.__init__N   s     V

r   r   Nr   c                    | j                   }| j                  }| j                  j                          t	        |d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.
        workingNr   )
r,   r-   r+   r   getattrr1   appendacquirepoprelease)r   r   r.   r/   r1   s        r   r   zLockWorker.do[   s     zz

%D1?&((GemNN4 LLN%"GKKN$   $NN4   $s   -B2 2Cc                 F    | j                   j                          d| _        y)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   s-    !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f 0 0 0r   