author | Claus Gittinger <cg@exept.de> |
Sun, 05 Jan 1997 21:23:36 +0100 | |
changeset 469 | 9b4318b56d9d |
parent 396 | 88bd6136ee67 |
child 485 | fe9c2ff10461 |
permissions | -rw-r--r-- |
0 | 1 |
" |
2 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
68 | 3 |
All Rights Reserved |
0 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
||
6 | 13 |
Queue subclass:#SharedQueue |
156
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
14 |
instanceVariableNames:'accessProtect dataAvailable spaceAvailable' |
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
15 |
classVariableNames:'' |
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
16 |
poolDictionaries:'' |
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
17 |
category:'Kernel-Processes' |
141 | 18 |
! |
0 | 19 |
|
469 | 20 |
!SharedQueue class methodsFor:'documentation'! |
0 | 21 |
|
30 | 22 |
copyright |
23 |
" |
|
24 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
68 | 25 |
All Rights Reserved |
30 | 26 |
|
27 |
This software is furnished under a license and may be used |
|
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
0 | 35 |
|
30 | 36 |
documentation |
37 |
" |
|
38 |
SharedQueues provide a safe mechanism for processes to communicate. |
|
39 |
They are basically Queues, with added secure access to the internals, |
|
40 |
allowing use from multiple processes (i.e. the access methods use |
|
41 |
critical regions to protect against confusion due to a process |
|
42 |
switch within a modification). |
|
43 |
||
44 |
Also, sharedQueues can be used for synchronization, since a reading |
|
45 |
process will be blocked when attempting to read an empty queue, while |
|
46 |
a writer will be blocked when attempting to write into a full queue. |
|
47 |
For nonBlocking read, use #isEmpty; for nonBlocking write, use #isFull. |
|
48 |
||
49 |
See samples in doc/coding. |
|
251 | 50 |
|
257 | 51 |
[author:] |
52 |
Claus Gittinger |
|
53 |
||
251 | 54 |
[see also:] |
55 |
Semaphore |
|
56 |
Process |
|
469 | 57 |
CodingExamples::SharedQueueExamples |
30 | 58 |
" |
59 |
! ! |
|
0 | 60 |
|
61 |
!SharedQueue methodsFor:'accessing'! |
|
62 |
||
63 |
next |
|
64 |
"return the next value in the queue; if it its empty, wait 'til |
|
65 |
something is put into the receiver. |
|
66 |
When the datum has been removed, signal space-availability to |
|
67 |
writers" |
|
68 |
||
69 |
|value ok| |
|
70 |
||
71 |
ok := false. |
|
72 |
[ok] whileFalse:[ |
|
68 | 73 |
[tally == 0] whileTrue:[ |
74 |
dataAvailable wait |
|
75 |
]. |
|
76 |
accessProtect critical:[ |
|
77 |
" |
|
78 |
this check is needed, since another process may |
|
79 |
have read the value in the meantime ... |
|
80 |
" |
|
81 |
tally == 0 ifFalse:[ |
|
82 |
value := super next. |
|
83 |
tally == (contentsArray size - 1) ifTrue:[ |
|
84 |
spaceAvailable signal |
|
85 |
]. |
|
86 |
ok := true |
|
87 |
] |
|
88 |
] |
|
0 | 89 |
]. |
90 |
^ value |
|
91 |
! |
|
92 |
||
93 |
nextPut:anObject |
|
94 |
"enter anObject into the queue; wait for available space, if |
|
95 |
the queue is full. After the put, signal availablity of a datum |
|
96 |
to readers." |
|
97 |
||
1 | 98 |
|ok| |
0 | 99 |
|
100 |
ok := false. |
|
101 |
[ok] whileFalse:[ |
|
68 | 102 |
[tally == contentsArray size] whileTrue:[ |
103 |
spaceAvailable wait |
|
104 |
]. |
|
105 |
accessProtect critical:[ |
|
106 |
tally == contentsArray size ifFalse:[ |
|
107 |
super nextPut:anObject. |
|
108 |
tally == 1 ifTrue:[ |
|
109 |
dataAvailable signal |
|
110 |
]. |
|
111 |
ok := true |
|
112 |
] |
|
113 |
] |
|
0 | 114 |
]. |
115 |
^ anObject |
|
156
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
116 |
! |
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
117 |
|
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
118 |
readSemaphore |
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
119 |
"return the semaphore which is signalled when data is available |
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
120 |
for reading." |
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
121 |
|
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
122 |
^ dataAvailable |
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
123 |
|
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
124 |
"Modified: 16.12.1995 / 13:47:11 / cg" |
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
125 |
! |
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
126 |
|
396 | 127 |
removeLast |
128 |
"return the last value in the queue; if it its empty, wait 'til |
|
129 |
something is put into the receiver. |
|
130 |
When the datum has been removed, signal space-availability to |
|
131 |
writers" |
|
132 |
||
133 |
|value ok| |
|
134 |
||
135 |
ok := false. |
|
136 |
[ok] whileFalse:[ |
|
137 |
[tally == 0] whileTrue:[ |
|
138 |
dataAvailable wait |
|
139 |
]. |
|
140 |
accessProtect critical:[ |
|
141 |
" |
|
142 |
this check is needed, since another process may |
|
143 |
have read the value in the meantime ... |
|
144 |
" |
|
145 |
tally == 0 ifFalse:[ |
|
146 |
value := super removeLast. |
|
147 |
tally == (contentsArray size - 1) ifTrue:[ |
|
148 |
spaceAvailable signal |
|
149 |
]. |
|
150 |
ok := true |
|
151 |
] |
|
152 |
] |
|
153 |
]. |
|
154 |
^ value |
|
155 |
||
156 |
"Created: 22.6.1996 / 18:50:12 / cg" |
|
157 |
! |
|
158 |
||
156
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
159 |
writeSemaphore |
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
160 |
"return the semaphore which is signalled when the queue has space |
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
161 |
for writing." |
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
162 |
|
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
163 |
^ spaceAvailable |
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
164 |
|
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
165 |
"Modified: 16.12.1995 / 13:47:07 / cg" |
0 | 166 |
! ! |
141 | 167 |
|
168 |
!SharedQueue methodsFor:'initialization'! |
|
169 |
||
170 |
init:size |
|
171 |
"initialize the receiver for size entries" |
|
172 |
||
173 |
super init:size. |
|
174 |
accessProtect := Semaphore forMutualExclusion. |
|
175 |
dataAvailable := Semaphore new. |
|
176 |
spaceAvailable := Semaphore new |
|
177 |
! ! |
|
178 |
||
469 | 179 |
!SharedQueue class methodsFor:'documentation'! |
141 | 180 |
|
181 |
version |
|
469 | 182 |
^ '$Header: /cvs/stx/stx/libbasic2/SharedQueue.st,v 1.17 1997-01-05 20:23:36 cg Exp $' |
141 | 183 |
! ! |