32 lines
826 B
Python
32 lines
826 B
Python
#
|
|
# Last Change: 2026-03-18
|
|
#
|
|
|
|
import threading
|
|
|
|
class Singleton:
|
|
"""
|
|
A thread lock protected singleton,
|
|
makes any inherting class into a singlton.
|
|
|
|
The SINGLETON_LOCK object is shared between all inherited children,
|
|
unless they initiate their own using the 'decouple_lock_from_parent_class' method.
|
|
"""
|
|
|
|
SINGLETON_INSTANCE = None
|
|
SINGLETON_LOCK = threading.Lock()
|
|
|
|
@classmethod
|
|
def decouple_lock_from_parent_class( cls ):
|
|
if cls.SINGLETON_LOCK is not super().SINGLETON_LOCK: return
|
|
cls.SINGLETON_LOCK = threading.Lock()
|
|
|
|
def __new__( cls, *args, **kwargs ):
|
|
if cls.SINGLETON_INSTANCE: return cls.SINGLETON_INSTANCE
|
|
|
|
with cls.SINGLETON_LOCK:
|
|
if cls.SINGLETON_INSTANCE: return cls.SINGLETON_INSTANCE
|
|
cls.SINGLETON_INSTANCE = super().__new__( cls, *args, **kwargs )
|
|
|
|
return cls.SINGLETON_INSTANCE
|