version 1.14, 2001/12/20 02:20:07
|
version 1.22, 2002/02/11 01:17:41
|
|
|
| |
#include <Pegasus/Common/HashTable.h> | #include <Pegasus/Common/HashTable.h> |
#include <Pegasus/Common/IPC.h> | #include <Pegasus/Common/IPC.h> |
|
#include <Pegasus/Common/Tracer.h> |
#include "MessageQueue.h" | #include "MessageQueue.h" |
| |
PEGASUS_USING_STD; | PEGASUS_USING_STD; |
|
|
static QueueTable _queueTable(128); | static QueueTable _queueTable(128); |
static Mutex q_table_mut = Mutex(); | static Mutex q_table_mut = Mutex(); |
| |
static Uint32 _GetNextQueueId() throw(IPCException) |
Uint32 MessageQueue::getNextQueueId() throw(IPCException) |
{ | { |
|
static Uint32 _nextQueueId = 2; |
| |
static Uint32 _queueId = 2; |
// |
static Mutex _id_mut = Mutex(); |
// Lock mutex: |
|
// |
| |
|
static Mutex _id_mut = Mutex(); |
_id_mut.lock(pegasus_thread_self()); | _id_mut.lock(pegasus_thread_self()); |
| |
// Handle wrap-around! |
// Assign next queue id. Handle wrap around and never assign zero as |
if (_queueId == 0) |
// a queue id: |
_queueId = MessageQueue::_CIMOM_Q_ID; |
|
Uint32 ret = _queueId++; |
|
_id_mut.unlock(); |
|
| |
return ret; |
if (_nextQueueId == 0) |
} |
_nextQueueId = 2; |
| |
Uint32 MessageQueue::_CIMOM_Q_ID = 1; |
Uint32 queueId = _nextQueueId++; |
| |
MessageQueue::MessageQueue(const char * name, Boolean async) |
// |
: _mut( ), _count(0), _front(0), _back(0), |
// Unlock mutex: |
_async(async), |
// |
_workThread(MessageQueue::workThread, this, false), |
|
_workSemaphore(0) |
|
| |
|
_id_mut.unlock(); |
| |
{ |
return queueId; |
if(name != NULL) |
|
{ |
|
strncpy(_name, name, 25); |
|
_name[25] = 0x00; |
|
} | } |
else |
|
memset(_name, 0x00,25); |
|
| |
q_table_mut.lock(pegasus_thread_self()); |
MessageQueue::MessageQueue( |
|
const char* name, |
|
Boolean async, |
|
Uint32 queueId) |
|
: _queueId(queueId), _count(0), _front(0), _back(0), _async(async) |
|
{ |
|
// |
|
// Copy the name: |
|
// |
| |
while (!_queueTable.insert(_queueId = _GetNextQueueId(), this)) |
PEG_FUNC_ENTER(TRC_DISPATCHER,"MessageQueue::MessageQueue()"); |
; |
|
q_table_mut.unlock(); |
|
| |
|
if (!name) |
|
name = ""; |
| |
if(_async == true) |
_name = new char[strlen(name) + 1]; |
{ |
strcpy(_name, name); |
_workThread.run(); |
|
} |
|
| |
} |
Tracer::trace(TRC_DISPATCHER, Tracer::LEVEL3, |
|
"MessageQueue::MessageQueue name = %s, queueId = %i", name, queueId); |
|
|
|
// |
|
// Insert into queue table: |
|
// |
| |
MessageQueue::~MessageQueue() |
|
{ |
|
// ATTN-A: thread safety! |
|
q_table_mut.lock(pegasus_thread_self()); | q_table_mut.lock(pegasus_thread_self()); |
| |
_queueTable.remove(_queueId); |
while (!_queueTable.insert(_queueId, this)) |
|
; |
|
|
q_table_mut.unlock(); | q_table_mut.unlock(); |
| |
if(_async == true) |
|
{ |
|
_workThread.cancel(); // cancel thread |
|
_workSemaphore.signal();// wake thread |
|
_workThread.join(); // wait for thread to complete |
|
} |
|
| |
|
PEG_FUNC_EXIT(TRC_DISPATCHER,"MessageQueue::MessageQueue()"); |
} | } |
| |
|
MessageQueue::~MessageQueue() |
PEGASUS_THREAD_RETURN PEGASUS_THREAD_CDECL MessageQueue::workThread(void * arg) |
|
{ | { |
// get thread from argument |
// ATTN-A: thread safety! |
Thread * thread = (Thread *)arg; |
|
| |
PEGASUS_ASSERT(thread != 0); |
PEG_FUNC_ENTER(TRC_DISPATCHER,"MessageQueue::~MessageQueue()"); |
| |
// get message queue from thread |
Tracer::trace(TRC_DISPATCHER, Tracer::LEVEL3, |
MessageQueue * queue = (MessageQueue *)thread->get_parm(); |
"MessageQueue::~MessageQueue queueId = %i, name = %s", _queueId, _name); |
| |
PEGASUS_ASSERT(queue != 0); |
q_table_mut.lock(pegasus_thread_self()); |
| |
while(true) |
_queueTable.remove(_queueId); |
{ |
q_table_mut.unlock(); |
if(thread->is_cancelled()) |
|
{ |
|
break; |
|
} |
|
| |
// wait for work |
// Free the name: |
queue->_workSemaphore.wait(); |
|
| |
// stop the thread when the message queue has been destroyed. |
delete [] _name; |
// ATTN: should check the thread cancel flag that is not yet exposed! |
|
if(MessageQueue::lookup(queue->_queueId) == 0) |
|
{ |
|
break; |
|
} |
|
| |
// ensure the queue has a message before dispatching |
PEG_FUNC_EXIT(TRC_DISPATCHER,"MessageQueue::~MessageQueue()"); |
if(queue->_count != 0) |
|
{ |
|
queue->handleEnqueue(); |
|
} |
|
} |
|
|
|
thread->exit_self(PEGASUS_THREAD_RETURN(0)); |
|
|
|
return(0); |
|
} | } |
| |
void MessageQueue::enqueue(Message* message) throw(IPCException) | void MessageQueue::enqueue(Message* message) throw(IPCException) |
{ | { |
|
|
if (!message) | if (!message) |
|
{ |
|
Tracer::trace(TRC_DISPATCHER, Tracer::LEVEL3, |
|
"MessageQueue::enqueue failure"); |
throw NullPointer(); | throw NullPointer(); |
|
} |
| |
if (getenv("PEGASUS_TRACE")) | if (getenv("PEGASUS_TRACE")) |
{ | { |
|
|
} | } |
message->_owner = this; | message->_owner = this; |
_count++; | _count++; |
if( _async == true ) |
|
{ |
|
_workSemaphore.signal(); |
|
} |
|
| |
_mut.unlock(); | _mut.unlock(); |
| |
if(_async == false ) |
|
handleEnqueue(); | handleEnqueue(); |
|
|
} | } |
| |
|
|
Message* MessageQueue::dequeue() throw(IPCException) | Message* MessageQueue::dequeue() throw(IPCException) |
{ | { |
_mut.lock(pegasus_thread_self()); | _mut.lock(pegasus_thread_self()); |
|
|
| |
MessageQueue* MessageQueue::lookup(Uint32 queueId) throw(IPCException) | MessageQueue* MessageQueue::lookup(Uint32 queueId) throw(IPCException) |
{ | { |
|
|
MessageQueue* queue = 0; | MessageQueue* queue = 0; |
q_table_mut.lock(pegasus_thread_self()); | q_table_mut.lock(pegasus_thread_self()); |
| |
|
|
| |
q_table_mut.unlock(); | q_table_mut.unlock(); |
| |
|
Tracer::trace(TRC_DISPATCHER, Tracer::LEVEL3, |
|
"MessageQueue::lookup failure queueId = %i", queueId); |
|
|
return 0; | return 0; |
} | } |
| |
| |
MessageQueue* MessageQueue::lookup(const char *name) throw(IPCException) | MessageQueue* MessageQueue::lookup(const char *name) throw(IPCException) |
{ | { |
|
|
if(name == NULL) | if(name == NULL) |
throw NullPointer(); | throw NullPointer(); |
q_table_mut.lock(pegasus_thread_self()); | q_table_mut.lock(pegasus_thread_self()); |
|
|
} | } |
q_table_mut.unlock(); | q_table_mut.unlock(); |
| |
|
Tracer::trace(TRC_DISPATCHER, Tracer::LEVEL3, |
|
"MessageQueue::lookup failure - name = %s", name); |
|
|
return 0; | return 0; |
} | } |
| |