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 |
|
|
13 |
Object subclass:#SharedQueue
|
|
14 |
instanceVariableNames:'contentsArray readPosition writePosition
|
|
15 |
tally
|
|
16 |
accessProtect dataAvailable spaceAvailable'
|
|
17 |
classVariableNames:''
|
|
18 |
poolDictionaries:''
|
|
19 |
category:'Collections-Ordered'!
|
|
20 |
|
|
21 |
SharedQueue comment:'
|
|
22 |
SharedQueues provide a safe mechanism for processes to communicate.
|
|
23 |
|
|
24 |
See samples in doc/coding.
|
|
25 |
|
3
|
26 |
$Header: /cvs/stx/stx/libbasic2/SharedQueue.st,v 1.3 1993-10-13 00:18:13 claus Exp $
|
0
|
27 |
|
|
28 |
'!
|
|
29 |
|
|
30 |
!SharedQueue class methodsFor:'instance creation'!
|
|
31 |
|
|
32 |
new
|
|
33 |
^ self new:10
|
|
34 |
!
|
|
35 |
|
|
36 |
new:size
|
|
37 |
^ super new init:size
|
|
38 |
! !
|
|
39 |
|
|
40 |
!SharedQueue methodsFor:'initialization'!
|
|
41 |
|
|
42 |
init:size
|
|
43 |
contentsArray := Array new:size.
|
|
44 |
readPosition := writePosition := 1.
|
|
45 |
tally := 0.
|
|
46 |
accessProtect := Semaphore forMutualExclusion.
|
|
47 |
dataAvailable := Semaphore new.
|
|
48 |
spaceAvailable := Semaphore new
|
|
49 |
! !
|
|
50 |
|
|
51 |
!SharedQueue methodsFor:'accessing'!
|
|
52 |
|
|
53 |
next
|
|
54 |
"return the next value in the queue; if it its empty, wait 'til
|
|
55 |
something is put into the receiver.
|
|
56 |
When the datum has been removed, signal space-availability to
|
|
57 |
writers"
|
|
58 |
|
|
59 |
|value ok|
|
|
60 |
|
|
61 |
ok := false.
|
|
62 |
[ok] whileFalse:[
|
|
63 |
[tally == 0] whileTrue:[
|
|
64 |
dataAvailable wait
|
|
65 |
].
|
|
66 |
accessProtect critical:[
|
|
67 |
"
|
|
68 |
this check is needed, since another process may
|
|
69 |
have read the value in the meantime ...
|
|
70 |
"
|
|
71 |
tally == 0 ifFalse:[
|
|
72 |
value := contentsArray at:readPosition.
|
|
73 |
readPosition := readPosition + 1.
|
|
74 |
readPosition > contentsArray size ifTrue:[
|
|
75 |
readPosition := 1
|
|
76 |
].
|
|
77 |
tally := tally - 1.
|
|
78 |
tally == (contentsArray size - 1) ifTrue:[
|
|
79 |
spaceAvailable signal
|
|
80 |
].
|
|
81 |
ok := true
|
|
82 |
]
|
|
83 |
]
|
|
84 |
].
|
|
85 |
^ value
|
|
86 |
!
|
|
87 |
|
|
88 |
nextPut:anObject
|
|
89 |
"enter anObject into the queue; wait for available space, if
|
|
90 |
the queue is full. After the put, signal availablity of a datum
|
|
91 |
to readers."
|
|
92 |
|
1
|
93 |
|ok|
|
0
|
94 |
|
|
95 |
ok := false.
|
|
96 |
[ok] whileFalse:[
|
|
97 |
[tally == contentsArray size] whileTrue:[
|
|
98 |
spaceAvailable wait
|
|
99 |
].
|
|
100 |
accessProtect critical:[
|
|
101 |
tally == contentsArray size ifFalse:[
|
|
102 |
contentsArray at:writePosition put:anObject.
|
|
103 |
writePosition := writePosition + 1.
|
|
104 |
writePosition > contentsArray size ifTrue:[
|
|
105 |
writePosition := 1
|
|
106 |
].
|
|
107 |
tally := tally + 1.
|
|
108 |
tally == 1 ifTrue:[
|
|
109 |
dataAvailable signal
|
|
110 |
].
|
|
111 |
ok := true
|
|
112 |
]
|
|
113 |
]
|
|
114 |
].
|
|
115 |
^ anObject
|
|
116 |
! !
|
|
117 |
|
|
118 |
!SharedQueue methodsFor:'queries'!
|
|
119 |
|
1
|
120 |
size
|
|
121 |
"return the number of elements in the shared queue"
|
0
|
122 |
|
|
123 |
^ tally
|
|
124 |
! !
|