B
    лd                 @   s    d Z ddlZG dd deZdS )zLocking related utils.    Nc               @   s\   e Zd ZdZdddgZdddZdd	 Zd
d Zdd Zdd Z	dd Z
G dd deZdS )	GroupLocka  A lock to allow many members of a group to access a resource exclusively.

  This lock provides a way to allow access to a resource by multiple threads
  belonging to a logical group at the same time, while restricting access to
  threads from all other groups. You can think of this as an extension of a
  reader-writer lock, where you allow multiple writers at the same time. We
  made it generic to support multiple groups instead of just two - readers and
  writers.

  Simple usage example with two groups accessing the same resource:

  ```python
  lock = GroupLock(num_groups=2)

  # In a member of group 0:
  with lock.group(0):
    # do stuff, access the resource
    # ...

  # In a member of group 1:
  with lock.group(1):
    # do stuff, access the resource
    # ...
  ```

  Using as a context manager with `.group(group_id)` is the easiest way. You
  can also use the `acquire` and `release` method directly.
  _ready_num_groups_group_member_counts   c             C   s>   |dk rt d| tt | _|| _dg| j | _dS )a6  Initialize a group lock.

    Args:
      num_groups: The number of groups that will be accessing the resource under
        consideration. Should be a positive number.

    Returns:
      A group lock that can then be used to synchronize code.

    Raises:
      ValueError: If num_groups is less than 1.
       zGArgument `num_groups` must be a positive integer. Received: num_groups=r   N)
ValueError	threading	ConditionLockr   r   r   )self
num_groups r   R/var/www/html/venv/lib/python3.7/site-packages/tensorflow/python/util/lock_util.py__init__4   s    zGroupLock.__init__c             C   s   |  | | | |S )zEnter a context where the lock is with group `group_id`.

    Args:
      group_id: The group for which to acquire and release the lock.

    Returns:
      A context manager which will acquire the lock for `group_id`.
    )_validate_group_id_Context)r   group_idr   r   r   groupI   s    	
zGroupLock.groupc             C   sN   |  | | j  x| |r,| j  qW | j|  d7  < | j  dS )z7Acquire the group lock for a specific group `group_id`.r   N)r   r   acquire_another_group_activewaitr   release)r   r   r   r   r   r   U   s    

zGroupLock.acquirec             C   sL   |  | | j  | j|  d8  < | j| dkr>| j  | j  dS )z7Release the group lock for a specific group `group_id`.r   r   N)r   r   r   r   
notify_allr   )r   r   r   r   r   r   _   s    


zGroupLock.releasec                s   t  fddt| jD S )Nc             3   s"   | ]\}}| kr|d kV  qdS )r   Nr   ).0gc)r   r   r   	<genexpr>k   s    z2GroupLock._another_group_active.<locals>.<genexpr>)any	enumerater   )r   r   r   )r   r   r   i   s    zGroupLock._another_group_activec             C   s,   |dk s|| j kr(td| j  d| d S )Nr   zQArgument `group_id` should verify `0 <= group_id < num_groups` (with `num_groups=z`). Received: group_id=)r   r   )r   r   r   r   r   r   m   s    zGroupLock._validate_group_idc               @   s0   e Zd ZdZddgZdd Zdd Zdd	 Zd
S )zGroupLock._Contextz'Context manager helper for `GroupLock`._lock	_group_idc             C   s   || _ || _d S )N)r    r!   )r   lockr   r   r   r   r   y   s    zGroupLock._Context.__init__c             C   s   | j | j d S )N)r    r   r!   )r   r   r   r   	__enter__}   s    zGroupLock._Context.__enter__c             C   s   ~~~| j | j d S )N)r    r   r!   )r   type_arg	value_argtraceback_argr   r   r   __exit__   s    zGroupLock._Context.__exit__N)__name__
__module____qualname____doc__	__slots__r   r#   r'   r   r   r   r   r   t   s
   r   N)r   )r(   r)   r*   r+   r,   r   r   r   r   r   r   objectr   r   r   r   r   r      s   



r   )r+   r	   r-   r   r   r   r   r   <module>   s   