ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/iom.h
(Generate patch)

Comparing gvpe/src/iom.h (file contents):
Revision 1.1 by pcg, Fri Mar 21 20:33:36 2003 UTC vs.
Revision 1.12 by pcg, Sat Apr 5 02:32:40 2003 UTC

19#ifndef VPE_IOM_H__ 19#ifndef VPE_IOM_H__
20#define VPE_IOM_H__ 20#define VPE_IOM_H__
21 21
22#include <vector> 22#include <vector>
23 23
24#include <cassert>
25
24#include <sys/poll.h> 26#include <sys/poll.h>
25 27
28#include "callback.h"
26#include "slog.h" 29#include "slog.h"
27 30
28typedef double timestamp; 31typedef double tstamp;
29 32
30extern timestamp NOW; 33extern tstamp NOW;
31 34
32template<class R, class A> class callback;
33struct io_watcher; 35struct io_watcher;
34struct time_watcher; 36struct time_watcher;
35 37
36class io_manager { 38class io_manager {
37 vector<pollfd> pfs; 39 vector<pollfd> pfs;
38 vector<io_watcher *> iow; 40 vector<io_watcher *> iow;
39 vector<time_watcher *> tw; // actually a heap 41 vector<time_watcher *> tw; // actually a heap
42
43 void idle_cb (time_watcher &w); time_watcher *idle;
40public: 44public:
41 45
46 void reschedule_time_watchers ();
47
42 // register a watcher 48 // register a watcher
43 void reg (int fd, short events, io_watcher *w); 49 void reg (io_watcher *w);
44 void unreg (io_watcher *w); 50 void unreg (io_watcher *w);
45 void reg (time_watcher *w); 51 void reg (time_watcher *w);
46 void unreg (time_watcher *w); 52 void unreg (time_watcher *w);
47 53
48 void loop (); 54 void loop ();
49 55
50 io_manager (); 56 io_manager ();
51 ~io_manager (); 57 ~io_manager ();
52}; 58};
53 59
54extern io_manager iom; 60extern io_manager iom; // a singleton, together with it's construction/destruction problems.
55 61
56template<class R, class A> 62struct io_watcher : callback2<void, io_watcher &, short> {
57class callback { 63 bool registered; // already registered?
58 struct object { }; 64 int fd;
65 short events;
59 66
60 void *obj; 67 template<class O1, class O2>
61 R (object::*meth)(A arg); 68 io_watcher (O1 *object, void (O2::*method)(io_watcher &, short))
69 : callback2<void, io_watcher &, short>(object,method)
70 , registered(false)
71 { }
62 72
63 // a proxy is a kind of recipe on how to call a specific class method 73 ~io_watcher ();
64 struct proxy_base {
65 virtual R call (void *obj, void (object::*meth)(A), A arg) = 0;
66 };
67 template<class O1, class O2>
68 struct proxy : proxy_base {
69 virtual R call (void *obj, void (object::*meth)(A), A arg)
70 {
71 ((reinterpret_cast<O1 *>(obj)) ->* (reinterpret_cast<void (O2::*)(A)>(meth)))
72 (arg);
73 }
74 };
75 74
76 proxy_base *prxy; 75 void set(int fd_, short events_);
77 76
78public: 77 void set(short events_)
79 template<class O1, class O2>
80 callback (O1 *object, void (O2::*method)(A))
81 { 78 {
82 static proxy<O1,O2> p; 79 set (fd, events_);
83 obj = reinterpret_cast<void *>(object);
84 meth = reinterpret_cast<void (object::*)(A)>(method);
85 prxy = &p;
86 } 80 }
87 81
88 R call(A arg) 82 void start (int fd_, short events_)
89 { 83 {
90 return prxy->call (obj, meth, arg); 84 set (fd_, events_);
91 }
92};
93
94struct io_watcher : callback<void, short> {
95 template<class O1, class O2>
96 io_watcher (int fd, short events, O1 *object, void (O2::*method)(short revents))
97 : callback<void, short>(object,method)
98 {
99 iom.reg (fd, events, this); 85 iom.reg (this);
100 } 86 }
101 87
102 ~io_watcher () 88 void stop ()
103 { 89 {
104 iom.unreg (this); 90 iom.unreg (this);
105 } 91 }
106}; 92};
107 93
108struct time_watcher : callback<void, timestamp &> { 94#define TSTAMP_CANCEL -1.
109 timestamp at;
110 95
111 void set (timestamp when); 96struct time_watcher : callback1<void, time_watcher &> {
97 bool registered; // already registered?
98 tstamp at;
112 99
113 template<class O1, class O2> 100 template<class O1, class O2>
114 time_watcher (timestamp when, O1 *object, void (O2::*method)(timestamp &)) 101 time_watcher (O1 *object, void (O2::*method)(time_watcher &))
115 : callback<void, timestamp &>(object,method) 102 : callback1<void, time_watcher &>(object,method)
116 , at(when) 103 , registered(false)
104 { }
105
106 ~time_watcher ();
107
108 void set (tstamp when);
109 void trigger ();
110
111 void operator ()()
112 {
113 trigger ();
114 }
115
116 void start ()
117 { 117 {
118 iom.reg (this); 118 iom.reg (this);
119 } 119 }
120 120
121 ~time_watcher () 121 void start (tstamp when)
122 {
123 set (when);
124 iom.reg (this);
125 }
126
127 void stop ()
122 { 128 {
123 iom.unreg (this); 129 iom.unreg (this);
130 }
131
132 void reset (tstamp when = TSTAMP_CANCEL)
133 {
134 stop ();
135 at = when;
124 } 136 }
125}; 137};
126 138
127#endif 139#endif
128 140

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines