--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Singleton.st Tue May 03 18:28:18 2005 +0200
@@ -0,0 +1,87 @@
+"{ Package: 'stx:libbasic2' }"
+
+Object subclass:#Singleton
+ instanceVariableNames:''
+ classVariableNames:'Lock'
+ poolDictionaries:''
+ category:'System-Support'
+!
+
+Singleton class instanceVariableNames:'theOnlyInstance'
+
+"
+ No other class instance variables are inherited by this class.
+"
+!
+
+!Singleton class methodsFor:'documentation'!
+
+documentation
+"
+ Subclasses of Singleton have only a single instance.
+ The instance creation methods ensure that there is only one instance.
+
+ Singletons that would have inherited from Object could inherit from
+ Singleton. For Singletons that do not inherit from Object,
+ you have to copy the Singleton's code to your class.
+
+ [author:]
+ Stefan Vogel (stefan@zwerg)
+
+ [class instance variables:]
+ theOnlyInstance Object The only instance of this class
+
+ [class variables:]
+ Lock RecursionLock A global lock to protect agains races during instance creation
+"
+! !
+
+!Singleton class methodsFor:'initialization'!
+
+initialize
+ Lock := RecursionLock new name:#Singleton
+! !
+
+!Singleton class methodsFor:'instance creation'!
+
+basicNew
+ "allocate a singleton in a thread-safe way.
+ Do lazy locking here"
+
+ theOnlyInstance isNil ifTrue:[
+ Lock critical:[
+ theOnlyInstance isNil ifTrue:[
+ theOnlyInstance := super basicNew.
+ ].
+ ]
+ ].
+ ^ theOnlyInstance.
+!
+
+basicNew:anInteger
+ "allocate a singleton in a thread-safe way.
+ Do lazy locking here"
+
+ theOnlyInstance isNil ifTrue:[
+ Lock critical:[
+ theOnlyInstance isNil ifTrue:[
+ theOnlyInstance := super basicNew:anInteger.
+ ].
+ ]
+ ].
+ ^ theOnlyInstance.
+! !
+
+!Singleton class methodsFor:'accessing'!
+
+theOnlyInstance
+ ^ theOnlyInstance
+! !
+
+!Singleton class methodsFor:'documentation'!
+
+version
+ ^ '$Header: /cvs/stx/stx/libbasic2/Singleton.st,v 1.1 2005-05-03 16:28:18 stefan Exp $'
+! !
+
+Singleton initialize!