1 martin 1.9 //%LICENSE////////////////////////////////////////////////////////////////
|
2 martin 1.10 //
|
3 martin 1.9 // Licensed to The Open Group (TOG) under one or more contributor license
4 // agreements. Refer to the OpenPegasusNOTICE.txt file distributed with
5 // this work for additional information regarding copyright ownership.
6 // Each contributor licenses this file to you under the OpenPegasus Open
7 // Source License; you may not use this file except in compliance with the
8 // License.
|
9 martin 1.10 //
|
10 martin 1.9 // Permission is hereby granted, free of charge, to any person obtaining a
11 // copy of this software and associated documentation files (the "Software"),
12 // to deal in the Software without restriction, including without limitation
13 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 // and/or sell copies of the Software, and to permit persons to whom the
15 // Software is furnished to do so, subject to the following conditions:
|
16 martin 1.10 //
|
17 martin 1.9 // The above copyright notice and this permission notice shall be included
18 // in all copies or substantial portions of the Software.
|
19 martin 1.10 //
|
20 martin 1.9 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
21 martin 1.10 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
22 martin 1.9 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23 // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
27 martin 1.10 //
|
28 martin 1.9 //////////////////////////////////////////////////////////////////////////
|
29 mike 1.2 //
30 //%/////////////////////////////////////////////////////////////////////////////
31
32 #ifndef Pegasus_AsyncQueue_h
33 #define Pegasus_AsyncQueue_h
34
35 #include <Pegasus/Common/Linkage.h>
36 #include <Pegasus/Common/List.h>
|
37 mike 1.3 #include <Pegasus/Common/Condition.h>
|
38 mike 1.2
39 PEGASUS_NAMESPACE_BEGIN
40
41 /** AsyncQueue implementation (formerly AsyncDQueue).
42 */
|
43 kumpf 1.5 template<class ElemType>
|
44 mike 1.2 class AsyncQueue
45 {
46 public:
47
|
48 kumpf 1.6 /** Constructor.
|
49 mike 1.2 */
|
50 kumpf 1.6 AsyncQueue();
|
51 mike 1.2
52 /** Destructor.
53 */
54 virtual ~AsyncQueue();
55
|
56 kumpf 1.8 /** Close the queue.
|
57 mike 1.2 */
|
58 mike 1.3 void close();
|
59 mike 1.2
60 /** Enqueue an element at the back of queue.
|
61 kumpf 1.8 @param element The element to enqueue.
62 @return True if the element is successfully enqueued, false if the
63 queue is closed.
|
64 mike 1.2 */
|
65 kumpf 1.8 Boolean enqueue(ElemType *element);
|
66 mike 1.2
67 /** Dequeue an element from the front of the queue. Return null immediately
|
68 kumpf 1.8 if queue is empty or closed.
69 @return A pointer to the element that was dequeued, or null if the
70 queue is empty or closed.
|
71 mike 1.2 */
72 ElemType *dequeue();
73
|
74 mike 1.3 /** Dequeue an element from the front of the queue (wait if the queue is
75 empty).
|
76 kumpf 1.8 @return A pointer to the element that was dequeued, or null if the
77 queue is closed (either before or while waiting for an element).
|
78 mike 1.2 */
79 ElemType *dequeue_wait();
80
81 /** Discard all the elements on the list. The size becomes zero afterwards.
82 */
83 void clear();
84
85 /** Return number of element in queue.
86 */
|
87 kumpf 1.6 Uint32 count() const { return _rep.size(); }
|
88 mike 1.2
89 /** Return true is queue is empty (has zero elements).
90 */
|
91 kumpf 1.6 Boolean is_empty() const { return _rep.size() == 0; }
|
92 mike 1.2
|
93 mike 1.3 /** Return true if the queue has been closed (in which case no new
94 elements may be enqueued).
|
95 mike 1.2 */
|
96 mike 1.3 Boolean is_closed() const { return _closed.get(); }
|
97 mike 1.2
98 private:
99
|
100 mike 1.3 Mutex _mutex;
101 Condition _not_empty;
102 AtomicInt _closed;
|
103 mike 1.2 typedef List<ElemType,NullLock> Rep;
104 Rep _rep;
105 };
106
|
107 kumpf 1.5 template<class ElemType>
|
108 kumpf 1.6 AsyncQueue<ElemType>::AsyncQueue() :
109 _mutex(Mutex::NON_RECURSIVE)
|
110 mike 1.2 {
111 }
112
|
113 kumpf 1.5 template<class ElemType>
|
114 mike 1.2 AsyncQueue<ElemType>::~AsyncQueue()
115 {
116 }
117
|
118 kumpf 1.5 template<class ElemType>
|
119 mike 1.3 void AsyncQueue<ElemType>::close()
|
120 mike 1.2 {
|
121 mike 1.3 AutoMutex auto_mutex(_mutex);
|
122 mike 1.2
|
123 mike 1.3 if (!is_closed())
|
124 mike 1.2 {
|
125 mike 1.3 _closed++;
126 _not_empty.signal();
|
127 mike 1.2 }
128 }
129
|
130 kumpf 1.5 template<class ElemType>
|
131 kumpf 1.8 Boolean AsyncQueue<ElemType>::enqueue(ElemType *element)
|
132 mike 1.2 {
|
133 mike 1.3 if (element)
134 {
135 AutoMutex auto_mutex(_mutex);
136
137 if (is_closed())
|
138 kumpf 1.8 {
139 return false;
140 }
|
141 mike 1.3
142 _rep.insert_back(element);
143 _not_empty.signal();
|
144 mike 1.2 }
|
145 kumpf 1.8
146 return true;
|
147 mike 1.2 }
148
|
149 kumpf 1.5 template<class ElemType>
|
150 mike 1.3 void AsyncQueue<ElemType>::clear()
|
151 mike 1.2 {
|
152 mike 1.3 AutoMutex auto_mutex(_mutex);
153 _rep.clear();
|
154 mike 1.2 }
155
|
156 kumpf 1.5 template<class ElemType>
|
157 mike 1.3 ElemType* AsyncQueue<ElemType>::dequeue()
|
158 mike 1.2 {
|
159 mike 1.3 AutoMutex auto_mutex(_mutex);
160
161 if (is_closed())
|
162 kumpf 1.8 {
163 return 0;
164 }
|
165 mike 1.2
|
166 kumpf 1.6 return _rep.remove_front();
|
167 mike 1.2 }
168
|
169 kumpf 1.5 template<class ElemType>
|
170 mike 1.3 ElemType* AsyncQueue<ElemType>::dequeue_wait()
|
171 mike 1.2 {
|
172 mike 1.3 AutoMutex auto_mutex(_mutex);
173
174 while (is_empty())
|
175 mike 1.2 {
|
176 mike 1.3 if (is_closed())
|
177 kumpf 1.8 {
178 return 0;
179 }
|
180 mike 1.2
|
181 mike 1.3 _not_empty.wait(_mutex);
|
182 mike 1.2 }
183
|
184 mike 1.3 if (is_closed())
|
185 kumpf 1.8 {
186 return 0;
187 }
|
188 mike 1.2
|
189 mike 1.3 ElemType* p = _rep.remove_front();
190 PEGASUS_DEBUG_ASSERT(p != 0);
|
191 mike 1.2
|
192 mike 1.3 return p;
|
193 mike 1.2 }
194
195 PEGASUS_NAMESPACE_END
196
197 #endif /* Pegasus_AsyncQueue_h */
|