--- gvpe/src/iom.h 2003/10/14 17:24:19 1.13 +++ gvpe/src/iom.h 2004/03/21 12:50:05 1.18 @@ -1,5 +1,6 @@ /* - iom.h -- I/O multiplexor + iom.h -- generic I/O multiplexor + Copyright (C) 2003, 2004 Marc Lehmann 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 @@ -16,40 +17,98 @@ Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#ifndef VPE_IOM_H__ -#define VPE_IOM_H__ +#ifndef IOM_H__ +#define IOM_H__ -#include - -#include - -#include "poll.h" +// required: +// - a vector template like simplevec or stl's vector +// - defines for all watcher types required in your app +// edit iom_conf.h as appropriate. +#include "iom_conf.h" #include "callback.h" -#include "slog.h" -typedef double tstamp; +#ifndef IOM_IO +# define IOM_IO 0 +#endif +#ifndef IOM_TIME +# define IOM_TIME 0 +#endif +#ifndef IOM_CHECK +# define IOM_CHECK 0 +#endif +#ifndef IOM_IDLE +# define IOM_IDLE 0 +#endif +typedef double tstamp; extern tstamp NOW; +struct watcher; +#if IOM_IO struct io_watcher; +#endif +#if IOM_TIME struct time_watcher; +#endif +#if IOM_CHECK +struct check_watcher; +#endif +#if IOM_IDLE +struct idle_watcher; +#endif + +template +struct io_manager_vec : vector { +#if IOM_CHECK + bool activity; +#endif + + void erase_unordered (unsigned int pos) + { + watcher *w = (*this)[size () - 1]; + pop_back (); + + if (size ()) + if ((*this)[pos] = w) + w->active = pos + 1; + } +}; class io_manager { - vector pfs; - vector iow; - vector tw; // actually a heap +#if IOM_IO + io_manager_vec iow; +#endif +#if IOM_CHECK + io_manager_vec cw; +#endif +#if IOM_TIME + io_manager_vec tw; +#endif +#if IOM_IDLE + io_manager_vec iw; +#endif - void idle_cb (time_watcher &w); time_watcher *idle; -public: + template + void reg (watcher *w, io_manager_vec &queue); - void reschedule_time_watchers (); + template + void unreg (watcher *w, io_manager_vec &queue); +public: // register a watcher - void reg (io_watcher *w); - void unreg (io_watcher *w); - void reg (time_watcher *w); - void unreg (time_watcher *w); +#if IOM_IO + void reg (io_watcher *w); void unreg (io_watcher *w); +#endif +#if IOM_TIME + void reg (time_watcher *w); void unreg (time_watcher *w); +#endif +#if IOM_CHECK + void reg (check_watcher *w); void unreg (check_watcher *w); +#endif +#if IOM_IDLE + void reg (idle_watcher *w); void unreg (idle_watcher *w); +#endif void loop (); @@ -59,82 +118,81 @@ extern io_manager iom; // a singleton, together with it's construction/destruction problems. -struct io_watcher : callback2 { - bool registered; // already registered? - int fd; - short events; +struct watcher { + int active; /* 0 == inactive, else index into respective vector */ - template - io_watcher (O1 *object, void (O2::*method)(io_watcher &, short)) - : callback2(object,method) - , registered(false) - { } + watcher() : active(0) { } +}; - ~io_watcher (); +#if IOM_IO +enum { EVENT_READ = 1, EVENT_WRITE = 2 }; - void set(int fd_, short events_); +struct io_watcher : watcher, callback2 { + int fd; + short events; - void set(short events_) - { - set (fd, events_); - } + void set (int fd_, short events_) { fd = fd_; events = events_; } - void start (int fd_, short events_) - { - set (fd_, events_); - iom.reg (this); - } + void set (short events_) { set (fd, events_); } + void start () { iom.reg (this); } + void start (int fd_, short events_) { set (fd_, events_); iom.reg (this); } + void stop () { iom.unreg (this); } - void stop () - { - iom.unreg (this); - } + template + io_watcher (O1 *object, void (O2::*method)(io_watcher &, short)) + : callback2(object,method) + { } + ~io_watcher () { stop (); } }; +#endif -#define TSTAMP_CANCEL -1. - -struct time_watcher : callback1 { - bool registered; // already registered? +#if IOM_TIME +struct time_watcher : watcher, callback1 { tstamp at; + void trigger (); + + void set (tstamp when) { at = when; } + void operator ()() { trigger (); } + void start () { iom.reg (this); } + void start (tstamp when) { set (when); iom.reg (this); } + void stop () { iom.unreg (this); } + template time_watcher (O1 *object, void (O2::*method)(time_watcher &)) - : callback1(object,method) - , registered(false) - { } + : callback1(object,method), at(0) + { } + ~time_watcher () { stop (); } +}; +#endif - ~time_watcher (); +#if IOM_CHECK +// run before checking for new events +struct check_watcher : watcher, callback1 { + void start () { iom.reg (this); } + void stop () { iom.unreg (this); } - void set (tstamp when); - void trigger (); + template + check_watcher (O1 *object, void (O2::*method)(check_watcher &)) + : callback1(object,method) + { } + ~check_watcher () { stop (); } +}; +#endif + +#if IOM_IDLE +// run after checking for any i/o, but before waiting +struct idle_watcher : watcher, callback1 { + void start () { iom.reg (this); } + void stop () { iom.unreg (this); } - void operator ()() - { - trigger (); - } - - void start () - { - iom.reg (this); - } - - void start (tstamp when) - { - set (when); - iom.reg (this); - } - - void stop () - { - iom.unreg (this); - } - - void reset (tstamp when = TSTAMP_CANCEL) - { - stop (); - at = when; - } + template + idle_watcher (O1 *object, void (O2::*method)(idle_watcher &)) + : callback1(object,method) + { } + ~idle_watcher () { stop (); } }; +#endif #endif