[Libwebsockets] [PATCH 07/11] libev: add initial implementation of event_ops->service()

Alejandro Mery amery at geeks.cl
Tue Dec 16 00:36:28 CET 2014


Signed-off-by: Alejandro Mery <amery at geeks.cl>
---
 lib/event-libev.c           | 59 +++++++++++++++++++++++++++++++++++++++++++++
 lib/private-libwebsockets.h |  2 ++
 2 files changed, 61 insertions(+)

diff --git a/lib/event-libev.c b/lib/event-libev.c
index 8fcc3d7..697f5e3 100644
--- a/lib/event-libev.c
+++ b/lib/event-libev.c
@@ -21,6 +21,37 @@
 
 #include "private-libwebsockets.h"
 
+/* error: dereferencing type-punned pointer will break strict-aliasing rules */
+#ifdef ev_timer_init
+#undef ev_timer_init
+static inline void ev_timer_init(struct ev_timer *w,
+				 void (*cb)(struct ev_loop *, struct ev_timer *, int),
+				 ev_tstamp after, ev_tstamp repeat)
+{
+	ev_init(w, cb);
+	ev_timer_set(w, after, repeat);
+}
+#endif
+
+/* ev_timer_set() also triggers dereferencing warnings, but a reset is more useful */
+static inline void ev_timer_reset(struct ev_loop *loop,
+				  struct ev_timer *w,
+				  ev_tstamp after, ev_tstamp repeat)
+{
+	ev_timer_stop(loop, w);
+	w->at = after;
+	w->repeat = repeat;
+	ev_timer_start(loop, w);
+}
+
+/* just a dummy placeholder to "break" ev_run(..., EVRUN_ONCE) */
+static void
+libev_timeout_cb(struct ev_loop *loop, struct ev_timer *w, int revents)
+{
+	lwsl_debug("%s(%p, %p, %u): libev timeout!\n",
+		   __func__, loop, w, revents);
+}
+
 static int
 libev_init(struct lws_context_creation_info *info,
 	   struct libwebsocket_context *context)
@@ -62,9 +93,37 @@ libev_init(struct lws_context_creation_info *info,
 
 	lwsl_notice(" libev backend: %s\n", backend_name);
 
+	/* update the internal timestamp so ev_now() works since before the first ev_run */
+	ev_now_update(evi->loop);
+
+	/* timeout watcher */
+	ev_timer_init(&evc->w_timeout, libev_timeout_cb, 0., 0.);
+
+	return 1;
+}
+
+static int
+libev_service(struct libwebsocket_context *context, int timeout_ms)
+{
+	int flags = EVRUN_ONCE;
+	struct _lws_libev_event_context *evc = &context->e.ev;
+
+	/* timeout */
+	if (timeout_ms > 0) {
+		ev_timer_reset(evc->loop, &evc->w_timeout,
+			       ((double)timeout_ms) / 1000.0,
+			       0.);
+	} else {
+		flags |= EVRUN_NOWAIT;
+	}
+
+	/* run once */
+	ev_run(evc->loop, flags);
+
 	return 0;
 }
 
 struct lws_event_ops lws_libev_event_ops = {
 	.init = libev_init,
+	.service = libev_service,
 };
diff --git a/lib/private-libwebsockets.h b/lib/private-libwebsockets.h
index eb909c7..5c2c034 100755
--- a/lib/private-libwebsockets.h
+++ b/lib/private-libwebsockets.h
@@ -416,6 +416,8 @@ extern struct lws_event_ops lws_libev_event_ops;
 #ifdef LWS_USE_LIBEV
 struct _lws_libev_event_context {
 	struct ev_loop *loop;
+
+	struct ev_timer w_timeout;
 	struct ev_io w_accept;
 	struct ev_signal w_sigint;
 };
-- 
2.2.0




More information about the Libwebsockets mailing list