root/src/eventer/eventer.c

Revision c4546c715aee6db46888e7ab0bb87fd2ce5357c6, 1.8 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 6 years ago)

I sure hope this doesn't break other platforms too badly. Solaris support sans the eventer... untested, of course. refs #32.

  • Property mode set to 100644
Line 
1 #define _EVENTER_C_
2 #include "eventer/eventer.h"
3 #include "utils/noit_hash.h"
4
5 eventer_t eventer_alloc() {
6   eventer_t e;
7   e = calloc(1, sizeof(*e));
8   e->opset = eventer_POSIX_fd_opset;
9   return e;
10 }
11
12 int eventer_timecompare(const void *av, const void *bv) {
13   /* Herein we avoid equality.  This function is only used as a comparator
14    * for a heap of timed events.  If they are equal, b is considered less
15    * just to maintain an order (despite it not being stable).
16    */
17   const eventer_t a = (eventer_t)av;
18   const eventer_t b = (eventer_t)bv;
19   if(a->whence.tv_sec < b->whence.tv_sec) return -1;
20   if(a->whence.tv_sec == b->whence.tv_sec &&
21      a->whence.tv_usec < b->whence.tv_usec) return -1;
22   return 1;
23 }
24
25 void eventer_free(eventer_t e) {
26   free(e);
27 }
28
29 static noit_hash_table __name_to_func = NOIT_HASH_EMPTY;
30 static noit_hash_table __func_to_name = NOIT_HASH_EMPTY;
31 int eventer_name_callback(const char *name, eventer_func_t f) {
32   void **fptr = malloc(sizeof(*fptr));
33   *fptr = (void *)f;
34   noit_hash_replace(&__name_to_func, strdup(name), strlen(name), (void *)f, free, NULL);
35   noit_hash_replace(&__func_to_name, (char *)fptr, sizeof(*fptr), strdup(name),
36                     free, free);
37   return 0;
38 }
39 eventer_func_t eventer_callback_for_name(const char *name) {
40   eventer_func_t f;
41   if(noit_hash_retrieve(&__name_to_func, name, strlen(name), (void **)&f))
42     return f;
43   return (eventer_func_t)NULL;
44 }
45 const char *eventer_name_for_callback(eventer_func_t f) {
46   const char *name;
47   if(noit_hash_retrieve(&__func_to_name, (char *)&f, sizeof(f), (void **)&name))
48     return name;
49   return NULL;
50 }
51
52 int eventer_choose(const char *name) {
53   eventer_impl_t choice;
54   for(choice = registered_eventers[0]; choice; choice++) {
55     if(!strcmp(choice->name, name)) {
56       __eventer = choice;
57       return 0;
58     }
59   }
60   return -1;
61 }
Note: See TracBrowser for help on using the browser.