Skip to content

Commit

Permalink
add sd-event support to example «minimal-http-server-eventlib-foreign»
Browse files Browse the repository at this point in the history
  • Loading branch information
zishfish committed Jan 6, 2021
1 parent b9ed407 commit e6fc0d8
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ if (LWS_WITH_GLIB)
set(extralibs ${extralibs} ${GLIB_LIBRARIES})
list(APPEND SRCS glib.c)
endif()
if (LWS_WITH_SDEVENT)
find_path(LIBSYSTEMD_INCLUDE_DIRS NAMES systemd/sd-event.h)
find_library(LIBSYSTEMD_LIBRARIES NAMES systemd)
message("libsystemd include dir: ${LIBSYSTEMD_INCLUDE_DIRS}")
message("libsystemd libraries: ${LIBSYSTEMD_LIBRARIES}")
include_directories("${LIBSYSTEMD_INCLUDE_DIRS}")
set(extralibs ${extralibs} ${LIBSYSTEMD_LIBRARIES})
list(APPEND SRCS libsdevent.c)
endif()


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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Commandline option|Meaning
--uv|Use the libuv event library (lws must have been configured with `-DLWS_WITH_LIBUV=1`)
--event|Use the libevent library (lws must have been configured with `-DLWS_WITH_LIBEVENT=1`)
--ev|Use the libev event library (lws must have been configured with `-DLWS_WITH_LIBEV=1`)
--sd|Use the systemd event library (lws must have been configured with `-DLWS_WITH_SDEVENT=1`)

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* lws-minimal-http-server-eventlib-foreign
*
* Written in 2020 by Christian Fuchs <[email protected]>
*
* This file is made available under the Creative Commons CC0 1.0
* Universal Public Domain Dedication.
*
* The sdevent specific code
*/

#include <libwebsockets.h>

#include <string.h>
#include <signal.h>

#include <systemd/sd-event.h>

#include "private.h"

static struct sd_event *sd_loop;

static sd_event_source *sd_timer;
static sd_event_source *sd_signal;

static int
timer_cb_sd(sd_event_source* source,
uint64_t now,
void* user)
{
foreign_timer_service(sd_loop);

if (sd_timer) {
sd_event_source_set_time(sd_timer, now + 1000000);
sd_event_source_set_enabled(sd_timer, SD_EVENT_ON);
}
}

static int
signal_cb_sd(sd_event_source* source,
const struct signalfd_siginfo *si,
void* user)
{
signal_cb(si->ssi_signo);
return 0;
}

static void
foreign_event_loop_init_and_run_libsdevent(void)
{
/* we create and start our "foreign loop" */

sd_event_default(&sd_loop);
sd_event_add_signal(sd_loop, &sd_signal, SIGINT, signal_cb_sd, NULL);
uint64_t now;
sd_event_now(sd_loop, CLOCK_MONOTONIC, &now);
sd_event_add_time(sd_loop, &sd_timer, CLOCK_MONOTONIC, now, (uint64_t) 1000, timer_cb_sd, NULL);

sd_event_loop(sd_loop);
}

static void
foreign_event_loop_stop_libsdevent(void)
{
sd_event_exit(sd_loop, 0);
}

static void
foreign_event_loop_cleanup_libsdevent(void)
{
sd_event_source_set_enabled(sd_timer, SD_EVENT_OFF);
sd_timer = sd_event_source_unref(sd_timer);

sd_event_source_set_enabled(sd_signal, SD_EVENT_OFF);
sd_signal = sd_event_source_unref(sd_signal);

sd_loop = sd_event_unref(sd_loop);
}

const struct ops ops_sdevent = {
foreign_event_loop_init_and_run_libsdevent,
foreign_event_loop_stop_libsdevent,
foreign_event_loop_cleanup_libsdevent
};

Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,17 @@ int main(int argc, const char **argv)
ops = &ops_glib;
lwsl_notice("%s: using glib loop\n", __func__);
} else
#endif
#if defined(LWS_WITH_SDEVENT)
if (lws_cmdline_option(argc, argv, "--sd")) {
info.options |= LWS_SERVER_OPTION_SDEVENT;
ops = &ops_sdevent;
lwsl_notice("%s: using sd-event loop\n", __func__);
} else
#endif
{
lwsl_err("This app only makes sense when used\n");
lwsl_err(" with a foreign loop, --uv, --event, --glib, or --ev\n");
lwsl_err(" with a foreign loop, --uv, --event, --glib, --ev or --sd\n");

return 1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ extern int lifetime, reported;
void foreign_timer_service(void *foreign_loop);
void signal_cb(int signum);

extern const struct ops ops_libuv, ops_libevent, ops_glib, ops_libev;
extern const struct ops ops_libuv, ops_libevent, ops_glib, ops_libev, ops_sdevent;

0 comments on commit e6fc0d8

Please sign in to comment.