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