root/platform/mysql/mycached/trunk/picoev.h @ 35019

Revision 35019, 11.7 kB (checked in by kazuho, 4 years ago)

update picoev

Line 
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  /* http://gmplib.org/list-archives/gmp-discuss/2008-March/003089.html */
38# define PICOEV_INLINE extern __inline__
39#endif
40
41#include <assert.h>
42#include <limits.h>
43#include <stdlib.h>
44#include <string.h>
45#include <time.h>
46
47#define PICOEV_IS_INITED (picoev.max_fd != 0) 
48#define PICOEV_IS_INITED_AND_FD_IN_RANGE(fd) \
49  (((unsigned)fd) < (unsigned)picoev.max_fd)
50#define PICOEV_TOO_MANY_LOOPS (picoev.num_loops != 0) /* use after ++ */
51#define PICOEV_FD_BELONGS_TO_LOOP(loop, fd) \
52  ((loop)->loop_id == picoev.fds[fd].loop_id)
53
54#define PICOEV_TIMEOUT_VEC_OF(loop, idx) \
55  ((loop)->timeout.vec + (idx) * picoev.timeout_vec_size)
56#define PICOEV_TIMEOUT_VEC_OF_VEC_OF(loop, idx) \
57  ((loop)->timeout.vec_of_vec + (idx) * picoev.timeout_vec_of_vec_size)
58#define PICOEV_RND_UP(v, d) (((v) + (d) - 1) / (d) * (d))
59
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 
69#define PICOEV_TIMEOUT_IDX_UNUSED (UCHAR_MAX)
70 
71  typedef unsigned short picoev_loop_id_t;
72 
73  typedef struct picoev_loop_st picoev_loop;
74 
75  typedef void picoev_handler(picoev_loop* loop, int fd, int revents,
76                              void* cb_arg);
77 
78  typedef struct picoev_fd_st {
79    /* use accessors! */
80    /* TODO adjust the size to match that of a cache line */
81    picoev_handler* callback;
82    void* cb_arg;
83    picoev_loop_id_t loop_id;
84    char events;
85    unsigned char timeout_idx; /* PICOEV_TIMEOUT_IDX_UNUSED if not used */
86    int _backend; /* can be used by the backend (never ever touched by core) */
87  } picoev_fd;
88 
89  struct picoev_loop_st {
90    /* read only */
91    picoev_loop_id_t loop_id;
92    struct {
93      short* vec;
94      short* vec_of_vec;
95      size_t base_idx;
96      time_t base_time;
97      int resolution;
98      void* _free_addr;
99    } timeout;
100    time_t now;
101  };
102 
103  typedef struct picoev_globals_st {
104    /* read only */
105    picoev_fd* fds;
106    void* _fds_free_addr;
107    int max_fd;
108    int num_loops;
109    size_t timeout_vec_size; /* # of elements in picoev_loop.timeout.vec[0] */
110    size_t timeout_vec_of_vec_size; /* ... in timeout.vec_of_vec[0] */
111  } picoev_globals;
112 
113  extern picoev_globals picoev;
114 
115  /* creates a new event loop (defined by each backend) */
116  picoev_loop* picoev_create_loop(int max_timeout);
117 
118  /* destroys a loop (defined by each backend) */
119  int picoev_destroy_loop(picoev_loop* loop);
120 
121  /* internal: initializes the backend */
122  int picoev_init_backend(void);
123 
124  /* internal: destroys the backend */
125  int picoev_deinit_backend(void);
126 
127  /* internal: updates events to be watched (defined by each backend) */
128  int picoev_update_events_internal(picoev_loop* loop, int fd, int events);
129 
130  /* internal: poll once and call the handlers (defined by each backend) */
131  int picoev_poll_once_internal(picoev_loop* loop, int max_wait);
132 
133  /* internal, aligned allocator */
134  PICOEV_INLINE
135  void* picoev_memalign(size_t sz, void** orig_addr) {
136    sz = sz + PICOEV_CACHE_LINE_SIZE - 1;
137    if ((*orig_addr = malloc(sz)) == NULL) {
138      return NULL;
139    }
140    memset(*orig_addr, 0, sz);
141    return
142      (void*)PICOEV_RND_UP((unsigned long)*orig_addr, PICOEV_CACHE_LINE_SIZE);
143  }
144 
145  /* initializes picoev */
146  PICOEV_INLINE
147  int picoev_init(int max_fd) {
148    assert(! PICOEV_IS_INITED);
149    assert(max_fd > 0);
150    if ((picoev.fds = (picoev_fd*)picoev_memalign(sizeof(picoev_fd) * max_fd,
151                                                  &picoev._fds_free_addr))
152        == NULL) {
153      return -1;
154    }
155    picoev.max_fd = max_fd;
156    picoev.num_loops = 0;
157    picoev.timeout_vec_size
158      = PICOEV_RND_UP(picoev.max_fd, PICOEV_SIMD_BITS) / PICOEV_SHORT_BITS;
159    picoev.timeout_vec_of_vec_size
160      = PICOEV_RND_UP(picoev.timeout_vec_size, PICOEV_SIMD_BITS)
161      / PICOEV_SHORT_BITS;
162    if (picoev_init_backend() != 0) {
163      free(picoev.fds);
164      picoev.fds = NULL;
165      picoev.max_fd = 0;
166      return -1;
167    }
168    return 0;
169  }
170 
171  /* deinitializes picoev */
172  PICOEV_INLINE
173  int picoev_deinit(void) {
174    assert(PICOEV_IS_INITED);
175    if (picoev_deinit_backend() != 0) {
176      return -1;
177    }
178    free(picoev._fds_free_addr);
179    picoev.fds = NULL;
180    picoev._fds_free_addr = NULL;
181    picoev.max_fd = 0;
182    picoev.num_loops = 0;
183    return 0;
184  }
185 
186  /* updates timeout */
187  PICOEV_INLINE
188  void picoev_set_timeout(picoev_loop* loop, int fd, int secs) {
189    picoev_fd* target;
190    short* vec, * vec_of_vec;
191    size_t vi = fd / PICOEV_SHORT_BITS, delta;
192    assert(PICOEV_IS_INITED_AND_FD_IN_RANGE(fd));
193    assert(PICOEV_FD_BELONGS_TO_LOOP(loop, fd));
194    target = picoev.fds + fd;
195    /* clear timeout */
196    if (target->timeout_idx != PICOEV_TIMEOUT_IDX_UNUSED) {
197      vec = PICOEV_TIMEOUT_VEC_OF(loop, target->timeout_idx);
198      if ((vec[vi] &= ~((unsigned short)SHRT_MIN >> (fd % PICOEV_SHORT_BITS)))
199          == 0) {
200        vec_of_vec = PICOEV_TIMEOUT_VEC_OF_VEC_OF(loop, target->timeout_idx);
201        vec_of_vec[vi / PICOEV_SHORT_BITS]
202          &= ~((unsigned short)SHRT_MIN >> (vi % PICOEV_SHORT_BITS));
203      }
204      target->timeout_idx = PICOEV_TIMEOUT_IDX_UNUSED;
205    }
206    if (secs != 0) {
207      delta = (loop->now + secs - loop->timeout.base_time)
208        / loop->timeout.resolution;
209      if (delta >= PICOEV_TIMEOUT_VEC_SIZE) {
210        delta = PICOEV_TIMEOUT_VEC_SIZE - 1;
211      }
212      target->timeout_idx =
213        (loop->timeout.base_idx + delta) % PICOEV_TIMEOUT_VEC_SIZE;
214      vec = PICOEV_TIMEOUT_VEC_OF(loop, target->timeout_idx);
215      vec[vi] |= (unsigned short)SHRT_MIN >> (fd % PICOEV_SHORT_BITS);
216      vec_of_vec = PICOEV_TIMEOUT_VEC_OF_VEC_OF(loop, target->timeout_idx);
217      vec_of_vec[vi / PICOEV_SHORT_BITS]
218        |= (unsigned short)SHRT_MIN >> (vi % PICOEV_SHORT_BITS);
219    }
220  }
221 
222  /* registers a file descriptor and callback argument to a event loop */
223  PICOEV_INLINE
224  int picoev_add(picoev_loop* loop, int fd, int events, int timeout_in_secs,
225                 picoev_handler* callback, void* cb_arg) {
226    picoev_fd* target;
227    assert(PICOEV_IS_INITED_AND_FD_IN_RANGE(fd));
228    target = picoev.fds + fd;
229    assert(target->loop_id == 0);
230    target->callback = callback;
231    target->cb_arg = cb_arg;
232    target->loop_id = loop->loop_id;
233    target->events = 0;
234    target->timeout_idx = PICOEV_TIMEOUT_IDX_UNUSED;
235    if (events != 0
236        && picoev_update_events_internal(loop, fd, events) != 0) {
237      target->loop_id = 0;
238      return -1;
239    }
240    picoev_set_timeout(loop, fd, timeout_in_secs);
241    return 0;
242  }
243 
244  /* unregisters a file descriptor from event loop */
245  PICOEV_INLINE
246  int picoev_del(picoev_loop* loop, int fd) {
247    picoev_fd* target;
248    assert(PICOEV_IS_INITED_AND_FD_IN_RANGE(fd));
249    target = picoev.fds + fd;
250    if (target->events != 0
251        && picoev_update_events_internal(loop, fd, 0) != 0) {
252      return -1;
253    }
254    picoev_set_timeout(loop, fd, 0);
255    target->loop_id = 0;
256    return 0;
257  }
258 
259  /* check if fd is registered (checks all loops if loop == NULL) */
260  PICOEV_INLINE
261  int picoev_is_active(picoev_loop* loop, int fd) {
262    assert(PICOEV_IS_INITED_AND_FD_IN_RANGE(fd));
263    return loop != NULL
264      ? picoev.fds[fd].loop_id == loop->loop_id
265      : picoev.fds[fd].loop_id != 0;
266  }
267 
268  /* returns events being watched for given descriptor */
269  PICOEV_INLINE
270  int picoev_get_events(picoev_loop* loop __attribute__((unused)), int fd) {
271    assert(PICOEV_IS_INITED_AND_FD_IN_RANGE(fd));
272    return picoev.fds[fd].events;
273  }
274 
275  /* sets events to be watched for given desriptor */
276  PICOEV_INLINE
277  int picoev_set_events(picoev_loop* loop, int fd, int events) {
278    assert(PICOEV_IS_INITED_AND_FD_IN_RANGE(fd));
279    if (picoev.fds[fd].events != events
280        && picoev_update_events_internal(loop, fd, events) != 0) {
281      return -1;
282    }
283    return 0;
284  }
285 
286  /* internal function */
287  PICOEV_INLINE
288  int picoev_init_loop_internal(picoev_loop* loop, int max_timeout) {
289    loop->loop_id = ++picoev.num_loops;
290    assert(PICOEV_TOO_MANY_LOOPS);
291    if ((loop->timeout.vec_of_vec
292         = (short*)picoev_memalign((picoev.timeout_vec_of_vec_size
293                                    + picoev.timeout_vec_size)
294                                   * sizeof(short) * PICOEV_TIMEOUT_VEC_SIZE,
295                                   &loop->timeout._free_addr))
296        == NULL) {
297      --picoev.num_loops;
298      return -1;
299    }
300    loop->timeout.vec = loop->timeout.vec_of_vec
301      + picoev.timeout_vec_of_vec_size * PICOEV_TIMEOUT_VEC_SIZE;
302    loop->timeout.base_idx = 0;
303    loop->timeout.base_time = time(NULL);
304    loop->timeout.resolution
305      = PICOEV_RND_UP(max_timeout, PICOEV_TIMEOUT_VEC_SIZE)
306      / PICOEV_TIMEOUT_VEC_SIZE;
307    return 0;
308  }
309 
310  /* internal function */
311  PICOEV_INLINE
312  void picoev_deinit_loop_internal(picoev_loop* loop) {
313    free(loop->timeout._free_addr);
314  }
315 
316  /* internal function */
317  PICOEV_INLINE
318  void picoev_handle_timeout_internal(picoev_loop* loop) {
319    size_t i, j, k;
320    for (;
321         loop->timeout.base_time <= loop->now - loop->timeout.resolution;
322         loop->timeout.base_idx
323           = (loop->timeout.base_idx + 1) % PICOEV_TIMEOUT_VEC_SIZE,
324           loop->timeout.base_time += loop->timeout.resolution) {
325      /* TODO use SIMD instructions */
326      short* vec = PICOEV_TIMEOUT_VEC_OF(loop, loop->timeout.base_idx);
327      short* vec_of_vec
328        = PICOEV_TIMEOUT_VEC_OF_VEC_OF(loop, loop->timeout.base_idx);
329      for (i = 0; i < picoev.timeout_vec_of_vec_size; ++i) {
330        short vv = vec_of_vec[i];
331        if (vv != 0) {
332          for (j = i * PICOEV_SHORT_BITS; vv != 0; j++, vv <<= 1) {
333            if (vv < 0) {
334              short v = vec[j];
335              assert(v != 0);
336              for (k = j * PICOEV_SHORT_BITS; v != 0; k++, v <<= 1) {
337                if (v < 0) {
338                  picoev_fd* fd = picoev.fds + k;
339                  assert(fd->loop_id == loop->loop_id);
340                  fd->timeout_idx = PICOEV_TIMEOUT_IDX_UNUSED;
341                  (*fd->callback)(loop, k, PICOEV_TIMEOUT, fd->cb_arg);
342                }
343              }
344              vec[j] = 0;
345            }
346          }
347          vec_of_vec[i] = 0;
348        }
349      }
350    }
351  }
352 
353  /* loop once */
354  PICOEV_INLINE
355  int picoev_loop_once(picoev_loop* loop, int max_wait) {
356    loop->now = time(NULL);
357    if (max_wait > loop->timeout.resolution) {
358      max_wait = loop->timeout.resolution;
359    }
360    if (picoev_poll_once_internal(loop, max_wait) != 0) {
361      return -1;
362    }
363    if (max_wait != 0) {
364      loop->now = time(NULL);
365    }
366    picoev_handle_timeout_internal(loop);
367    return 0;
368  }
369 
370#undef PICOEV_INLINE
371
372#ifdef __cplusplus
373}
374#endif
375
376#endif
Note: See TracBrowser for help on using the browser.