aboutsummaryrefslogtreecommitdiffstats
path: root/src/evmgr.c
blob: d26502df488e8c19a7360df6fb08fea1c4bf744b (plain)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
 * Copyright (C) 2016-2019 "IoT.bzh"
 * Author José Bollo <jose.bollo@iot.bzh>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define _GNU_SOURCE

#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <time.h>
#include <sys/syscall.h>
#include <pthread.h>
#include <errno.h>
#include <assert.h>
#include <sys/eventfd.h>

#include <systemd/sd-event.h>

#include "evmgr.h"
#include "verbose.h"

#if WITH_SYSTEMD
#include "systemd.h"
#else
#include "fdev-epoll.h"
#include "fdev.h"
#endif

/** Description of handled event loops */
struct evmgr
{
	int efd;               /**< event notification */
	unsigned state;        /**< encoded state */
	void *holder;          /**< holder of the evmgr */
#if WITH_SYSTEMD
	struct sd_event *sdev; /**< the systemd event loop */
#endif
#if WITH_FDEV_EPOLL
	struct fdev_epoll *fdev_epoll;
	struct fdev *fdev;
#endif
};

#define EVLOOP_STATE_WAIT           1U
#define EVLOOP_STATE_RUN            2U


/**
 * Internal callback for evmgr management.
 * The effect of this function is hidden: it exits
 * the waiting poll if any.
 */
static void evmgr_on_efd_event(struct evmgr *evmgr)
{
	uint64_t x;
	read(evmgr->efd, &x, sizeof x);
}

/**
 * wakeup the event loop if needed by sending
 * an event.
 */
void evmgr_wakeup(struct evmgr *evmgr)
{
	uint64_t x;

	if (evmgr->state & EVLOOP_STATE_WAIT) {
		x = 1;
		write(evmgr->efd, &x, sizeof x);
	}
}

/**
 */
void *evmgr_holder(struct evmgr *evmgr)
{
	return evmgr->holder;
}

/**
 */
int evmgr_release_if(struct evmgr *evmgr, void *holder)
{
	if (evmgr->holder != holder)
		return 0;
	evmgr->holder = 0;
	return 1;
}

/**
 */
int evmgr_try_hold(struct evmgr *evmgr, void *holder)
{
	if (!evmgr->holder)
		evmgr->holder = holder;
	return evmgr->holder == holder;
}

/******************************************************************************/
/******************************************************************************/
/******  SYSTEM D                                                        ******/
/******************************************************************************/
/******************************************************************************/

#if WITH_SYSTEMD

/**
 * Run the event loop is set.
 */
void evmgr_run(struct evmgr *evmgr)
{
	int rc;
	struct sd_event *se;

	evmgr->state = EVLOOP_STATE_WAIT|EVLOOP_STATE_RUN;
	se = evmgr->sdev;
	rc = sd_event_prepare(se);
	if (rc < 0) {
		errno = -rc;
		CRITICAL("sd_event_prepare returned an error (state: %d): %m", sd_event_get_state(se));
		abort();
	} else {
		if (rc == 0) {
			rc = sd_event_wait(se, (uint64_t)(int64_t)-1);
			if (rc < 0) {
				errno = -rc;
				ERROR("sd_event_wait returned an error (state: %d): %m", sd_event_get_state(se));
			}
		}
		evmgr->state = EVLOOP_STATE_RUN;
		if (rc > 0) {
			rc = sd_event_dispatch(se);
			if (rc < 0) {
				errno = -rc;
				ERROR("sd_event_dispatch returned an error (state: %d): %m", sd_event_get_state(se));
			}
		}
	}
	evmgr->state = 0;
}

void evmgr_job_run(int signum, struct evmgr *evmgr)
{
	if (signum)
		evmgr->state = 0;
	else
		evmgr_run(evmgr);
}

int evmgr_can_run(struct evmgr *evmgr)
{
	return !evmgr->state;
}

/**
 * Internal callback for evmgr management.
 * The effect of this function is hidden: it exits
 * the waiting poll if any. Then it wakes up a thread
 * awaiting the evmgr using signal.
 */
static int on_evmgr_efd(sd_event_source *s, int fd, uint32_t revents, void *userdata)
{
	struct evmgr *evmgr = userdata;
	evmgr_on_efd_event(evmgr);
	return 1;
}

/**
 * Gets a sd_event item for the current thread.
 * @return a sd_event or NULL in case of error
 */
int evmgr_create(struct evmgr **result)
{
	int rc;
	struct evmgr *evmgr;

	/* creates the evmgr on need */
	evmgr = malloc(sizeof *evmgr);
	if (!evmgr) {
		ERROR("out of memory");
		rc = -ENOMEM;
		goto error;
	}

	/* creates the eventfd for waking up polls */
	evmgr->efd = eventfd(0, EFD_CLOEXEC|EFD_SEMAPHORE);
	if (evmgr->efd < 0) {
		ERROR("can't make eventfd for events");
		rc = -errno;
		goto error1;
	}
	/* create the systemd event loop */
	evmgr->sdev = systemd_get_event_loop();
	if (!evmgr->sdev) {
		ERROR("can't make new event loop");
		goto error2;
	}
	/* put the eventfd in the event loop */
	rc = sd_event_add_io(evmgr->sdev, NULL, evmgr->efd, EPOLLIN, on_evmgr_efd, evmgr);
	if (rc < 0) {
		ERROR("can't register eventfd");
		goto error2;
	}

	/* start the creation */
	evmgr->state = 0;
	evmgr->holder = 0;
	*result = evmgr;
	return 0;


error2:
	close(evmgr->efd);
error1:
	free(evmgr);
error:
	*result = 0;
	return rc;
}

#endif
#if WITH_FDEV_EPOLL

/**
 * Run the event loop is set.
 */
void evmgr_run(struct evmgr *evmgr)
{
	int rc;

	evmgr->state = EVLOOP_STATE_WAIT|EVLOOP_STATE_RUN;
	rc = fdev_epoll_wait_and_dispatch(evmgr->fdev_epoll, -1);
	evmgr->state = 0;
}

void evmgr_job_run(int signum, struct evmgr *evmgr)
{
	if (signum)
		evmgr->state = 0;
	else
		evmgr_run(evmgr);
}

int evmgr_can_run(struct evmgr *evmgr)
{
	return !evmgr->state;
}

struct fdev_epoll *evmgr_get_fdev_epoll(struct evmgr *evmgr)
{
	return evmgr->fdev_epoll;
}

/**
 * Internal callback for evmgr management.
 * The effect of this function is hidden: it exits
 * the waiting poll if any. Then it wakes up a thread
 * awaiting the evmgr using signal.
 */
static void on_evmgr_efd(void *closure, uint32_t event, struct fdev *fdev)
{
	struct evmgr *evmgr = closure;
	evmgr_on_efd_event(evmgr);
}

/**
 * Gets a sd_event item for the current thread.
 * @return a sd_event or NULL in case of error
 */
int evmgr_create(struct evmgr **result)
{
	int rc;
	struct evmgr *evmgr;

	/* creates the evmgr on need */
	evmgr = malloc(sizeof *evmgr);
	if (!evmgr) {
		ERROR("out of memory");
		rc = -ENOMEM;
		goto error;
	}

	/* creates the eventfd for waking up polls */
	evmgr->efd = eventfd(0, EFD_CLOEXEC|EFD_SEMAPHORE);
	if (evmgr->efd < 0) {
		ERROR("can't make eventfd for events");
		rc = -errno;
		goto error1;
	}

	/* create the systemd event loop */
	evmgr->fdev_epoll = fdev_epoll_create();
	if (!evmgr->fdev_epoll) {
		ERROR("can't make new event loop");
		goto error2;
	}
	/* put the eventfd in the event loop */
	evmgr->fdev = fdev_epoll_add(evmgr->fdev_epoll, evmgr->efd);
	if (evmgr->fdev == NULL) {
		ERROR("can't add eventfd");
		goto error3;
	}

	fdev_set_callback(evmgr->fdev, on_evmgr_efd, evmgr);
	fdev_set_events(evmgr->fdev, EPOLLIN);

	/* start the creation */
	evmgr->state = 0;
	evmgr->holder = 0;
	*result = evmgr;
	return 0;


error3:
	fdev_epoll_destroy(evmgr->fdev_epoll);
error2:
	close(evmgr->efd);
error1:
	free(evmgr);
error:
	*result = 0;
	return rc;
}

#endif