22555
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
21189
|
3 |
"
|
21696
|
4 |
COPYRIGHT (c) 2016 by eXept Software AG
|
21189
|
5 |
All Rights Reserved
|
|
6 |
|
|
7 |
This software is furnished under a license and may be used
|
|
8 |
only in accordance with the terms of that license and with the
|
|
9 |
inclusion of the above copyright notice. This software may not
|
|
10 |
be provided or otherwise made available to, or used by, any
|
|
11 |
other person. No title to or ownership of the software is
|
|
12 |
hereby transferred.
|
|
13 |
"
|
|
14 |
"{ Package: 'stx:libbasic' }"
|
|
15 |
|
|
16 |
"{ NameSpace: Smalltalk }"
|
|
17 |
|
|
18 |
Semaphore subclass:#EventSemaphore
|
|
19 |
instanceVariableNames:''
|
|
20 |
classVariableNames:''
|
|
21 |
poolDictionaries:''
|
|
22 |
category:'Kernel-Processes'
|
|
23 |
!
|
|
24 |
|
|
25 |
!EventSemaphore class methodsFor:'documentation'!
|
|
26 |
|
|
27 |
copyright
|
|
28 |
"
|
21696
|
29 |
COPYRIGHT (c) 2016 by eXept Software AG
|
21189
|
30 |
All Rights Reserved
|
|
31 |
|
|
32 |
This software is furnished under a license and may be used
|
|
33 |
only in accordance with the terms of that license and with the
|
|
34 |
inclusion of the above copyright notice. This software may not
|
|
35 |
be provided or otherwise made available to, or used by, any
|
|
36 |
other person. No title to or ownership of the software is
|
|
37 |
hereby transferred.
|
|
38 |
"
|
|
39 |
!
|
|
40 |
|
|
41 |
documentation
|
|
42 |
"
|
|
43 |
Processes wait for an EventSemaphores until it is signaled.
|
|
44 |
The EventSemaphore is not consumed and remains signaled until manually reset.
|
|
45 |
|
|
46 |
[author:]
|
|
47 |
Stefan Vogel
|
|
48 |
|
|
49 |
[see also:]
|
|
50 |
Semaphore
|
|
51 |
"
|
|
52 |
!
|
|
53 |
|
|
54 |
example
|
|
55 |
"
|
|
56 |
Create an event and signal it.
|
22555
|
57 |
After being signaled, the waiter on the event returns immediately.
|
21189
|
58 |
|
|
59 |
[exBegin]
|
|
60 |
|event|
|
|
61 |
|
|
62 |
event := EventSemaphore new.
|
|
63 |
[ event wait. Transcript showCR:'Process 1 continued' ] forkAt:9.
|
|
64 |
[ event wait. Transcript showCR:'Process 2 continued' ] forkAt:9.
|
|
65 |
event signal.
|
|
66 |
event wait.
|
|
67 |
event wait.
|
|
68 |
[exEnd]
|
|
69 |
"
|
|
70 |
! !
|
|
71 |
|
|
72 |
!EventSemaphore class methodsFor:'signaling'!
|
|
73 |
|
|
74 |
new:n
|
|
75 |
"count must be 0 or 1"
|
|
76 |
|
|
77 |
(n == 0 or:[n == 1]) ifTrue:[
|
21491
|
78 |
^ super new:n.
|
21189
|
79 |
].
|
|
80 |
^ self error:'invalid count'.
|
21491
|
81 |
|
|
82 |
"Modified: / 20-02-2017 / 10:27:06 / stefan"
|
21189
|
83 |
! !
|
|
84 |
|
|
85 |
!EventSemaphore methodsFor:'blocked'!
|
|
86 |
|
|
87 |
signalForAll
|
|
88 |
"blocked, since it would only set the event if there was anyone waiting"
|
|
89 |
|
|
90 |
^ self shouldNotImplement.
|
|
91 |
!
|
|
92 |
|
|
93 |
signalIf
|
|
94 |
"blocked, since it would only set the event if there was anyone waiting"
|
|
95 |
|
|
96 |
^ self shouldNotImplement.
|
|
97 |
! !
|
|
98 |
|
|
99 |
!EventSemaphore methodsFor:'misc'!
|
|
100 |
|
|
101 |
reset
|
|
102 |
"reset the event to the non-signaled state"
|
|
103 |
|
|
104 |
count := 0
|
|
105 |
! !
|
|
106 |
|
|
107 |
!EventSemaphore methodsFor:'semaphoreSet interface'!
|
|
108 |
|
|
109 |
checkAndAddWaitingProcess:process
|
|
110 |
"interface for SemaphoreSet.
|
|
111 |
If the semaphore is available, return true.
|
|
112 |
Otherwise register our process to be wakened up once the semaphore is available
|
|
113 |
and return false.
|
|
114 |
ATTENTION: this must be invoked with OperatingSystem-interrupts-blocked.
|
|
115 |
"
|
|
116 |
|
|
117 |
count > 0 ifTrue:[
|
|
118 |
^ true
|
|
119 |
].
|
|
120 |
(waitingProcesses notNil and:[(waitingProcesses includesIdentical:process)]) ifFalse:[
|
|
121 |
self addWaitingProcess:process.
|
|
122 |
].
|
|
123 |
^ false
|
|
124 |
|
|
125 |
"Modified: / 14-12-1995 / 10:32:17 / stefan"
|
|
126 |
"Modified: / 11-08-2011 / 14:36:20 / cg"
|
|
127 |
! !
|
|
128 |
|
|
129 |
!EventSemaphore methodsFor:'signaling'!
|
|
130 |
|
|
131 |
signal
|
|
132 |
"redefined to limit count to 1"
|
|
133 |
|
|
134 |
self signalOnce.
|
|
135 |
! !
|
|
136 |
|
|
137 |
!EventSemaphore methodsFor:'waiting'!
|
|
138 |
|
|
139 |
wait
|
|
140 |
"once signaled, do not decrement the count"
|
|
141 |
|
|
142 |
self waitUncounted
|
|
143 |
!
|
|
144 |
|
22081
|
145 |
waitWithTimeoutMs:milliSeconds state:newState
|
|
146 |
"wait for the semaphore, but abort the wait after some time.
|
|
147 |
return the receiver if the semaphore triggered normal, nil if we return
|
|
148 |
due to a timeout.
|
|
149 |
With zero timeout, this can be used to poll a semaphore (returning
|
|
150 |
the receiver if the semaphore is available, nil if not).
|
|
151 |
However, polling is not the intended use of semaphores, though.
|
|
152 |
If milliSeconds is nil, wait without timeout.
|
|
153 |
|
|
154 |
Redefined: once signaled, do not decrement the count"
|
|
155 |
|
23037
|
156 |
<resource: #skipInDebuggersWalkBack>
|
|
157 |
|
23500
|
158 |
^ self waitUncountedWithTimeoutMs:milliSeconds state:newState.
|
22081
|
159 |
|
|
160 |
"Modified: / 24-07-2017 / 21:52:53 / cg"
|
23037
|
161 |
"Modified: / 30-05-2018 / 13:57:25 / Claus Gittinger"
|
23500
|
162 |
"Modified: / 01-11-2018 / 15:40:49 / Stefan Vogel"
|
21189
|
163 |
! !
|
|
164 |
|
|
165 |
!EventSemaphore class methodsFor:'documentation'!
|
|
166 |
|
|
167 |
version
|
|
168 |
^ '$Header$'
|
|
169 |
!
|
|
170 |
|
|
171 |
version_CVS
|
|
172 |
^ '$Header$'
|
|
173 |
! !
|
|
174 |
|