#ifndef __SelectableRequestQueue_h_ #define __SelectableRequestQueue_h_ #include "Messages.h" #include "PipeConditionVar.h" /* This is a mixture of a RequestQueue and a PipeConditionVar. * The basic RequeueQueue only allows you to wait until you have a request * in the queue. Using this you can wait for items in the request queue, or * a timeout, or another handle, whichever is ready first. */ class SelectableRequestQueue : public RequestQueue { private: PipeConditionVar _cond; public: SelectableRequestQueue(std::string name) : RequestQueue(name) { } SelectableRequestQueue(std::string name, int logPeriod) : RequestQueue(name, logPeriod) { } // You must add items with this function or the queue will not work right. void newRequest(Request *request); // Use these functions to add this queue to a select() or poll() function. // See the docs for PipeConditionVar for more info. int getWaitHandle() { return _cond.getWaitHandle(); } void resetWaitHandle() { // Call this BEFORE you start to read from the queue. _cond.clear(); } // This is a quick way to to wait for just a timeout or a message in the // queue. This does not look at any other handles. We interpret the timeval // just like select(). It is the number of seconds and microseconds to wait // starting from now, not a specific time. void waitForRequest(timeval const *maxPause) const { // If you see "SelectableRequestQueue::waitForRequest" in the log file, // that comes from waitForRequest() with no arguments. When you call // this function, it appears as "poll" in the logs because // PipeConditionVar::wait() uses a PollSet. _cond.wait(maxPause); } // This is a quick way to use the queue just like a normal RequestQueue. // Without explictly adding this function the compiler sometimes got // confused. void waitForRequest() { RequestQueue::waitForRequest(); } void waitWithTimeout(int seconds, int microseconds = 0) { timeval time; time.tv_sec = seconds; time.tv_usec = microseconds; waitForRequest(&time); resetWaitHandle(); } }; #endif