0
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
|
3 |
All Rights Reserved
|
|
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
|
|
14 |
instanceVariableNames:'accessProtect dataAvailable spaceAvailable'
|
0
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Collections-Ordered'!
|
|
18 |
|
|
19 |
SharedQueue comment:'
|
|
20 |
SharedQueues provide a safe mechanism for processes to communicate.
|
|
21 |
|
|
22 |
See samples in doc/coding.
|
|
23 |
|
14
|
24 |
$Header: /cvs/stx/stx/libbasic2/SharedQueue.st,v 1.5 1994-01-09 21:24:30 claus Exp $
|
0
|
25 |
|
|
26 |
'!
|
|
27 |
|
|
28 |
!SharedQueue methodsFor:'initialization'!
|
|
29 |
|
|
30 |
init:size
|
14
|
31 |
"initialize the receiver for size entries"
|
|
32 |
|
6
|
33 |
super init:size.
|
0
|
34 |
accessProtect := Semaphore forMutualExclusion.
|
|
35 |
dataAvailable := Semaphore new.
|
|
36 |
spaceAvailable := Semaphore new
|
|
37 |
! !
|
|
38 |
|
|
39 |
!SharedQueue methodsFor:'accessing'!
|
|
40 |
|
|
41 |
next
|
|
42 |
"return the next value in the queue; if it its empty, wait 'til
|
|
43 |
something is put into the receiver.
|
|
44 |
When the datum has been removed, signal space-availability to
|
|
45 |
writers"
|
|
46 |
|
|
47 |
|value ok|
|
|
48 |
|
|
49 |
ok := false.
|
|
50 |
[ok] whileFalse:[
|
|
51 |
[tally == 0] whileTrue:[
|
|
52 |
dataAvailable wait
|
|
53 |
].
|
|
54 |
accessProtect critical:[
|
|
55 |
"
|
|
56 |
this check is needed, since another process may
|
|
57 |
have read the value in the meantime ...
|
|
58 |
"
|
|
59 |
tally == 0 ifFalse:[
|
6
|
60 |
value := super next.
|
0
|
61 |
tally == (contentsArray size - 1) ifTrue:[
|
|
62 |
spaceAvailable signal
|
|
63 |
].
|
|
64 |
ok := true
|
|
65 |
]
|
|
66 |
]
|
|
67 |
].
|
|
68 |
^ value
|
|
69 |
!
|
|
70 |
|
|
71 |
nextPut:anObject
|
|
72 |
"enter anObject into the queue; wait for available space, if
|
|
73 |
the queue is full. After the put, signal availablity of a datum
|
|
74 |
to readers."
|
|
75 |
|
1
|
76 |
|ok|
|
0
|
77 |
|
|
78 |
ok := false.
|
|
79 |
[ok] whileFalse:[
|
|
80 |
[tally == contentsArray size] whileTrue:[
|
|
81 |
spaceAvailable wait
|
|
82 |
].
|
|
83 |
accessProtect critical:[
|
|
84 |
tally == contentsArray size ifFalse:[
|
6
|
85 |
super nextPut:anObject.
|
0
|
86 |
tally == 1 ifTrue:[
|
|
87 |
dataAvailable signal
|
|
88 |
].
|
|
89 |
ok := true
|
|
90 |
]
|
|
91 |
]
|
|
92 |
].
|
|
93 |
^ anObject
|
|
94 |
! !
|