author | Claus Gittinger <cg@exept.de> |
Sat, 16 Dec 1995 13:47:44 +0100 | |
changeset 156 | 109b1c9342b2 |
parent 141 | 2804943fc6f0 |
child 251 | 58fc29adb012 |
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 |
|
30 | 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. |
|
50 |
" |
|
51 |
! ! |
|
0 | 52 |
|
53 |
!SharedQueue methodsFor:'accessing'! |
|
54 |
||
55 |
next |
|
56 |
"return the next value in the queue; if it its empty, wait 'til |
|
57 |
something is put into the receiver. |
|
58 |
When the datum has been removed, signal space-availability to |
|
59 |
writers" |
|
60 |
||
61 |
|value ok| |
|
62 |
||
63 |
ok := false. |
|
64 |
[ok] whileFalse:[ |
|
68 | 65 |
[tally == 0] whileTrue:[ |
66 |
dataAvailable wait |
|
67 |
]. |
|
68 |
accessProtect critical:[ |
|
69 |
" |
|
70 |
this check is needed, since another process may |
|
71 |
have read the value in the meantime ... |
|
72 |
" |
|
73 |
tally == 0 ifFalse:[ |
|
74 |
value := super next. |
|
75 |
tally == (contentsArray size - 1) ifTrue:[ |
|
76 |
spaceAvailable signal |
|
77 |
]. |
|
78 |
ok := true |
|
79 |
] |
|
80 |
] |
|
0 | 81 |
]. |
82 |
^ value |
|
83 |
! |
|
84 |
||
85 |
nextPut:anObject |
|
86 |
"enter anObject into the queue; wait for available space, if |
|
87 |
the queue is full. After the put, signal availablity of a datum |
|
88 |
to readers." |
|
89 |
||
1 | 90 |
|ok| |
0 | 91 |
|
92 |
ok := false. |
|
93 |
[ok] whileFalse:[ |
|
68 | 94 |
[tally == contentsArray size] whileTrue:[ |
95 |
spaceAvailable wait |
|
96 |
]. |
|
97 |
accessProtect critical:[ |
|
98 |
tally == contentsArray size ifFalse:[ |
|
99 |
super nextPut:anObject. |
|
100 |
tally == 1 ifTrue:[ |
|
101 |
dataAvailable signal |
|
102 |
]. |
|
103 |
ok := true |
|
104 |
] |
|
105 |
] |
|
0 | 106 |
]. |
107 |
^ 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
|
108 |
! |
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
109 |
|
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
110 |
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
|
111 |
"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
|
112 |
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
|
113 |
|
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
114 |
^ 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
|
115 |
|
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 |
"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
|
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 |
|
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 |
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
|
120 |
"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
|
121 |
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
|
122 |
|
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 |
^ 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
|
124 |
|
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 |
"Modified: 16.12.1995 / 13:47:07 / cg" |
0 | 126 |
! ! |
141 | 127 |
|
128 |
!SharedQueue methodsFor:'initialization'! |
|
129 |
||
130 |
init:size |
|
131 |
"initialize the receiver for size entries" |
|
132 |
||
133 |
super init:size. |
|
134 |
accessProtect := Semaphore forMutualExclusion. |
|
135 |
dataAvailable := Semaphore new. |
|
136 |
spaceAvailable := Semaphore new |
|
137 |
! ! |
|
138 |
||
139 |
!SharedQueue class methodsFor:'documentation'! |
|
140 |
||
141 |
version |
|
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
|
142 |
^ '$Header: /cvs/stx/stx/libbasic2/SharedQueue.st,v 1.13 1995-12-16 12:47:44 cg Exp $' |
141 | 143 |
! ! |