summaryrefslogtreecommitdiff
path: root/src/libmowgli/eventloop/eventloop.h
blob: 42a257c640b8ac32b577f7933106d5cf7562a8e4 (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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
 * Copyright (c) 2011, 2012 William Pitcock <nenolod@dereferenced.org>.
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef __MOWGLI_EVENTLOOP_EVENTLOOP_H__
#define __MOWGLI_EVENTLOOP_EVENTLOOP_H__

#ifdef MOWGLI_OS_OSX

# include <mach/mach.h>
# include <mach/mach_time.h>

#endif

#ifndef _WIN32

typedef int mowgli_descriptor_t;

#else

typedef SOCKET mowgli_descriptor_t;

#endif

typedef enum
{
	MOWGLI_EVENTLOOP_TYPE_POLLABLE,
	MOWGLI_EVENTLOOP_TYPE_HELPER,
	MOWGLI_EVENTLOOP_TYPE_ERROR = -1
} mowgli_eventloop_io_type_t;

typedef struct
{
	mowgli_eventloop_io_type_t type;
} mowgli_eventloop_io_obj_t;

typedef struct _mowgli_eventloop mowgli_eventloop_t;

typedef struct _mowgli_pollable mowgli_eventloop_pollable_t;
typedef struct _mowgli_helper mowgli_eventloop_helper_proc_t;

typedef struct _mowgli_linebuf mowgli_linebuf_t;

typedef enum
{
	MOWGLI_EVENTLOOP_IO_READ,
	MOWGLI_EVENTLOOP_IO_WRITE,
	MOWGLI_EVENTLOOP_IO_ERROR = -1
} mowgli_eventloop_io_dir_t;

typedef void mowgli_eventloop_io_t;

/* checked casts */
static inline mowgli_eventloop_pollable_t *
mowgli_eventloop_io_pollable(mowgli_eventloop_io_t *io)
{
	mowgli_eventloop_io_obj_t *obj = (mowgli_eventloop_io_obj_t *) io;

	return_val_if_fail(io != NULL, NULL);
	return_val_if_fail(obj->type == MOWGLI_EVENTLOOP_TYPE_POLLABLE, NULL);

	return (mowgli_eventloop_pollable_t *) io;
}

static inline mowgli_eventloop_helper_proc_t *
mowgli_eventloop_io_helper(mowgli_eventloop_io_t *io)
{
	mowgli_eventloop_io_obj_t *obj = (mowgli_eventloop_io_obj_t *) io;

	return_val_if_fail(io != NULL, NULL);
	return_val_if_fail(obj->type == MOWGLI_EVENTLOOP_TYPE_HELPER, NULL);

	return (mowgli_eventloop_helper_proc_t *) io;
}

static inline mowgli_eventloop_io_type_t
mowgli_eventloop_io_type(mowgli_eventloop_io_t *io)
{
	mowgli_eventloop_io_obj_t *obj = (mowgli_eventloop_io_obj_t *) io;

	return_val_if_fail(io != NULL, MOWGLI_EVENTLOOP_TYPE_ERROR);

	return obj->type;
}

typedef void mowgli_eventloop_io_cb_t (mowgli_eventloop_t * eventloop, mowgli_eventloop_io_t * io, mowgli_eventloop_io_dir_t dir, void *userdata);

struct _mowgli_pollable
{
	mowgli_eventloop_io_obj_t type;

	mowgli_descriptor_t fd;
	unsigned int slot;
	unsigned int events;

	mowgli_eventloop_io_cb_t *read_function;
	mowgli_eventloop_io_cb_t *write_function;
	mowgli_eventloop_io_cb_t *error_function;

	void *userdata;

	mowgli_node_t node;

	mowgli_eventloop_t *eventloop;
};

typedef struct
{
	void (*timeout_once)(mowgli_eventloop_t *eventloop, int timeout);
	void (*run_once)(mowgli_eventloop_t *eventloop);
	void (*pollsetup)(mowgli_eventloop_t *eventloop);
	void (*pollshutdown)(mowgli_eventloop_t *eventloop);
	void (*setselect)(mowgli_eventloop_t *eventloop, mowgli_eventloop_pollable_t *pollable, mowgli_eventloop_io_dir_t dir, mowgli_eventloop_io_cb_t *event_function);
	void (*select)(mowgli_eventloop_t *eventloop, int time);
	void (*destroy)(mowgli_eventloop_t *eventloop, mowgli_eventloop_pollable_t *pollable);
} mowgli_eventloop_ops_t;

struct _mowgli_eventloop
{
	time_t currtime;
	time_t deadline;

	const char *last_ran;

	mowgli_list_t timer_list;
	mowgli_mutex_t mutex;

	mowgli_eventloop_ops_t *eventloop_ops;
	void *poller;

	bool death_requested;

	void *data;

	time_t epochbias;
};

typedef void mowgli_event_dispatch_func_t (void *userdata);

typedef struct
{
	mowgli_node_t node;

	mowgli_event_dispatch_func_t *func;
	void *arg;
	const char *name;
	time_t frequency;
	time_t deadline;
	bool active;
} mowgli_eventloop_timer_t;

static inline void
mowgli_eventloop_set_time(mowgli_eventloop_t *eventloop, time_t newtime)
{
	return_if_fail(eventloop != NULL);

	eventloop->currtime = newtime;
}

static inline time_t
mowgli_eventloop_get_time(mowgli_eventloop_t *eventloop)
{
	return_val_if_fail(eventloop != NULL, 0);

	return eventloop->epochbias + eventloop->currtime;
}

static inline void
mowgli_eventloop_synchronize(mowgli_eventloop_t *eventloop)
{
	long long time_;

#if defined(CLOCK_MONOTONIC)
	struct timespec tp;

	clock_gettime(CLOCK_MONOTONIC, &tp);
	time_ = tp.tv_sec;
#elif defined(CLOCK_HIGHRES)
	struct timespec tp;

	clock_gettime(CLOCK_HIGHRES, &tp);
	time_ = tp.tv_sec;
#elif defined(MOWGLI_OS_WIN)
	static ULONGLONG (CALLBACK *GetTickCount64)(void) = NULL;
	static OSVERSIONINFOEX *winver = NULL;
	static bool load_err = false;

	if (winver == NULL)
	{
		winver = mowgli_alloc(sizeof(OSVERSIONINFOEX));
		winver->dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);

		if (!GetVersionEx((OSVERSIONINFO *) winver))
		{
			mowgli_free(winver);
			winver = NULL;	/* FIXME */
		}
	}

	if (winver && (winver->dwMajorVersion >= 6))
	{
		if ((GetTickCount64 == NULL) && !load_err)
		{
			HINSTANCE hKernel32;

			hKernel32 = GetModuleHandle("KERNEL32");
			GetTickCount64 = GetProcAddress(hKernel32, "GetTickCount64");

			if (GetTickCount64 == NULL)
				load_err = true;
		}

		if (load_err)
		{
			time_ = time(NULL);
		}
		else
		{
			soft_assert(GetTickCount64 != NULL);

			time_ = (int) (GetTickCount64() * 1e-3);
		}
	}
	else
	{
		time_ = time(NULL);
	}

#elif defined(MOWGLI_OS_OSX)
	static mach_timebase_info_data_t timebase;

	if (timebase.denom == 0)
		mach_timebase_info(&timebase);

	time_ = (int) (mach_absolute_time() * timebase.numer / timebase.denom * 1e-9);
#else
	time_ = time(NULL);
#endif
	mowgli_eventloop_set_time(eventloop, (time_t) time_);
}

/* Sets the bias of eventloop->currtime relative to Jan 1 00:00:00 1970 */
static inline void
mowgli_eventloop_calibrate(mowgli_eventloop_t *eventloop)
{
	mowgli_eventloop_synchronize(eventloop);
	eventloop->epochbias = time(NULL) - eventloop->currtime;
}

static inline bool
mowgli_eventloop_ignore_errno(int error)
{
	switch (error)
	{
#ifdef EINPROGRESS
	case EINPROGRESS:
#endif
#if defined(EWOULDBLOCK)
	case EWOULDBLOCK:
#endif
#if defined(EAGAIN) && (EWOULDBLOCK != EAGAIN)
	case EAGAIN:
#endif
#ifdef ETIME
	case ETIME:
#endif
#ifdef EINTR
	case EINTR:
#endif
#ifdef ERESTART
	case ERESTART:
#endif
#ifdef ENOBUFS
	case ENOBUFS:
#endif
#ifdef ENOENT
	case ENOENT:
#endif
		return true;
	default:
		break;
	}

	return false;
}

typedef void mowgli_eventloop_helper_start_fn_t (mowgli_eventloop_helper_proc_t * helper, void *userdata);

struct _mowgli_helper
{
	mowgli_eventloop_io_obj_t type;

	mowgli_process_t *child;
	mowgli_eventloop_t *eventloop;

	mowgli_descriptor_t fd;
	mowgli_eventloop_pollable_t *pfd;

	mowgli_eventloop_io_cb_t *read_function;

	void *userdata;
};

/* helper.c */
extern mowgli_eventloop_helper_proc_t *mowgli_helper_create(mowgli_eventloop_t *eventloop, mowgli_eventloop_helper_start_fn_t *start_fn, const char *helpername, void *userdata);

/* creation of helpers inside other executable images */
extern mowgli_eventloop_helper_proc_t *mowgli_helper_spawn(mowgli_eventloop_t *eventloop, const char *path, char *const argv[]);
extern mowgli_eventloop_helper_proc_t *mowgli_helper_setup(mowgli_eventloop_t *eventloop);

/* synchronization of helpers happens on reading from mowgli_eventloop_helper_proc_t::in_pfd. */
extern void mowgli_helper_set_read_cb(mowgli_eventloop_t *eventloop, mowgli_eventloop_helper_proc_t *helper, mowgli_eventloop_io_cb_t *read_fn);
extern void mowgli_helper_destroy(mowgli_eventloop_t *eventloop, mowgli_eventloop_helper_proc_t *helper);

/* null_pollops.c */
extern void mowgli_simple_eventloop_run_once(mowgli_eventloop_t *eventloop);
extern void mowgli_simple_eventloop_timeout_once(mowgli_eventloop_t *eventloop, int timeout);
extern void mowgli_simple_eventloop_error_handler(mowgli_eventloop_t *eventloop, mowgli_eventloop_io_t *io, mowgli_eventloop_io_dir_t dir, void *userdata);

/* eventloop.c */
extern mowgli_eventloop_t *mowgli_eventloop_create(void);
extern void mowgli_eventloop_destroy(mowgli_eventloop_t *eventloop);
extern void mowgli_eventloop_run(mowgli_eventloop_t *eventloop);
extern void mowgli_eventloop_run_once(mowgli_eventloop_t *eventloop);
extern void mowgli_eventloop_timeout_once(mowgli_eventloop_t *eventloop, int timeout);
extern void mowgli_eventloop_break(mowgli_eventloop_t *eventloop);
extern void mowgli_eventloop_timers_only(mowgli_eventloop_t *eventloop);
extern void mowgli_eventloop_set_data(mowgli_eventloop_t *eventloop, void *data);
extern void *mowgli_eventloop_get_data(mowgli_eventloop_t *eventloop);

/* timer.c */
extern mowgli_eventloop_timer_t *mowgli_timer_add(mowgli_eventloop_t *eventloop, const char *name, mowgli_event_dispatch_func_t *func, void *arg, time_t when);
extern mowgli_eventloop_timer_t *mowgli_timer_add_once(mowgli_eventloop_t *eventloop, const char *name, mowgli_event_dispatch_func_t *func, void *arg, time_t when);
extern void mowgli_timer_destroy(mowgli_eventloop_t *eventloop, mowgli_eventloop_timer_t *timer);
extern void mowgli_eventloop_run_timers(mowgli_eventloop_t *eventloop);
extern time_t mowgli_eventloop_next_timer(mowgli_eventloop_t *eventloop);
extern mowgli_eventloop_timer_t *mowgli_timer_find(mowgli_eventloop_t *eventloop, mowgli_event_dispatch_func_t *func, void *arg);

/* pollable.c */
extern mowgli_eventloop_pollable_t *mowgli_pollable_create(mowgli_eventloop_t *eventloop, mowgli_descriptor_t fd, void *userdata);
extern void mowgli_pollable_destroy(mowgli_eventloop_t *eventloop, mowgli_eventloop_pollable_t *pollable);
extern void mowgli_pollable_setselect(mowgli_eventloop_t *eventloop, mowgli_eventloop_pollable_t *pollable, mowgli_eventloop_io_dir_t dir, mowgli_eventloop_io_cb_t *event_function);
extern void mowgli_pollable_set_nonblocking(mowgli_eventloop_pollable_t *pollable, bool nonblocking);
extern void mowgli_pollable_set_cloexec(mowgli_eventloop_pollable_t *pollable, bool cloexec);
extern void mowgli_pollable_trigger(mowgli_eventloop_t *eventloop, mowgli_eventloop_pollable_t *pollable, mowgli_eventloop_io_dir_t dir);

#endif