File: libsdevent.c

package info (click to toggle)
libwebsockets 4.3.5-2
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 31,288 kB
  • sloc: ansic: 194,407; javascript: 1,550; sh: 1,387; cpp: 505; java: 461; perl: 405; xml: 118; makefile: 76; awk: 5
file content (86 lines) | stat: -rw-r--r-- 1,842 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
 * lws-minimal-http-server-eventlib-foreign
 *
 * Written in 2020 by Christian Fuchs <christian.fuchs@scs.ch>
 *
 * 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);
	}

	return 0;
}

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

static void
foreign_event_loop_init_and_run_libsdevent(void)
{
	uint64_t now;

	/* 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);

	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
};