author | Claus Gittinger <cg@exept.de> |
Fri, 05 Feb 2010 13:58:13 +0100 | |
changeset 12698 | a4e7529547bc |
parent 11282 | 5e04b4bd310a |
child 14769 | 898e4754340b |
child 18011 | deb0c3355881 |
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 |
]. |
|
196 |
] doWhile:[gotSema isNil]. |
|
197 |
||
198 |
" |
|
199 |
we finally got at least one of our semaphores. |
|
200 |
Now unregister from any semaphore, we are registered on. |
|
201 |
" |
|
202 |
self do:[:eachSema| |
|
203 |
(eachSema == gotSema and:[mustUnregisterAllSemas not]) ifTrue:[ |
|
204 |
"done with registered semaphores" |
|
205 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
206 |
^ gotSema |
|
207 |
]. |
|
208 |
eachSema removeWaitingProcess:currentProcess. |
|
1031 | 209 |
]. |
8569 | 210 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
757 | 211 |
^ gotSema |
212 |
||
769
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
213 |
"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
|
214 |
"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
|
215 |
! |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
216 |
|
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
217 |
waitWithTimeout:seconds |
12698
a4e7529547bc
comment/format in: #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
11282
diff
changeset
|
218 |
"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
|
219 |
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
|
220 |
|
6787
a1703f7cd4fd
waitWithTimeout: protocol as in Semaphore
Claus Gittinger <cg@exept.de>
parents:
6421
diff
changeset
|
221 |
|millis| |
a1703f7cd4fd
waitWithTimeout: protocol as in Semaphore
Claus Gittinger <cg@exept.de>
parents:
6421
diff
changeset
|
222 |
|
a1703f7cd4fd
waitWithTimeout: protocol as in Semaphore
Claus Gittinger <cg@exept.de>
parents:
6421
diff
changeset
|
223 |
seconds notNil ifTrue:[ |
a1703f7cd4fd
waitWithTimeout: protocol as in Semaphore
Claus Gittinger <cg@exept.de>
parents:
6421
diff
changeset
|
224 |
millis := seconds * 1000 |
a1703f7cd4fd
waitWithTimeout: protocol as in Semaphore
Claus Gittinger <cg@exept.de>
parents:
6421
diff
changeset
|
225 |
]. |
a1703f7cd4fd
waitWithTimeout: protocol as in Semaphore
Claus Gittinger <cg@exept.de>
parents:
6421
diff
changeset
|
226 |
^ self waitWithTimeoutMs:millis. |
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 |
"Modified: 15.12.1995 / 23:10:54 / stefan" |
a1703f7cd4fd
waitWithTimeout: protocol as in Semaphore
Claus Gittinger <cg@exept.de>
parents:
6421
diff
changeset
|
229 |
"Modified: 20.8.1997 / 18:33:23 / cg" |
a1703f7cd4fd
waitWithTimeout: protocol as in Semaphore
Claus Gittinger <cg@exept.de>
parents:
6421
diff
changeset
|
230 |
! |
a1703f7cd4fd
waitWithTimeout: protocol as in Semaphore
Claus Gittinger <cg@exept.de>
parents:
6421
diff
changeset
|
231 |
|
a1703f7cd4fd
waitWithTimeout: protocol as in Semaphore
Claus Gittinger <cg@exept.de>
parents:
6421
diff
changeset
|
232 |
waitWithTimeoutMs:milliSeconds |
a1703f7cd4fd
waitWithTimeout: protocol as in Semaphore
Claus Gittinger <cg@exept.de>
parents:
6421
diff
changeset
|
233 |
"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
|
234 |
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
|
235 |
|
8569 | 236 |
|currentProcess gotSema wasBlocked timeoutBlock timeoutOccured mustUnregisterAllSemas| |
769
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
237 |
|
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
238 |
currentProcess := Processor activeProcess. |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
239 |
timeoutOccured := false. |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
240 |
|
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
241 |
wasBlocked := OperatingSystem blockInterrupts. |
1031 | 242 |
[ |
8569 | 243 |
gotSema := self detect:[:eachSema| |
244 |
eachSema checkAndAddWaitingProcess:currentProcess |
|
245 |
] ifNone:[]. |
|
246 |
||
247 |
gotSema isNil ifTrue:[ |
|
248 |
mustUnregisterAllSemas := true. |
|
249 |
(milliSeconds notNil and:[timeoutBlock isNil]) ifTrue:[ |
|
250 |
|now endTime| |
|
769
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
251 |
|
8569 | 252 |
milliSeconds == 0 ifTrue:[ |
253 |
"with zero-timeout, this is a poll" |
|
254 |
self do:[:eachSema | |
|
255 |
eachSema removeWaitingProcess:currentProcess. |
|
256 |
]. |
|
257 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
258 |
^ nil |
|
259 |
]. |
|
260 |
"calculate the end-time" |
|
261 |
now := OperatingSystem getMillisecondTime. |
|
262 |
endTime := OperatingSystem millisecondTimeAdd:now and:milliSeconds. |
|
1031 | 263 |
|
8569 | 264 |
timeoutBlock := [timeoutOccured := true. timeoutBlock := nil. Processor resume:currentProcess]. |
265 |
Processor addTimedBlock:timeoutBlock for:currentProcess atMilliseconds:endTime. |
|
266 |
]. |
|
267 |
[ |
|
268 |
currentProcess suspendWithState:#wait. |
|
269 |
] ifCurtailed:[ |
|
11282
5e04b4bd310a
changed #waitWithTimeoutMs: - comments
Stefan Vogel <sv@exept.de>
parents:
11257
diff
changeset
|
270 |
"interrupts are not blocked when entered through Processor>>#interruptActive" |
11257 | 271 |
OperatingSystem blockInterrupts. |
8569 | 272 |
timeoutBlock notNil ifTrue:[ |
273 |
Processor removeTimedBlock:timeoutBlock. |
|
274 |
timeoutBlock := nil. |
|
275 |
]. |
|
276 |
self do:[:eachSema | |
|
277 |
eachSema removeWaitingProcess:currentProcess. |
|
278 |
]. |
|
279 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
280 |
]. |
|
1031 | 281 |
]. |
8569 | 282 |
] doUntil:[gotSema notNil or:[timeoutOccured]]. |
769
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
283 |
|
8569 | 284 |
timeoutBlock notNil ifTrue:[ |
285 |
Processor removeTimedBlock:timeoutBlock. |
|
286 |
timeoutBlock := nil. |
|
1031 | 287 |
]. |
8569 | 288 |
|
289 |
"we finally got a timeout or at least one of our semaphores. |
|
290 |
Unregister from any semaphore, we are registered with" |
|
291 |
||
292 |
self do:[:eachSema| |
|
293 |
(eachSema == gotSema and:[mustUnregisterAllSemas not]) ifTrue:[ |
|
294 |
"done with registered semaphores" |
|
295 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
296 |
^ gotSema |
|
297 |
]. |
|
298 |
]. |
|
299 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
769
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
300 |
^ gotSema |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
301 |
|
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
302 |
"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
|
303 |
"Modified: 20.8.1997 / 18:33:23 / cg" |
757 | 304 |
! ! |
305 |
||
306 |
!SemaphoreSet class methodsFor:'documentation'! |
|
307 |
||
308 |
version |
|
12698
a4e7529547bc
comment/format in: #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
11282
diff
changeset
|
309 |
^ '$Header: /cvs/stx/stx/libbasic/SemaphoreSet.st,v 1.19 2010-02-05 12:58:13 cg Exp $' |
a4e7529547bc
comment/format in: #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
11282
diff
changeset
|
310 |
! |
a4e7529547bc
comment/format in: #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
11282
diff
changeset
|
311 |
|
a4e7529547bc
comment/format in: #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
11282
diff
changeset
|
312 |
version_CVS |
a4e7529547bc
comment/format in: #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
11282
diff
changeset
|
313 |
^ '$Header: /cvs/stx/stx/libbasic/SemaphoreSet.st,v 1.19 2010-02-05 12:58:13 cg Exp $' |
757 | 314 |
! ! |