o
    6Îh!  ã                   @   s,   d dl Z dZG dd„ dƒZG dd„ dƒZdS )é    NzMateusz Kobosc                   @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )ÚRWLocka  
    Read-Write locking primitive

    Synchronization object used in a solution of so-called second
    readers-writers problem. In this problem, many readers can simultaneously
    access a share, and a writer has an exclusive access to this share.
    Additionally, the following constraints should be met:
    1) no reader should be kept waiting if the share is currently opened for
       reading unless a writer is also waiting for the share,
    2) no writer should be kept waiting for the share longer than absolutely
       necessary.

    The implementation is based on [1, secs. 4.2.2, 4.2.6, 4.2.7]
    with a modification -- adding an additional lock (C{self.__readers_queue})
    -- in accordance with [2].

    Sources:
    [1] A.B. Downey: "The little book of semaphores", Version 2.1.5, 2008
    [2] P.J. Courtois, F. Heymans, D.L. Parnas:
        "Concurrent Control with 'Readers' and 'Writers'",
        Communications of the ACM, 1971 (via [3])
    [3] http://en.wikipedia.org/wiki/Readers-writers_problem
    c                 C   s2   t ƒ | _t ƒ | _t ¡ | _t ¡ | _t ¡ | _dS )zz
        A lock giving an even higher priority to the writer in certain
        cases (see [2] for a discussion).
        N)Ú_LightSwitchÚ_RWLock__read_switchÚ_RWLock__write_switchÚ	threadingÚLockÚ_RWLock__no_readersÚ_RWLock__no_writersÚ_RWLock__readers_queue©Úself© r   úa/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/ecdsa/_rwlock.pyÚ__init__$   s
   

zRWLock.__init__c                 C   s:   | j  ¡  | j ¡  | j | j¡ | j ¡  | j  ¡  d S ©N)r
   Úacquirer   r   r	   Úreleaser   r   r   r   Úreader_acquire/   s
   


zRWLock.reader_acquirec                 C   s   | j  | j¡ d S r   )r   r   r	   r   r   r   r   Úreader_release6   s   zRWLock.reader_releasec                 C   s   | j  | j¡ | j ¡  d S r   )r   r   r   r	   r   r   r   r   Úwriter_acquire9   s   zRWLock.writer_acquirec                 C   s   | j  ¡  | j | j¡ d S r   )r	   r   r   r   r   r   r   r   Úwriter_release=   s   
zRWLock.writer_releaseN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r   r   r      s    r   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	r   z‘An auxiliary "light switch"-like object. The first thread turns on the
    "switch", the last one turns it off (see [1, sec. 4.2.2] for details).c                 C   s   d| _ t ¡ | _d S )Nr   )Ú_LightSwitch__counterr   r   Ú_LightSwitch__mutexr   r   r   r   r   F   s   z_LightSwitch.__init__c                 C   s8   | j  ¡  |  jd7  _| jdkr| ¡  | j  ¡  d S )Né   ©r   r   r   r   ©r   Úlockr   r   r   r   J   ó
   

z_LightSwitch.acquirec                 C   s8   | j  ¡  |  jd8  _| jdkr| ¡  | j  ¡  d S )Nr   r   r   r   r   r   r   r   Q   r!   z_LightSwitch.releaseN)r   r   r   r   r   r   r   r   r   r   r   r   B   s
    r   )r   Ú
__author__r   r   r   r   r   r   Ú<module>   s   7