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