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