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 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
|
/********************************************************************************
* *
* C a l l b a c k D i s p a t c h e r *
* *
*********************************************************************************
* Copyright (C) 2006,2022 by Jeroen van der Zijp. All Rights Reserved. *
********************************************************************************/
#include "xincs.h"
#include "fxver.h"
#include "fxdefs.h"
#include "fxmath.h"
#include "FXAtomic.h"
#include "FXElement.h"
#include "FXHash.h"
#include "FXCallback.h"
#include "FXAutoThreadStorageKey.h"
#include "FXThread.h"
#include "FXException.h"
#include "FXReactor.h"
#include "FXDispatcher.h"
/*
Notes:
- FXDispatcher extends FXReactor by implementing convenient callbacks for
handles, signals, timers, and idle processing performed by FXReactor.
- The callbacks may connect to member functions of classes, but also global
functions.
- Special handles [e.g. wakeup pipe or display connection], may be added by
addHandle(hnd,mode) instead of addHandle(cb,hnd,mode,ptr). In this case,
no hash entry will be added, which means when these types of handles are
dispatched via overrides of dispatchHandle(), they must be filtered out prior
to being processed by this implementation of dispatchHandle(); otherwise, a
core dump may result.
- Likewise, addSignal(sig,async) may be used in lieu of addSignal(cb,sig,ptr,async)
to establis special ways of handling select signals. The addSignal(sig,async)
will not establish a handler callback, and thus when such a signal is raised,
it must be filtered via overrides of dispatchSignal prior to being processed
by this implementation of dispatchSignal(); otherwise, a core dump may result.
- Sample usage:
disp->addInterval(TimeoutCallback::create<MyClass,&MyClass::memfunc>(target),dt,ptr);
FIXME maybe this is better:
disp->addInterval(FXObject* tgt,FXSelector sel,FXTime ns=1000000000,FXptr ptr=nullptr);
OK, if message map only contains function-pointers [method_call() template-generated
function call addresses], then we can look up this method-call:
long (*caller)(FXObject*,FXSelector,void*);
caller=metaClass.search(sel);
We can store caller into callback struct!
Then:
caller(target,this,FXSEL(SEL_TIMEOUT,message),userdata);
*/
using namespace FX;
/*******************************************************************************/
namespace FX {
// Handle callback
struct FXDispatcher::Handle {
HandleCallback cb; // Callback
void *ptr; // User data
};
// Signal callback
struct FXDispatcher::Signal {
SignalCallback cb; // Callback
void *ptr; // User data
};
// Timer callback
struct FXDispatcher::Timer {
TimeoutCallback cb; // Callback
FXTime due; // When timer is due (ns)
Timer *next; // Next timeout in list
void *ptr; // User data
};
// Idle callback
struct FXDispatcher::Idle {
IdleCallback cb; // Callback
Idle *next; // Next chore in list
void *ptr; // User data
};
/*******************************************************************************/
// Construct dispatcher object
FXDispatcher::FXDispatcher():signals(nullptr),timers(nullptr),idles(nullptr),timerrecs(nullptr),idlerecs(nullptr){
}
// Initialize dispatcher
FXbool FXDispatcher::init(){
if(FXReactor::init()){
callocElms(signals,64);
timers=nullptr;
idles=nullptr;
timerrecs=nullptr;
idlerecs=nullptr;
return true;
}
return false;
}
/*******************************************************************************/
// Add signal to signal-set observed by the dispatcher
FXbool FXDispatcher::addSignal(SignalCallback cb,FXint sig,void* ptr,FXbool async){
if(FXReactor::addSignal(sig,async)){
signals[sig]=new Signal;
signals[sig]->cb=cb; // Set callback
signals[sig]->ptr=ptr; // Set pointer
return true;
}
return false;
}
// Add signal to signal-set observed by the dispatcher
FXbool FXDispatcher::addSignal(FXint sig,FXbool async){
return FXReactor::addSignal(sig,async);
}
// Remove signal from signal-set observed by the dispatcher
FXbool FXDispatcher::remSignal(FXint sig){
if(FXReactor::remSignal(sig)){
delete signals[sig];
signals[sig]=nullptr;
return true;
}
return false;
}
// Dispatch when a signal was fired
FXbool FXDispatcher::dispatchSignal(FXint sig){
if(hasSignal(sig)){
return signals[sig] && signals[sig]->cb(this,sig,signals[sig]->ptr);
}
return false;
}
/*******************************************************************************/
// Add timeout callback cb at time due (ns since Epoch).
void* FXDispatcher::addTimeout(TimeoutCallback cb,FXTime due,void* ptr){
void* res=nullptr;
if(isInitialized()){
Timer **tt=&timers,*t,*x;
while((x=*tt)!=nullptr){
if(x->cb==cb){
*tt=x->next;
res=x->ptr;
t=x;
goto a;
}
tt=&x->next;
}
if(timerrecs){
t=timerrecs;
timerrecs=t->next;
}
else{
t=new Timer;
}
a: t->cb=cb;
t->due=due;
t->next=nullptr;
t->ptr=ptr;
tt=&timers;
while((x=*tt) && x->due<=t->due){
tt=&x->next;
}
t->next=*tt;
*tt=t;
}
return res;
}
// Add timeout callback cb after time interval (ns).
void* FXDispatcher::addInterval(TimeoutCallback cb,FXTime interval,void* ptr){
return addTimeout(cb,FXThread::time()+interval,ptr);
}
// Remove timeout callback cb.
void* FXDispatcher::remTimeout(TimeoutCallback cb){
void* res=nullptr;
if(isInitialized()){
Timer **tt=&timers,*t;
while((t=*tt)!=nullptr){
if(t->cb==cb){
*tt=t->next;
res=t->ptr;
t->next=timerrecs;
timerrecs=t;
continue;
}
tt=&t->next;
}
}
return res;
}
// Return the remaining time, in nanoseconds
FXTime FXDispatcher::getTimeout(TimeoutCallback cb) const {
for(Timer *t=timers; t; t=t->next){
if(t->cb==cb) return t->due;
}
return forever;
}
// Return timeout when something needs to happen
FXTime FXDispatcher::nextTimeout(){
return timers ? timers->due : forever;
}
// Return true if timeout callback cb been set.
FXbool FXDispatcher::hasTimeout(TimeoutCallback cb) const {
for(Timer *t=timers; t; t=t->next){
if(t->cb==cb) return true;
}
return false;
}
// Dispatch when timeout expires
FXbool FXDispatcher::dispatchTimeout(FXTime due){
Timer *t=timers;
if(t && t->due<=due){
timers=t->next;
t->next=timerrecs;
timerrecs=t;
return t->cb(this,t->due,t->ptr);
}
return false;
}
/*******************************************************************************/
// Add idle callback be executed when dispatch about to block.
void* FXDispatcher::addIdle(IdleCallback cb,void* ptr){
void* res=nullptr;
if(isInitialized()){
Idle **cc=&idles,*c,*x;
while((x=*cc)!=nullptr){ // Search list for cb
if(x->cb==cb){
*cc=x->next;
res=x->ptr;
c=x;
goto a;
}
cc=&x->next;
}
if(idlerecs){ // Recycled chore
c=idlerecs;
idlerecs=c->next;
}
else{ // Fresh chore
c=new Idle;
}
a: c->cb=cb;
c->ptr=ptr;
c->next=nullptr;
while((x=*cc)!=nullptr){ // Continue to end of list
cc=&x->next;
}
*cc=c;
}
return res;
}
// Remove idle callback cb.
void* FXDispatcher::remIdle(IdleCallback cb){
void *res=nullptr;
if(isInitialized()){
Idle **cc=&idles,*c;
while((c=*cc)!=nullptr){
if(c->cb==cb){
*cc=c->next;
res=c->ptr;
c->next=idlerecs;
idlerecs=c;
continue;
}
cc=&c->next;
}
}
return res;
}
// Return true if idle callback cb been set.
FXbool FXDispatcher::hasIdle(IdleCallback cb) const {
for(Idle *c=idles; c; c=c->next){
if(c->cb==cb) return true;
}
return false;
}
// Dispatch one idle callback.
FXbool FXDispatcher::dispatchIdle(){
Idle *c=idles;
if(c){
idles=c->next;
c->next=idlerecs;
idlerecs=c;
return c->cb(this,c->ptr);
}
return false;
}
/*******************************************************************************/
// Add callback cb with new handle hnd to watch-list
FXbool FXDispatcher::addHandle(HandleCallback cb,FXInputHandle hnd,FXuint mode,void* ptr){
if(FXReactor::addHandle(hnd,mode)){
Handle *handle=new Handle();
handle->cb=cb;
handle->ptr=ptr;
handles.insert(reinterpret_cast<FXptr>(hnd),handle);
return true;
}
return false;
}
// Add new handle hnd to watch-list
FXbool FXDispatcher::addHandle(FXInputHandle hnd,FXuint mode){
return FXReactor::addHandle(hnd,mode);
}
// Remove handle hnd from list
FXbool FXDispatcher::remHandle(FXInputHandle hnd){
if(FXReactor::remHandle(hnd)){
Handle *handle=static_cast<Handle*>(handles.remove(reinterpret_cast<FXptr>(hnd)));
delete handle;
return true;
}
return false;
}
// Return true if handle has been set.
FXbool FXDispatcher::hasHandle(FXInputHandle hnd) const {
if(isInitialized()){
return 0<=handles.find(reinterpret_cast<FXptr>(hnd));
}
return false;
}
// Dispatch when when handle hnd is signaled with mode
FXbool FXDispatcher::dispatchHandle(FXInputHandle hnd,FXuint mode,FXuint){
Handle *handle=static_cast<Handle*>(handles[reinterpret_cast<FXptr>(hnd)]);
return handle && handle->cb(this,hnd,mode,handle->ptr);
}
/*******************************************************************************/
// Exit dispatcher
FXbool FXDispatcher::exit(){
if(FXReactor::exit()){
Idle *c;
Timer *t;
FXival i;
for(i=0; i<handles.no(); ++i){
if(handles.empty(i)) continue;
delete static_cast<Handle*>(handles.data(i));
}
while((t=timers)!=nullptr){
timers=t->next;
delete t;
}
while((t=timerrecs)!=nullptr){
timerrecs=t->next;
delete t;
}
while((c=idles)!=nullptr){
idles=c->next;
delete c;
}
while((c=idlerecs)!=nullptr){
idlerecs=c->next;
delete c;
}
for(i=0; i<64; ++i){
delete signals[i];
}
freeElms(signals);
handles.clear();
timers=nullptr;
idles=nullptr;
timerrecs=nullptr;
idlerecs=nullptr;
return true;
}
return false;
}
// Destroy dispatcher object
FXDispatcher::~FXDispatcher(){
exit();
}
}
|