version 1.10, 2001/12/17 14:33:01
|
version 1.37, 2003/11/07 19:18:46
|
|
|
//%///////////////////////////////////////////////////////////////////////////// |
//%2003//////////////////////////////////////////////////////////////////////// |
// | // |
// Copyright (c) 2000, 2001 The Open group, BMC Software, Tivoli Systems, IBM |
// Copyright (c) 2000, 2001, 2002 BMC Software, Hewlett-Packard Development |
|
// Company, L. P., IBM Corp., The Open Group, Tivoli Systems. |
|
// Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L. P.; |
|
// IBM Corp.; EMC Corporation, The Open Group. |
// | // |
// Permission is hereby granted, free of charge, to any person obtaining a copy | // Permission is hereby granted, free of charge, to any person obtaining a copy |
// of this software and associated documentation files (the "Software"), to | // of this software and associated documentation files (the "Software"), to |
|
|
| |
#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" |
|
#include "MessageQueueService.h" |
PEGASUS_USING_STD; | PEGASUS_USING_STD; |
| |
PEGASUS_NAMESPACE_BEGIN | PEGASUS_NAMESPACE_BEGIN |
|
|
typedef HashTable<Uint32, MessageQueue*, EqualFunc<Uint32>, HashFunc<Uint32> > | typedef HashTable<Uint32, MessageQueue*, EqualFunc<Uint32>, HashFunc<Uint32> > |
QueueTable; | QueueTable; |
| |
static QueueTable _queueTable(128); |
static QueueTable _queueTable(256); |
static Mutex q_table_mut = Mutex(); |
static Mutex q_table_mut ; |
| |
static Uint32 _GetNextQueueId() throw(IPCException) |
void MessageQueue::remove_myself(Uint32 qid) |
{ | { |
|
q_table_mut.lock(pegasus_thread_self()); |
| |
static Uint32 _queueId = 2; |
_queueTable.remove(qid); |
static Mutex _id_mut = Mutex(); |
q_table_mut.unlock(); |
|
} |
| |
_id_mut.lock(pegasus_thread_self()); |
|
| |
// Handle wrap-around! |
Uint32 MessageQueue::getNextQueueId() throw(IPCException) |
if (_queueId == 0) |
{ |
_queueId = MessageQueue::_CIMOM_Q_ID; |
static Uint32 _nextQueueId = 2; |
Uint32 ret = _queueId++; |
|
_id_mut.unlock(); |
|
| |
return ret; |
// |
} |
// Lock mutex: |
|
// |
|
|
|
static Mutex _id_mut ; |
|
_id_mut.lock(pegasus_thread_self()); |
| |
Uint32 MessageQueue::_CIMOM_Q_ID = 1; |
Uint32 queueId; |
| |
MessageQueue::MessageQueue(const char * name) |
// Assign the next queue ID that is not already in use |
: _mut( ), _count(0), _front(0), _back(0), |
do |
_workThread(MessageQueue::workThread, this, false), |
|
_workSemaphore(0) |
|
{ | { |
if(name != NULL) |
// Handle wrap around and never assign zero or one as a queue id: |
|
if (_nextQueueId == 0) |
{ | { |
strncpy(_name, name, 25); |
_nextQueueId = 2; |
_name[25] = 0x00; |
|
} | } |
else |
|
memset(_name, 0x00,25); |
|
| |
q_table_mut.lock(pegasus_thread_self()); |
queueId = _nextQueueId++; |
|
} while (lookup(queueId) != 0); |
| |
while (!_queueTable.insert(_queueId = _GetNextQueueId(), this)) |
// |
; |
// Unlock mutex: |
q_table_mut.unlock(); |
// |
|
|
|
_id_mut.unlock(); |
| |
_workThread.run(); |
return queueId; |
} | } |
| |
MessageQueue::~MessageQueue() |
|
|
|
|
MessageQueue::MessageQueue( |
|
const char* name, |
|
Boolean async, |
|
Uint32 queueId) |
|
: _queueId(queueId), _capabilities(0), _count(0), _front(0), _back(0), _async(async) |
{ | { |
// ATTN-A: thread safety! |
// |
q_table_mut.lock(pegasus_thread_self()); |
// Copy the name: |
|
// |
| |
_queueTable.remove(_queueId); |
PEG_METHOD_ENTER(TRC_MESSAGEQUEUESERVICE,"MessageQueue::MessageQueue()"); |
q_table_mut.unlock(); |
|
| |
_workThread.cancel(); // cancel thread |
if (!name) |
_workSemaphore.signal();// wake thread |
name = ""; |
_workThread.join(); // wait for thread to complete |
|
} |
|
| |
PEGASUS_THREAD_RETURN PEGASUS_THREAD_CDECL MessageQueue::workThread(void * arg) |
_name = new char[strlen(name) + 1]; |
{ |
strcpy(_name, name); |
// get thread from argument |
|
Thread * thread = (Thread *)arg; |
Tracer::trace(TRC_MESSAGEQUEUESERVICE, Tracer::LEVEL3, |
|
"MessageQueue::MessageQueue name = %s, queueId = %i", name, queueId); |
| |
PEGASUS_ASSERT(thread != 0); |
// |
|
// Insert into queue table: |
|
// |
| |
// get message queue from thread |
q_table_mut.lock(pegasus_thread_self()); |
MessageQueue * queue = (MessageQueue *)thread->get_parm(); |
|
| |
PEGASUS_ASSERT(queue != 0); |
while (!_queueTable.insert(_queueId, this)) |
|
; |
| |
while(true) |
q_table_mut.unlock(); |
{ |
|
thread->sleep(1); |
|
continue; |
|
| |
// wait for work |
|
queue->_workSemaphore.wait(); |
|
| |
// stop the thread when the message queue has been destroyed. |
PEG_METHOD_EXIT(); |
// 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 |
MessageQueue::~MessageQueue() |
if(queue->_count != 0) |
|
{ | { |
queue->handleEnqueue(); |
// ATTN-A: thread safety! |
} |
|
| |
} |
PEG_METHOD_ENTER(TRC_MESSAGEQUEUESERVICE,"MessageQueue::~MessageQueue()"); |
| |
thread->exit_self(PEGASUS_THREAD_RETURN(0)); |
Tracer::trace(TRC_MESSAGEQUEUESERVICE, Tracer::LEVEL3, |
|
"MessageQueue::~MessageQueue queueId = %i, name = %s", _queueId, _name); |
| |
return(0); |
q_table_mut.lock(pegasus_thread_self()); |
} |
|
| |
void MessageQueue::enqueue(Message* message) throw(IPCException) |
_queueTable.remove(_queueId); |
{ |
q_table_mut.unlock(); |
if (!message) |
|
throw NullPointer(); |
// Free the name: |
| |
|
delete [] _name; |
| |
|
PEG_METHOD_EXIT(); |
|
} |
|
|
|
void MessageQueue::enqueue(Message* message) |
|
{ |
|
PEG_METHOD_ENTER(TRC_MESSAGEQUEUESERVICE,"MessageQueue::enqueue()"); |
| |
if (getenv("PEGASUS_TRACE")) |
if (!message) |
{ | { |
cout << "===== " << getQueueName() << ": "; |
Tracer::trace(TRC_MESSAGEQUEUESERVICE, Tracer::LEVEL3, |
message->print(cout); |
"MessageQueue::enqueue failure"); |
|
PEG_METHOD_EXIT(); |
|
throw NullPointer(); |
} | } |
| |
|
PEG_TRACE_STRING( TRC_MESSAGEQUEUESERVICE, Tracer::LEVEL3, |
|
String("Queue name: ") + getQueueName() ) ; |
|
Tracer::trace ( TRC_MESSAGEQUEUESERVICE, |
|
Tracer::LEVEL3, |
|
"Message: [%s, %d]", |
|
MessageTypeToString(message->getType()), |
|
message->getKey() ); |
|
|
_mut.lock(pegasus_thread_self()); | _mut.lock(pegasus_thread_self()); |
if (_back) | if (_back) |
{ | { |
|
|
message->_next = 0; | message->_next = 0; |
} | } |
message->_owner = this; | message->_owner = this; |
|
|
_count++; | _count++; |
_mut.unlock(); |
Tracer::trace(TRC_MESSAGEQUEUESERVICE, Tracer::LEVEL4, |
|
"MessageQueue::enqueue _queueId = %d, _count = %d", _queueId, _count); |
| |
// _workSemaphore.signal(); |
_mut.unlock(); |
| |
handleEnqueue(); | handleEnqueue(); |
|
PEG_METHOD_EXIT(); |
} | } |
| |
Message* MessageQueue::dequeue() throw(IPCException) | Message* MessageQueue::dequeue() throw(IPCException) |
{ | { |
|
PEG_METHOD_ENTER(TRC_MESSAGEQUEUESERVICE,"MessageQueue::dequeue()"); |
|
|
_mut.lock(pegasus_thread_self()); | _mut.lock(pegasus_thread_self()); |
if (_front) | if (_front) |
{ | { |
|
|
| |
if (_back == message) | if (_back == message) |
_back = 0; | _back = 0; |
|
|
_count--; | _count--; |
|
Tracer::trace(TRC_MESSAGEQUEUESERVICE, Tracer::LEVEL4, |
|
"MessageQueue::dequeue _queueId = %d, _count = %d", |
|
_queueId, _count); |
|
|
_mut.unlock(); | _mut.unlock(); |
message->_next = 0; | message->_next = 0; |
message->_prev = 0; | message->_prev = 0; |
message->_owner = 0; | message->_owner = 0; |
|
|
|
PEG_METHOD_EXIT(); |
return message; | return message; |
} | } |
_mut.unlock(); | _mut.unlock(); |
|
|
|
PEG_METHOD_EXIT(); |
return 0; | return 0; |
} | } |
|
; |
|
|
| |
void MessageQueue::remove(Message* message) throw(IPCException) | void MessageQueue::remove(Message* message) throw(IPCException) |
{ | { |
|
PEG_METHOD_ENTER(TRC_MESSAGEQUEUESERVICE,"MessageQueue::remove()"); |
|
|
if (!message) | if (!message) |
|
{ |
|
PEG_METHOD_EXIT(); |
throw NullPointer(); | throw NullPointer(); |
|
} |
| |
if (message->_owner != this) | if (message->_owner != this) |
|
{ |
|
PEG_METHOD_EXIT(); |
throw NoSuchMessageOnQueue(); | throw NoSuchMessageOnQueue(); |
|
} |
| |
_mut.lock(pegasus_thread_self()); | _mut.lock(pegasus_thread_self()); |
| |
|
|
_front = message->_next; | _front = message->_next; |
| |
_count--; | _count--; |
|
Tracer::trace(TRC_MESSAGEQUEUESERVICE, Tracer::LEVEL4, |
|
"MessageQueue::remove _count = %d", _count); |
|
|
_mut.unlock(); | _mut.unlock(); |
| |
message->_prev = 0; | message->_prev = 0; |
message->_next = 0; | message->_next = 0; |
message->_owner = 0; | message->_owner = 0; |
|
|
|
PEG_METHOD_EXIT(); |
} | } |
| |
Message* MessageQueue::findByType(Uint32 type) throw(IPCException) | Message* MessageQueue::findByType(Uint32 type) throw(IPCException) |
|
|
| |
const char* MessageQueue::getQueueName() const | const char* MessageQueue::getQueueName() const |
{ | { |
if(_name[0] != 0x00) |
|
return _name; | return _name; |
return "unknown"; |
|
} | } |
| |
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_MESSAGEQUEUESERVICE, 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()); |
|
|
for(QueueTable::Iterator i = _queueTable.start(); i; i++) | for(QueueTable::Iterator i = _queueTable.start(); i; i++) |
{ | { |
// ATTN: Need to decide how many characters to compare in queue names | // ATTN: Need to decide how many characters to compare in queue names |
if(! strncmp( ((MessageQueue *)i.value())->getQueueName(), name, 25) ) |
if(! strcmp( ((MessageQueue *)i.value())->getQueueName(), name) ) |
{ | { |
q_table_mut.unlock(); | q_table_mut.unlock(); |
return( (MessageQueue *)i.value()); | return( (MessageQueue *)i.value()); |
|
|
} | } |
q_table_mut.unlock(); | q_table_mut.unlock(); |
| |
|
Tracer::trace(TRC_MESSAGEQUEUESERVICE, Tracer::LEVEL3, |
|
"MessageQueue::lookup failure - name = %s", name); |
|
|
return 0; | return 0; |
} | } |
| |