root/src/eventer/eventer.c

Revision 88a71780101cbf23034aa0cb840f9f0368fda2dd, 3.5 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 5 years ago)

fixes #126

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2007, OmniTI Computer Consulting, 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
7  * met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  *       notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  *       copyright notice, this list of conditions and the following
13  *       disclaimer in the documentation and/or other materials provided
14  *       with the distribution.
15  *     * Neither the name OmniTI Computer Consulting, Inc. nor the names
16  *       of its contributors may be used to endorse or promote products
17  *       derived from this software without specific prior written
18  *       permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #define _EVENTER_C_
34 #include "eventer/eventer.h"
35 #include "utils/noit_hash.h"
36
37 eventer_t eventer_alloc() {
38   eventer_t e;
39   e = calloc(1, sizeof(*e));
40   e->opset = eventer_POSIX_fd_opset;
41   return e;
42 }
43
44 int eventer_timecompare(const void *av, const void *bv) {
45   /* Herein we avoid equality.  This function is only used as a comparator
46    * for a heap of timed events.  If they are equal, b is considered less
47    * just to maintain an order (despite it not being stable).
48    */
49   const eventer_t a = (eventer_t)av;
50   const eventer_t b = (eventer_t)bv;
51   if(a->whence.tv_sec < b->whence.tv_sec) return -1;
52   if(a->whence.tv_sec == b->whence.tv_sec &&
53      a->whence.tv_usec < b->whence.tv_usec) return -1;
54   return 1;
55 }
56
57 void eventer_free(eventer_t e) {
58   free(e);
59 }
60
61 static noit_hash_table __name_to_func = NOIT_HASH_EMPTY;
62 static noit_hash_table __func_to_name = NOIT_HASH_EMPTY;
63 int eventer_name_callback(const char *name, eventer_func_t f) {
64   void **fptr = malloc(sizeof(*fptr));
65   *fptr = (void *)f;
66   noit_hash_replace(&__name_to_func, strdup(name), strlen(name), (void *)f, free, NULL);
67   noit_hash_replace(&__func_to_name, (char *)fptr, sizeof(*fptr), strdup(name),
68                     free, free);
69   return 0;
70 }
71 eventer_func_t eventer_callback_for_name(const char *name) {
72   void *vf;
73   if(noit_hash_retrieve(&__name_to_func, name, strlen(name), &vf))
74     return (eventer_func_t)vf;
75   return (eventer_func_t)NULL;
76 }
77 const char *eventer_name_for_callback(eventer_func_t f) {
78   const char *name;
79   if(noit_hash_retr_str(&__func_to_name, (char *)&f, sizeof(f), &name))
80     return name;
81   return NULL;
82 }
83
84 int eventer_choose(const char *name) {
85   int i = 0;
86   eventer_impl_t choice;
87   for(choice = registered_eventers[i];
88       choice;
89       choice = registered_eventers[++i]) {
90     if(!strcmp(choice->name, name)) {
91       __eventer = choice;
92       return 0;
93     }
94   }
95   return -1;
96 }
Note: See TracBrowser for help on using the browser.