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