root/src/eventer/eventer.c

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

And make it all work on OpenBSD (ld -E ? sigh...)

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