1 /*
2 * Copyright (C) 2013 Simon Richter
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 3 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, see <http://www.gnu.org/licenses/>.
16 */
17
18 #ifdef HAVE_CONFIG_H
19 #include <config.h>
20 #endif
21
22 #include "messagepump.h"
23
24 #include "timeval_op.h"
25
26 #include <sys/time.h>
27
28 namespace librevisa {
29
messagepump()30 messagepump::messagepump() throw()
31 {
32 return;
33 }
34
register_watch(watch & w)35 void messagepump::register_watch(watch &w)
36 {
37 watches.push_front(w);
38 }
39
unregister_watch(watch & w)40 void messagepump::unregister_watch(watch &w)
41 {
42 w.fd = -1;
43 }
44
update_watch(watch & w,fd_event event)45 void messagepump::update_watch(watch &w, fd_event event)
46 {
47 w.event = event;
48 }
49
get_events(watch & w)50 messagepump::fd_event messagepump::get_events(watch &w)
51 {
52 fd_event ret = none;
53 if(FD_ISSET(w.fd, &readfds))
54 ret |= read;
55 if(FD_ISSET(w.fd, &writefds))
56 ret |= write;
57 if(FD_ISSET(w.fd, &exceptfds))
58 ret |= except;
59 return ret;
60 }
61
register_timeout(timeout & t)62 void messagepump::register_timeout(timeout &t)
63 {
64 timeouts.push_front(t);
65 }
66
unregister_timeout(timeout & t)67 void messagepump::unregister_timeout(timeout &t)
68 {
69 t.tv.tv_sec = -1;
70 }
71
update_timeout(timeout & t,timeval const * tv)72 void messagepump::update_timeout(timeout &t, timeval const *tv)
73 {
74 if(!tv)
75 tv = &null_timeout;
76 t.tv = *tv;
77 return;
78 }
79
run(unsigned int stopafter)80 void messagepump::run(unsigned int stopafter)
81 {
82 timeval now;
83 ::gettimeofday(&now, 0);
84
85 timeval const limit = now + stopafter * 1000;
86
87 for(;;)
88 {
89 bool restart = false;
90 bool have_timeout = false;
91 timeval next = limit;
92 for(timeout_iterator i = timeouts.begin(); i != timeouts.end(); ++i)
93 {
94 while(i != timeouts.end() && i->tv.tv_sec == -1)
95 {
96 timeout &t = *i;
97 i = timeouts.erase(i);
98 t.cleanup();
99 }
100 if(i == timeouts.end())
101 break;
102 if(i->tv == null_timeout)
103 continue;
104 if(i->tv < now)
105 {
106 i->tv = null_timeout;
107 i->notify_timeout();
108 restart = true;
109 continue;
110 }
111 have_timeout = true;
112 if(i->tv < next)
113 next = i->tv;
114
115 if(i == timeouts.end())
116 break;
117 }
118
119 if(restart)
120 continue;
121
122 next -= now;
123
124 FD_ZERO(&readfds);
125 FD_ZERO(&writefds);
126 FD_ZERO(&exceptfds);
127
128 int maxfd = -1;
129
130 for(watch_iterator i = watches.begin(); i != watches.end(); ++i)
131 {
132 while(i != watches.end() && i->fd == -1)
133 {
134 watch &w = *i;
135 i = watches.erase(i);
136 w.cleanup();
137 };
138 if(i == watches.end())
139 break;
140
141 if(i->event & read)
142 FD_SET(i->fd, &readfds);
143 if(i->event & write)
144 FD_SET(i->fd, &writefds);
145 if(i->event & except)
146 FD_SET(i->fd, &exceptfds);
147 if(i->event && i->fd > maxfd)
148 maxfd = i->fd;
149 }
150
151 if(!have_timeout && maxfd == -1)
152 return;
153
154 int rc = ::select(maxfd + 1, &readfds, &writefds, &exceptfds, &next);
155 if(rc == -1)
156 return;
157 if(rc > 0)
158 {
159 for(watch_iterator i = watches.begin(); i != watches.end(); ++i)
160 {
161 fd_event ev = get_events(*i);
162 if(ev)
163 i->notify_fd_event(i->fd, ev);
164 }
165 }
166
167 ::gettimeofday(&now, 0);
168 if(limit < now)
169 return;
170 }
171 }
172
173 timeval const messagepump::null_timeout = { 0, 1000000 };
174
175 messagepump main;
176
177 }
178