root/src/eventer/eventer.c

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

oh my... http

  • 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   void **fptr = malloc(sizeof(*fptr));
34   *fptr = (void *)f;
35   noit_hash_replace(&__name_to_func, strdup(name), strlen(name), f, free, NULL);
36   noit_hash_replace(&__func_to_name, (char *)fptr, sizeof(*fptr), strdup(name),
37                     free, free);
38   return 0;
39 }
40 eventer_func_t eventer_callback_for_name(const char *name) {
41   eventer_func_t f;
42   if(noit_hash_retrieve(&__name_to_func, name, strlen(name), (void **)&f))
43     return f;
44   return (eventer_func_t)NULL;
45 }
46 const char *eventer_name_for_callback(eventer_func_t f) {
47   const char *name;
48   if(noit_hash_retrieve(&__func_to_name, (char *)&f, sizeof(f), (void **)&name))
49     return name;
50   return NULL;
51 }
52
53 int eventer_choose(const char *name) {
54   eventer_impl_t choice;
55   for(choice = registered_eventers[0]; choice; choice++) {
56     if(!strcmp(choice->name, name)) {
57       __eventer = choice;
58       return 0;
59     }
60   }
61   return -1;
62 }
Note: See TracBrowser for help on using the browser.