BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file BReactor_glib.c |
||
3 | * @author Ambroz Bizjak <ambrop7@gmail.com> |
||
4 | * |
||
5 | * @section LICENSE |
||
6 | * |
||
7 | * Redistribution and use in source and binary forms, with or without |
||
8 | * modification, are permitted provided that the following conditions are met: |
||
9 | * 1. Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * 2. Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
13 | * documentation and/or other materials provided with the distribution. |
||
14 | * 3. Neither the name of the author nor the |
||
15 | * names of its contributors may be used to endorse or promote products |
||
16 | * derived from this software without specific prior written permission. |
||
17 | * |
||
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
||
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
||
20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
||
21 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
||
22 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
||
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||
24 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
||
25 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||
27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
28 | */ |
||
29 | |||
30 | #include <stdlib.h> |
||
31 | #include <string.h> |
||
32 | |||
33 | #include <misc/offset.h> |
||
34 | #include <base/BLog.h> |
||
35 | |||
36 | #include "BReactor_glib.h" |
||
37 | |||
38 | #include <generated/blog_channel_BReactor.h> |
||
39 | |||
40 | struct fd_source { |
||
41 | GSource source; |
||
42 | BFileDescriptor *bfd; |
||
43 | }; |
||
44 | |||
45 | static void assert_timer (BSmallTimer *bt) |
||
46 | { |
||
47 | ASSERT(bt->is_small == 0 || bt->is_small == 1) |
||
48 | ASSERT(bt->active == 0 || bt->active == 1) |
||
49 | ASSERT(!bt->active || bt->reactor) |
||
50 | ASSERT(!bt->active || bt->source) |
||
51 | } |
||
52 | |||
53 | static void dispatch_pending (BReactor *o) |
||
54 | { |
||
55 | while (!o->exiting && BPendingGroup_HasJobs(&o->pending_jobs)) { |
||
56 | BPendingGroup_ExecuteJob(&o->pending_jobs); |
||
57 | } |
||
58 | } |
||
59 | |||
60 | static void reset_limits (BReactor *o) |
||
61 | { |
||
62 | LinkedList1Node *list_node; |
||
63 | while (list_node = LinkedList1_GetFirst(&o->active_limits_list)) { |
||
64 | BReactorLimit *limit = UPPER_OBJECT(list_node, BReactorLimit, active_limits_list_node); |
||
65 | ASSERT(limit->count > 0) |
||
66 | limit->count = 0; |
||
67 | LinkedList1_Remove(&o->active_limits_list, &limit->active_limits_list_node); |
||
68 | } |
||
69 | } |
||
70 | |||
71 | static gushort get_glib_wait_events (int ev) |
||
72 | { |
||
73 | gushort gev = G_IO_ERR | G_IO_HUP; |
||
74 | |||
75 | if (ev & BREACTOR_READ) { |
||
76 | gev |= G_IO_IN; |
||
77 | } |
||
78 | |||
79 | if (ev & BREACTOR_WRITE) { |
||
80 | gev |= G_IO_OUT; |
||
81 | } |
||
82 | |||
83 | return gev; |
||
84 | } |
||
85 | |||
86 | static int get_fd_dispatchable_events (BFileDescriptor *bfd) |
||
87 | { |
||
88 | ASSERT(bfd->active) |
||
89 | |||
90 | int ev = 0; |
||
91 | |||
92 | if ((bfd->waitEvents & BREACTOR_READ) && (bfd->pollfd.revents & G_IO_IN)) { |
||
93 | ev |= BREACTOR_READ; |
||
94 | } |
||
95 | |||
96 | if ((bfd->waitEvents & BREACTOR_WRITE) && (bfd->pollfd.revents & G_IO_OUT)) { |
||
97 | ev |= BREACTOR_WRITE; |
||
98 | } |
||
99 | |||
100 | if ((bfd->pollfd.revents & G_IO_ERR)) { |
||
101 | ev |= BREACTOR_ERROR; |
||
102 | } |
||
103 | |||
104 | if ((bfd->pollfd.revents & G_IO_HUP)) { |
||
105 | ev |= BREACTOR_HUP; |
||
106 | } |
||
107 | |||
108 | return ev; |
||
109 | } |
||
110 | |||
111 | static gboolean timer_source_handler (gpointer data) |
||
112 | { |
||
113 | BSmallTimer *bt = (void *)data; |
||
114 | assert_timer(bt); |
||
115 | ASSERT(bt->active) |
||
116 | |||
117 | BReactor *reactor = bt->reactor; |
||
118 | |||
119 | if (reactor->exiting) { |
||
120 | return FALSE; |
||
121 | } |
||
122 | |||
123 | g_source_destroy(bt->source); |
||
124 | g_source_unref(bt->source); |
||
125 | bt->active = 0; |
||
126 | DebugCounter_Decrement(&reactor->d_timers_ctr); |
||
127 | |||
128 | if (bt->is_small) { |
||
129 | bt->handler.smalll(bt); |
||
130 | } else { |
||
131 | BTimer *btimer = UPPER_OBJECT(bt, BTimer, base); |
||
132 | bt->handler.heavy(btimer->user); |
||
133 | } |
||
134 | |||
135 | dispatch_pending(reactor); |
||
136 | reset_limits(reactor); |
||
137 | |||
138 | return FALSE; |
||
139 | } |
||
140 | |||
141 | static gboolean fd_source_func_prepare (GSource *source, gint *timeout) |
||
142 | { |
||
143 | BFileDescriptor *bfd = ((struct fd_source *)source)->bfd; |
||
144 | ASSERT(bfd->active) |
||
145 | ASSERT(bfd->source == source) |
||
146 | |||
147 | *timeout = -1; |
||
148 | return FALSE; |
||
149 | } |
||
150 | |||
151 | static gboolean fd_source_func_check (GSource *source) |
||
152 | { |
||
153 | BFileDescriptor *bfd = ((struct fd_source *)source)->bfd; |
||
154 | ASSERT(bfd->active) |
||
155 | ASSERT(bfd->source == source) |
||
156 | |||
157 | return (get_fd_dispatchable_events(bfd) ? TRUE : FALSE); |
||
158 | } |
||
159 | |||
160 | static gboolean fd_source_func_dispatch (GSource *source, GSourceFunc callback, gpointer user_data) |
||
161 | { |
||
162 | BFileDescriptor *bfd = ((struct fd_source *)source)->bfd; |
||
163 | BReactor *reactor = bfd->reactor; |
||
164 | ASSERT(bfd->active) |
||
165 | ASSERT(bfd->source == source) |
||
166 | |||
167 | if (reactor->exiting) { |
||
168 | return TRUE; |
||
169 | } |
||
170 | |||
171 | int events = get_fd_dispatchable_events(bfd); |
||
172 | if (!events) { |
||
173 | return TRUE; |
||
174 | } |
||
175 | |||
176 | bfd->handler(bfd->user, events); |
||
177 | dispatch_pending(reactor); |
||
178 | reset_limits(reactor); |
||
179 | |||
180 | return TRUE; |
||
181 | } |
||
182 | |||
183 | void BSmallTimer_Init (BSmallTimer *bt, BSmallTimer_handler handler) |
||
184 | { |
||
185 | bt->handler.smalll = handler; |
||
186 | bt->active = 0; |
||
187 | bt->is_small = 1; |
||
188 | } |
||
189 | |||
190 | int BSmallTimer_IsRunning (BSmallTimer *bt) |
||
191 | { |
||
192 | assert_timer(bt); |
||
193 | |||
194 | return bt->active; |
||
195 | } |
||
196 | |||
197 | void BTimer_Init (BTimer *bt, btime_t msTime, BTimer_handler handler, void *user) |
||
198 | { |
||
199 | bt->base.handler.heavy = handler; |
||
200 | bt->base.active = 0; |
||
201 | bt->base.is_small = 0; |
||
202 | bt->user = user; |
||
203 | bt->msTime = msTime; |
||
204 | } |
||
205 | |||
206 | int BTimer_IsRunning (BTimer *bt) |
||
207 | { |
||
208 | return BSmallTimer_IsRunning(&bt->base); |
||
209 | } |
||
210 | |||
211 | void BFileDescriptor_Init (BFileDescriptor *bs, int fd, BFileDescriptor_handler handler, void *user) |
||
212 | { |
||
213 | bs->fd = fd; |
||
214 | bs->handler = handler; |
||
215 | bs->user = user; |
||
216 | bs->active = 0; |
||
217 | } |
||
218 | |||
219 | int BReactor_Init (BReactor *bsys) |
||
220 | { |
||
221 | return BReactor_InitFromExistingGMainLoop(bsys, g_main_loop_new(NULL, FALSE), 1); |
||
222 | } |
||
223 | |||
224 | void BReactor_Free (BReactor *bsys) |
||
225 | { |
||
226 | DebugObject_Free(&bsys->d_obj); |
||
227 | DebugCounter_Free(&bsys->d_timers_ctr); |
||
228 | DebugCounter_Free(&bsys->d_limits_ctr); |
||
229 | DebugCounter_Free(&bsys->d_fds_counter); |
||
230 | ASSERT(!BPendingGroup_HasJobs(&bsys->pending_jobs)) |
||
231 | ASSERT(LinkedList1_IsEmpty(&bsys->active_limits_list)) |
||
232 | |||
233 | // free job queue |
||
234 | BPendingGroup_Free(&bsys->pending_jobs); |
||
235 | |||
236 | // unref main loop if needed |
||
237 | if (bsys->unref_gloop_on_free) { |
||
238 | g_main_loop_unref(bsys->gloop); |
||
239 | } |
||
240 | } |
||
241 | |||
242 | int BReactor_Exec (BReactor *bsys) |
||
243 | { |
||
244 | DebugObject_Access(&bsys->d_obj); |
||
245 | |||
246 | // dispatch pending jobs (until exiting) and reset limits |
||
247 | dispatch_pending(bsys); |
||
248 | reset_limits(bsys); |
||
249 | |||
250 | // if exiting, do not enter glib loop |
||
251 | if (bsys->exiting) { |
||
252 | return bsys->exit_code; |
||
253 | } |
||
254 | |||
255 | // enter glib loop |
||
256 | g_main_loop_run(bsys->gloop); |
||
257 | |||
258 | ASSERT(bsys->exiting) |
||
259 | |||
260 | return bsys->exit_code; |
||
261 | } |
||
262 | |||
263 | void BReactor_Quit (BReactor *bsys, int code) |
||
264 | { |
||
265 | DebugObject_Access(&bsys->d_obj); |
||
266 | |||
267 | // remember exiting |
||
268 | bsys->exiting = 1; |
||
269 | bsys->exit_code = code; |
||
270 | |||
271 | // request termination of glib loop |
||
272 | g_main_loop_quit(bsys->gloop); |
||
273 | } |
||
274 | |||
275 | void BReactor_SetSmallTimer (BReactor *bsys, BSmallTimer *bt, int mode, btime_t time) |
||
276 | { |
||
277 | DebugObject_Access(&bsys->d_obj); |
||
278 | assert_timer(bt); |
||
279 | |||
280 | // remove timer if it's already set |
||
281 | BReactor_RemoveSmallTimer(bsys, bt); |
||
282 | |||
283 | // if mode is absolute, subtract current time |
||
284 | if (mode == BTIMER_SET_ABSOLUTE) { |
||
285 | btime_t now = btime_gettime(); |
||
286 | time = (time < now ? 0 : time - now); |
||
287 | } |
||
288 | |||
289 | // set active and reactor |
||
290 | bt->active = 1; |
||
291 | bt->reactor = bsys; |
||
292 | |||
293 | // init source |
||
294 | bt->source = g_timeout_source_new(time); |
||
295 | g_source_set_callback(bt->source, timer_source_handler, bt, NULL); |
||
296 | g_source_attach(bt->source, g_main_loop_get_context(bsys->gloop)); |
||
297 | |||
298 | DebugCounter_Increment(&bsys->d_timers_ctr); |
||
299 | } |
||
300 | |||
301 | void BReactor_RemoveSmallTimer (BReactor *bsys, BSmallTimer *bt) |
||
302 | { |
||
303 | DebugObject_Access(&bsys->d_obj); |
||
304 | assert_timer(bt); |
||
305 | |||
306 | // do nothing if timer is not active |
||
307 | if (!bt->active) { |
||
308 | return; |
||
309 | } |
||
310 | |||
311 | // free source |
||
312 | g_source_destroy(bt->source); |
||
313 | g_source_unref(bt->source); |
||
314 | |||
315 | // set not active |
||
316 | bt->active = 0; |
||
317 | |||
318 | DebugCounter_Decrement(&bsys->d_timers_ctr); |
||
319 | } |
||
320 | |||
321 | void BReactor_SetTimer (BReactor *bsys, BTimer *bt) |
||
322 | { |
||
323 | BReactor_SetSmallTimer(bsys, &bt->base, BTIMER_SET_RELATIVE, bt->msTime); |
||
324 | } |
||
325 | |||
326 | void BReactor_SetTimerAfter (BReactor *bsys, BTimer *bt, btime_t after) |
||
327 | { |
||
328 | BReactor_SetSmallTimer(bsys, &bt->base, BTIMER_SET_RELATIVE, after); |
||
329 | } |
||
330 | |||
331 | void BReactor_SetTimerAbsolute (BReactor *bsys, BTimer *bt, btime_t time) |
||
332 | { |
||
333 | BReactor_SetSmallTimer(bsys, &bt->base, BTIMER_SET_ABSOLUTE, time); |
||
334 | } |
||
335 | |||
336 | void BReactor_RemoveTimer (BReactor *bsys, BTimer *bt) |
||
337 | { |
||
338 | return BReactor_RemoveSmallTimer(bsys, &bt->base); |
||
339 | } |
||
340 | |||
341 | BPendingGroup * BReactor_PendingGroup (BReactor *bsys) |
||
342 | { |
||
343 | DebugObject_Access(&bsys->d_obj); |
||
344 | |||
345 | return &bsys->pending_jobs; |
||
346 | } |
||
347 | |||
348 | int BReactor_Synchronize (BReactor *bsys, BSmallPending *ref) |
||
349 | { |
||
350 | DebugObject_Access(&bsys->d_obj); |
||
351 | ASSERT(ref) |
||
352 | |||
353 | while (!bsys->exiting) { |
||
354 | ASSERT(BPendingGroup_HasJobs(&bsys->pending_jobs)) |
||
355 | |||
356 | if (BPendingGroup_PeekJob(&bsys->pending_jobs) == ref) { |
||
357 | return 1; |
||
358 | } |
||
359 | |||
360 | BPendingGroup_ExecuteJob(&bsys->pending_jobs); |
||
361 | } |
||
362 | |||
363 | return 0; |
||
364 | } |
||
365 | |||
366 | int BReactor_AddFileDescriptor (BReactor *bsys, BFileDescriptor *bs) |
||
367 | { |
||
368 | DebugObject_Access(&bsys->d_obj); |
||
369 | ASSERT(!bs->active) |
||
370 | |||
371 | // set active, no wait events, and set reactor |
||
372 | bs->active = 1; |
||
373 | bs->waitEvents = 0; |
||
374 | bs->reactor = bsys; |
||
375 | |||
376 | // create source |
||
377 | bs->source = g_source_new(&bsys->fd_source_funcs, sizeof(struct fd_source)); |
||
378 | ((struct fd_source *)bs->source)->bfd = bs; |
||
379 | |||
380 | // init pollfd |
||
381 | bs->pollfd.fd = bs->fd; |
||
382 | bs->pollfd.events = get_glib_wait_events(bs->waitEvents); |
||
383 | bs->pollfd.revents = 0; |
||
384 | |||
385 | // start source |
||
386 | g_source_add_poll(bs->source, &bs->pollfd); |
||
387 | g_source_attach(bs->source, g_main_loop_get_context(bsys->gloop)); |
||
388 | |||
389 | DebugCounter_Increment(&bsys->d_fds_counter); |
||
390 | return 1; |
||
391 | } |
||
392 | |||
393 | void BReactor_RemoveFileDescriptor (BReactor *bsys, BFileDescriptor *bs) |
||
394 | { |
||
395 | DebugObject_Access(&bsys->d_obj); |
||
396 | DebugCounter_Decrement(&bsys->d_fds_counter); |
||
397 | ASSERT(bs->active) |
||
398 | |||
399 | // free source |
||
400 | g_source_destroy(bs->source); |
||
401 | g_source_unref(bs->source); |
||
402 | |||
403 | // set not active |
||
404 | bs->active = 0; |
||
405 | } |
||
406 | |||
407 | void BReactor_SetFileDescriptorEvents (BReactor *bsys, BFileDescriptor *bs, int events) |
||
408 | { |
||
409 | DebugObject_Access(&bsys->d_obj); |
||
410 | ASSERT(bs->active) |
||
411 | ASSERT(!(events&~(BREACTOR_READ|BREACTOR_WRITE))) |
||
412 | |||
413 | // set new wait events |
||
414 | bs->waitEvents = events; |
||
415 | |||
416 | // update pollfd wait events |
||
417 | bs->pollfd.events = get_glib_wait_events(bs->waitEvents); |
||
418 | } |
||
419 | |||
420 | int BReactor_InitFromExistingGMainLoop (BReactor *bsys, GMainLoop *gloop, int unref_gloop_on_free) |
||
421 | { |
||
422 | ASSERT(gloop) |
||
423 | ASSERT(unref_gloop_on_free == !!unref_gloop_on_free) |
||
424 | |||
425 | // set not exiting |
||
426 | bsys->exiting = 0; |
||
427 | |||
428 | // set gloop and unref on free flag |
||
429 | bsys->gloop = gloop; |
||
430 | bsys->unref_gloop_on_free = unref_gloop_on_free; |
||
431 | |||
432 | // init fd source functions table |
||
433 | memset(&bsys->fd_source_funcs, 0, sizeof(bsys->fd_source_funcs)); |
||
434 | bsys->fd_source_funcs.prepare = fd_source_func_prepare; |
||
435 | bsys->fd_source_funcs.check = fd_source_func_check; |
||
436 | bsys->fd_source_funcs.dispatch = fd_source_func_dispatch; |
||
437 | bsys->fd_source_funcs.finalize = NULL; |
||
438 | |||
439 | // init job queue |
||
440 | BPendingGroup_Init(&bsys->pending_jobs); |
||
441 | |||
442 | // init active limits list |
||
443 | LinkedList1_Init(&bsys->active_limits_list); |
||
444 | |||
445 | DebugCounter_Init(&bsys->d_fds_counter); |
||
446 | DebugCounter_Init(&bsys->d_limits_ctr); |
||
447 | DebugCounter_Init(&bsys->d_timers_ctr); |
||
448 | DebugObject_Init(&bsys->d_obj); |
||
449 | return 1; |
||
450 | } |
||
451 | |||
452 | GMainLoop * BReactor_GetGMainLoop (BReactor *bsys) |
||
453 | { |
||
454 | DebugObject_Access(&bsys->d_obj); |
||
455 | |||
456 | return bsys->gloop; |
||
457 | } |
||
458 | |||
459 | int BReactor_SynchronizeAll (BReactor *bsys) |
||
460 | { |
||
461 | DebugObject_Access(&bsys->d_obj); |
||
462 | |||
463 | dispatch_pending(bsys); |
||
464 | |||
465 | return !bsys->exiting; |
||
466 | } |
||
467 | |||
468 | void BReactorLimit_Init (BReactorLimit *o, BReactor *reactor, int limit) |
||
469 | { |
||
470 | DebugObject_Access(&reactor->d_obj); |
||
471 | ASSERT(limit > 0) |
||
472 | |||
473 | // init arguments |
||
474 | o->reactor = reactor; |
||
475 | o->limit = limit; |
||
476 | |||
477 | // set count zero |
||
478 | o->count = 0; |
||
479 | |||
480 | DebugCounter_Increment(&reactor->d_limits_ctr); |
||
481 | DebugObject_Init(&o->d_obj); |
||
482 | } |
||
483 | |||
484 | void BReactorLimit_Free (BReactorLimit *o) |
||
485 | { |
||
486 | BReactor *reactor = o->reactor; |
||
487 | DebugObject_Free(&o->d_obj); |
||
488 | DebugCounter_Decrement(&reactor->d_limits_ctr); |
||
489 | |||
490 | // remove from active limits list |
||
491 | if (o->count > 0) { |
||
492 | LinkedList1_Remove(&reactor->active_limits_list, &o->active_limits_list_node); |
||
493 | } |
||
494 | } |
||
495 | |||
496 | int BReactorLimit_Increment (BReactorLimit *o) |
||
497 | { |
||
498 | BReactor *reactor = o->reactor; |
||
499 | DebugObject_Access(&o->d_obj); |
||
500 | |||
501 | // check count against limit |
||
502 | if (o->count >= o->limit) { |
||
503 | return 0; |
||
504 | } |
||
505 | |||
506 | // increment count |
||
507 | o->count++; |
||
508 | |||
509 | // if limit was zero, add to active limits list |
||
510 | if (o->count == 1) { |
||
511 | LinkedList1_Append(&reactor->active_limits_list, &o->active_limits_list_node); |
||
512 | } |
||
513 | |||
514 | return 1; |
||
515 | } |
||
516 | |||
517 | void BReactorLimit_SetLimit (BReactorLimit *o, int limit) |
||
518 | { |
||
519 | DebugObject_Access(&o->d_obj); |
||
520 | ASSERT(limit > 0) |
||
521 | |||
522 | // set limit |
||
523 | o->limit = limit; |
||
524 | } |