// Copyright (c) 2013 by István Váradi // This file is part of VSCPL, a simple cross-platform utility library // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: // 1. Redistributions of source code must retain the above copyright notice, this // list of conditions and the following disclaimer. // 2. Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // The views and conclusions contained in the software and documentation are those // of the authors and should not be interpreted as representing official policies, // either expressed or implied, of the FreeBSD Project. //------------------------------------------------------------------------------ #include "Waiter.h" #include "Waitable.h" #include #include //------------------------------------------------------------------------------ using hu::varadiistvan::scpl::io::Waiter; //------------------------------------------------------------------------------ inline bool Waiter::hasReady() const { for(waitables_t::const_iterator i = waitables.begin(); i!=waitables.end(); ++i) { Waitable* waitable = i->second; if (waitable->ready()) { return true; } } return false; } //------------------------------------------------------------------------------ inline size_t Waiter::setupPollFDs(pollfd* pollFDs) { size_t numValid = 0; for(waitables_t::const_iterator i = waitables.begin(); i!=waitables.end(); ++i) { Waitable* waitable = i->second; if (waitable->events==0) continue; pollfd& pollFD = pollFDs[numValid++]; pollFD.fd = waitable->fd; pollFD.events = waitable->events; pollFD.revents = 0; } return numValid; } //------------------------------------------------------------------------------ inline void Waiter::processPollFDs(const pollfd* pollFDs, size_t size) { for(size_t i = 0; isecond; waitable->handleEvents(pollFD.revents); } } } //------------------------------------------------------------------------------ Waiter::~Waiter() { for(waitables_t::iterator i = waitables.begin(); i!=waitables.end(); ++i) { Waitable* waitable = i->second; waitable->waiter = 0; } } //------------------------------------------------------------------------------ bool Waiter::wait(int timeout) { if (hasReady()) return true; pollfd pollFDs[numWaitables]; size_t numValid = setupPollFDs(pollFDs); int result = poll(pollFDs, numValid, timeout); if (result<0) { setErrorCode(errno); return false; } else if (result>0) { processPollFDs(pollFDs, numValid); } return hasReady(); } //------------------------------------------------------------------------------ void Waiter::add(Waitable* waitable) { assert(waitable->fd>=0); assert(waitables.find(waitable->fd)==waitables.end()); waitables[waitable->fd] = waitable; ++numWaitables; } //------------------------------------------------------------------------------ void Waiter::remove(Waitable* waitable) { assert(numWaitables>0); assert(waitables.find(waitable->fd)!=waitables.end()); assert(waitables[waitable->fd]==waitable); waitables.erase(waitable->fd); --numWaitables; } //------------------------------------------------------------------------------ // Local Variables: // mode: C++ // c-basic-offset: 4 // indent-tabs-mode: nil // End: