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.14 by pcg, Thu Oct 16 02:41:21 2003 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines