author | Stefan Vogel <sv@exept.de> |
Wed, 11 Mar 2020 10:09:01 +0100 | |
changeset 25334 | 8f44c9352333 |
parent 24357 | cd704e0c423f |
permissions | -rw-r--r-- |
24197 | 1 |
"{ Encoding: utf8 }" |
2 |
||
775
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
3 |
" |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
4 |
COPYRIGHT (c) 1995 by Stefan Vogel / Claus Gittinger |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
5 |
All Rights Reserved |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
6 |
|
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
7 |
This software is furnished under a license and may be used |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
8 |
only in accordance with the terms of that license and with the |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
9 |
inclusion of the above copyright notice. This software may not |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
10 |
be provided or otherwise made available to, or used by, any |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
11 |
other person. No title to or ownership of the software is |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
12 |
hereby transferred. |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
13 |
" |
5570 | 14 |
"{ Package: 'stx:libbasic' }" |
15 |
||
20185 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
757 | 18 |
IdentitySet subclass:#SemaphoreSet |
19 |
instanceVariableNames:'' |
|
20 |
classVariableNames:'' |
|
21 |
poolDictionaries:'' |
|
22 |
category:'Kernel-Processes' |
|
23 |
! |
|
24 |
||
25 |
!SemaphoreSet class methodsFor:'documentation'! |
|
26 |
||
775
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
27 |
copyright |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
28 |
" |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
29 |
COPYRIGHT (c) 1995 by Stefan Vogel / Claus Gittinger |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
30 |
All Rights Reserved |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
31 |
|
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
32 |
This software is furnished under a license and may be used |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
33 |
only in accordance with the terms of that license and with the |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
34 |
inclusion of the above copyright notice. This software may not |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
35 |
be provided or otherwise made available to, or used by, any |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
36 |
other person. No title to or ownership of the software is |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
37 |
hereby transferred. |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
38 |
" |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
39 |
|
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
40 |
! |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
41 |
|
757 | 42 |
documentation |
43 |
" |
|
2876
fb3fed7470be
oops - must unregister from individual semaphores
Claus Gittinger <cg@exept.de>
parents:
1801
diff
changeset
|
44 |
SemaphoreSets allow waiting until one of several semaphores becomes available. |
fb3fed7470be
oops - must unregister from individual semaphores
Claus Gittinger <cg@exept.de>
parents:
1801
diff
changeset
|
45 |
They provide a waiting protocol which is compatible to Semaphore, |
fb3fed7470be
oops - must unregister from individual semaphores
Claus Gittinger <cg@exept.de>
parents:
1801
diff
changeset
|
46 |
i.e. #wait and #waitWithTimeOut. |
1273 | 47 |
|
48 |
[see also:] |
|
49 |
Semaphore |
|
50 |
Process ProcessorScheduler |
|
1294 | 51 |
|
52 |
[author:] |
|
53 |
Stefan Vogel |
|
757 | 54 |
" |
55 |
! |
|
56 |
||
775
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
57 |
examples |
769
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
58 |
" |
775
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
59 |
the following example forks a process which waits on any |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
60 |
of sema1, sema2 to be signalled. The main thread signals those. |
1316 | 61 |
[exBegin] |
775
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
62 |
|sema1 sema2 semaSet proc| |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
63 |
|
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
64 |
sema1 := Semaphore new. |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
65 |
sema2 := Semaphore new. |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
66 |
semaSet := SemaphoreSet with:sema1 with:sema2. |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
67 |
|
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
68 |
proc := [ |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
69 |
[ |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
70 |
|ret name| |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
71 |
|
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
72 |
ret := semaSet wait. |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
73 |
ret == sema1 ifTrue:[ |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
74 |
name := 'sema1' |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
75 |
] ifFalse:[ |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
76 |
ret == sema2 ifTrue:[ |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
77 |
name := 'sema2' |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
78 |
] |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
79 |
]. |
1422 | 80 |
Transcript showCR: name, ' raised'. |
775
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
81 |
ret == sema2 ifTrue:[ |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
82 |
proc terminate |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
83 |
] |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
84 |
] loop |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
85 |
] fork. |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
86 |
|
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
87 |
(Delay forSeconds:3) wait. |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
88 |
sema1 signal. |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
89 |
(Delay forSeconds:3) wait. |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
90 |
sema2 signal. |
1316 | 91 |
[exEnd] |
775
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
92 |
|
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
93 |
|
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
94 |
the following example forks a process which waits on any |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
95 |
of sema1, sema2 to be signalled, or a timeout to occur. |
1316 | 96 |
[exBegin] |
769
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
97 |
|sema1 sema2 semaSet proc| |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
98 |
|
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
99 |
sema1 := Semaphore new. |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
100 |
sema2 := Semaphore new. |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
101 |
semaSet := SemaphoreSet with:sema1 with:sema2. |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
102 |
|
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
103 |
proc := [ |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
104 |
[ |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
105 |
|ret name| |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
106 |
|
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
107 |
ret := semaSet waitWithTimeout:5. |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
108 |
ret == sema1 ifTrue:[ |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
109 |
name := 'sema1' |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
110 |
] ifFalse:[ |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
111 |
ret == sema2 ifTrue:[ |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
112 |
name := 'sema2' |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
113 |
] ifFalse:[ |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
114 |
name := ret printString |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
115 |
] |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
116 |
]. |
1422 | 117 |
Transcript showCR: name, ' raised'. |
769
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
118 |
ret isNil ifTrue:[ |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
119 |
proc terminate |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
120 |
] |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
121 |
] loop |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
122 |
] fork. |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
123 |
|
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
124 |
(Delay forSeconds:3) wait. |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
125 |
sema1 signal. |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
126 |
(Delay forSeconds:3) wait. |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
127 |
sema2 signal. |
1316 | 128 |
[exEnd] |
775
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
129 |
|
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
130 |
|
769
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
131 |
|
775
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
132 |
the following example forks a process which waits on input |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
133 |
to arrive on any of 2 sharedQueues (with timeout) |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
134 |
The main thread writes data into those queues. |
1316 | 135 |
[exBegin] |
775
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
136 |
|q1 q2 semaSet proc| |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
137 |
|
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
138 |
q1 := SharedQueue new. |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
139 |
q2 := SharedQueue new. |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
140 |
semaSet := SemaphoreSet with:(q1 readSemaphore) with:(q2 readSemaphore). |
757 | 141 |
|
142 |
proc := [ |
|
143 |
[ |
|
775
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
144 |
|ret whatHappened| |
757 | 145 |
|
775
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
146 |
ret := semaSet waitWithTimeout:5. |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
147 |
ret == q1 readSemaphore ifTrue:[ |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
148 |
Transcript show:'q1 has data: '; show:q1 next; cr. |
757 | 149 |
] ifFalse:[ |
775
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
150 |
ret == q2 readSemaphore ifTrue:[ |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
151 |
Transcript show:'q2 has data: '; show:q2 next; cr. |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
152 |
] ifFalse:[ |
1422 | 153 |
Transcript showCR:'timeout' |
757 | 154 |
] |
155 |
]. |
|
156 |
] loop |
|
157 |
] fork. |
|
158 |
||
159 |
(Delay forSeconds:3) wait. |
|
775
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
160 |
q1 nextPut:'one'. |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
161 |
(Delay forSeconds:2) wait. |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
162 |
q1 nextPut:'two'. |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
163 |
(Delay forSeconds:2) wait. |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
164 |
q1 nextPut:'three'. |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
165 |
(Delay forSeconds:6) wait. |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
166 |
proc terminate. |
1316 | 167 |
[exEnd] |
757 | 168 |
" |
169 |
! ! |
|
170 |
||
8569 | 171 |
!SemaphoreSet methodsFor:'waiting'! |
757 | 172 |
|
173 |
wait |
|
775
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
174 |
"wait for any of the semaphores in the set to be signalled. |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
175 |
Return the (first) semaphore which is triggered." |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
176 |
|
8569 | 177 |
|currentProcess gotSema wasBlocked mustUnregisterAllSemas| |
757 | 178 |
|
179 |
currentProcess := Processor activeProcess. |
|
8569 | 180 |
mustUnregisterAllSemas := false. |
757 | 181 |
|
182 |
wasBlocked := OperatingSystem blockInterrupts. |
|
183 |
[ |
|
8569 | 184 |
gotSema := self detect:[:eachSema| |
185 |
eachSema checkAndAddWaitingProcess:currentProcess |
|
186 |
] ifNone:[]. |
|
757 | 187 |
|
8569 | 188 |
gotSema isNil ifTrue:[ |
189 |
mustUnregisterAllSemas := true. |
|
190 |
[ |
|
191 |
currentProcess suspendWithState:#wait. |
|
192 |
] ifCurtailed:[ |
|
11257 | 193 |
OperatingSystem blockInterrupts. |
8569 | 194 |
self do:[:eachSema | |
195 |
eachSema removeWaitingProcess:currentProcess. |
|
196 |
]. |
|
197 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
198 |
]. |
|
199 |
]. |
|
16287 | 200 |
|
201 |
gotSema isNil |
|
202 |
] whileTrue. |
|
8569 | 203 |
|
204 |
" |
|
205 |
we finally got at least one of our semaphores. |
|
206 |
Now unregister from any semaphore, we are registered on. |
|
207 |
" |
|
208 |
self do:[:eachSema| |
|
209 |
(eachSema == gotSema and:[mustUnregisterAllSemas not]) ifTrue:[ |
|
210 |
"done with registered semaphores" |
|
211 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
212 |
^ gotSema |
|
213 |
]. |
|
214 |
eachSema removeWaitingProcess:currentProcess. |
|
1031 | 215 |
]. |
8569 | 216 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
757 | 217 |
^ gotSema |
218 |
||
769
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
219 |
"Modified: 15.12.1995 / 23:10:07 / stefan" |
2876
fb3fed7470be
oops - must unregister from individual semaphores
Claus Gittinger <cg@exept.de>
parents:
1801
diff
changeset
|
220 |
"Modified: 20.8.1997 / 18:33:09 / cg" |
769
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
221 |
! |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
222 |
|
21505 | 223 |
waitWithTimeout:secondsOrNilOrTimeDuration |
20185 | 224 |
"wait for any of the semaphore, but abort the wait after some time (seconds). |
24357 | 225 |
Return the (first) triggered semaphore if any, nil if we return due to a timeout. |
226 |
||
227 |
The argument may be a time duration or the number of seconds as integer |
|
228 |
or float (i.e. use 0.1 for a 100ms timeout). |
|
229 |
With zero timeout, this can be used to poll a semaphore (returning |
|
230 |
the receiver if the semaphore is available, nil if not). |
|
231 |
However, polling is not the intended use of semaphores, though. |
|
232 |
If the argument is nil, wait without timeout (forever)." |
|
770
402958905760
protocol of waitWithTImeOut: changed
Claus Gittinger <cg@exept.de>
parents:
769
diff
changeset
|
233 |
|
6787
a1703f7cd4fd
waitWithTimeout: protocol as in Semaphore
Claus Gittinger <cg@exept.de>
parents:
6421
diff
changeset
|
234 |
|millis| |
a1703f7cd4fd
waitWithTimeout: protocol as in Semaphore
Claus Gittinger <cg@exept.de>
parents:
6421
diff
changeset
|
235 |
|
21505 | 236 |
secondsOrNilOrTimeDuration notNil ifTrue:[ |
237 |
secondsOrNilOrTimeDuration isNumber ifTrue:[ |
|
24197 | 238 |
millis := (secondsOrNilOrTimeDuration * 1000) asInteger. |
21505 | 239 |
] ifFalse:[ |
24197 | 240 |
"a TimeDuration" |
241 |
millis := secondsOrNilOrTimeDuration asTruncatedMilliseconds. |
|
21505 | 242 |
]. |
6787
a1703f7cd4fd
waitWithTimeout: protocol as in Semaphore
Claus Gittinger <cg@exept.de>
parents:
6421
diff
changeset
|
243 |
]. |
a1703f7cd4fd
waitWithTimeout: protocol as in Semaphore
Claus Gittinger <cg@exept.de>
parents:
6421
diff
changeset
|
244 |
^ self waitWithTimeoutMs:millis. |
a1703f7cd4fd
waitWithTimeout: protocol as in Semaphore
Claus Gittinger <cg@exept.de>
parents:
6421
diff
changeset
|
245 |
|
21505 | 246 |
"Modified: / 20-08-1997 / 18:33:23 / cg" |
247 |
"Modified: / 21-02-2017 / 14:48:48 / stefan" |
|
24197 | 248 |
"Modified: / 27-05-2019 / 21:45:55 / Stefan Vogel" |
24357 | 249 |
"Modified (comment): / 25-06-2019 / 14:21:40 / Claus Gittinger" |
6787
a1703f7cd4fd
waitWithTimeout: protocol as in Semaphore
Claus Gittinger <cg@exept.de>
parents:
6421
diff
changeset
|
250 |
! |
a1703f7cd4fd
waitWithTimeout: protocol as in Semaphore
Claus Gittinger <cg@exept.de>
parents:
6421
diff
changeset
|
251 |
|
a1703f7cd4fd
waitWithTimeout: protocol as in Semaphore
Claus Gittinger <cg@exept.de>
parents:
6421
diff
changeset
|
252 |
waitWithTimeoutMs:milliSeconds |
20185 | 253 |
"wait for any of the semaphore, but abort the wait after some time. |
6787
a1703f7cd4fd
waitWithTimeout: protocol as in Semaphore
Claus Gittinger <cg@exept.de>
parents:
6421
diff
changeset
|
254 |
Return the (first) triggered semaphore if any, nil if we return due to a timeout." |
a1703f7cd4fd
waitWithTimeout: protocol as in Semaphore
Claus Gittinger <cg@exept.de>
parents:
6421
diff
changeset
|
255 |
|
20255 | 256 |
|currentProcess gotSema wasBlocked timeoutBlock timeoutOccurred mustUnregisterAllSemas| |
769
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
257 |
|
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
258 |
currentProcess := Processor activeProcess. |
20255 | 259 |
timeoutOccurred := false. |
769
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
260 |
|
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
261 |
wasBlocked := OperatingSystem blockInterrupts. |
1031 | 262 |
[ |
8569 | 263 |
gotSema := self detect:[:eachSema| |
264 |
eachSema checkAndAddWaitingProcess:currentProcess |
|
265 |
] ifNone:[]. |
|
266 |
||
267 |
gotSema isNil ifTrue:[ |
|
268 |
mustUnregisterAllSemas := true. |
|
20255 | 269 |
(milliSeconds notNil and:[timeoutBlock isNil]) ifTrue:[ |
8569 | 270 |
|now endTime| |
769
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
271 |
|
8569 | 272 |
milliSeconds == 0 ifTrue:[ |
273 |
"with zero-timeout, this is a poll" |
|
274 |
self do:[:eachSema | |
|
275 |
eachSema removeWaitingProcess:currentProcess. |
|
276 |
]. |
|
277 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
278 |
^ nil |
|
279 |
]. |
|
280 |
"calculate the end-time" |
|
281 |
now := OperatingSystem getMillisecondTime. |
|
282 |
endTime := OperatingSystem millisecondTimeAdd:now and:milliSeconds. |
|
1031 | 283 |
|
14769 | 284 |
timeoutBlock := [ |
20255 | 285 |
timeoutOccurred := true. |
286 |
timeoutBlock := nil. |
|
14769 | 287 |
Processor resume:currentProcess. |
288 |
]. |
|
8569 | 289 |
Processor addTimedBlock:timeoutBlock for:currentProcess atMilliseconds:endTime. |
290 |
]. |
|
291 |
[ |
|
292 |
currentProcess suspendWithState:#wait. |
|
293 |
] ifCurtailed:[ |
|
11282
5e04b4bd310a
changed #waitWithTimeoutMs: - comments
Stefan Vogel <sv@exept.de>
parents:
11257
diff
changeset
|
294 |
"interrupts are not blocked when entered through Processor>>#interruptActive" |
11257 | 295 |
OperatingSystem blockInterrupts. |
8569 | 296 |
timeoutBlock notNil ifTrue:[ |
297 |
Processor removeTimedBlock:timeoutBlock. |
|
298 |
timeoutBlock := nil. |
|
299 |
]. |
|
300 |
self do:[:eachSema | |
|
301 |
eachSema removeWaitingProcess:currentProcess. |
|
302 |
]. |
|
303 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
304 |
]. |
|
1031 | 305 |
]. |
16287 | 306 |
|
20255 | 307 |
gotSema isNil and:[timeoutOccurred not] |
16287 | 308 |
] whileTrue. |
769
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
309 |
|
8569 | 310 |
timeoutBlock notNil ifTrue:[ |
311 |
Processor removeTimedBlock:timeoutBlock. |
|
312 |
timeoutBlock := nil. |
|
1031 | 313 |
]. |
8569 | 314 |
|
315 |
"we finally got a timeout or at least one of our semaphores. |
|
316 |
Unregister from any semaphore, we are registered with" |
|
317 |
||
318 |
self do:[:eachSema| |
|
319 |
(eachSema == gotSema and:[mustUnregisterAllSemas not]) ifTrue:[ |
|
320 |
"done with registered semaphores" |
|
321 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
322 |
^ gotSema |
|
323 |
]. |
|
324 |
]. |
|
325 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
769
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
326 |
^ gotSema |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
327 |
|
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
328 |
"Modified: 15.12.1995 / 23:10:54 / stefan" |
2876
fb3fed7470be
oops - must unregister from individual semaphores
Claus Gittinger <cg@exept.de>
parents:
1801
diff
changeset
|
329 |
"Modified: 20.8.1997 / 18:33:23 / cg" |
757 | 330 |
! ! |
331 |
||
332 |
!SemaphoreSet class methodsFor:'documentation'! |
|
333 |
||
334 |
version |
|
20185 | 335 |
^ '$Header$' |
12698
a4e7529547bc
comment/format in: #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
11282
diff
changeset
|
336 |
! |
a4e7529547bc
comment/format in: #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
11282
diff
changeset
|
337 |
|
a4e7529547bc
comment/format in: #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
11282
diff
changeset
|
338 |
version_CVS |
20185 | 339 |
^ '$Header$' |
757 | 340 |
! ! |
14769 | 341 |