1 /*
2 * lws-minimal-http-server-eventlib-foreign
3 *
4 * Written in 2010-2020 by Andy Green <andy@warmcat.com>
5 *
6 * This file is made available under the Creative Commons CC0 1.0
7 * Universal Public Domain Dedication.
8 *
9 * The libev specific code
10 */
11
12 #include <libwebsockets.h>
13
14 #include <ev.h>
15
16 #include <string.h>
17 #include <signal.h>
18
19 #include "private.h"
20
21 static struct ev_loop *loop_ev;
22 static struct ev_timer timer_outer_ev;
23 static struct ev_signal sighandler_ev;
24
25 static void
timer_cb_ev(struct ev_loop * loop,struct ev_timer * watcher,int revents)26 timer_cb_ev(struct ev_loop *loop, struct ev_timer *watcher, int revents)
27 {
28 foreign_timer_service(loop_ev);
29 }
30
31 static void
signal_cb_ev(struct ev_loop * loop,struct ev_signal * watcher,int revents)32 signal_cb_ev(struct ev_loop *loop, struct ev_signal *watcher, int revents)
33 {
34 signal_cb(watcher->signum);
35 }
36
37 static void
foreign_event_loop_init_and_run_libev(void)38 foreign_event_loop_init_and_run_libev(void)
39 {
40 /* we create and start our "foreign loop" */
41
42 loop_ev = ev_loop_new(0);
43
44 ev_signal_init(&sighandler_ev, signal_cb_ev, SIGINT);
45 ev_signal_start(loop_ev, &sighandler_ev);
46
47 ev_timer_init(&timer_outer_ev, timer_cb_ev, 0, 1);
48 ev_timer_start(loop_ev, &timer_outer_ev);
49
50 ev_run(loop_ev, 0);
51 }
52
53 static void
foreign_event_loop_stop_libev(void)54 foreign_event_loop_stop_libev(void)
55 {
56 ev_break(loop_ev, EVBREAK_ALL);
57 }
58
59 static void
foreign_event_loop_cleanup_libev(void)60 foreign_event_loop_cleanup_libev(void)
61 {
62 /* cleanup the foreign loop assets */
63
64 ev_timer_stop(loop_ev, &timer_outer_ev);
65 ev_signal_stop(loop_ev, &sighandler_ev);
66
67 ev_run(loop_ev, 0);
68 ev_loop_destroy(loop_ev);
69 }
70
71 const struct ops ops_libev = {
72 foreign_event_loop_init_and_run_libev,
73 foreign_event_loop_stop_libev,
74 foreign_event_loop_cleanup_libev
75 };
76
77