00001 00002 // 00003 // CommandQueue.cc 00004 // ------- 00005 // 00006 // Created on: Dec 13, 2011 00007 // Andrei Spataru : aspataru@cern.ch 00009 00010 #include "EventFilter/ResourceBroker/interface/CommandQueue.h" 00011 00012 #include <iostream> 00013 00014 using namespace evf::rb_statemachine; 00015 00016 using std::cout; 00017 using std::endl; 00018 00019 CommandQueue::CommandQueue() { 00020 sem_init(&lock_, 0, 1); 00021 // commands semaphore initialized to 0 00022 // no commands on the queue initially 00023 sem_init(&commandsSem_, 0, 0); 00024 } 00025 00026 CommandQueue::~CommandQueue() { 00027 } 00028 00029 void CommandQueue::enqEvent(EventPtr evType) { 00030 lock(); 00031 eventQueue_.push(evType); 00032 sem_post(&commandsSem_); 00033 unlock(); 00034 } 00035 00036 EventPtr CommandQueue::deqEvent() { 00037 sem_wait(&commandsSem_); 00038 EventPtr eventPtr; 00039 00040 lock(); 00041 eventPtr = eventQueue_.front(); 00042 eventQueue_.pop(); 00043 unlock(); 00044 00045 return eventPtr; 00046 } 00047 00048 void CommandQueue::lock() { 00049 while (0 != sem_wait(&lock_)) { 00050 cout << "SMEventScheduler: cannot obtain lock!" << endl; 00051 sleep(1); 00052 } 00053 } 00054 00055 void CommandQueue::unlock() { 00056 sem_post(&lock_); 00057 }