1 /* 2 * uloop - event loop implementation 3 * 4 * Copyright (C) 2010-2013 Felix Fietkau <nbd@openwrt.org> 5 * 6 * Permission to use, copy, modify, and/or distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 #ifndef _ULOOP_H__ 19 #define _ULOOP_H__ 20 21 #include <sys/time.h> 22 #include <sys/types.h> 23 #include <stdbool.h> 24 #include <stdint.h> 25 #include <signal.h> 26 27 #if defined(__APPLE__) || defined(__FreeBSD__) 28 #define USE_KQUEUE 29 #else 30 #define USE_EPOLL 31 #endif 32 33 #include "list.h" 34 35 struct uloop_fd; 36 struct uloop_timeout; 37 struct uloop_process; 38 struct uloop_interval; 39 struct uloop_signal; 40 41 typedef void (*uloop_fd_handler)(struct uloop_fd *u, unsigned int events); 42 typedef void (*uloop_timeout_handler)(struct uloop_timeout *t); 43 typedef void (*uloop_process_handler)(struct uloop_process *c, int ret); 44 typedef void (*uloop_interval_handler)(struct uloop_interval *t); 45 typedef void (*uloop_signal_handler)(struct uloop_signal *s); 46 47 #define ULOOP_READ (1 << 0) 48 #define ULOOP_WRITE (1 << 1) 49 #define ULOOP_EDGE_TRIGGER (1 << 2) 50 #define ULOOP_BLOCKING (1 << 3) 51 52 #define ULOOP_ERROR_CB (1 << 6) 53 #define ULOOP_PRIORITY (1 << 7) 54 55 /* internal flags */ 56 #define ULOOP_EVENT_BUFFERED (1 << 4) 57 #ifdef USE_KQUEUE 58 #define ULOOP_EDGE_DEFER (1 << 5) 59 #endif 60 61 #define ULOOP_EVENT_MASK (ULOOP_READ | ULOOP_WRITE | ULOOP_PRIORITY) 62 63 struct uloop_fd 64 { 65 uloop_fd_handler cb; 66 int fd; 67 bool eof; 68 bool error; 69 bool registered; 70 uint8_t flags; 71 }; 72 73 struct uloop_timeout 74 { 75 struct list_head list; 76 bool pending; 77 78 uloop_timeout_handler cb; 79 struct timeval time; 80 }; 81 82 struct uloop_process 83 { 84 struct list_head list; 85 bool pending; 86 87 uloop_process_handler cb; 88 pid_t pid; 89 }; 90 91 struct uloop_interval 92 { 93 uloop_interval_handler cb; 94 uint64_t expirations; 95 96 union { 97 struct uloop_fd ufd; 98 struct { 99 int64_t fired; 100 unsigned int msecs; 101 } time; 102 } priv; 103 }; 104 105 struct uloop_signal 106 { 107 struct list_head list; 108 struct sigaction orig; 109 bool pending; 110 111 uloop_signal_handler cb; 112 int signo; 113 }; 114 115 extern bool uloop_cancelled; 116 extern bool uloop_handle_sigchld; 117 extern uloop_fd_handler uloop_fd_set_cb; 118 119 int uloop_fd_add(struct uloop_fd *sock, unsigned int flags); 120 int uloop_fd_delete(struct uloop_fd *sock); 121 122 int uloop_get_next_timeout(void); 123 int uloop_timeout_add(struct uloop_timeout *timeout); 124 int uloop_timeout_set(struct uloop_timeout *timeout, int msecs); 125 int uloop_timeout_cancel(struct uloop_timeout *timeout); 126 int uloop_timeout_remaining(struct uloop_timeout *timeout) __attribute__((deprecated("use uloop_timeout_remaining64"))); 127 int64_t uloop_timeout_remaining64(struct uloop_timeout *timeout); 128 129 int uloop_process_add(struct uloop_process *p); 130 int uloop_process_delete(struct uloop_process *p); 131 132 int uloop_interval_set(struct uloop_interval *timer, unsigned int msecs); 133 int uloop_interval_cancel(struct uloop_interval *timer); 134 int64_t uloop_interval_remaining(struct uloop_interval *timer); 135 136 int uloop_signal_add(struct uloop_signal *s); 137 int uloop_signal_delete(struct uloop_signal *s); 138 139 bool uloop_cancelling(void); 140 141 static inline void uloop_end(void) 142 { 143 uloop_cancelled = true; 144 } 145 146 int uloop_init(void); 147 int uloop_run_timeout(int timeout); 148 static inline int uloop_run(void) 149 { 150 return uloop_run_timeout(-1); 151 } 152 void uloop_done(void); 153 154 #endif 155
This page was automatically generated by LXR 0.3.1. • OpenWrt