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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines