(file) Return to MessageQueue.cpp CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Common

Diff for /pegasus/src/Pegasus/Common/MessageQueue.cpp between version 1.13 and 1.24

version 1.13, 2001/12/20 00:37:21 version 1.24, 2002/02/20 22:00:51
Line 28 
Line 28 
  
 #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
Line 40 
Line 41 
 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());  
  
     while (!_queueTable.insert(_queueId = _GetNextQueueId(), this))  
        ;  
     q_table_mut.unlock();  
   
 // << Wed Dec 19 16:34:26 2001 mdd >>  
     _async = false;  
  
     if(_async == true)  MessageQueue::MessageQueue(
       const char* name,
       Boolean async,
       Uint32 queueId)
      : _queueId(queueId), _capabilities(0), _count(0), _front(0), _back(0), _async(async)
     {     {
        _workThread.run();      //
     }      // Copy the name:
       //
  
 }     PEG_FUNC_ENTER(TRC_DISPATCHER,"MessageQueue::MessageQueue()");
  
 MessageQueue::~MessageQueue()      if (!name)
 {          name = "";
     // ATTN-A: thread safety!  
     q_table_mut.lock(pegasus_thread_self());  
  
     _queueTable.remove(_queueId);      _name = new char[strlen(name) + 1];
     q_table_mut.unlock();      strcpy(_name, name);
  
     if(_async == true)      Tracer::trace(TRC_DISPATCHER, Tracer::LEVEL3,
     {          "MessageQueue::MessageQueue  name = %s, queueId = %i", name, queueId);
        _workThread.cancel();    // cancel thread  
        _workSemaphore.signal();// wake thread  
        _workThread.join();     // wait for thread to complete  
     }  
  
 }      //
       // Insert into queue table:
       //
  
       q_table_mut.lock(pegasus_thread_self());
  
 PEGASUS_THREAD_RETURN PEGASUS_THREAD_CDECL MessageQueue::workThread(void * arg)      while (!_queueTable.insert(_queueId, this))
 {         ;
         // get thread from argument  
         Thread * thread = (Thread *)arg;  
  
         PEGASUS_ASSERT(thread != 0);      q_table_mut.unlock();
  
         // get message queue from thread  
         MessageQueue * queue = (MessageQueue *)thread->get_parm();  
  
         PEGASUS_ASSERT(queue != 0);     PEG_FUNC_EXIT(TRC_DISPATCHER,"MessageQueue::MessageQueue()");
   }
  
         while(true)  MessageQueue::~MessageQueue()
         {  
                 if(thread->is_cancelled())  
                 {                 {
                         break;      // ATTN-A: thread safety!
                 }  
  
                 thread->sleep(1);      PEG_FUNC_ENTER(TRC_DISPATCHER,"MessageQueue::~MessageQueue()");
                 continue;  
  
                 // wait for work      Tracer::trace(TRC_DISPATCHER, Tracer::LEVEL3,
                 queue->_workSemaphore.wait();          "MessageQueue::~MessageQueue queueId = %i, name = %s", _queueId, _name);
  
                 // stop the thread when the message queue has been destroyed.      q_table_mut.lock(pegasus_thread_self());
                 // 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      _queueTable.remove(_queueId);
                 if(queue->_count != 0)      q_table_mut.unlock();
                 {  
                         queue->handleEnqueue();      // Free the name:
                 }  
         }  
  
         thread->exit_self(PEGASUS_THREAD_RETURN(0));      delete [] _name;
  
         return(0);      PEG_FUNC_EXIT(TRC_DISPATCHER,"MessageQueue::~MessageQueue()");
 } }
  
 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"))
     {     {
Line 164 
Line 144 
        message->print(cout);        message->print(cout);
     }     }
  
   
     _mut.lock(pegasus_thread_self());     _mut.lock(pegasus_thread_self());
     if (_back)     if (_back)
     {     {
Line 181 
Line 162 
     }     }
     message->_owner = this;     message->_owner = this;
     _count++;     _count++;
   
     _mut.unlock();     _mut.unlock();
  
     if( _async == true )  
        _workSemaphore.signal();  
     else  
        handleEnqueue();        handleEnqueue();
   
 } }
  
 Message* MessageQueue::dequeue() throw(IPCException) Message* MessageQueue::dequeue() throw(IPCException)
Line 212 
Line 192 
     return 0;     return 0;
 } }
  
   
   
 void MessageQueue::remove(Message* message) throw(IPCException) void MessageQueue::remove(Message* message) throw(IPCException)
 { {
     if (!message)     if (!message)
Line 318 
Line 300 
  
 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());
  
Line 331 
Line 314 
  
     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());
Line 353 
Line 340 
    }    }
    q_table_mut.unlock();    q_table_mut.unlock();
  
      Tracer::trace(TRC_DISPATCHER, Tracer::LEVEL3,
           "MessageQueue::lookup failure - name = %s", name);
   
    return 0;    return 0;
 } }
  


Legend:
Removed from v.1.13  
changed lines
  Added in v.1.24

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2