Singleton.st
author Stefan Vogel <sv@exept.de>
Wed, 22 Feb 2017 15:33:50 +0100
changeset 4346 7a4c996ac8f0
parent 1927 d1ba381a17ae
child 4504 e2ce4c099568
permissions -rw-r--r--
#BUGFIX by stefan class: SharedQueue fix bug in #nextIfEmpty: if exceptionblock returns. add missing methods added: #remove:ifAbsent: #reverseDo: comment/format in: #commonWriteWith: changed: #do: #next #nextIfEmpty: #nextPut: #nextPutFirst: #nextWithTimeout: #removeIdentical:ifAbsent: #removeLast
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1927
d1ba381a17ae copyright
Claus Gittinger <cg@exept.de>
parents: 1643
diff changeset
     1
"
d1ba381a17ae copyright
Claus Gittinger <cg@exept.de>
parents: 1643
diff changeset
     2
 COPYRIGHT (c) 2006 by eXept Software AG
d1ba381a17ae copyright
Claus Gittinger <cg@exept.de>
parents: 1643
diff changeset
     3
              All Rights Reserved
d1ba381a17ae copyright
Claus Gittinger <cg@exept.de>
parents: 1643
diff changeset
     4
d1ba381a17ae copyright
Claus Gittinger <cg@exept.de>
parents: 1643
diff changeset
     5
 This software is furnished under a license and may be used
d1ba381a17ae copyright
Claus Gittinger <cg@exept.de>
parents: 1643
diff changeset
     6
 only in accordance with the terms of that license and with the
d1ba381a17ae copyright
Claus Gittinger <cg@exept.de>
parents: 1643
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
d1ba381a17ae copyright
Claus Gittinger <cg@exept.de>
parents: 1643
diff changeset
     8
 be provided or otherwise made available to, or used by, any
d1ba381a17ae copyright
Claus Gittinger <cg@exept.de>
parents: 1643
diff changeset
     9
 other person.  No title to or ownership of the software is
d1ba381a17ae copyright
Claus Gittinger <cg@exept.de>
parents: 1643
diff changeset
    10
 hereby transferred.
d1ba381a17ae copyright
Claus Gittinger <cg@exept.de>
parents: 1643
diff changeset
    11
"
1552
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    12
"{ Package: 'stx:libbasic2' }"
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    13
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    14
Object subclass:#Singleton
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    15
	instanceVariableNames:''
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    16
	classVariableNames:'Lock'
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    17
	poolDictionaries:''
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    18
	category:'System-Support'
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    19
!
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    20
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    21
Singleton class instanceVariableNames:'theOnlyInstance'
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    22
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    23
"
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    24
 No other class instance variables are inherited by this class.
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    25
"
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    26
!
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    27
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    28
!Singleton class methodsFor:'documentation'!
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    29
1927
d1ba381a17ae copyright
Claus Gittinger <cg@exept.de>
parents: 1643
diff changeset
    30
copyright
d1ba381a17ae copyright
Claus Gittinger <cg@exept.de>
parents: 1643
diff changeset
    31
"
d1ba381a17ae copyright
Claus Gittinger <cg@exept.de>
parents: 1643
diff changeset
    32
 COPYRIGHT (c) 2006 by eXept Software AG
d1ba381a17ae copyright
Claus Gittinger <cg@exept.de>
parents: 1643
diff changeset
    33
              All Rights Reserved
d1ba381a17ae copyright
Claus Gittinger <cg@exept.de>
parents: 1643
diff changeset
    34
d1ba381a17ae copyright
Claus Gittinger <cg@exept.de>
parents: 1643
diff changeset
    35
 This software is furnished under a license and may be used
d1ba381a17ae copyright
Claus Gittinger <cg@exept.de>
parents: 1643
diff changeset
    36
 only in accordance with the terms of that license and with the
d1ba381a17ae copyright
Claus Gittinger <cg@exept.de>
parents: 1643
diff changeset
    37
 inclusion of the above copyright notice.   This software may not
d1ba381a17ae copyright
Claus Gittinger <cg@exept.de>
parents: 1643
diff changeset
    38
 be provided or otherwise made available to, or used by, any
d1ba381a17ae copyright
Claus Gittinger <cg@exept.de>
parents: 1643
diff changeset
    39
 other person.  No title to or ownership of the software is
d1ba381a17ae copyright
Claus Gittinger <cg@exept.de>
parents: 1643
diff changeset
    40
 hereby transferred.
d1ba381a17ae copyright
Claus Gittinger <cg@exept.de>
parents: 1643
diff changeset
    41
"
d1ba381a17ae copyright
Claus Gittinger <cg@exept.de>
parents: 1643
diff changeset
    42
!
d1ba381a17ae copyright
Claus Gittinger <cg@exept.de>
parents: 1643
diff changeset
    43
1552
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    44
documentation
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    45
"
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    46
    Subclasses of Singleton have only a single instance.
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    47
    The instance creation methods ensure that there is only one instance.
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    48
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    49
    Singletons that would have inherited from Object could inherit from
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    50
    Singleton. For Singletons that do not inherit from Object, 
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    51
    you have to copy the Singleton's code to your class.
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    52
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    53
    [author:]
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    54
        Stefan Vogel (stefan@zwerg)
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    55
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    56
    [class instance variables:]
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    57
        theOnlyInstance     Object          The only instance of this class
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    58
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    59
    [class variables:]
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    60
        Lock                RecursionLock   A global lock to protect agains races during instance creation
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    61
"
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    62
! !
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    63
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    64
!Singleton class methodsFor:'initialization'!
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    65
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    66
initialize
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    67
    Lock := RecursionLock new name:#Singleton
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    68
! !
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    69
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    70
!Singleton class methodsFor:'instance creation'!
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    71
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    72
basicNew
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    73
    "allocate a singleton in a thread-safe way.
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    74
     Do lazy locking here"
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    75
    
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    76
    theOnlyInstance isNil ifTrue:[
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    77
        Lock critical:[
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    78
            theOnlyInstance isNil ifTrue:[
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    79
                theOnlyInstance := super basicNew.
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    80
            ].
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    81
        ]
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    82
    ].
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    83
    ^ theOnlyInstance.
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    84
!
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    85
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    86
basicNew:anInteger
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    87
    "allocate a singleton in a thread-safe way.
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    88
     Do lazy locking here"
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    89
    
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    90
    theOnlyInstance isNil ifTrue:[
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    91
        Lock critical:[
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    92
            theOnlyInstance isNil ifTrue:[
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    93
                theOnlyInstance := super basicNew:anInteger.
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    94
            ].
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    95
        ]
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    96
    ].
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    97
    ^ theOnlyInstance.
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    98
! !
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    99
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   100
!Singleton class methodsFor:'accessing'!
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   101
1643
2c59ef0737c7 access method for Lock
Stefan Vogel <sv@exept.de>
parents: 1552
diff changeset
   102
singletonLock
2c59ef0737c7 access method for Lock
Stefan Vogel <sv@exept.de>
parents: 1552
diff changeset
   103
    "can be used by other classes that are not subclasses of Singleton"
2c59ef0737c7 access method for Lock
Stefan Vogel <sv@exept.de>
parents: 1552
diff changeset
   104
2c59ef0737c7 access method for Lock
Stefan Vogel <sv@exept.de>
parents: 1552
diff changeset
   105
    ^ Lock
2c59ef0737c7 access method for Lock
Stefan Vogel <sv@exept.de>
parents: 1552
diff changeset
   106
!
2c59ef0737c7 access method for Lock
Stefan Vogel <sv@exept.de>
parents: 1552
diff changeset
   107
1552
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   108
theOnlyInstance
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   109
    ^ theOnlyInstance
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   110
! !
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   111
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   112
!Singleton class methodsFor:'documentation'!
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   113
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   114
version
1927
d1ba381a17ae copyright
Claus Gittinger <cg@exept.de>
parents: 1643
diff changeset
   115
    ^ '$Header: /cvs/stx/stx/libbasic2/Singleton.st,v 1.3 2008-01-22 22:31:57 cg Exp $'
1552
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   116
! !
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   117
541c7a691f34 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   118
Singleton initialize!