Singleton.st
author Claus Gittinger <cg@exept.de>
Sat, 02 May 2020 21:40:13 +0200
changeset 5476 7355a4b11cb6
parent 4933 f6c830dda3b0
permissions -rw-r--r--
#FEATURE by cg class: Socket class added: #newTCPclientToHost:port:domain:domainOrder:withTimeout: changed: #newTCPclientToHost:port:domain:withTimeout:

"
 COPYRIGHT (c) 2006 by eXept Software AG
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
"{ Package: 'stx:libbasic2' }"

"{ NameSpace: Smalltalk }"

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'!

copyright
"
 COPYRIGHT (c) 2006 by eXept Software AG
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
!

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

    "Modified: / 30-08-2017 / 15:20:24 / cg"
! !

!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.
!

new
    "allocate a singleton in a thread-safe way.
     Do lazy locking here"
    
    theOnlyInstance isNil ifTrue:[
        Lock critical:[
            theOnlyInstance isNil ifTrue:[
                theOnlyInstance := super basicNew initialize.
            ].
        ]
    ].
    ^ theOnlyInstance.

    "Created: / 01-04-2019 / 12:05:25 / Stefan Vogel"
!

new:count
    "allocate a singleton in a thread-safe way.
     Do lazy locking here"
    
    theOnlyInstance isNil ifTrue:[
        Lock critical:[
            theOnlyInstance isNil ifTrue:[
                theOnlyInstance := (super basicNew:count) initialize.
            ].
        ]
    ].
    ^ theOnlyInstance.

    "Created: / 01-04-2019 / 12:05:42 / Stefan Vogel"
!

releaseInstance
    "release the singleton's instance.
     Use only for debugging!!"
    
    Lock critical:[
        theOnlyInstance := nil.
    ].

    "Created: / 01-04-2019 / 12:02:58 / Stefan Vogel"
! !

!Singleton class methodsFor:'accessing'!

singletonLock
    "can be used by other classes that are not subclasses of Singleton"

    ^ Lock
!

theOnlyInstance
    theOnlyInstance isNil ifTrue:[
        Lock critical:[
            theOnlyInstance isNil ifTrue:[
                theOnlyInstance := self new
            ].
        ]
    ].
    ^ theOnlyInstance.

    "Modified: / 27-03-2018 / 12:44:24 / stefan"
! !

!Singleton class methodsFor:'documentation'!

version
    ^ '$Header$'
! !


Singleton initialize!