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