ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/iom.h
Revision: 1.10
Committed: Wed Apr 2 21:02:25 2003 UTC (21 years, 1 month ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.9: +16 -11 lines
Log Message:
§§

File Contents

# User Rev Content
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     #include <sys/poll.h>
25    
26 pcg 1.8 #include "callback.h"
27 pcg 1.1 #include "slog.h"
28    
29 pcg 1.2 typedef double tstamp;
30 pcg 1.1
31 pcg 1.2 extern tstamp NOW;
32 pcg 1.1
33     struct io_watcher;
34     struct time_watcher;
35    
36     class io_manager {
37     vector<pollfd> pfs;
38 pcg 1.10 vector<io_watcher *> iow;
39 pcg 1.1 vector<time_watcher *> tw; // actually a heap
40 pcg 1.4
41 pcg 1.10 void idle_cb (time_watcher &w); time_watcher *idle;
42 pcg 1.1 public:
43    
44 pcg 1.6 void reschedule_time_watchers ();
45    
46 pcg 1.1 // register a watcher
47 pcg 1.10 void reg (io_watcher *w);
48 pcg 1.7 void unreg (io_watcher *w);
49 pcg 1.1 void reg (time_watcher *w);
50 pcg 1.7 void unreg (time_watcher *w);
51 pcg 1.1
52     void loop ();
53    
54     io_manager ();
55     ~io_manager ();
56     };
57    
58     extern io_manager iom;
59    
60 pcg 1.10 struct io_watcher : callback2<void, io_watcher &, short> {
61     int fd;
62     short events;
63    
64 pcg 1.1 template<class O1, class O2>
65 pcg 1.10 io_watcher (O1 *object, void (O2::*method)(io_watcher &, short))
66     : callback2<void, io_watcher &, short>(object,method)
67 pcg 1.2 { }
68    
69 pcg 1.6 ~io_watcher ()
70     {
71     iom.unreg (this);
72     }
73    
74 pcg 1.10 void start (int fd_, short events_)
75 pcg 1.1 {
76 pcg 1.10 fd = fd_;
77     events = events_;
78     iom.reg (this);
79 pcg 1.1 }
80    
81 pcg 1.7 void stop ()
82 pcg 1.4 {
83     iom.unreg (this);
84     }
85 pcg 1.1 };
86    
87 pcg 1.4 #define TSTAMP_CANCEL -1.
88    
89 pcg 1.10 struct time_watcher : callback1<void, time_watcher &> {
90 pcg 1.6 bool registered; // already registered?
91 pcg 1.2 tstamp at;
92    
93     template<class O1, class O2>
94 pcg 1.10 time_watcher (O1 *object, void (O2::*method)(time_watcher &))
95     : callback1<void, time_watcher &>(object,method)
96 pcg 1.6 , registered(false)
97 pcg 1.2 { }
98 pcg 1.6
99     ~time_watcher ()
100     {
101     iom.unreg (this);
102     }
103 pcg 1.1
104 pcg 1.2 void set (tstamp when);
105 pcg 1.4 void trigger ();
106 pcg 1.1
107 pcg 1.4 void operator ()()
108 pcg 1.1 {
109 pcg 1.4 trigger ();
110 pcg 1.1 }
111    
112 pcg 1.4 void start ();
113     void start (tstamp when)
114 pcg 1.1 {
115 pcg 1.2 set (when);
116 pcg 1.4 }
117    
118 pcg 1.7 void stop ()
119 pcg 1.4 {
120     iom.unreg (this);
121     }
122    
123     void reset (tstamp when = TSTAMP_CANCEL)
124     {
125     stop ();
126     at = when;
127 pcg 1.1 }
128     };
129    
130     #endif
131