Format: application/x-python-code
1 o
2
3 ��h� � @ sD d dl Z d dlZG dd� de�ZG dd� de�Zdd� Zdd � ZdS )
4 � Nc @ sL e Zd ZdZe�� ZdZdZdZ dd� Z
5 dd� Zdd� Zd 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 Nc O s� |� dd�rt|�t|� dksJ d��| jsm| jd u r9| j� | jd u r*t�� | _W d � n1 s4w Y | jsm| j�& | js\t�| �| _| j� � dd� | _ W d � | jS W d � | jS 1 shw Y | jS )N�sharedFr z\Cannot use constructor arguments when accessing a Singleton without specifying shared=False.c _ s d S �N� )�args�kwargsr r �Nc:\Users\shubh\Videos\agent\nova\Lib\site-packages\debugpy/common/singleton.py�<lambda>= s z#Singleton.__new__.<locals>.<lambda>)
16 �get�len� _instance�_lock�
17 _lock_lock� threading�RLock�object�__new__�__init__)�clsr r r r r r s. "�
18
19
20 ��
21
22 �
23 �
24 �
25 �
26 zSingleton.__new__c O s� |� dd�}| �/ |rt| �jdusJ d��dt| �_nt| �jdu s'J d��W d � dS W d � dS 1 s:w Y dS )aI Initializes 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 r TFz%Cannot access a non-shared Singleton.NzSingleton is already created.)�pop�type�
36 _is_shared)�selfr r r r r r r A s ��"�zSingleton.__init__c C s t | �j�� | S )z1Lock this singleton to prevent concurrent access.)r r
37 �acquire�r r r r � __enter__W s zSingleton.__enter__c C s t | �j�� dS )z1Unlock this singleton to allow concurrent access.N)r r
38 �release)r �exc_type� exc_value�exc_tbr r r �__exit__\ s zSingleton.__exit__c C s dt | �_dS )zEShare this singleton, if it was originally created with shared=False.TN)r r r r r r �share` s zSingleton.share)�__name__�
39 __module__�__qualname__�__doc__r r r r
40 r r r r r r r! r r r r r s !r c sL e Zd ZdZe� Z e� Z � fdd�Zedd� �Z dd� Z
41 dd � Z� ZS )
42 �ThreadSafeSingletonaZ A 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 c s* t � j|i |�� tt| �j�t| �_d S r )�superr �setr �readonly_attrs�r r r �� __class__r r r � s zThreadSafeSingleton.__init__c C s* t | �j}|jdd�sJ d��|�� d S )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.)r r
55 r r )r �lockr r r �
56 assert_locked� s
57
58 �z!ThreadSafeSingleton.assert_lockedc C s>