From 6d88c555019f32509f303e23dcfbba824fecd2ee Mon Sep 17 00:00:00 2001 From: Chris Hiszpanski Date: Thu, 18 Apr 2019 00:55:30 -0700 Subject: Initial public commit. mDNS and SDP are functional. Otherwise, library is still very much a work in progress. All tests pass. --- include/runloop.h | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 include/runloop.h (limited to 'include/runloop.h') diff --git a/include/runloop.h b/include/runloop.h new file mode 100644 index 0000000..ea840e7 --- /dev/null +++ b/include/runloop.h @@ -0,0 +1,111 @@ +/* + * liburtc + * Copyright (C) 2019 Chris Hiszpanski + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/** + * @file runloop.h + * Event loop + * A more elaborate file description. + */ + +#ifndef URTC_RUNLOOP_H +#define URTC_RUNLOOP_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include + +typedef void *(*callback_t)(int fd, void *arg); + +typedef struct { + // File descriptor sets for poll(). + struct pollfd *fds; + nfds_t nfds; + + // Callback array. Index into array is file descriptor. + callback_t *callbacks; + void **args; + + // Pipe for aborting blocked poll(). Allows dynamic adds/removes. + int abortpipe[2]; + bool done; + + // Run loop thread ID + pthread_t tid; +} runloop_t; + +/** + * Create a new run loop + * + * Each run loop creates a separate thread which then repeatedly poll()s + * for events. Upon receiving an event, the run loop calls the respective + * file descriptor's callback function. + * + * @param[out] rl Pointer to run loop struct + */ +int urtc__runloop_create(runloop_t *rl); + +/** + * Add file descriptor to run loop + * + * @param rl Run loop + * @param fd File descriptor + * @param events Events to monitor (see struct pollfd) + * @param cb Callback to run on activity + * @param arg User-specified argument to pass to callback + * + * @return Zero on success. Negative on error. + */ +int urtc__runloop_add( + runloop_t *rl, + int fd, + short events, + void *(*cb)(int fd, void *arg), + void *arg +); + +/** + * Remove a file descriptor from the run loop. No more events will be + * processed for this file descriptor. + * + * @param rl Run loop. + * @param fd File descriptor. + * + * @return Zero on success. Negative on error. + */ +int urtc__runloop_remove(runloop_t *rl, int fd); + +int urtc__runloop_join(runloop_t *rl); + +/** + * Destroy a run loop + * + * @param rl Pointer to run loop object + * + * @return Zero on success; negative on error. On error, errno is set. + */ +int urtc__runloop_destroy(runloop_t *rl); + +#ifdef __cplusplus +} +#endif + +#endif /* URTC_RUNLOOP_H */ -- cgit v1.2.3