| 1 |
pcg |
1.1 |
/* |
| 2 |
|
|
iom.h -- generic I/O multiplexor |
| 3 |
|
|
Copyright (C) 2003 Marc Lehmann <pcg@goof.com> |
| 4 |
|
|
|
| 5 |
|
|
This program is free software; you can redistribute it and/or modify |
| 6 |
|
|
it under the terms of the GNU General Public License as published by |
| 7 |
|
|
the Free Software Foundation; either version 2 of the License, or |
| 8 |
|
|
(at your option) any later version. |
| 9 |
|
|
|
| 10 |
|
|
This program is distributed in the hope that it will be useful, |
| 11 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
|
|
GNU General Public License for more details. |
| 14 |
|
|
|
| 15 |
|
|
You should have received a copy of the GNU General Public License |
| 16 |
|
|
along with this program; if not, write to the Free Software |
| 17 |
|
|
Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 |
|
|
*/ |
| 19 |
|
|
|
| 20 |
|
|
#ifndef VPE_IOM_H__ |
| 21 |
|
|
#define VPE_IOM_H__ |
| 22 |
|
|
|
| 23 |
|
|
#include <cassert> |
| 24 |
|
|
|
| 25 |
|
|
#include "rxvtvec.h" |
| 26 |
|
|
#include "callback.h" |
| 27 |
|
|
|
| 28 |
pcg |
1.2 |
#define IOM_IO 1 |
| 29 |
|
|
#define IOM_TIME 1 |
| 30 |
|
|
#undef IOM_CHECK |
| 31 |
|
|
|
| 32 |
|
|
#if IOM_IO |
| 33 |
|
|
typedef double tstamp; |
| 34 |
|
|
extern tstamp NOW; |
| 35 |
|
|
|
| 36 |
|
|
struct io_watcher; |
| 37 |
|
|
#endif |
| 38 |
|
|
#if IOM_TIME |
| 39 |
|
|
struct time_watcher; |
| 40 |
|
|
#endif |
| 41 |
|
|
#if IOM_CHECK |
| 42 |
|
|
struct check_watcher; |
| 43 |
|
|
#endif |
| 44 |
pcg |
1.1 |
|
| 45 |
pcg |
1.2 |
class io_manager { |
| 46 |
|
|
#if IOM_IO |
| 47 |
|
|
simplevec<io_watcher *> iow; |
| 48 |
|
|
#endif |
| 49 |
|
|
#if IOM_CHECK |
| 50 |
|
|
simplevec<check_watcher *> cw; |
| 51 |
|
|
#endif |
| 52 |
|
|
#if IOM_TIME |
| 53 |
|
|
simplevec<time_watcher *> tw; |
| 54 |
|
|
#endif |
| 55 |
pcg |
1.1 |
|
| 56 |
pcg |
1.2 |
template<class watcher> |
| 57 |
|
|
void reg (watcher *w, simplevec<watcher *> &queue); |
| 58 |
pcg |
1.1 |
|
| 59 |
pcg |
1.2 |
template<class watcher> |
| 60 |
|
|
void unreg (watcher *w, simplevec<watcher *> &queue); |
| 61 |
pcg |
1.1 |
|
| 62 |
|
|
public: |
| 63 |
|
|
// register a watcher |
| 64 |
pcg |
1.2 |
#if IOM_IO |
| 65 |
|
|
void reg (io_watcher *w); void unreg (io_watcher *w); |
| 66 |
|
|
#endif |
| 67 |
|
|
#if IOM_TIME |
| 68 |
|
|
void reg (time_watcher *w); void unreg (time_watcher *w); |
| 69 |
|
|
#endif |
| 70 |
|
|
#if IOM_CHECK |
| 71 |
|
|
void reg (check_watcher *w); void unreg (check_watcher *w); |
| 72 |
|
|
#endif |
| 73 |
pcg |
1.1 |
|
| 74 |
|
|
void loop (); |
| 75 |
|
|
|
| 76 |
|
|
io_manager (); |
| 77 |
|
|
~io_manager (); |
| 78 |
|
|
}; |
| 79 |
|
|
|
| 80 |
|
|
extern io_manager iom; // a singleton, together with it's construction/destruction problems. |
| 81 |
|
|
|
| 82 |
pcg |
1.2 |
#if IOM_IO |
| 83 |
pcg |
1.1 |
enum { EVENT_READ = 1, EVENT_WRITE = 2 }; |
| 84 |
|
|
|
| 85 |
|
|
struct io_watcher : callback2<void, io_watcher &, short> { |
| 86 |
|
|
int fd; |
| 87 |
|
|
short events; |
| 88 |
|
|
|
| 89 |
|
|
template<class O1, class O2> |
| 90 |
|
|
io_watcher (O1 *object, void (O2::*method)(io_watcher &, short)) |
| 91 |
|
|
: callback2<void, io_watcher &, short>(object,method) |
| 92 |
|
|
{ } |
| 93 |
|
|
|
| 94 |
|
|
~io_watcher (); |
| 95 |
|
|
|
| 96 |
|
|
void set(int fd_, short events_) { fd = fd_; events = events_; } |
| 97 |
|
|
|
| 98 |
|
|
void set(short events_) { set (fd, events_); } |
| 99 |
|
|
void start (int fd_, short events_) { set (fd_, events_); iom.reg (this); } |
| 100 |
|
|
void stop () { iom.unreg (this); } |
| 101 |
|
|
}; |
| 102 |
pcg |
1.2 |
#endif |
| 103 |
pcg |
1.1 |
|
| 104 |
pcg |
1.2 |
#if IOM_TIME |
| 105 |
|
|
enum { TSTAMP_CANCEL = -1 }; |
| 106 |
pcg |
1.1 |
|
| 107 |
|
|
struct time_watcher : callback1<void, time_watcher &> { |
| 108 |
|
|
tstamp at; |
| 109 |
|
|
|
| 110 |
|
|
template<class O1, class O2> |
| 111 |
|
|
time_watcher (O1 *object, void (O2::*method)(time_watcher &)) |
| 112 |
|
|
: callback1<void, time_watcher &>(object,method) |
| 113 |
|
|
{ } |
| 114 |
|
|
|
| 115 |
|
|
~time_watcher (); |
| 116 |
|
|
|
| 117 |
|
|
void trigger (); |
| 118 |
|
|
|
| 119 |
|
|
void set (tstamp when) { at = when; } |
| 120 |
|
|
void operator ()() { trigger (); } |
| 121 |
|
|
void start () { iom.reg (this); } |
| 122 |
|
|
void start (tstamp when) { set (when); iom.reg (this); } |
| 123 |
|
|
void stop () { iom.unreg (this); } |
| 124 |
|
|
|
| 125 |
|
|
void reset (tstamp when = TSTAMP_CANCEL) |
| 126 |
|
|
{ |
| 127 |
|
|
stop (); |
| 128 |
|
|
at = when; |
| 129 |
|
|
} |
| 130 |
|
|
}; |
| 131 |
pcg |
1.2 |
#endif |
| 132 |
|
|
|
| 133 |
|
|
#if IOM_CHECK |
| 134 |
|
|
// run before checking for new events |
| 135 |
|
|
struct check_watcher : callback1<void, check_watcher &> { |
| 136 |
|
|
template<class O1, class O2> |
| 137 |
|
|
check_watcher (O1 *object, void (O2::*method)(check_watcher &)) |
| 138 |
|
|
: callback1<void, check_watcher &>(object,method) |
| 139 |
|
|
{ } |
| 140 |
|
|
|
| 141 |
|
|
~check_watcher (); |
| 142 |
|
|
|
| 143 |
|
|
void start () { iom.reg (this); } |
| 144 |
|
|
void stop () { iom.unreg (this); } |
| 145 |
|
|
}; |
| 146 |
|
|
#endif |
| 147 |
pcg |
1.1 |
|
| 148 |
|
|
#endif |
| 149 |
|
|
|