root/src/eventer/eventer.c

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

glue up some actual XML

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