U
    -WjZ  ã                   @  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rNd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   úic:\users\psant\downloads\hotstarbackend\school\venv38\Lib\site-packages\twisted/_threads/_threadworker.pyr      s   r   ÚTÚUc                   @  s*   e Zd Zdddœdd„Zddœdd„Zd	S )
ÚSimpleQueuer   ÚNone)ÚitemÚreturnc                 C  s   d S ©Nr   )Úselfr   r   r   r   Úput!   s    zSimpleQueue.put©r   c                 C  s   d S r   r   ©r   r   r   r   Úget$   s    zSimpleQueue.getN)r   r   r   r   r!   r   r   r   r   r       s   r   z/Callable[[Callable[[], T | U], U], Iterator[T]]Ú	smartiterc                   @  s>   e Zd ZdZdddœdd„Zddd	œd
d„Zd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.
    z(Callable[[Callable[[], object]], object]z8SimpleQueue[Callable[[], object] | Literal[Stop.Thread]])ÚstartThreadÚqueuec                   s,   ˆ | _ tƒ | _d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)Ú_qr   Ú_hasQuit)r   r$   r%   r)   r   r(   r   Ú__init__7   s    zThreadWorker.__init__úCallable[[], None]r   )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)r+   Úcheckr*   r   )r   r'   r   r   r   ÚdoO   s    
zThreadWorker.dor   c                 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   ÚquitX   s    
zThreadWorker.quitN©r   r   r   Ú__doc__r,   r/   r1   r   r   r   r   r#   -   s   	r#   c                   @  s(   e Zd Zddœdd„Zddœdd„ZdS )	Ú
SimpleLockÚboolr   c                 C  s   d S r   r   r    r   r   r   Úacquirec   s    zSimpleLock.acquirer   c                 C  s   d S r   r   r    r   r   r   Úreleasef   s    zSimpleLock.releaseN)r   r   r   r6   r7   r   r   r   r   r4   b   s   r4   c                   @  s>   e Zd ZdZdddœdd„Zddd	œd
d„Zddœdd„ZdS )Ú
LockWorkerzE
    An L{IWorker} implemented based on a mutual-exclusion lock.
    r4   zthreading.local)ÚlockÚ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   r9   r:   r   r   r   r,   p   s    	zLockWorker.__init__r-   r   )r)   r   c                 C  sŽ   | j }| j}| j ¡  t|ddƒ}|dkr€|dk	s:tdƒ‚g  }|_| |¡ | ¡  z|rj| 
d¡ƒ  qXW 5 | 	¡  d|_X n
| |¡ 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   )r<   r=   r;   r.   ÚgetattrÚAssertionErrorr>   Úappendr6   r7   Úpop)r   r)   r9   r:   r>   r   r   r   r/   }   s    



zLockWorker.dor   c                 C  s   | j  ¡  d| _dS )z*
        Quit this L{LockWorker}.
        N)r;   r0   r<   r    r   r   r   r1   —   s    
zLockWorker.quitNr2   r   r   r   r   r8   j   s   r8   ) r3   Ú
__future__r   Úenumr   r   Útypingr   r   r   r   r	   r
   Ú	threadingZzope.interfacer   Z_conveniencer   Z	_ithreadsr   r   r   r&   r   r   r   Ú__annotations__Úiterr"   r#   r4   r8   r   r   r   r   Ú<module>   s(    	4