author | Stefan Vogel <sv@exept.de> |
Wed, 09 Nov 2005 16:56:10 +0100 | |
changeset 8979 | 869510c58b9c |
parent 8569 | 657853ae04cd |
child 11252 | a8fd089d1655 |
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 |
" |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
12 |
|
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
13 |
|
5570 | 14 |
"{ Package: 'stx:libbasic' }" |
15 |
||
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. |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
59 |
--------------------------------------------------------------- |
1316 | 60 |
[exBegin] |
775
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
61 |
|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
|
62 |
|
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
63 |
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
|
64 |
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
|
65 |
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
|
66 |
|
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
67 |
proc := [ |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
68 |
[ |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
69 |
|ret name| |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
70 |
|
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
71 |
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
|
72 |
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
|
73 |
name := 'sema1' |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
74 |
] ifFalse:[ |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
75 |
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
|
76 |
name := 'sema2' |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
77 |
] |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
78 |
]. |
1422 | 79 |
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
|
80 |
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
|
81 |
proc terminate |
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 |
] loop |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
84 |
] fork. |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
85 |
|
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
86 |
(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
|
87 |
sema1 signal. |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
88 |
(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
|
89 |
sema2 signal. |
1316 | 90 |
[exEnd] |
775
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 |
|
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
93 |
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
|
94 |
of sema1, sema2 to be signalled, or a timeout to occur. |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
95 |
--------------------------------------------------------------- |
1316 | 96 |
[exBegin] |
769
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
97 |
|sema1 sema2 semaSet proc| |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
98 |
|
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
99 |
sema1 := Semaphore new. |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
100 |
sema2 := Semaphore new. |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
101 |
semaSet := SemaphoreSet with:sema1 with:sema2. |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
102 |
|
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
103 |
proc := [ |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
104 |
[ |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
105 |
|ret name| |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
106 |
|
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
107 |
ret := semaSet waitWithTimeout:5. |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
108 |
ret == sema1 ifTrue:[ |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
109 |
name := 'sema1' |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
110 |
] ifFalse:[ |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
111 |
ret == sema2 ifTrue:[ |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
112 |
name := 'sema2' |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
113 |
] ifFalse:[ |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
114 |
name := ret printString |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
115 |
] |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
116 |
]. |
1422 | 117 |
Transcript showCR: name, ' raised'. |
769
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
118 |
ret isNil ifTrue:[ |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
119 |
proc terminate |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
120 |
] |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
121 |
] loop |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
122 |
] fork. |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
123 |
|
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
124 |
(Delay forSeconds:3) wait. |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
125 |
sema1 signal. |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
126 |
(Delay forSeconds:3) wait. |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
127 |
sema2 signal. |
1316 | 128 |
[exEnd] |
775
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
129 |
|
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
130 |
|
769
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
131 |
|
775
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
132 |
the following example forks a process which waits on input |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
133 |
to arrive on any of 2 sharedQueues (with timeout) |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
134 |
The main thread writes data into those queues. |
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
135 |
--------------------------------------------------------------- |
1316 | 136 |
[exBegin] |
775
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
137 |
|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
|
138 |
|
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
139 |
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
|
140 |
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
|
141 |
semaSet := SemaphoreSet with:(q1 readSemaphore) with:(q2 readSemaphore). |
757 | 142 |
|
143 |
proc := [ |
|
144 |
[ |
|
775
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
145 |
|ret whatHappened| |
757 | 146 |
|
775
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
147 |
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
|
148 |
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
|
149 |
Transcript show:'q1 has data: '; show:q1 next; cr. |
757 | 150 |
] ifFalse:[ |
775
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
151 |
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
|
152 |
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
|
153 |
] ifFalse:[ |
1422 | 154 |
Transcript showCR:'timeout' |
757 | 155 |
] |
156 |
]. |
|
157 |
] loop |
|
158 |
] fork. |
|
159 |
||
160 |
(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
|
161 |
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
|
162 |
(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
|
163 |
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
|
164 |
(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
|
165 |
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
|
166 |
(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
|
167 |
proc terminate. |
1316 | 168 |
[exEnd] |
757 | 169 |
" |
170 |
! ! |
|
171 |
||
8569 | 172 |
!SemaphoreSet methodsFor:'waiting'! |
757 | 173 |
|
174 |
wait |
|
775
7b6c00a4708a
documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
175 |
"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
|
176 |
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
|
177 |
|
8569 | 178 |
|currentProcess gotSema wasBlocked mustUnregisterAllSemas| |
757 | 179 |
|
180 |
currentProcess := Processor activeProcess. |
|
8569 | 181 |
mustUnregisterAllSemas := false. |
757 | 182 |
|
183 |
wasBlocked := OperatingSystem blockInterrupts. |
|
184 |
[ |
|
8569 | 185 |
gotSema := self detect:[:eachSema| |
186 |
eachSema checkAndAddWaitingProcess:currentProcess |
|
187 |
] ifNone:[]. |
|
757 | 188 |
|
8569 | 189 |
gotSema isNil ifTrue:[ |
190 |
mustUnregisterAllSemas := true. |
|
191 |
[ |
|
192 |
currentProcess suspendWithState:#wait. |
|
193 |
] ifCurtailed:[ |
|
194 |
self do:[:eachSema | |
|
195 |
eachSema removeWaitingProcess:currentProcess. |
|
196 |
]. |
|
197 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
198 |
]. |
|
199 |
]. |
|
200 |
] doWhile:[gotSema isNil]. |
|
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 |
|
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
221 |
waitWithTimeout:seconds |
770
402958905760
protocol of waitWithTImeOut: changed
Claus Gittinger <cg@exept.de>
parents:
769
diff
changeset
|
222 |
"wait for any of the the semaphore, but abort the wait after some time. |
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 |
|
a1703f7cd4fd
waitWithTimeout: protocol as in Semaphore
Claus Gittinger <cg@exept.de>
parents:
6421
diff
changeset
|
227 |
seconds notNil ifTrue:[ |
a1703f7cd4fd
waitWithTimeout: protocol as in Semaphore
Claus Gittinger <cg@exept.de>
parents:
6421
diff
changeset
|
228 |
millis := seconds * 1000 |
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 |
^ self waitWithTimeoutMs:millis. |
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 |
"Modified: 15.12.1995 / 23:10:54 / stefan" |
a1703f7cd4fd
waitWithTimeout: protocol as in Semaphore
Claus Gittinger <cg@exept.de>
parents:
6421
diff
changeset
|
233 |
"Modified: 20.8.1997 / 18:33:23 / cg" |
a1703f7cd4fd
waitWithTimeout: protocol as in Semaphore
Claus Gittinger <cg@exept.de>
parents:
6421
diff
changeset
|
234 |
! |
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 |
waitWithTimeoutMs:milliSeconds |
a1703f7cd4fd
waitWithTimeout: protocol as in Semaphore
Claus Gittinger <cg@exept.de>
parents:
6421
diff
changeset
|
237 |
"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
|
238 |
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
|
239 |
|
8569 | 240 |
|currentProcess gotSema wasBlocked timeoutBlock timeoutOccured mustUnregisterAllSemas| |
769
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
241 |
|
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
242 |
currentProcess := Processor activeProcess. |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
243 |
timeoutOccured := false. |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
244 |
|
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
245 |
wasBlocked := OperatingSystem blockInterrupts. |
1031 | 246 |
[ |
8569 | 247 |
gotSema := self detect:[:eachSema| |
248 |
eachSema checkAndAddWaitingProcess:currentProcess |
|
249 |
] ifNone:[]. |
|
250 |
||
251 |
gotSema isNil ifTrue:[ |
|
252 |
mustUnregisterAllSemas := true. |
|
253 |
(milliSeconds notNil and:[timeoutBlock isNil]) ifTrue:[ |
|
254 |
|now endTime| |
|
769
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
255 |
|
8569 | 256 |
milliSeconds == 0 ifTrue:[ |
257 |
"with zero-timeout, this is a poll" |
|
258 |
self do:[:eachSema | |
|
259 |
eachSema removeWaitingProcess:currentProcess. |
|
260 |
]. |
|
261 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
262 |
^ nil |
|
263 |
]. |
|
264 |
"calculate the end-time" |
|
265 |
now := OperatingSystem getMillisecondTime. |
|
266 |
endTime := OperatingSystem millisecondTimeAdd:now and:milliSeconds. |
|
1031 | 267 |
|
8569 | 268 |
timeoutBlock := [timeoutOccured := true. timeoutBlock := nil. Processor resume:currentProcess]. |
269 |
Processor addTimedBlock:timeoutBlock for:currentProcess atMilliseconds:endTime. |
|
270 |
]. |
|
271 |
[ |
|
272 |
currentProcess suspendWithState:#wait. |
|
273 |
] ifCurtailed:[ |
|
274 |
timeoutBlock notNil ifTrue:[ |
|
275 |
Processor removeTimedBlock:timeoutBlock. |
|
276 |
timeoutBlock := nil. |
|
277 |
]. |
|
278 |
self do:[:eachSema | |
|
279 |
eachSema removeWaitingProcess:currentProcess. |
|
280 |
]. |
|
281 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
282 |
]. |
|
1031 | 283 |
]. |
8569 | 284 |
] doUntil:[gotSema notNil or:[timeoutOccured]]. |
769
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
285 |
|
8569 | 286 |
timeoutBlock notNil ifTrue:[ |
287 |
Processor removeTimedBlock:timeoutBlock. |
|
288 |
timeoutBlock := nil. |
|
1031 | 289 |
]. |
8569 | 290 |
|
291 |
"we finally got a timeout or at least one of our semaphores. |
|
292 |
Unregister from any semaphore, we are registered with" |
|
293 |
||
294 |
self do:[:eachSema| |
|
295 |
(eachSema == gotSema and:[mustUnregisterAllSemas not]) ifTrue:[ |
|
296 |
"done with registered semaphores" |
|
297 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
298 |
^ gotSema |
|
299 |
]. |
|
300 |
]. |
|
301 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
769
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
302 |
^ gotSema |
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
303 |
|
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
304 |
"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
|
305 |
"Modified: 20.8.1997 / 18:33:23 / cg" |
757 | 306 |
! ! |
307 |
||
308 |
!SemaphoreSet class methodsFor:'documentation'! |
|
309 |
||
310 |
version |
|
8569 | 311 |
^ '$Header: /cvs/stx/stx/libbasic/SemaphoreSet.st,v 1.15 2004-09-21 18:06:25 stefan Exp $' |
757 | 312 |
! ! |