
    BVh                     &    d Z ddlZ G d de      Zy)zLocking related utils.    Nc                   T    e Zd ZdZg dZddZd Zd Zd Zd Z	d Z
 G d	 d
e      Zy)	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_countsc                     |dk  rt        d|       t        j                  t        j                               | _        || _        dg| j
                  z  | _        y)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_groupss     P/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/util/lock_util.py__init__zGroupLock.__init__4   s_     A~"",/0 0 %%inn&67DK!D!"d&6&6 6D    c                 H    | j                  |       | j                  | |      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_ids     r   groupzGroupLock.groupI   s#     	H%==x((r   c                 6   | j                  |       | j                  j                          | j                  |      r,| j                  j	                          | j                  |      r,| j
                  |xx   dz  cc<   | j                  j                          y)z7Acquire the group lock for a specific group `group_id`.r	   N)r   r   acquire_another_group_activewaitr   releaser   s     r   r   zGroupLock.acquireU   st    H%KK

$
$X
.
kk 
$
$X
.h'1,'KKr   c                    | j                  |       | j                  j                          | j                  |xx   dz  cc<   | j                  |   dk(  r| j                  j	                          | j                  j                          y)z7Release the group lock for a specific group `group_id`.r	   r   N)r   r   r   r   
notify_allr   r   s     r   r   zGroupLock.release_   si    H%KKh'1,'  *a/
kkKKr   c                 R    t        fdt        | j                        D              S )Nc              3   :   K   | ]  \  }}|k7  s|d kD    yw)r   N ).0gcr   s      r   	<genexpr>z2GroupLock._another_group_active.<locals>.<genexpr>j   s&      Q!Q!x-AQs   
)any	enumerater   r   s    `r   r   zGroupLock._another_group_activei   s,     Q#D$=$=>Q Q Qr   c                 b    |dk  s|| j                   k\  rt        d| j                    d|       y )Nr   zQArgument `group_id` should verify `0 <= group_id < num_groups` (with `num_groups=z`). Received: group_id=)r   r
   r   s     r   r   zGroupLock._validate_group_idm   sH    !|x4#3#33#//0 1  (z+, , 4r   c                   *    e Zd ZdZddgZd Zd Zd Zy)GroupLock._Contextz'Context manager helper for `GroupLock`._lock	_group_idc                      || _         || _        y N)r,   r-   )r   lockr   s      r   r   zGroupLock._Context.__init__y   s    djdnr   c                 N    | j                   j                  | j                         y r/   )r,   r   r-   )r   s    r   	__enter__zGroupLock._Context.__enter__}   s    
jj(r   c                 T    ~~~| j                   j                  | j                         y r/   )r,   r   r-   )r   type_arg	value_argtraceback_args       r   __exit__zGroupLock._Context.__exit__   s    
I}
jj(r   N)__name__
__module____qualname____doc__	__slots__r   r2   r7   r"   r   r   r   r+   t   s    1+&I ))r   r   N)   )r8   r9   r:   r;   r<   r   r   r   r   r   r   objectr   r"   r   r   r   r      s:    : @)7*
)Q,) )r   r   )r;   r   r>   r   r"   r   r   <module>r?      s     n) n)r   