1 | /* |
1 | /* |
2 | iom.h -- I/O multiplexor |
2 | iom.h -- generic I/O multiplexor |
|
|
3 | Copyright (C) 2003 Marc Lehmann <pcg@goof.com> |
3 | |
4 | |
4 | This program is free software; you can redistribute it and/or modify |
5 | 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 | 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 | the Free Software Foundation; either version 2 of the License, or |
7 | (at your option) any later version. |
8 | (at your option) any later version. |
… | |
… | |
19 | #ifndef VPE_IOM_H__ |
20 | #ifndef VPE_IOM_H__ |
20 | #define VPE_IOM_H__ |
21 | #define VPE_IOM_H__ |
21 | |
22 | |
22 | #include <vector> |
23 | #include <vector> |
23 | |
24 | |
|
|
25 | #include <cassert> |
|
|
26 | |
24 | #include <sys/poll.h> |
27 | #include "poll.h" |
25 | |
28 | |
26 | #include "callback.h" |
29 | #include "callback.h" |
27 | #include "slog.h" |
30 | #include "slog.h" |
28 | |
31 | |
29 | typedef double tstamp; |
32 | typedef double tstamp; |
… | |
… | |
53 | |
56 | |
54 | io_manager (); |
57 | io_manager (); |
55 | ~io_manager (); |
58 | ~io_manager (); |
56 | }; |
59 | }; |
57 | |
60 | |
58 | extern io_manager iom; |
61 | extern io_manager iom; // a singleton, together with it's construction/destruction problems. |
59 | |
62 | |
60 | struct io_watcher : callback2<void, io_watcher &, short> { |
63 | struct io_watcher : callback2<void, io_watcher &, short> { |
|
|
64 | bool registered; // already registered? |
61 | int fd; |
65 | int fd; |
62 | short events; |
66 | short events; |
63 | |
67 | |
64 | template<class O1, class O2> |
68 | template<class O1, class O2> |
65 | io_watcher (O1 *object, void (O2::*method)(io_watcher &, short)) |
69 | io_watcher (O1 *object, void (O2::*method)(io_watcher &, short)) |
66 | : callback2<void, io_watcher &, short>(object,method) |
70 | : callback2<void, io_watcher &, short>(object,method) |
|
|
71 | , registered(false) |
67 | { } |
72 | { } |
68 | |
73 | |
69 | ~io_watcher () |
74 | ~io_watcher (); |
|
|
75 | |
|
|
76 | void set(int fd_, short events_); |
|
|
77 | |
|
|
78 | void set(short events_) |
70 | { |
79 | { |
71 | iom.unreg (this); |
80 | set (fd, events_); |
72 | } |
81 | } |
73 | |
82 | |
74 | void start (int fd_, short events_) |
83 | void start (int fd_, short events_) |
75 | { |
84 | { |
76 | fd = fd_; |
85 | set (fd_, events_); |
77 | events = events_; |
|
|
78 | iom.reg (this); |
86 | iom.reg (this); |
79 | } |
87 | } |
80 | |
88 | |
81 | void stop () |
89 | void stop () |
82 | { |
90 | { |
… | |
… | |
94 | time_watcher (O1 *object, void (O2::*method)(time_watcher &)) |
102 | time_watcher (O1 *object, void (O2::*method)(time_watcher &)) |
95 | : callback1<void, time_watcher &>(object,method) |
103 | : callback1<void, time_watcher &>(object,method) |
96 | , registered(false) |
104 | , registered(false) |
97 | { } |
105 | { } |
98 | |
106 | |
99 | ~time_watcher () |
107 | ~time_watcher (); |
100 | { |
|
|
101 | iom.unreg (this); |
|
|
102 | } |
|
|
103 | |
108 | |
104 | void set (tstamp when); |
109 | void set (tstamp when); |
105 | void trigger (); |
110 | void trigger (); |
106 | |
111 | |
107 | void operator ()() |
112 | void operator ()() |
108 | { |
113 | { |
109 | trigger (); |
114 | trigger (); |
110 | } |
115 | } |
111 | |
116 | |
112 | void start (); |
117 | void start () |
|
|
118 | { |
|
|
119 | iom.reg (this); |
|
|
120 | } |
|
|
121 | |
113 | void start (tstamp when) |
122 | void start (tstamp when) |
114 | { |
123 | { |
115 | set (when); |
124 | set (when); |
|
|
125 | iom.reg (this); |
116 | } |
126 | } |
117 | |
127 | |
118 | void stop () |
128 | void stop () |
119 | { |
129 | { |
120 | iom.unreg (this); |
130 | iom.unreg (this); |