| 1 |
/* |
| 2 |
* Copyright (c) 2011, 2012 William Pitcock <[email protected]>. |
| 3 |
* |
| 4 |
* Permission to use, copy, modify, and/or distribute this software for any |
| 5 |
* purpose with or without fee is hereby granted, provided that the above |
| 6 |
* copyright notice and this permission notice appear in all copies. |
| 7 |
* |
| 8 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 9 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 10 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 11 |
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, |
| 12 |
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 13 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 14 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 15 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 16 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| 17 |
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 18 |
* POSSIBILITY OF SUCH DAMAGE. |
| 19 |
*/ |
| 20 |
|
| 21 |
#ifndef MOWGLI_SRC_LIBMOWGLI_EVENTLOOP_EVENTLOOP_H_INCLUDE_GUARD |
| 22 |
#define MOWGLI_SRC_LIBMOWGLI_EVENTLOOP_EVENTLOOP_H_INCLUDE_GUARD 1 |
| 23 |
|
| 24 |
#include "container/list.h" |
| 25 |
#include "core/assert.h" |
| 26 |
#include "core/process.h" |
| 27 |
#include "core/stdinc.h" |
| 28 |
#include "thread/mutex.h" |
| 29 |
|
| 30 |
#ifdef MOWGLI_OS_OSX |
| 31 |
# include <mach/mach.h> |
| 32 |
# include <mach/mach_time.h> |
| 33 |
#endif |
| 34 |
|
| 35 |
#ifndef _WIN32 |
| 36 |
|
| 37 |
typedef int mowgli_descriptor_t; |
| 38 |
|
| 39 |
#else |
| 40 |
|
| 41 |
typedef SOCKET mowgli_descriptor_t; |
| 42 |
|
| 43 |
#endif |
| 44 |
|
| 45 |
typedef enum |
| 46 |
{ |
| 47 |
MOWGLI_EVENTLOOP_TYPE_POLLABLE, |
| 48 |
MOWGLI_EVENTLOOP_TYPE_HELPER, |
| 49 |
MOWGLI_EVENTLOOP_TYPE_ERROR = -1 |
| 50 |
} mowgli_eventloop_io_type_t; |
| 51 |
|
| 52 |
typedef struct |
| 53 |
{ |
| 54 |
mowgli_eventloop_io_type_t type; |
| 55 |
} mowgli_eventloop_io_obj_t; |
| 56 |
|
| 57 |
typedef struct _mowgli_eventloop mowgli_eventloop_t; |
| 58 |
|
| 59 |
typedef struct _mowgli_pollable mowgli_eventloop_pollable_t; |
| 60 |
typedef struct _mowgli_helper mowgli_eventloop_helper_proc_t; |
| 61 |
|
| 62 |
typedef struct _mowgli_linebuf mowgli_linebuf_t; |
| 63 |
|
| 64 |
typedef enum |
| 65 |
{ |
| 66 |
MOWGLI_EVENTLOOP_IO_READ, |
| 67 |
MOWGLI_EVENTLOOP_IO_WRITE, |
| 68 |
MOWGLI_EVENTLOOP_IO_ERROR = -1 |
| 69 |
} mowgli_eventloop_io_dir_t; |
| 70 |
|
| 71 |
typedef void mowgli_eventloop_io_t; |
| 72 |
|
| 73 |
/* checked casts */ |
| 74 |
static inline mowgli_eventloop_pollable_t * |
| 75 |
mowgli_eventloop_io_pollable(mowgli_eventloop_io_t *io) |
| 76 |
{ |
| 77 |
mowgli_eventloop_io_obj_t *obj = (mowgli_eventloop_io_obj_t *) io; |
| 78 |
|
| 79 |
return_val_if_fail(io != NULL, NULL); |
| 80 |
return_val_if_fail(obj->type == MOWGLI_EVENTLOOP_TYPE_POLLABLE, NULL); |
| 81 |
|
| 82 |
return (mowgli_eventloop_pollable_t *) io; |
| 83 |
} |
| 84 |
|
| 85 |
static inline mowgli_eventloop_helper_proc_t * |
| 86 |
mowgli_eventloop_io_helper(mowgli_eventloop_io_t *io) |
| 87 |
{ |
| 88 |
mowgli_eventloop_io_obj_t *obj = (mowgli_eventloop_io_obj_t *) io; |
| 89 |
|
| 90 |
return_val_if_fail(io != NULL, NULL); |
| 91 |
return_val_if_fail(obj->type == MOWGLI_EVENTLOOP_TYPE_HELPER, NULL); |
| 92 |
|
| 93 |
return (mowgli_eventloop_helper_proc_t *) io; |
| 94 |
} |
| 95 |
|
| 96 |
static inline mowgli_eventloop_io_type_t |
| 97 |
mowgli_eventloop_io_type(mowgli_eventloop_io_t *io) |
| 98 |
{ |
| 99 |
mowgli_eventloop_io_obj_t *obj = (mowgli_eventloop_io_obj_t *) io; |
| 100 |
|
| 101 |
return_val_if_fail(io != NULL, MOWGLI_EVENTLOOP_TYPE_ERROR); |
| 102 |
|
| 103 |
return obj->type; |
| 104 |
} |
| 105 |
|
| 106 |
typedef void mowgli_eventloop_io_cb_t (mowgli_eventloop_t * eventloop, mowgli_eventloop_io_t * io, mowgli_eventloop_io_dir_t dir, void *userdata); |
| 107 |
|
| 108 |
struct _mowgli_pollable |
| 109 |
{ |
| 110 |
mowgli_eventloop_io_obj_t type; |
| 111 |
|
| 112 |
mowgli_descriptor_t fd; |
| 113 |
unsigned int slot; |
| 114 |
unsigned int events; |
| 115 |
|
| 116 |
mowgli_eventloop_io_cb_t *read_function; |
| 117 |
mowgli_eventloop_io_cb_t *write_function; |
| 118 |
mowgli_eventloop_io_cb_t *error_function; |
| 119 |
|
| 120 |
void *userdata; |
| 121 |
|
| 122 |
mowgli_node_t node; |
| 123 |
|
| 124 |
mowgli_eventloop_t *eventloop; |
| 125 |
|
| 126 |
bool removed; |
| 127 |
}; |
| 128 |
|
| 129 |
typedef struct |
| 130 |
{ |
| 131 |
void (*timeout_once)(mowgli_eventloop_t *eventloop, int timeout); |
| 132 |
void (*run_once)(mowgli_eventloop_t *eventloop); |
| 133 |
void (*pollsetup)(mowgli_eventloop_t *eventloop); |
| 134 |
void (*pollshutdown)(mowgli_eventloop_t *eventloop); |
| 135 |
void (*setselect)(mowgli_eventloop_t *eventloop, mowgli_eventloop_pollable_t *pollable, mowgli_eventloop_io_dir_t dir, mowgli_eventloop_io_cb_t *event_function); |
| 136 |
void (*select)(mowgli_eventloop_t *eventloop, int time); |
| 137 |
void (*destroy)(mowgli_eventloop_t *eventloop, mowgli_eventloop_pollable_t *pollable); |
| 138 |
} mowgli_eventloop_ops_t; |
| 139 |
|
| 140 |
struct _mowgli_eventloop |
| 141 |
{ |
| 142 |
time_t currtime; |
| 143 |
time_t deadline; |
| 144 |
|
| 145 |
const char *last_ran; |
| 146 |
|
| 147 |
mowgli_list_t timer_list; |
| 148 |
mowgli_mutex_t mutex; |
| 149 |
|
| 150 |
mowgli_eventloop_ops_t *eventloop_ops; |
| 151 |
void *poller; |
| 152 |
|
| 153 |
bool death_requested; |
| 154 |
|
| 155 |
void *data; |
| 156 |
|
| 157 |
time_t epochbias; |
| 158 |
|
| 159 |
mowgli_list_t destroyed_pollable_list; |
| 160 |
bool processing_events; |
| 161 |
}; |
| 162 |
|
| 163 |
typedef void mowgli_event_dispatch_func_t (void *userdata); |
| 164 |
|
| 165 |
typedef struct |
| 166 |
{ |
| 167 |
mowgli_node_t node; |
| 168 |
|
| 169 |
mowgli_event_dispatch_func_t *func; |
| 170 |
void *arg; |
| 171 |
const char *name; |
| 172 |
time_t frequency; |
| 173 |
time_t deadline; |
| 174 |
bool active; |
| 175 |
} mowgli_eventloop_timer_t; |
| 176 |
|
| 177 |
static inline void |
| 178 |
mowgli_eventloop_set_time(mowgli_eventloop_t *eventloop, time_t newtime) |
| 179 |
{ |
| 180 |
return_if_fail(eventloop != NULL); |
| 181 |
|
| 182 |
eventloop->currtime = newtime; |
| 183 |
} |
| 184 |
|
| 185 |
static inline time_t |
| 186 |
mowgli_eventloop_get_time(mowgli_eventloop_t *eventloop) |
| 187 |
{ |
| 188 |
return_val_if_fail(eventloop != NULL, 0); |
| 189 |
|
| 190 |
return eventloop->epochbias + eventloop->currtime; |
| 191 |
} |
| 192 |
|
| 193 |
static inline void |
| 194 |
mowgli_eventloop_synchronize(mowgli_eventloop_t *eventloop) |
| 195 |
{ |
| 196 |
long long time_; |
| 197 |
|
| 198 |
#if defined(CLOCK_MONOTONIC) |
| 199 |
struct timespec tp; |
| 200 |
|
| 201 |
clock_gettime(CLOCK_MONOTONIC, &tp); |
| 202 |
time_ = tp.tv_sec; |
| 203 |
#elif defined(CLOCK_HIGHRES) |
| 204 |
struct timespec tp; |
| 205 |
|
| 206 |
clock_gettime(CLOCK_HIGHRES, &tp); |
| 207 |
time_ = tp.tv_sec; |
| 208 |
#elif defined(MOWGLI_OS_WIN) |
| 209 |
static ULONGLONG (CALLBACK *GetTickCount64)(void) = NULL; |
| 210 |
static OSVERSIONINFOEX *winver = NULL; |
| 211 |
static bool load_err = false; |
| 212 |
|
| 213 |
if (winver == NULL) |
| 214 |
{ |
| 215 |
winver = mowgli_alloc(sizeof(OSVERSIONINFOEX)); |
| 216 |
winver->dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); |
| 217 |
|
| 218 |
if (!GetVersionEx((OSVERSIONINFO *) winver)) |
| 219 |
{ |
| 220 |
mowgli_free(winver); |
| 221 |
winver = NULL; /* FIXME */ |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
if (winver && (winver->dwMajorVersion >= 6)) |
| 226 |
{ |
| 227 |
if ((GetTickCount64 == NULL) && !load_err) |
| 228 |
{ |
| 229 |
HINSTANCE hKernel32; |
| 230 |
|
| 231 |
hKernel32 = GetModuleHandle("KERNEL32"); |
| 232 |
GetTickCount64 = GetProcAddress(hKernel32, "GetTickCount64"); |
| 233 |
|
| 234 |
if (GetTickCount64 == NULL) |
| 235 |
load_err = true; |
| 236 |
} |
| 237 |
|
| 238 |
if (load_err) |
| 239 |
{ |
| 240 |
time_ = time(NULL); |
| 241 |
} |
| 242 |
else |
| 243 |
{ |
| 244 |
soft_assert(GetTickCount64 != NULL); |
| 245 |
|
| 246 |
time_ = (int) (GetTickCount64() * 1e-3); |
| 247 |
} |
| 248 |
} |
| 249 |
else |
| 250 |
{ |
| 251 |
time_ = time(NULL); |
| 252 |
} |
| 253 |
|
| 254 |
#elif defined(MOWGLI_OS_OSX) |
| 255 |
static mach_timebase_info_data_t timebase; |
| 256 |
|
| 257 |
if (timebase.denom == 0) |
| 258 |
mach_timebase_info(&timebase); |
| 259 |
|
| 260 |
time_ = (int) (mach_absolute_time() * timebase.numer / timebase.denom * 1e-9); |
| 261 |
#else |
| 262 |
time_ = time(NULL); |
| 263 |
#endif |
| 264 |
mowgli_eventloop_set_time(eventloop, (time_t) time_); |
| 265 |
} |
| 266 |
|
| 267 |
/* Sets the bias of eventloop->currtime relative to Jan 1 00:00:00 1970 */ |
| 268 |
static inline void |
| 269 |
mowgli_eventloop_calibrate(mowgli_eventloop_t *eventloop) |
| 270 |
{ |
| 271 |
mowgli_eventloop_synchronize(eventloop); |
| 272 |
eventloop->epochbias = time(NULL) - eventloop->currtime; |
| 273 |
} |
| 274 |
|
| 275 |
static inline bool |
| 276 |
mowgli_eventloop_ignore_errno(int error) |
| 277 |
{ |
| 278 |
switch (error) |
| 279 |
{ |
| 280 |
#ifdef EINPROGRESS |
| 281 |
case EINPROGRESS: |
| 282 |
#endif |
| 283 |
#if defined(EWOULDBLOCK) |
| 284 |
case EWOULDBLOCK: |
| 285 |
#endif |
| 286 |
#if defined(EAGAIN) && (EWOULDBLOCK != EAGAIN) |
| 287 |
case EAGAIN: |
| 288 |
#endif |
| 289 |
#ifdef ETIME |
| 290 |
case ETIME: |
| 291 |
#endif |
| 292 |
#ifdef EINTR |
| 293 |
case EINTR: |
| 294 |
#endif |
| 295 |
#ifdef ERESTART |
| 296 |
case ERESTART: |
| 297 |
#endif |
| 298 |
#ifdef ENOBUFS |
| 299 |
case ENOBUFS: |
| 300 |
#endif |
| 301 |
#ifdef ENOENT |
| 302 |
case ENOENT: |
| 303 |
#endif |
| 304 |
return true; |
| 305 |
default: |
| 306 |
break; |
| 307 |
} |
| 308 |
|
| 309 |
return false; |
| 310 |
} |
| 311 |
|
| 312 |
typedef void mowgli_eventloop_helper_start_fn_t (mowgli_eventloop_helper_proc_t * helper, void *userdata); |
| 313 |
|
| 314 |
struct _mowgli_helper |
| 315 |
{ |
| 316 |
mowgli_eventloop_io_obj_t type; |
| 317 |
|
| 318 |
mowgli_process_t *child; |
| 319 |
mowgli_eventloop_t *eventloop; |
| 320 |
|
| 321 |
mowgli_descriptor_t fd; |
| 322 |
mowgli_eventloop_pollable_t *pfd; |
| 323 |
|
| 324 |
mowgli_eventloop_io_cb_t *read_function; |
| 325 |
|
| 326 |
void *userdata; |
| 327 |
}; |
| 328 |
|
| 329 |
/* helper.c */ |
| 330 |
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); |
| 331 |
|
| 332 |
/* creation of helpers inside other executable images */ |
| 333 |
extern mowgli_eventloop_helper_proc_t *mowgli_helper_spawn(mowgli_eventloop_t *eventloop, const char *path, char *const argv[]); |
| 334 |
extern mowgli_eventloop_helper_proc_t *mowgli_helper_setup(mowgli_eventloop_t *eventloop); |
| 335 |
|
| 336 |
/* synchronization of helpers happens on reading from mowgli_eventloop_helper_proc_t::in_pfd. */ |
| 337 |
extern void mowgli_helper_set_read_cb(mowgli_eventloop_t *eventloop, mowgli_eventloop_helper_proc_t *helper, mowgli_eventloop_io_cb_t *read_fn); |
| 338 |
extern void mowgli_helper_destroy(mowgli_eventloop_t *eventloop, mowgli_eventloop_helper_proc_t *helper); |
| 339 |
|
| 340 |
/* null_pollops.c */ |
| 341 |
extern void mowgli_simple_eventloop_run_once(mowgli_eventloop_t *eventloop); |
| 342 |
extern void mowgli_simple_eventloop_timeout_once(mowgli_eventloop_t *eventloop, int timeout); |
| 343 |
extern void mowgli_simple_eventloop_error_handler(mowgli_eventloop_t *eventloop, mowgli_eventloop_io_t *io, mowgli_eventloop_io_dir_t dir, void *userdata); |
| 344 |
|
| 345 |
/* eventloop.c */ |
| 346 |
extern mowgli_eventloop_t *mowgli_eventloop_create(void); |
| 347 |
extern void mowgli_eventloop_destroy(mowgli_eventloop_t *eventloop); |
| 348 |
extern void mowgli_eventloop_run(mowgli_eventloop_t *eventloop); |
| 349 |
extern void mowgli_eventloop_run_once(mowgli_eventloop_t *eventloop); |
| 350 |
extern void mowgli_eventloop_timeout_once(mowgli_eventloop_t *eventloop, int timeout); |
| 351 |
extern void mowgli_eventloop_break(mowgli_eventloop_t *eventloop); |
| 352 |
extern void mowgli_eventloop_timers_only(mowgli_eventloop_t *eventloop); |
| 353 |
extern void mowgli_eventloop_set_data(mowgli_eventloop_t *eventloop, void *data); |
| 354 |
extern void *mowgli_eventloop_get_data(mowgli_eventloop_t *eventloop); |
| 355 |
|
| 356 |
/* timer.c */ |
| 357 |
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); |
| 358 |
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); |
| 359 |
extern void mowgli_timer_destroy(mowgli_eventloop_t *eventloop, mowgli_eventloop_timer_t *timer); |
| 360 |
extern void mowgli_eventloop_run_timers(mowgli_eventloop_t *eventloop); |
| 361 |
extern time_t mowgli_eventloop_next_timer(mowgli_eventloop_t *eventloop); |
| 362 |
extern mowgli_eventloop_timer_t *mowgli_timer_find(mowgli_eventloop_t *eventloop, mowgli_event_dispatch_func_t *func, void *arg); |
| 363 |
|
| 364 |
/* pollable.c */ |
| 365 |
extern mowgli_eventloop_pollable_t *mowgli_pollable_create(mowgli_eventloop_t *eventloop, mowgli_descriptor_t fd, void *userdata); |
| 366 |
extern void mowgli_pollable_free(mowgli_eventloop_pollable_t *pollable); |
| 367 |
extern void mowgli_pollable_destroy(mowgli_eventloop_t *eventloop, mowgli_eventloop_pollable_t *pollable); |
| 368 |
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); |
| 369 |
extern void mowgli_pollable_set_nonblocking(mowgli_eventloop_pollable_t *pollable, bool nonblocking); |
| 370 |
extern void mowgli_pollable_set_cloexec(mowgli_eventloop_pollable_t *pollable, bool cloexec); |
| 371 |
extern void mowgli_pollable_trigger(mowgli_eventloop_t *eventloop, mowgli_eventloop_pollable_t *pollable, mowgli_eventloop_io_dir_t dir); |
| 372 |
|
| 373 |
#endif /* MOWGLI_SRC_LIBMOWGLI_EVENTLOOP_EVENTLOOP_H_INCLUDE_GUARD */ |