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