singleton.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. # Copyright (c) Microsoft Corporation. All rights reserved.
  2. # Licensed under the MIT License. See LICENSE in the project root
  3. # for license information.
  4. import functools
  5. import threading
  6. class Singleton(object):
  7. """A base class for a class of a singleton object.
  8. For any derived class T, the first invocation of T() will create the instance,
  9. and any future invocations of T() will return that instance.
  10. Concurrent invocations of T() from different threads are safe.
  11. """
  12. # A dual-lock scheme is necessary to be thread safe while avoiding deadlocks.
  13. # _lock_lock is shared by all singleton types, and is used to construct their
  14. # respective _lock instances when invoked for a new type. Then _lock is used
  15. # to synchronize all further access for that type, including __init__. This way,
  16. # __init__ for any given singleton can access another singleton, and not get
  17. # deadlocked if that other singleton is trying to access it.
  18. _lock_lock = threading.RLock()
  19. _lock = None
  20. # Specific subclasses will get their own _instance set in __new__.
  21. _instance = None
  22. _is_shared = None # True if shared, False if exclusive
  23. def __new__(cls, *args, **kwargs):
  24. # Allow arbitrary args and kwargs if shared=False, because that is guaranteed
  25. # to construct a new singleton if it succeeds. Otherwise, this call might end
  26. # up returning an existing instance, which might have been constructed with
  27. # different arguments, so allowing them is misleading.
  28. assert not kwargs.get("shared", False) or (len(args) + len(kwargs)) == 0, (
  29. "Cannot use constructor arguments when accessing a Singleton without "
  30. "specifying shared=False."
  31. )
  32. # Avoid locking as much as possible with repeated double-checks - the most
  33. # common path is when everything is already allocated.
  34. if not cls._instance:
  35. # If there's no per-type lock, allocate it.
  36. if cls._lock is None:
  37. with cls._lock_lock:
  38. if cls._lock is None:
  39. cls._lock = threading.RLock()
  40. # Now that we have a per-type lock, we can synchronize construction.
  41. if not cls._instance:
  42. with cls._lock:
  43. if not cls._instance:
  44. cls._instance = object.__new__(cls)
  45. # To prevent having __init__ invoked multiple times, call
  46. # it here directly, and then replace it with a stub that
  47. # does nothing - that stub will get auto-invoked on return,
  48. # and on all future singleton accesses.
  49. cls._instance.__init__()
  50. cls.__init__ = lambda *args, **kwargs: None
  51. return cls._instance
  52. def __init__(self, *args, **kwargs):
  53. """Initializes the singleton instance. Guaranteed to only be invoked once for
  54. any given type derived from Singleton.
  55. If shared=False, the caller is requesting a singleton instance for their own
  56. exclusive use. This is only allowed if the singleton has not been created yet;
  57. if so, it is created and marked as being in exclusive use. While it is marked
  58. as such, all attempts to obtain an existing instance of it immediately raise
  59. an exception. The singleton can eventually be promoted to shared use by calling
  60. share() on it.
  61. """
  62. shared = kwargs.pop("shared", True)
  63. with self:
  64. if shared:
  65. assert (
  66. type(self)._is_shared is not False
  67. ), "Cannot access a non-shared Singleton."
  68. type(self)._is_shared = True
  69. else:
  70. assert type(self)._is_shared is None, "Singleton is already created."
  71. def __enter__(self):
  72. """Lock this singleton to prevent concurrent access."""
  73. type(self)._lock.acquire()
  74. return self
  75. def __exit__(self, exc_type, exc_value, exc_tb):
  76. """Unlock this singleton to allow concurrent access."""
  77. type(self)._lock.release()
  78. def share(self):
  79. """Share this singleton, if it was originally created with shared=False."""
  80. type(self)._is_shared = True
  81. class ThreadSafeSingleton(Singleton):
  82. """A singleton that incorporates a lock for thread-safe access to its members.
  83. The lock can be acquired using the context manager protocol, and thus idiomatic
  84. use is in conjunction with a with-statement. For example, given derived class T::
  85. with T() as t:
  86. t.x = t.frob(t.y)
  87. All access to the singleton from the outside should follow this pattern for both
  88. attributes and method calls. Singleton members can assume that self is locked by
  89. the caller while they're executing, but recursive locking of the same singleton
  90. on the same thread is also permitted.
  91. """
  92. threadsafe_attrs = frozenset()
  93. """Names of attributes that are guaranteed to be used in a thread-safe manner.
  94. This is typically used in conjunction with share() to simplify synchronization.
  95. """
  96. readonly_attrs = frozenset()
  97. """Names of attributes that are readonly. These can be read without locking, but
  98. cannot be written at all.
  99. Every derived class gets its own separate set. Thus, for any given singleton type
  100. T, an attribute can be made readonly after setting it, with T.readonly_attrs.add().
  101. """
  102. def __init__(self, *args, **kwargs):
  103. super().__init__(*args, **kwargs)
  104. # Make sure each derived class gets a separate copy.
  105. type(self).readonly_attrs = set(type(self).readonly_attrs)
  106. # Prevent callers from reading or writing attributes without locking, except for
  107. # reading attributes listed in threadsafe_attrs, and methods specifically marked
  108. # with @threadsafe_method. Such methods should perform the necessary locking to
  109. # ensure thread safety for the callers.
  110. @staticmethod
  111. def assert_locked(self):
  112. lock = type(self)._lock
  113. assert lock.acquire(blocking=False), (
  114. "ThreadSafeSingleton accessed without locking. Either use with-statement, "
  115. "or if it is a method or property, mark it as @threadsafe_method or with "
  116. "@autolocked_method, as appropriate."
  117. )
  118. lock.release()
  119. def __getattribute__(self, name):
  120. value = object.__getattribute__(self, name)
  121. if name not in (type(self).threadsafe_attrs | type(self).readonly_attrs):
  122. if not getattr(value, "is_threadsafe_method", False):
  123. ThreadSafeSingleton.assert_locked(self)
  124. return value
  125. def __setattr__(self, name, value):
  126. assert name not in type(self).readonly_attrs, "This attribute is read-only."
  127. if name not in type(self).threadsafe_attrs:
  128. ThreadSafeSingleton.assert_locked(self)
  129. return object.__setattr__(self, name, value)
  130. def threadsafe_method(func):
  131. """Marks a method of a ThreadSafeSingleton-derived class as inherently thread-safe.
  132. A method so marked must either not use any singleton state, or lock it appropriately.
  133. """
  134. func.is_threadsafe_method = True
  135. return func
  136. def autolocked_method(func):
  137. """Automatically synchronizes all calls of a method of a ThreadSafeSingleton-derived
  138. class by locking the singleton for the duration of each call.
  139. """
  140. @functools.wraps(func)
  141. @threadsafe_method
  142. def lock_and_call(self, *args, **kwargs):
  143. with self:
  144. return func(self, *args, **kwargs)
  145. return lock_and_call