Skip to content

Commit e6fc0d8

Browse files
committed
add sd-event support to example «minimal-http-server-eventlib-foreign»
1 parent b9ed407 commit e6fc0d8

File tree

5 files changed

+105
-2
lines changed

5 files changed

+105
-2
lines changed

minimal-examples/http-server/minimal-http-server-eventlib-foreign/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ if (LWS_WITH_GLIB)
7272
set(extralibs ${extralibs} ${GLIB_LIBRARIES})
7373
list(APPEND SRCS glib.c)
7474
endif()
75+
if (LWS_WITH_SDEVENT)
76+
find_path(LIBSYSTEMD_INCLUDE_DIRS NAMES systemd/sd-event.h)
77+
find_library(LIBSYSTEMD_LIBRARIES NAMES systemd)
78+
message("libsystemd include dir: ${LIBSYSTEMD_INCLUDE_DIRS}")
79+
message("libsystemd libraries: ${LIBSYSTEMD_LIBRARIES}")
80+
include_directories("${LIBSYSTEMD_INCLUDE_DIRS}")
81+
set(extralibs ${extralibs} ${LIBSYSTEMD_LIBRARIES})
82+
list(APPEND SRCS libsdevent.c)
83+
endif()
84+
7585

7686
message("Extra libs: ${extralibs}")
7787

minimal-examples/http-server/minimal-http-server-eventlib-foreign/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Commandline option|Meaning
66
--uv|Use the libuv event library (lws must have been configured with `-DLWS_WITH_LIBUV=1`)
77
--event|Use the libevent library (lws must have been configured with `-DLWS_WITH_LIBEVENT=1`)
88
--ev|Use the libev event library (lws must have been configured with `-DLWS_WITH_LIBEV=1`)
9+
--sd|Use the systemd event library (lws must have been configured with `-DLWS_WITH_SDEVENT=1`)
910

1011
Notice libevent and libev cannot coexist in the one library. But all the other combinations are OK.
1112

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* lws-minimal-http-server-eventlib-foreign
3+
*
4+
* Written in 2020 by Christian Fuchs <[email protected]>
5+
*
6+
* This file is made available under the Creative Commons CC0 1.0
7+
* Universal Public Domain Dedication.
8+
*
9+
* The sdevent specific code
10+
*/
11+
12+
#include <libwebsockets.h>
13+
14+
#include <string.h>
15+
#include <signal.h>
16+
17+
#include <systemd/sd-event.h>
18+
19+
#include "private.h"
20+
21+
static struct sd_event *sd_loop;
22+
23+
static sd_event_source *sd_timer;
24+
static sd_event_source *sd_signal;
25+
26+
static int
27+
timer_cb_sd(sd_event_source* source,
28+
uint64_t now,
29+
void* user)
30+
{
31+
foreign_timer_service(sd_loop);
32+
33+
if (sd_timer) {
34+
sd_event_source_set_time(sd_timer, now + 1000000);
35+
sd_event_source_set_enabled(sd_timer, SD_EVENT_ON);
36+
}
37+
}
38+
39+
static int
40+
signal_cb_sd(sd_event_source* source,
41+
const struct signalfd_siginfo *si,
42+
void* user)
43+
{
44+
signal_cb(si->ssi_signo);
45+
return 0;
46+
}
47+
48+
static void
49+
foreign_event_loop_init_and_run_libsdevent(void)
50+
{
51+
/* we create and start our "foreign loop" */
52+
53+
sd_event_default(&sd_loop);
54+
sd_event_add_signal(sd_loop, &sd_signal, SIGINT, signal_cb_sd, NULL);
55+
uint64_t now;
56+
sd_event_now(sd_loop, CLOCK_MONOTONIC, &now);
57+
sd_event_add_time(sd_loop, &sd_timer, CLOCK_MONOTONIC, now, (uint64_t) 1000, timer_cb_sd, NULL);
58+
59+
sd_event_loop(sd_loop);
60+
}
61+
62+
static void
63+
foreign_event_loop_stop_libsdevent(void)
64+
{
65+
sd_event_exit(sd_loop, 0);
66+
}
67+
68+
static void
69+
foreign_event_loop_cleanup_libsdevent(void)
70+
{
71+
sd_event_source_set_enabled(sd_timer, SD_EVENT_OFF);
72+
sd_timer = sd_event_source_unref(sd_timer);
73+
74+
sd_event_source_set_enabled(sd_signal, SD_EVENT_OFF);
75+
sd_signal = sd_event_source_unref(sd_signal);
76+
77+
sd_loop = sd_event_unref(sd_loop);
78+
}
79+
80+
const struct ops ops_sdevent = {
81+
foreign_event_loop_init_and_run_libsdevent,
82+
foreign_event_loop_stop_libsdevent,
83+
foreign_event_loop_cleanup_libsdevent
84+
};
85+

minimal-examples/http-server/minimal-http-server-eventlib-foreign/minimal-http-server-eventlib-foreign.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,17 @@ int main(int argc, const char **argv)
196196
ops = &ops_glib;
197197
lwsl_notice("%s: using glib loop\n", __func__);
198198
} else
199+
#endif
200+
#if defined(LWS_WITH_SDEVENT)
201+
if (lws_cmdline_option(argc, argv, "--sd")) {
202+
info.options |= LWS_SERVER_OPTION_SDEVENT;
203+
ops = &ops_sdevent;
204+
lwsl_notice("%s: using sd-event loop\n", __func__);
205+
} else
199206
#endif
200207
{
201208
lwsl_err("This app only makes sense when used\n");
202-
lwsl_err(" with a foreign loop, --uv, --event, --glib, or --ev\n");
209+
lwsl_err(" with a foreign loop, --uv, --event, --glib, --ev or --sd\n");
203210

204211
return 1;
205212
}

minimal-examples/http-server/minimal-http-server-eventlib-foreign/private.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ extern int lifetime, reported;
1212
void foreign_timer_service(void *foreign_loop);
1313
void signal_cb(int signum);
1414

15-
extern const struct ops ops_libuv, ops_libevent, ops_glib, ops_libev;
15+
extern const struct ops ops_libuv, ops_libevent, ops_glib, ops_libev, ops_sdevent;

0 commit comments

Comments
 (0)