Format: application/x-python-code
⬇ Download File (5.7 KB)
   1  o
   2  
   3  ��h��@sDddlZddlZGdd�de�ZGdd�de�Zdd�Zdd	�ZdS)
   4  �Nc@sLeZdZdZe��ZdZdZdZ	dd�Z
   5  dd�Zdd�Zd	d
   6  �Z
   7  dd�ZdS)
   8  �	Singletona
   9  A base class for a class of a singleton object.
  10  
  11      For any derived class T, the first invocation of T() will create the instance,
  12      and any future invocations of T() will return that instance.
  13  
  14      Concurrent invocations of T() from different threads are safe.
  15      NcOs�|�dd�rt|�t|�dksJd��|jsm|jdur9|j�|jdur*t��|_Wd�n1s4wY|jsm|j�&|js\t�|�|_|j�	�dd�|_	Wd�|jSWd�|jS1shwY|jS)N�sharedFrz\Cannot use constructor arguments when accessing a Singleton without specifying shared=False.c_sdS�N�)�args�kwargsrr�Nc:\Users\shubh\Videos\agent\nova\Lib\site-packages\debugpy/common/singleton.py�<lambda>=sz#Singleton.__new__.<locals>.<lambda>)
  16  �get�len�	_instance�_lock�
  17  _lock_lock�	threading�RLock�object�__new__�__init__)�clsrrrrrr s."�
  18  
  19  
  20  ��
  21  
  22  �
  23  �
  24  �
  25  �
  26  zSingleton.__new__cOs�|�dd�}|�/|rt|�jdusJd��dt|�_nt|�jdus'Jd��Wd�dSWd�dS1s:wYdS)aIInitializes the singleton instance. Guaranteed to only be invoked once for
  27          any given type derived from Singleton.
  28  
  29          If shared=False, the caller is requesting a singleton instance for their own
  30          exclusive use. This is only allowed if the singleton has not been created yet;
  31          if so, it is created and marked as being in exclusive use. While it is marked
  32          as such, all attempts to obtain an existing instance of it immediately raise
  33          an exception. The singleton can eventually be promoted to shared use by calling
  34          share() on it.
  35          rTFz%Cannot access a non-shared Singleton.NzSingleton is already created.)�pop�type�
  36  _is_shared)�selfrrrrrrrAs��"�zSingleton.__init__cCst|�j��|S)z1Lock this singleton to prevent concurrent access.)rr
  37  �acquire�rrrr�	__enter__WszSingleton.__enter__cCst|�j��dS)z1Unlock this singleton to allow concurrent access.N)rr
  38  �release)r�exc_type�	exc_value�exc_tbrrr�__exit__\szSingleton.__exit__cCsdt|�_dS)zEShare this singleton, if it was originally created with shared=False.TN)rrrrrr�share`szSingleton.share)�__name__�
  39  __module__�__qualname__�__doc__rrrr
  40  rrrrrr r!rrrrr	s!rcsLeZdZdZe�Z	e�Z	�fdd�Zedd��Z	dd�Z
  41  dd	�Z�ZS)
  42  �ThreadSafeSingletonaZA singleton that incorporates a lock for thread-safe access to its members.
  43  
  44      The lock can be acquired using the context manager protocol, and thus idiomatic
  45      use is in conjunction with a with-statement. For example, given derived class T::
  46  
  47          with T() as t:
  48              t.x = t.frob(t.y)
  49  
  50      All access to the singleton from the outside should follow this pattern for both
  51      attributes and method calls. Singleton members can assume that self is locked by
  52      the caller while they're executing, but recursive locking of the same singleton
  53      on the same thread is also permitted.
  54      cs*t�j|i|��tt|�j�t|�_dSr)�superr�setr�readonly_attrs�rrr��	__class__rrr�szThreadSafeSingleton.__init__cCs*t|�j}|jdd�sJd��|��dS)NF)�blockingz�ThreadSafeSingleton accessed without locking. Either use with-statement, or if it is a method or property, mark it as @threadsafe_method or with @autolocked_method, as appropriate.)rr
  55  rr)r�lockrrr�
  56  assert_locked�s
  57  
  58  �z!ThreadSafeSingleton.assert_lockedcCs>t�||�}|t|�jt|�jBvrt|dd�st�|�|S)N�is_threadsafe_methodF)r�__getattribute__r�threadsafe_attrsr)�getattrr&r/�r�name�valuerrrr1�s
  59  
  60  z$ThreadSafeSingleton.__getattribute__cCs<|t|�jvsJd��|t|�jvrt�|�t�|||�S)NzThis attribute is read-only.)rr)r2r&r/r�__setattr__r4rrrr7�s
  61  zThreadSafeSingleton.__setattr__)
  62  r"r#r$r%�	frozensetr2r)r�staticmethodr/r1r7�
  63  __classcell__rrr+rr&es
  64  
  65  	r&cCs
  66  d|_|S)z�Marks a method of a ThreadSafeSingleton-derived class as inherently thread-safe.
  67  
  68      A method so marked must either not use any singleton state, or lock it appropriately.
  69      T)r0��funcrrr�threadsafe_method�sr=cst���t�fdd���}|S)z�Automatically synchronizes all calls of a method of a ThreadSafeSingleton-derived
  70      class by locking the singleton for the duration of each call.
  71      cs>|��|g|�Ri|��Wd�S1swYdSrrr*r;rr�
  72  lock_and_call�s$�z(autolocked_method.<locals>.lock_and_call)�	functools�wrapsr=)r<r>rr;r�autolocked_method�srA)r?rrrr&r=rArrrr�<module>s\?
  73