o
    b                     @   s^   d Z ddlmZ ddlmZ ddlmZ e ZeeG dd dZ	eeG dd	 d	Z
d
S )zE
Implementation of an L{IWorker} based on native threads and queues.
    )implementer   )Quit)IExclusiveWorkerc                   @   (   e Zd ZdZdd Zdd Z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.
    c                    s&    | _ t | _ fd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                     s   t  jtD ]} |   qd S )N)iterget_stop)taskqueue @/usr/lib/python3/dist-packages/twisted/_threads/_threadworker.pywork-   s   z#ThreadWorker.__init__.<locals>.workN)_qr   _hasQuit)selfstartThreadr   r   r   r   r   __init__   s   zThreadWorker.__init__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)r   checkr   put)r   r   r   r   r   do3      
zThreadWorker.doc                 C   s   | j   | jt dS )zT
        Reject all future work and stop the thread started by C{__init__}.
        N)r   setr   r   r
   r   r   r   r   quit<   r   zThreadWorker.quitN__name__
__module____qualname____doc__r   r   r   r   r   r   r   r      s
    	r   c                   @   r   )	
LockWorkerzE
    An L{IWorker} implemented based on a mutual-exclusion lock.
    c                 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   locklocalr   r   r   r   L   s   	
zLockWorker.__init__c                 C   s   | j }| j}| j  t|dd}|du rJg  }|_|| |  z|r8|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.
        workingNr   )
r$   r%   r#   r   getattrr(   appendacquirepoprelease)r   r   r&   r'   r(   r   r   r   r   Y   s(   




zLockWorker.doc                 C   s   | j   d| _dS )z*
        Quit this L{LockWorker}.
        N)r#   r   r$   r   r   r   r   r   r   s   

zLockWorker.quitNr   r   r   r   r   r"   F   s
    r"   N)r!   zope.interfacer   _conveniencer   	_ithreadsr   objectr
   r   r"   r   r   r   r   <module>   s   3