version 1.7, 2008/09/16 18:37:03
|
version 1.8, 2008/09/17 18:47:22
|
|
|
#include <Pegasus/Common/Linkage.h> | #include <Pegasus/Common/Linkage.h> |
#include <Pegasus/Common/List.h> | #include <Pegasus/Common/List.h> |
#include <Pegasus/Common/Condition.h> | #include <Pegasus/Common/Condition.h> |
#include <Pegasus/Common/IPCExceptions.h> |
|
| |
PEGASUS_NAMESPACE_BEGIN | PEGASUS_NAMESPACE_BEGIN |
| |
|
|
*/ | */ |
virtual ~AsyncQueue(); | virtual ~AsyncQueue(); |
| |
/** Close the queue so that subsequent enqueue() and dequeue() requests |
/** Close the queue. |
result in ListClosed() exceptions. |
|
*/ | */ |
void close(); | void close(); |
| |
/** Enqueue an element at the back of queue. | /** Enqueue an element at the back of queue. |
|
@param element The element to enqueue. |
|
@return True if the element is successfully enqueued, false if the |
|
queue is closed. |
*/ | */ |
void enqueue(ElemType *element); |
Boolean enqueue(ElemType *element); |
| |
/** Dequeue an element from the front of the queue. Return null immediately | /** Dequeue an element from the front of the queue. Return null immediately |
if queue is empty. |
if queue is empty or closed. |
|
@return A pointer to the element that was dequeued, or null if the |
|
queue is empty or closed. |
*/ | */ |
ElemType *dequeue(); | ElemType *dequeue(); |
| |
/** Dequeue an element from the front of the queue (wait if the queue is | /** Dequeue an element from the front of the queue (wait if the queue is |
empty). | empty). |
|
@return A pointer to the element that was dequeued, or null if the |
|
queue is closed (either before or while waiting for an element). |
*/ | */ |
ElemType *dequeue_wait(); | ElemType *dequeue_wait(); |
| |
|
|
} | } |
| |
template<class ElemType> | template<class ElemType> |
void AsyncQueue<ElemType>::enqueue(ElemType *element) |
Boolean AsyncQueue<ElemType>::enqueue(ElemType *element) |
{ | { |
if (element) | if (element) |
{ | { |
AutoMutex auto_mutex(_mutex); | AutoMutex auto_mutex(_mutex); |
| |
if (is_closed()) | if (is_closed()) |
throw ListClosed(); |
{ |
|
return false; |
|
} |
| |
_rep.insert_back(element); | _rep.insert_back(element); |
_not_empty.signal(); | _not_empty.signal(); |
} | } |
|
|
|
return true; |
} | } |
| |
template<class ElemType> | template<class ElemType> |
|
|
AutoMutex auto_mutex(_mutex); | AutoMutex auto_mutex(_mutex); |
| |
if (is_closed()) | if (is_closed()) |
throw ListClosed(); |
{ |
|
return 0; |
|
} |
| |
return _rep.remove_front(); | return _rep.remove_front(); |
} | } |
|
|
while (is_empty()) | while (is_empty()) |
{ | { |
if (is_closed()) | if (is_closed()) |
throw ListClosed(); |
{ |
|
return 0; |
|
} |
| |
_not_empty.wait(_mutex); | _not_empty.wait(_mutex); |
} | } |
| |
if (is_closed()) | if (is_closed()) |
throw ListClosed(); |
{ |
|
return 0; |
|
} |
| |
ElemType* p = _rep.remove_front(); | ElemType* p = _rep.remove_front(); |
PEGASUS_DEBUG_ASSERT(p != 0); | PEGASUS_DEBUG_ASSERT(p != 0); |