(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.14 and 1.49.2.1

version 1.14, 2001/12/20 02:20:07 version 1.49.2.1, 2006/07/27 23:11:51
Line 1 
Line 1 
 //%/////////////////////////////////////////////////////////////////////////////  //%2006////////////////////////////////////////////////////////////////////////
 // //
 // 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.
   // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
   // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
   // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
   // EMC Corporation; VERITAS Software Corporation; The Open Group.
   // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
   // EMC Corporation; Symantec 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
Line 22 
Line 31 
 // //
 // Author: Mike Brasher (mbrasher@bmc.com) // Author: Mike Brasher (mbrasher@bmc.com)
 // //
 // Modified By:  // Modified By: Amit K Arora, IBM (amita@in.ibm.com) for Bug#1090
   //              Josephine Eskaline Joyce, IBM (jojustin@in.ibm.com) for Bug#2076
   //              David Dillard, VERITAS Software Corp.
   //                  (david.dillard@veritas.com)
   //              Aruran, IBM (ashanmug@in.ibm.com) for Bug# 3475
 // //
 //%///////////////////////////////////////////////////////////////////////////// //%/////////////////////////////////////////////////////////////////////////////
  
 #include <Pegasus/Common/HashTable.h> #include <Pegasus/Common/HashTable.h>
 #include <Pegasus/Common/IPC.h>  #include <Pegasus/Common/Tracer.h>
 #include "MessageQueue.h" #include "MessageQueue.h"
   #include "MessageQueueService.h"
   #include "IDFactory.h"
  
 PEGASUS_USING_STD; PEGASUS_USING_STD;
  
Line 37 
Line 52 
 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)
 { {
       AutoMutex autoMut(q_table_mut);
       _queueTable.remove(qid);
   }
  
    static Uint32 _queueId = 2;  static IDFactory _qidFactory(CIMOM_Q_ID + 1);
    static Mutex _id_mut = Mutex();  
   
    _id_mut.lock(pegasus_thread_self());  
  
    // Handle wrap-around!  Uint32 MessageQueue::getNextQueueId()
    if (_queueId == 0)  {
       _queueId = MessageQueue::_CIMOM_Q_ID;      return _qidFactory.getID();
    Uint32 ret = _queueId++;  }
    _id_mut.unlock();  
  
    return ret;  void MessageQueue::putQueueId(Uint32 queueId)
   {
       if (queueId != CIMOM_Q_ID)
           _qidFactory.putID(queueId);
 } }
  
 Uint32 MessageQueue::_CIMOM_Q_ID = 1;  MessageQueue::MessageQueue(
       const char* name,
       Boolean async,
       Uint32 queueId)
      : _queueId(queueId), _capabilities(0), _count(0), _front(0), _back(0), _async(async)
   {
       //
       // Copy the name:
       //
  
 MessageQueue::MessageQueue(const char * name, Boolean async)      PEG_METHOD_ENTER(TRC_MESSAGEQUEUESERVICE,"MessageQueue::MessageQueue()");
         : _mut( ), _count(0), _front(0), _back(0),  
           _async(async),  
           _workThread(MessageQueue::workThread, this, false),  
           _workSemaphore(0)  
  
       if (!name)
           name = "";
  
 {      _name = new char[strlen(name) + 1];
    if(name != NULL)      strcpy(_name, name);
    {  
       strncpy(_name, name, 25);  
       _name[25] = 0x00;  
    }  
    else  
       memset(_name, 0x00,25);  
  
     q_table_mut.lock(pegasus_thread_self());      Tracer::trace(TRC_MESSAGEQUEUESERVICE, Tracer::LEVEL3,
           "MessageQueue::MessageQueue  name = %s, queueId = %u", name, queueId);
  
     while (!_queueTable.insert(_queueId = _GetNextQueueId(), this))      //
       // Insert into queue table:
       //
       AutoMutex autoMut(q_table_mut);
       while (!_queueTable.insert(_queueId, this))
        ;        ;
     q_table_mut.unlock();  
   
   
     if(_async == true)  
     {  
        _workThread.run();  
     }  
  
       PEG_METHOD_EXIT();
 } }
  
 MessageQueue::~MessageQueue() MessageQueue::~MessageQueue()
 { {
     // ATTN-A: thread safety!     // ATTN-A: thread safety!
     q_table_mut.lock(pegasus_thread_self());      PEG_METHOD_ENTER(TRC_MESSAGEQUEUESERVICE,"MessageQueue::~MessageQueue()");
       Tracer::trace(TRC_MESSAGEQUEUESERVICE, Tracer::LEVEL3,
     _queueTable.remove(_queueId);          "MessageQueue::~MessageQueue queueId = %i, name = %s", _queueId, _name);
     q_table_mut.unlock();  
  
     if(_async == true)  
     {     {
        _workThread.cancel();    // cancel thread          AutoMutex autoMut(q_table_mut);
        _workSemaphore.signal();// wake thread          _queueTable.remove(_queueId);
        _workThread.join();     // wait for thread to complete      } // mutex unlocks here
     }  
  
 }      // Free the name:
  
       delete [] _name;
  
 PEGASUS_THREAD_RETURN PEGASUS_THREAD_CDECL MessageQueue::workThread(void * arg)      while(_front)
 { {
         // get thread from argument         Message* tmp = _front;
         Thread * thread = (Thread *)arg;         _front = _front->_next;
          delete tmp;
         PEGASUS_ASSERT(thread != 0);      }
  
         // get message queue from thread      // Return the queue id.
         MessageQueue * queue = (MessageQueue *)thread->get_parm();  
  
         PEGASUS_ASSERT(queue != 0);      putQueueId(_queueId);
  
         while(true)      PEG_METHOD_EXIT();
         {  
            if(thread->is_cancelled())  
            {  
               break;  
            }            }
  
            // wait for work  void MessageQueue::enqueue(Message* message)
            queue->_workSemaphore.wait();  
   
            // stop the thread when the message queue has been destroyed.  
            // ATTN: should check the thread cancel flag that is not yet exposed!  
            if(MessageQueue::lookup(queue->_queueId) == 0)  
            {            {
               break;      PEG_METHOD_ENTER(TRC_MESSAGEQUEUESERVICE,"MessageQueue::enqueue()");
            }  
  
            // ensure the queue has a message before dispatching      if (!message)
            if(queue->_count != 0)  
            {            {
               queue->handleEnqueue();          Tracer::trace(TRC_MESSAGEQUEUESERVICE, Tracer::LEVEL3,
            }                      "MessageQueue::enqueue failure");
         }          PEG_METHOD_EXIT();
           throw NullPointer();
         thread->exit_self(PEGASUS_THREAD_RETURN(0));  
   
         return(0);  
 } }
  
 void MessageQueue::enqueue(Message* message) throw(IPCException)      PEG_TRACE_STRING( TRC_MESSAGEQUEUESERVICE, Tracer::LEVEL3,
 {                        String("Queue name: ") + getQueueName() ) ;
     if (!message)      Tracer::trace   ( TRC_MESSAGEQUEUESERVICE,
        throw NullPointer();                        Tracer::LEVEL3,
                         "Message: [%s]",
                         MessageTypeToString(message->getType()));
  
     if (getenv("PEGASUS_TRACE"))  
     {     {
        cout << "===== " << getQueueName() << ": ";      AutoMutex autoMut(_mut);
        message->print(cout);  
     }  
   
     _mut.lock(pegasus_thread_self());  
     if (_back)     if (_back)
     {     {
        _back->_next = message;        _back->_next = message;
Line 175 
Line 171 
        message->_next = 0;        message->_next = 0;
     }     }
     message->_owner = this;     message->_owner = this;
   
     _count++;     _count++;
     if( _async == true )      Tracer::trace(TRC_MESSAGEQUEUESERVICE, Tracer::LEVEL4,
     {                    "MessageQueue::enqueue _queueId = %d, _count = %d", _queueId, _count);
        _workSemaphore.signal();  
     }  
  
     _mut.unlock();      } // mutex unlocks here
  
     if(_async == false )  
        handleEnqueue();        handleEnqueue();
       PEG_METHOD_EXIT();
 } }
  
 Message* MessageQueue::dequeue() throw(IPCException)  Message* MessageQueue::dequeue()
 { {
    _mut.lock(pegasus_thread_self());      PEG_METHOD_ENTER(TRC_MESSAGEQUEUESERVICE,"MessageQueue::dequeue()");
   
       AutoMutex autoMut(_mut);
     if (_front)     if (_front)
     {     {
         Message* message = _front;         Message* message = _front;
Line 199 
Line 196 
  
         if (_back == message)         if (_back == message)
             _back = 0;             _back = 0;
   
         _count--;         _count--;
         _mut.unlock();          Tracer::trace(TRC_MESSAGEQUEUESERVICE, Tracer::LEVEL4,
               "MessageQueue::dequeue _queueId = %d, _count = %d",
               _queueId, _count);
   
         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();  
       PEG_METHOD_EXIT();
     return 0;     return 0;
 } }
  
 void MessageQueue::remove(Message* message) throw(IPCException)  
   
   void MessageQueue::remove(Message* message)
 { {
       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());      {
       AutoMutex autoMut(_mut);
  
     if (message->_next)     if (message->_next)
         message->_next->_prev = message->_prev;         message->_next->_prev = message->_prev;
Line 231 
Line 246 
         _front = message->_next;         _front = message->_next;
  
     _count--;     _count--;
     _mut.unlock();      Tracer::trace(TRC_MESSAGEQUEUESERVICE, Tracer::LEVEL4,
          "MessageQueue::remove _count = %d", _count);
   
       } // mutex unlocks here
  
     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)
 { {
    _mut.lock(pegasus_thread_self());      AutoMutex autoMut(_mut);
  
     for (Message* m = front(); m; m = m->getNext())     for (Message* m = front(); m; m = m->getNext())
     {     {
        if (m->getType() == type)        if (m->getType() == type)
        {        {
           _mut.unlock();  
           return m;           return m;
        }        }
     }     }
     _mut.unlock();  
     return 0;  
 }  
   
 Message* MessageQueue::findByKey(Uint32 key) throw(IPCException)  
 {  
    _mut.lock(pegasus_thread_self());  
   
     for (Message* m = front(); m; m = m->getNext())  
     {  
        if (m->getKey() == key)  
        {  
           _mut.unlock();  
           return m;  
        }  
  
     }  
     _mut.unlock();  
     return 0;     return 0;
 } }
  
 void MessageQueue::print(ostream& os) const throw(IPCException)  #ifdef PEGASUS_DEBUG
   void MessageQueue::print(ostream& os) const
 { {
    const_cast<MessageQueue *>(this)->_mut.lock(pegasus_thread_self());      AutoMutex autoMut(const_cast<MessageQueue *>(this)->_mut);
  
    for (const Message* m = front(); m; m = m->getNext())    for (const Message* m = front(); m; m = m->getNext())
         m->print(os);         m->print(os);
    const_cast<MessageQueue *>(this)->_mut.unlock();  
 }  
   
 Message* MessageQueue::find(Uint32 type, Uint32 key) throw(IPCException)  
 {  
    _mut.lock(pegasus_thread_self());  
   
     for (Message* m = front(); m; m = m->getNext())  
     {  
        if (m->getType() == type && m->getKey() == key)  
        {  
           _mut.unlock();  
           return m;  
        }  
     }  
     _mut.unlock();  
   
     return 0;  
 }  
   
 void MessageQueue::lock() throw(IPCException)  
 {  
    _mut.lock(pegasus_thread_self());  
 }  
   
 void MessageQueue::unlock()  
 {  
    _mut.unlock();  
 } }
   #endif
  
 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)
 { {
   
     MessageQueue* queue = 0;     MessageQueue* queue = 0;
     q_table_mut.lock(pegasus_thread_self());      AutoMutex autoMut(q_table_mut);
  
     if (_queueTable.lookup(queueId, queue))     if (_queueTable.lookup(queueId, queue))
     {     {
        q_table_mut.unlock();  
        return queue;        return queue;
     }     }
  
     // Not found!     // Not found!
  
     q_table_mut.unlock();      Tracer::trace(TRC_MESSAGEQUEUESERVICE, Tracer::LEVEL3,
           "MessageQueue::lookup failure queueId = %u", queueId);
  
     return 0;     return 0;
 } }
  
  
 MessageQueue* MessageQueue::lookup(const char *name) throw(IPCException)  MessageQueue* MessageQueue::lookup(const char *name)
 { {
   
    if(name == NULL)    if(name == NULL)
       throw NullPointer();       throw NullPointer();
    q_table_mut.lock(pegasus_thread_self());  
  
       AutoMutex autoMut(q_table_mut);
    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();  
          return( (MessageQueue *)i.value());          return( (MessageQueue *)i.value());
       }       }
   
    }    }
    q_table_mut.unlock();  
       Tracer::trace(TRC_MESSAGEQUEUESERVICE, Tracer::LEVEL3,
                       "MessageQueue::lookup failure - name = %s", name);
  
    return 0;    return 0;
 } }


Legend:
Removed from v.1.14  
changed lines
  Added in v.1.49.2.1

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2