[6] | 1 | // Copyright (c) 2013 by István Váradi
|
---|
| 2 |
|
---|
| 3 | // This file is part of VSCPL, a simple cross-platform utility library
|
---|
| 4 |
|
---|
| 5 | // Redistribution and use in source and binary forms, with or without
|
---|
| 6 | // modification, are permitted provided that the following conditions are met:
|
---|
| 7 |
|
---|
| 8 | // 1. Redistributions of source code must retain the above copyright notice, this
|
---|
| 9 | // list of conditions and the following disclaimer.
|
---|
| 10 | // 2. Redistributions in binary form must reproduce the above copyright notice,
|
---|
| 11 | // this list of conditions and the following disclaimer in the documentation
|
---|
| 12 | // and/or other materials provided with the distribution.
|
---|
| 13 |
|
---|
| 14 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
---|
| 15 | // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
---|
| 16 | // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
---|
| 17 | // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
---|
| 18 | // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
---|
| 19 | // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
---|
| 20 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
---|
| 21 | // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 22 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
---|
| 23 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 24 |
|
---|
| 25 | // The views and conclusions contained in the software and documentation are those
|
---|
| 26 | // of the authors and should not be interpreted as representing official policies,
|
---|
| 27 | // either expressed or implied, of the FreeBSD Project.
|
---|
| 28 |
|
---|
| 29 | //------------------------------------------------------------------------------
|
---|
| 30 |
|
---|
| 31 | #include "Waiter.h"
|
---|
| 32 |
|
---|
| 33 | #include "Waitable.h"
|
---|
| 34 |
|
---|
| 35 | #include <cassert>
|
---|
| 36 | #include <cerrno>
|
---|
| 37 |
|
---|
| 38 | //------------------------------------------------------------------------------
|
---|
| 39 |
|
---|
| 40 | using hu::varadiistvan::scpl::io::Waiter;
|
---|
| 41 |
|
---|
| 42 | //------------------------------------------------------------------------------
|
---|
| 43 |
|
---|
| 44 | inline bool Waiter::hasReady() const
|
---|
| 45 | {
|
---|
| 46 | for(waitables_t::const_iterator i = waitables.begin(); i!=waitables.end();
|
---|
| 47 | ++i)
|
---|
| 48 | {
|
---|
| 49 | Waitable* waitable = i->second;
|
---|
| 50 | if (waitable->ready()) {
|
---|
| 51 | return true;
|
---|
| 52 | }
|
---|
| 53 | }
|
---|
| 54 | return false;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | //------------------------------------------------------------------------------
|
---|
| 58 |
|
---|
| 59 | inline size_t Waiter::setupPollFDs(pollfd* pollFDs)
|
---|
| 60 | {
|
---|
| 61 | size_t numValid = 0;
|
---|
| 62 |
|
---|
| 63 | for(waitables_t::const_iterator i = waitables.begin(); i!=waitables.end();
|
---|
| 64 | ++i)
|
---|
| 65 | {
|
---|
| 66 | Waitable* waitable = i->second;
|
---|
| 67 | if (waitable->events==0) continue;
|
---|
| 68 |
|
---|
| 69 | pollfd& pollFD = pollFDs[numValid++];
|
---|
| 70 |
|
---|
| 71 | pollFD.fd = waitable->fd;
|
---|
| 72 | pollFD.events = waitable->events;
|
---|
| 73 | pollFD.revents = 0;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | return numValid;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | //------------------------------------------------------------------------------
|
---|
| 80 |
|
---|
| 81 | inline void Waiter::processPollFDs(const pollfd* pollFDs, size_t size)
|
---|
| 82 | {
|
---|
| 83 | for(size_t i = 0; i<size; ++i) {
|
---|
| 84 | const pollfd& pollFD = pollFDs[i];
|
---|
| 85 | if (pollFD.revents!=0) {
|
---|
| 86 | waitables_t::iterator j = waitables.find(pollFD.fd);
|
---|
| 87 | assert(j!=waitables.end());
|
---|
| 88 | Waitable* waitable = j->second;
|
---|
| 89 | waitable->handleEvents(pollFD.revents);
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | //------------------------------------------------------------------------------
|
---|
| 95 |
|
---|
| 96 | Waiter::~Waiter()
|
---|
| 97 | {
|
---|
| 98 | for(waitables_t::iterator i = waitables.begin(); i!=waitables.end(); ++i)
|
---|
| 99 | {
|
---|
| 100 | Waitable* waitable = i->second;
|
---|
| 101 | waitable->waiter = 0;
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | //------------------------------------------------------------------------------
|
---|
| 106 |
|
---|
| 107 | bool Waiter::wait(int timeout)
|
---|
| 108 | {
|
---|
| 109 | if (hasReady()) return true;
|
---|
| 110 |
|
---|
| 111 | pollfd pollFDs[numWaitables];
|
---|
| 112 | size_t numValid = setupPollFDs(pollFDs);
|
---|
| 113 |
|
---|
| 114 | int result = poll(pollFDs, numValid, timeout);
|
---|
| 115 |
|
---|
| 116 | if (result<0) {
|
---|
| 117 | setErrorCode(errno);
|
---|
| 118 | return false;
|
---|
| 119 | } else if (result>0) {
|
---|
| 120 | processPollFDs(pollFDs, numValid);
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | return hasReady();
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | //------------------------------------------------------------------------------
|
---|
| 127 |
|
---|
| 128 | void Waiter::add(Waitable* waitable)
|
---|
| 129 | {
|
---|
| 130 | assert(waitable->fd>=0);
|
---|
| 131 | assert(waitables.find(waitable->fd)==waitables.end());
|
---|
| 132 | waitables[waitable->fd] = waitable;
|
---|
| 133 | ++numWaitables;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | //------------------------------------------------------------------------------
|
---|
| 137 |
|
---|
| 138 | void Waiter::remove(Waitable* waitable)
|
---|
| 139 | {
|
---|
| 140 | assert(numWaitables>0);
|
---|
| 141 | assert(waitables.find(waitable->fd)!=waitables.end());
|
---|
| 142 | assert(waitables[waitable->fd]==waitable);
|
---|
| 143 | waitables.erase(waitable->fd);
|
---|
| 144 | --numWaitables;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | //------------------------------------------------------------------------------
|
---|
| 148 |
|
---|
| 149 | // Local Variables:
|
---|
| 150 | // mode: C++
|
---|
| 151 | // c-basic-offset: 4
|
---|
| 152 | // indent-tabs-mode: nil
|
---|
| 153 | // End:
|
---|