root/src/eventer/eventer.c

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

first whack at using fcntl everywhere. Heads up, a lot could break. resf #167.

  • 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 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <fcntl.h>
39
40 eventer_t eventer_alloc() {
41   eventer_t e;
42   e = calloc(1, sizeof(*e));
43   e->opset = eventer_POSIX_fd_opset;
44   return e;
45 }
46
47 int eventer_timecompare(const void *av, const void *bv) {
48   /* Herein we avoid equality.  This function is only used as a comparator
49    * for a heap of timed events.  If they are equal, b is considered less
50    * just to maintain an order (despite it not being stable).
51    */
52   const eventer_t a = (eventer_t)av;
53   const eventer_t b = (eventer_t)bv;
54   if(a->whence.tv_sec < b->whence.tv_sec) return -1;
55   if(a->whence.tv_sec == b->whence.tv_sec &&
56      a->whence.tv_usec < b->whence.tv_usec) return -1;
57   return 1;
58 }
59
60 void eventer_free(eventer_t e) {
61   free(e);
62 }
63
64 int eventer_set_fd_nonblocking(int fd) {
65   int flags;
66   if(((flags = fcntl(fd, F_GETFL, 0)) == -1) ||
67      (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1))
68     return -1;
69   return 0;
70 }
71 int eventer_set_fd_blocking(int fd) {
72   int flags;
73   if(((flags = fcntl(fd, F_GETFL, 0)) == -1) ||
74      (fcntl(fd, F_SETFL, flags & ~O_NONBLOCK) == -1))
75     return -1;
76   return 0;
77 }
78
79 static noit_hash_table __name_to_func = NOIT_HASH_EMPTY;
80 static noit_hash_table __func_to_name = NOIT_HASH_EMPTY;
81 int eventer_name_callback(const char *name, eventer_func_t f) {
82   void **fptr = malloc(sizeof(*fptr));
83   *fptr = (void *)f;
84   noit_hash_replace(&__name_to_func, strdup(name), strlen(name), (void *)f, free, NULL);
85   noit_hash_replace(&__func_to_name, (char *)fptr, sizeof(*fptr), strdup(name),
86                     free, free);
87   return 0;
88 }
89 eventer_func_t eventer_callback_for_name(const char *name) {
90   void *vf;
91   if(noit_hash_retrieve(&__name_to_func, name, strlen(name), &vf))
92     return (eventer_func_t)vf;
93   return (eventer_func_t)NULL;
94 }
95 const char *eventer_name_for_callback(eventer_func_t f) {
96   const char *name;
97   if(noit_hash_retr_str(&__func_to_name, (char *)&f, sizeof(f), &name))
98     return name;
99   return NULL;
100 }
101
102 int eventer_choose(const char *name) {
103   int i = 0;
104   eventer_impl_t choice;
105   for(choice = registered_eventers[i];
106       choice;
107       choice = registered_eventers[++i]) {
108     if(!strcmp(choice->name, name)) {
109       __eventer = choice;
110       return 0;
111     }
112   }
113   return -1;
114 }
Note: See TracBrowser for help on using the browser.