v2 / thirdparty / picoev / src / picoev.h
404 lines · 366 sloc · 12.73 KB · 7b345e207d31b58c9c0d288e3410867076aa054d
Raw
1/*
2 * Copyright (c) 2009, Cybozu Labs, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * * Neither the name of the <ORGANIZATION> nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#ifndef picoev_h
31#define picoev_h
32
33#ifdef __cplusplus
34extern "C" {
35# define PICOEV_INLINE inline
36#else
37# define PICOEV_INLINE static __inline__
38#endif
39
40#include <assert.h>
41#include <limits.h>
42#include <stdlib.h>
43#include <string.h>
44#include <time.h>
45
46#define PICOEV_IS_INITED (picoev.max_fd != 0)
47#define PICOEV_IS_INITED_AND_FD_IN_RANGE(fd) \
48 (((unsigned)fd) < (unsigned)picoev.max_fd)
49#define PICOEV_TOO_MANY_LOOPS (picoev.num_loops != 0) /* use after ++ */
50#define PICOEV_FD_BELONGS_TO_LOOP(loop, fd) \
51 ((loop)->loop_id == picoev.fds[fd].loop_id)
52
53#define PICOEV_TIMEOUT_VEC_OF(loop, idx) \
54 ((loop)->timeout.vec + (idx) * picoev.timeout_vec_size)
55#define PICOEV_TIMEOUT_VEC_OF_VEC_OF(loop, idx) \
56 ((loop)->timeout.vec_of_vec + (idx) * picoev.timeout_vec_of_vec_size)
57#define PICOEV_RND_UP(v, d) (((v) + (d) - 1) / (d) * (d))
58
59#define PICOEV_PAGE_SIZE 4096
60#define PICOEV_CACHE_LINE_SIZE 32 /* in bytes, ok if greater than the actual */
61#define PICOEV_SIMD_BITS 128
62#define PICOEV_TIMEOUT_VEC_SIZE 128
63#define PICOEV_SHORT_BITS (sizeof(short) * 8)
64
65#define PICOEV_READ 1
66#define PICOEV_WRITE 2
67#define PICOEV_TIMEOUT 4
68#define PICOEV_ADD 0x40000000
69#define PICOEV_DEL 0x20000000
70#define PICOEV_READWRITE (PICOEV_READ | PICOEV_WRITE)
71
72#define PICOEV_TIMEOUT_IDX_UNUSED (UCHAR_MAX)
73
74 typedef unsigned short picoev_loop_id_t;
75
76 typedef struct picoev_loop_st picoev_loop;
77
78 typedef void picoev_handler(picoev_loop* loop, int fd, int revents,
79 void* cb_arg);
80
81 typedef struct picoev_fd_st {
82 /* use accessors! */
83 /* TODO adjust the size to match that of a cache line */
84 picoev_handler* callback;
85 void* cb_arg;
86 picoev_loop_id_t loop_id;
87 char events;
88 unsigned char timeout_idx; /* PICOEV_TIMEOUT_IDX_UNUSED if not used */
89 int _backend; /* can be used by backends (never modified by core) */
90 } picoev_fd;
91
92 struct picoev_loop_st {
93 /* read only */
94 picoev_loop_id_t loop_id;
95 struct {
96 short* vec;
97 short* vec_of_vec;
98 size_t base_idx;
99 time_t base_time;
100 int resolution;
101 void* _free_addr;
102 } timeout;
103 time_t now;
104 };
105
106 typedef struct picoev_globals_st {
107 /* read only */
108 picoev_fd* fds;
109 void* _fds_free_addr;
110 int max_fd;
111 int num_loops;
112 size_t timeout_vec_size; /* # of elements in picoev_loop.timeout.vec[0] */
113 size_t timeout_vec_of_vec_size; /* ... in timeout.vec_of_vec[0] */
114 } picoev_globals;
115
116 extern picoev_globals picoev;
117
118 /* creates a new event loop (defined by each backend) */
119 picoev_loop* picoev_create_loop(int max_timeout);
120
121 /* destroys a loop (defined by each backend) */
122 int picoev_destroy_loop(picoev_loop* loop);
123
124 /* internal: updates events to be watched (defined by each backend) */
125 int picoev_update_events_internal(picoev_loop* loop, int fd, int events);
126
127 /* internal: poll once and call the handlers (defined by each backend) */
128 int picoev_poll_once_internal(picoev_loop* loop, int max_wait);
129
130 /* internal, aligned allocator with address scrambling to avoid cache
131 line contention */
132 PICOEV_INLINE
133 void* picoev_memalign(size_t sz, void** orig_addr, int clear) {
134 sz = sz + PICOEV_PAGE_SIZE + PICOEV_CACHE_LINE_SIZE;
135 if ((*orig_addr = malloc(sz)) == NULL) {
136 return NULL;
137 }
138 if (clear != 0) {
139 memset(*orig_addr, 0, sz);
140 }
141 return
142 (void*)PICOEV_RND_UP((unsigned long)*orig_addr
143 + (rand() % PICOEV_PAGE_SIZE),
144 PICOEV_CACHE_LINE_SIZE);
145 }
146
147 /* initializes picoev */
148 PICOEV_INLINE
149 int picoev_init(int max_fd) {
150 assert(! PICOEV_IS_INITED);
151 assert(max_fd > 0);
152 if ((picoev.fds = (picoev_fd*)picoev_memalign(sizeof(picoev_fd) * max_fd,
153 &picoev._fds_free_addr, 1))
154 == NULL) {
155 return -1;
156 }
157 picoev.max_fd = max_fd;
158 picoev.num_loops = 0;
159 picoev.timeout_vec_size
160 = PICOEV_RND_UP(picoev.max_fd, PICOEV_SIMD_BITS) / PICOEV_SHORT_BITS;
161 picoev.timeout_vec_of_vec_size
162 = PICOEV_RND_UP(picoev.timeout_vec_size, PICOEV_SIMD_BITS)
163 / PICOEV_SHORT_BITS;
164 return 0;
165 }
166
167 /* deinitializes picoev */
168 PICOEV_INLINE
169 int picoev_deinit(void) {
170 assert(PICOEV_IS_INITED);
171 free(picoev._fds_free_addr);
172 picoev.fds = NULL;
173 picoev._fds_free_addr = NULL;
174 picoev.max_fd = 0;
175 picoev.num_loops = 0;
176 return 0;
177 }
178
179 /* updates timeout */
180 PICOEV_INLINE
181 void picoev_set_timeout(picoev_loop* loop, int fd, int secs) {
182 picoev_fd* target;
183 short* vec, * vec_of_vec;
184 size_t vi = fd / PICOEV_SHORT_BITS, delta;
185 assert(PICOEV_IS_INITED_AND_FD_IN_RANGE(fd));
186 assert(PICOEV_FD_BELONGS_TO_LOOP(loop, fd));
187 target = picoev.fds + fd;
188 /* clear timeout */
189 if (target->timeout_idx != PICOEV_TIMEOUT_IDX_UNUSED) {
190 vec = PICOEV_TIMEOUT_VEC_OF(loop, target->timeout_idx);
191 if ((vec[vi] &= ~((unsigned short)SHRT_MIN >> (fd % PICOEV_SHORT_BITS)))
192 == 0) {
193 vec_of_vec = PICOEV_TIMEOUT_VEC_OF_VEC_OF(loop, target->timeout_idx);
194 vec_of_vec[vi / PICOEV_SHORT_BITS]
195 &= ~((unsigned short)SHRT_MIN >> (vi % PICOEV_SHORT_BITS));
196 }
197 target->timeout_idx = PICOEV_TIMEOUT_IDX_UNUSED;
198 }
199 if (secs != 0) {
200 delta = (loop->now + secs - loop->timeout.base_time)
201 / loop->timeout.resolution;
202 if (delta >= PICOEV_TIMEOUT_VEC_SIZE) {
203 delta = PICOEV_TIMEOUT_VEC_SIZE - 1;
204 }
205 target->timeout_idx =
206 (loop->timeout.base_idx + delta) % PICOEV_TIMEOUT_VEC_SIZE;
207 vec = PICOEV_TIMEOUT_VEC_OF(loop, target->timeout_idx);
208 vec[vi] |= (unsigned short)SHRT_MIN >> (fd % PICOEV_SHORT_BITS);
209 vec_of_vec = PICOEV_TIMEOUT_VEC_OF_VEC_OF(loop, target->timeout_idx);
210 vec_of_vec[vi / PICOEV_SHORT_BITS]
211 |= (unsigned short)SHRT_MIN >> (vi % PICOEV_SHORT_BITS);
212 }
213 }
214
215 /* registers a file descriptor and callback argument to a event loop */
216 PICOEV_INLINE
217 int picoev_add(picoev_loop* loop, int fd, int events, int timeout_in_secs,
218 picoev_handler* callback, void* cb_arg) {
219 picoev_fd* target;
220 if (!PICOEV_IS_INITED_AND_FD_IN_RANGE(fd)) { return -1; }
221 target = picoev.fds + fd;
222 assert(target->loop_id == 0);
223 target->callback = callback;
224 target->cb_arg = cb_arg;
225 target->loop_id = loop->loop_id;
226 target->events = 0;
227 target->timeout_idx = PICOEV_TIMEOUT_IDX_UNUSED;
228 if (picoev_update_events_internal(loop, fd, events | PICOEV_ADD) != 0) {
229 target->loop_id = 0;
230 return -1;
231 }
232 picoev_set_timeout(loop, fd, timeout_in_secs);
233 return 0;
234 }
235
236 /* unregisters a file descriptor from event loop */
237 PICOEV_INLINE
238 int picoev_del(picoev_loop* loop, int fd) {
239 picoev_fd* target;
240 assert(PICOEV_IS_INITED_AND_FD_IN_RANGE(fd));
241 target = picoev.fds + fd;
242 if (picoev_update_events_internal(loop, fd, PICOEV_DEL) != 0) {
243 return -1;
244 }
245 picoev_set_timeout(loop, fd, 0);
246 target->loop_id = 0;
247 return 0;
248 }
249
250 /* check if fd is registered (checks all loops if loop == NULL) */
251 PICOEV_INLINE
252 int picoev_is_active(picoev_loop* loop, int fd) {
253 assert(PICOEV_IS_INITED_AND_FD_IN_RANGE(fd));
254 return loop != NULL
255 ? picoev.fds[fd].loop_id == loop->loop_id
256 : picoev.fds[fd].loop_id != 0;
257 }
258
259 /* returns events being watched for given descriptor */
260 PICOEV_INLINE
261 int picoev_get_events(picoev_loop* loop __attribute__((unused)), int fd) {
262 assert(PICOEV_IS_INITED_AND_FD_IN_RANGE(fd));
263 return picoev.fds[fd].events & PICOEV_READWRITE;
264 }
265
266 /* sets events to be watched for given desriptor */
267 PICOEV_INLINE
268 int picoev_set_events(picoev_loop* loop, int fd, int events) {
269 assert(PICOEV_IS_INITED_AND_FD_IN_RANGE(fd));
270 if (picoev.fds[fd].events != events
271 && picoev_update_events_internal(loop, fd, events) != 0) {
272 return -1;
273 }
274 return 0;
275 }
276
277 /* returns callback for given descriptor */
278 PICOEV_INLINE
279 picoev_handler* picoev_get_callback(picoev_loop* loop __attribute__((unused)),
280 int fd, void** cb_arg) {
281 assert(PICOEV_IS_INITED_AND_FD_IN_RANGE(fd));
282 if (cb_arg != NULL) {
283 *cb_arg = picoev.fds[fd].cb_arg;
284 }
285 return picoev.fds[fd].callback;
286 }
287
288 /* sets callback for given descriptor */
289 PICOEV_INLINE
290 void picoev_set_callback(picoev_loop* loop __attribute__((unused)), int fd,
291 picoev_handler* callback, void** cb_arg) {
292 assert(PICOEV_IS_INITED_AND_FD_IN_RANGE(fd));
293 if (cb_arg != NULL) {
294 picoev.fds[fd].cb_arg = *cb_arg;
295 }
296 picoev.fds[fd].callback = callback;
297 }
298
299 /* function to iterate registered information. To start iteration, set curfd
300 to -1 and call the function until -1 is returned */
301 PICOEV_INLINE
302 int picoev_next_fd(picoev_loop* loop, int curfd) {
303 if (curfd != -1) {
304 assert(PICOEV_IS_INITED_AND_FD_IN_RANGE(curfd));
305 }
306 while (++curfd < picoev.max_fd) {
307 if (loop->loop_id == picoev.fds[curfd].loop_id) {
308 return curfd;
309 }
310 }
311 return -1;
312 }
313
314 /* internal function */
315 PICOEV_INLINE
316 int picoev_init_loop_internal(picoev_loop* loop, int max_timeout) {
317 loop->loop_id = ++picoev.num_loops;
318 assert(PICOEV_TOO_MANY_LOOPS);
319 if ((loop->timeout.vec_of_vec
320 = (short*)picoev_memalign((picoev.timeout_vec_of_vec_size
321 + picoev.timeout_vec_size)
322 * sizeof(short) * PICOEV_TIMEOUT_VEC_SIZE,
323 &loop->timeout._free_addr, 1))
324 == NULL) {
325 --picoev.num_loops;
326 return -1;
327 }
328 loop->timeout.vec = loop->timeout.vec_of_vec
329 + picoev.timeout_vec_of_vec_size * PICOEV_TIMEOUT_VEC_SIZE;
330 loop->timeout.base_idx = 0;
331 loop->timeout.base_time = time(NULL);
332 loop->timeout.resolution
333 = PICOEV_RND_UP(max_timeout, PICOEV_TIMEOUT_VEC_SIZE)
334 / PICOEV_TIMEOUT_VEC_SIZE;
335 return 0;
336 }
337
338 /* internal function */
339 PICOEV_INLINE
340 void picoev_deinit_loop_internal(picoev_loop* loop) {
341 free(loop->timeout._free_addr);
342 }
343
344 /* internal function */
345 PICOEV_INLINE
346 void picoev_handle_timeout_internal(picoev_loop* loop) {
347 size_t i, j, k;
348 for (;
349 loop->timeout.base_time <= loop->now - loop->timeout.resolution;
350 loop->timeout.base_idx
351 = (loop->timeout.base_idx + 1) % PICOEV_TIMEOUT_VEC_SIZE,
352 loop->timeout.base_time += loop->timeout.resolution) {
353 /* TODO use SIMD instructions */
354 short* vec = PICOEV_TIMEOUT_VEC_OF(loop, loop->timeout.base_idx);
355 short* vec_of_vec
356 = PICOEV_TIMEOUT_VEC_OF_VEC_OF(loop, loop->timeout.base_idx);
357 for (i = 0; i < picoev.timeout_vec_of_vec_size; ++i) {
358 short vv = vec_of_vec[i];
359 if (vv != 0) {
360 for (j = i * PICOEV_SHORT_BITS; vv != 0; j++, vv <<= 1) {
361 if (vv < 0) {
362 short v = vec[j];
363 assert(v != 0);
364 for (k = j * PICOEV_SHORT_BITS; v != 0; k++, v <<= 1) {
365 if (v < 0) {
366 picoev_fd* fd = picoev.fds + k;
367 assert(fd->loop_id == loop->loop_id);
368 fd->timeout_idx = PICOEV_TIMEOUT_IDX_UNUSED;
369 (*fd->callback)(loop, k, PICOEV_TIMEOUT, fd->cb_arg);
370 }
371 }
372 vec[j] = 0;
373 }
374 }
375 vec_of_vec[i] = 0;
376 }
377 }
378 }
379 }
380
381 /* loop once */
382 PICOEV_INLINE
383 int picoev_loop_once(picoev_loop* loop, int max_wait) {
384 loop->now = time(NULL);
385 if (max_wait > loop->timeout.resolution) {
386 max_wait = loop->timeout.resolution;
387 }
388 if (picoev_poll_once_internal(loop, max_wait) != 0) {
389 return -1;
390 }
391 if (max_wait != 0) {
392 loop->now = time(NULL);
393 }
394 picoev_handle_timeout_internal(loop);
395 return 0;
396 }
397
398#undef PICOEV_INLINE
399
400#ifdef __cplusplus
401}
402#endif
403
404#endif
405