PLearn 0.1
|
A class for polled IO with PStreams. More...
#include <Poll.h>
Public Member Functions | |
void | setStreamsToWatch (const vector< PStream > &streams) |
int | waitForEvents (uint32_t timeout=0, bool shuffle_events_=false) |
PStream | getNextPendingEvent () |
Protected Attributes | |
vector< PStream > | m_streams_to_watch |
The PStream's to watch for IO. | |
vector< PRPollDesc > | m_poll_descriptors |
The underlying structure used by NSPR's PR_Poll to notify us of pending IO. | |
unsigned int | m_next_unexamined_event |
Counter used to iterate through the m_poll_descriptors in getNextPendingEvent. | |
int | last_n_poll_events |
bool | shuffle_events |
TVec< int > | shuffled_index |
A class for polled IO with PStreams.
Currently supports only PrPStreamBufs.
PStream PLearn::Poll::getNextPendingEvent | ( | ) |
Definition at line 113 of file Poll.cc.
Referenced by PLearn::PLearnService::watchServers().
{ while (m_next_unexamined_event < m_poll_descriptors.size()) { int i = m_next_unexamined_event++; if(shuffle_events) i= shuffled_index[i]; if ((last_n_poll_events > 0 && m_poll_descriptors[i].out_flags & PR_POLL_READ) || !m_streams_to_watch[i]->inbufEmpty()) return m_streams_to_watch[i]; } PLERROR("Poll::getNextPendingEvent: called with no more pending events!"); // We never reach this because of the PLERROR. Used to silence // a gcc warning. return PStream(); }
void PLearn::Poll::setStreamsToWatch | ( | const vector< PStream > & | streams | ) |
Definition at line 54 of file Poll.cc.
References i, PLearn::PrPStreamBuf::in, and PLERROR.
Referenced by PLearn::PLearnService::watchServers().
{ m_streams_to_watch.clear(); m_poll_descriptors.resize(streams.size()); int i = 0; for (vector<PStream>::const_iterator it = streams.begin(); it != streams.end(); ++it, ++i) { PStreamBuf* st = *it; PrPStreamBuf* pr_st = dynamic_cast<PrPStreamBuf*>(st); if (!pr_st) PLERROR("Poll::setStreamsToWatch: only PrPStreamBuf streams supported!"); m_streams_to_watch.push_back(*it); m_poll_descriptors[i].fd = pr_st->in; m_poll_descriptors[i].in_flags = PR_POLL_READ; } }
Definition at line 74 of file Poll.cc.
References PLearn::getPrErrorString(), i, PLERROR, and PLearn::shuffleElements().
Referenced by PLearn::PLearnService::watchServers().
{ if (m_poll_descriptors.size() == 0) PLERROR("Poll::waitforEvents: called with no streams to watch."); shuffle_events= shuffle_events_; if(shuffle_events)//shuffle index vec if necessary { shuffled_index= TVec<int>(0, m_poll_descriptors.size()-1, 1); shuffleElements(shuffled_index); } m_next_unexamined_event = 0; //first, check for non-empty buffers (ready to read) int nevents= 0; for(unsigned int i= 0; i < m_poll_descriptors.size(); ++i) if(!m_streams_to_watch[i]->inbufEmpty()) ++nevents; if(nevents > 0)//if we already have some events, poll w/ no wait timeout= PR_INTERVAL_NO_WAIT; //poll underlying streams last_n_poll_events= PR_Poll(&m_poll_descriptors[0], PRIntn(m_poll_descriptors.size()), timeout); if(last_n_poll_events < 0) PLERROR((string("Poll::waitForEvents: poll error: ") + getPrErrorString()).c_str()); nevents= 0;// now count _all_ events (non-empty buffers + stream polling) for(unsigned int i= 0; i < m_poll_descriptors.size(); ++i) if ((last_n_poll_events > 0 && m_poll_descriptors[i].out_flags & PR_POLL_READ) || !m_streams_to_watch[i]->inbufEmpty()) ++nevents; return nevents; }
int PLearn::Poll::last_n_poll_events [protected] |
unsigned int PLearn::Poll::m_next_unexamined_event [protected] |
vector<PRPollDesc> PLearn::Poll::m_poll_descriptors [protected] |
vector<PStream> PLearn::Poll::m_streams_to_watch [protected] |
bool PLearn::Poll::shuffle_events [protected] |
TVec<int> PLearn::Poll::shuffled_index [protected] |