1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
|
/*******************************************************************************
* Goggles Audio Player Library *
********************************************************************************
* Copyright (C) 2010-2021 by Sander Jansen. All Rights Reserved *
* --- *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see http://www.gnu.org/licenses. *
********************************************************************************/
#include "ap_defs.h"
#include "ap_reactor.h"
#ifdef _WIN32
#include <windows.h>
#else
#if defined(__linux__)
#define HAVE_PPOLL // On Linux we have ppoll
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <signal.h>
#endif
#include <poll.h>
#include <errno.h>
#endif
namespace ap {
Reactor::Reactor() : pfds(nullptr),nfds(0),mfds(0), timers(nullptr) {
}
Reactor::~Reactor() {
freeElms(pfds);
/// Delete all inputs
for (FXint i=0;i<inputs.no();i++){
delete inputs[i];
}
inputs.clear();
/// Delete all deferred
for (FXint i=0;i<deferred.no();i++){
delete deferred[i];
}
deferred.clear();
/// Delete all timers
while(timers) {
Timer * n = timers->next;
delete timers;
timers=n;
}
}
#ifdef DEBUG
void Reactor::debug() {
int ntimers = 0;
for (Timer * t=timers;t;t=t->next) ntimers++;
fxmessage("[reactor] timers=%d inputs=%ld deferred=%ld\n",ntimers,inputs.no(),deferred.no());
}
#endif
void Reactor::dispatch() {
#ifdef _WIN32
if (result>=WAIT_OBJECT_0 && result<(WAIT_OBJECT_0+nfds)) {
FXint obj = 0;
for (FXint i=0;i<inputs.no();i++) {
if ((inputs[i]->mode&Input::Disabled) || (inputs[i]->mode&(Input::Readable|Input::Writable|Input::Exception))==0) {
continue;
}
if (obj>=(result-WAIT_OBJECT_0) {
if (WaitForSingleObject(pfds[obj],0)==WAIT_OBJECT_0) {
inputs[obj]->mode|=Input::IsReadable|Input::Writable;
inputs[obj]->onSignal();
}
}
obj++;
}
}
#else
FXint i;
for (i=inputs.no()-1;i>=0;i--) {
if (pfds[i].revents) {
inputs[i]->mode|=((pfds[i].revents&POLLIN ) ? Input::IsReadable : 0);
inputs[i]->mode|=((pfds[i].revents&POLLOUT) ? Input::IsWritable : 0);
inputs[i]->mode|=((pfds[i].revents&(POLLERR|POLLHUP))? Input::IsException: 0);
inputs[i]->onSignal();
}
}
FXint offset=inputs.no();
for (i=0;i<native.no();i++){
native[i]->dispatch(pfds+offset);
offset+=native[i]->no();
}
#endif
FXTime now = FXThread::time();
for (Timer * t = timers;t;t=t->next) {
if (t->time>0 && t->time<=now) {
t->time=0;
t->onExpired();
if (timers==nullptr) break;
t = timers; // so onExpired can remove itself from the list...
}
}
}
void Reactor::wait(FXTime timeout) {
#ifdef _WIN32
result = WaitForMultipleObjects(nfds,pfds,false,(timeout>0) ? timeout / NANOSECONDS_PER_MILLISECOND : FINITE);
#else
FXint n;
if (timeout>=0) {
#ifdef HAVE_PPOLL
struct timespec ts;
ts.tv_sec = timeout / NANOSECONDS_PER_SECOND;
ts.tv_nsec = timeout % NANOSECONDS_PER_SECOND;
#endif
do {
#ifdef HAVE_PPOLL
n = ppoll(pfds,nfds,&ts,nullptr);
#else
n = poll(pfds,nfds,(timeout/NANOSECONDS_PER_MILLISECOND));
#endif
}
while(n==-1 && errno==EINTR);
}
else {
do {
#ifdef HAVE_PPOLL
n = ppoll(pfds,nfds,nullptr,nullptr);
#else
n = poll(pfds,nfds,-1);
#endif
}
while(n==-1 && errno==EINTR);
}
#endif
}
FXTime Reactor::prepare() {
FXTime timeout;
FXint i;
#ifdef _WIN32
nfds = inputs.no();
if (nfds>mfds) {
mfds=nfds;
if (pfds==nullptr)
allocElms(pfds,mfds);
else
resizeElms(pfds,mfds);
}
for (i=0;i<inputs.no();i++) {
if ((inputs[i]->mode&Input::Disabled) || (inputs[i]->mode&(Input::Readable|Input::Writable|Input::Exception))==0) {
continue;
}
pfds[i] = inputs[i]->handle;
inputs[i]->mode&=~(Input::IsReadable|Input::IsWritable|Input::IsException);
}
#else
nfds = inputs.no();
for (i=0;i<native.no();i++) nfds+=native[i]->no();
if (nfds>mfds) {
mfds=nfds;
if (pfds==nullptr)
allocElms(pfds,mfds);
else
resizeElms(pfds,mfds);
}
for (i=0;i<inputs.no();i++) {
if ((inputs[i]->mode&Input::Disabled) || (inputs[i]->mode&(Input::Readable|Input::Writable|Input::Exception))==0) {
pfds[i].fd = -inputs[i]->handle;
pfds[i].events = 0;
pfds[i].revents = 0;
}
else {
pfds[i].fd = inputs[i]->handle;
pfds[i].events = 0;
pfds[i].events |= (inputs[i]->mode&Input::Readable) ? POLLIN : 0;
pfds[i].events |= (inputs[i]->mode&Input::Writable) ? POLLOUT : 0;
pfds[i].events |= (inputs[i]->mode&Input::Exception) ? POLLERR|POLLHUP : 0;
pfds[i].revents = 0;
inputs[i]->mode&=~(Input::IsReadable|Input::IsWritable|Input::IsException);
}
}
FXint offset = inputs.no();
for (i=0;i<native.no();i++) {
native[i]->prepare(pfds+offset);
offset+=native[i]->no();
}
#endif
FXTime now = FXThread::time();
Timer * t = timers;
while(t && t->time<now) t=t->next;
if (t)
timeout = t->time - now;
else
timeout = -1;
return timeout;
}
void Reactor::addNative(Native*n) {
native.append(n);
}
void Reactor::removeNative(Native*n){
native.remove(n);
}
void Reactor::addInput(Input*w) {
inputs.append(w);
}
void Reactor::removeInput(Input*w){
inputs.remove(w);
}
void Reactor::addTimer(Timer*t,FXTime time) {
t->time = time;
Timer**tt;
for (tt=&timers;*tt &&((*tt)->time<t->time);tt=&(*tt)->next) {}
t->next=*tt;
*tt=t;
}
void Reactor::removeTimer(Timer*timer) {
for (Timer**tt=&timers;*tt;tt=&(*tt)->next){
if ((*tt)==timer){
Timer * next = (*tt)->next;
*tt=next;
break;
}
}
}
void Reactor::addDeferred(Deferred*d) {
deferred.append(d);
}
void Reactor::removeDeferred(Deferred*d){
deferred.remove(d);
}
FXbool Reactor::dispatchDeferred() {
FXbool done=false;
for (FXint i=deferred.no()-1;i>=0;i--){
if ((deferred[i]->mode&Deferred::Disabled)==0) {
deferred[i]->run();
done=true;
}
}
return done;
}
void Reactor::runPending() {
if (dispatchDeferred()==false) {
prepare();
wait(0);
dispatch();
}
}
void Reactor::runOnce() {
if (dispatchDeferred()==false) {
FXTime timeout = prepare();
wait(timeout);
dispatch();
}
}
void Reactor::runOnce(FXTime wakeup) {
if (dispatchDeferred()==false) {
FXTime timeout = prepare();
if (timeout<0 || wakeup<timeout)
timeout = wakeup;
wait(timeout);
dispatch();
}
}
}
|