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