PLearn 0.1
Public Member Functions | Protected Attributes
PLearn::Poll Class Reference

A class for polled IO with PStreams. More...

#include <Poll.h>

Collaboration diagram for PLearn::Poll:
Collaboration graph
[legend]

List of all members.

Public Member Functions

void setStreamsToWatch (const vector< PStream > &streams)
int waitForEvents (uint32_t timeout=0, bool shuffle_events_=false)
PStream getNextPendingEvent ()

Protected Attributes

vector< PStreamm_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< intshuffled_index

Detailed Description

A class for polled IO with PStreams.

Currently supports only PrPStreamBufs.

Definition at line 60 of file Poll.h.


Member Function Documentation

PStream PLearn::Poll::getNextPendingEvent ( )

Definition at line 113 of file Poll.cc.

References i, and PLERROR.

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();
}

Here is the caller graph for this function:

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;
    }

}

Here is the caller graph for this function:

int PLearn::Poll::waitForEvents ( uint32_t  timeout = 0,
bool  shuffle_events_ = false 
)

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;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 81 of file Poll.h.

Counter used to iterate through the m_poll_descriptors in getNextPendingEvent.

Definition at line 79 of file Poll.h.

vector<PRPollDesc> PLearn::Poll::m_poll_descriptors [protected]

The underlying structure used by NSPR's PR_Poll to notify us of pending IO.

Definition at line 75 of file Poll.h.

The PStream's to watch for IO.

Definition at line 71 of file Poll.h.

Definition at line 82 of file Poll.h.

Definition at line 83 of file Poll.h.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines