ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/iom.h
Revision: 1.9
Committed: Wed Apr 2 05:15:00 2003 UTC (21 years, 1 month ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.8: +5 -5 lines
Log Message:
*** empty 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.5 vector<const io_watcher *> iow;
39 pcg 1.1 vector<time_watcher *> tw; // actually a heap
40 pcg 1.4
41     void idle_cb (tstamp &ts); 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     void reg (int fd, short events, 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.9 struct io_watcher : callback2<void, int, short> {
61 pcg 1.1 template<class O1, class O2>
62 pcg 1.9 io_watcher (O1 *object, void (O2::*method)(int fd, short revents))
63     : callback2<void, int, short>(object,method)
64 pcg 1.2 { }
65    
66 pcg 1.6 ~io_watcher ()
67     {
68     iom.unreg (this);
69     }
70    
71 pcg 1.2 void start (int fd, short events)
72 pcg 1.1 {
73     iom.reg (fd, events, this);
74     }
75    
76 pcg 1.7 void stop ()
77 pcg 1.4 {
78     iom.unreg (this);
79     }
80 pcg 1.1 };
81    
82 pcg 1.4 #define TSTAMP_CANCEL -1.
83    
84 pcg 1.9 struct time_watcher : callback1<void, tstamp &> {
85 pcg 1.6 bool registered; // already registered?
86 pcg 1.2 tstamp at;
87    
88     template<class O1, class O2>
89     time_watcher (O1 *object, void (O2::*method)(tstamp &))
90 pcg 1.9 : callback1<void, tstamp &>(object,method)
91 pcg 1.6 , registered(false)
92 pcg 1.2 { }
93 pcg 1.6
94     ~time_watcher ()
95     {
96     iom.unreg (this);
97     }
98 pcg 1.1
99 pcg 1.2 void set (tstamp when);
100 pcg 1.4 void trigger ();
101 pcg 1.1
102 pcg 1.4 void operator ()()
103 pcg 1.1 {
104 pcg 1.4 trigger ();
105 pcg 1.1 }
106    
107 pcg 1.4 void start ();
108     void start (tstamp when)
109 pcg 1.1 {
110 pcg 1.2 set (when);
111 pcg 1.4 }
112    
113 pcg 1.7 void stop ()
114 pcg 1.4 {
115     iom.unreg (this);
116     }
117    
118     void reset (tstamp when = TSTAMP_CANCEL)
119     {
120     stop ();
121     at = when;
122 pcg 1.1 }
123     };
124    
125     #endif
126