Debian Codegraph @

libmowgli-2/src/libmowgli/eventloop/pollable.c

raw
1 /*
2 * Copyright (c) 2011 William Pitcock <[email protected]>.
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
9 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
11 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
12 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
13 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
14 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
15 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
16 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
17 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
18 * POSSIBILITY OF SUCH DAMAGE.
19 */
20
21 #include "mowgli.h"
22
23 static mowgli_heap_t *pollable_heap = NULL;
24
25 mowgli_eventloop_pollable_t *
26 mowgli_pollable_create(mowgli_eventloop_t *eventloop, mowgli_descriptor_t fd, void *userdata)
27 {
28 mowgli_eventloop_pollable_t *pollable;
29
30 return_val_if_fail(eventloop != NULL, NULL);
31
32 if (pollable_heap == NULL)
33 pollable_heap = mowgli_heap_create(sizeof(mowgli_eventloop_pollable_t), 16, BH_NOW);
34
35 pollable = mowgli_heap_alloc(pollable_heap);
36
37 pollable->eventloop = eventloop;
38 pollable->type.type = MOWGLI_EVENTLOOP_TYPE_POLLABLE;
39 pollable->fd = fd;
40 pollable->userdata = userdata;
41
42 return pollable;
43 }
44
45 void
46 mowgli_pollable_free(mowgli_eventloop_pollable_t *pollable)
47 {
48 mowgli_heap_free(pollable_heap, pollable);
49 }
50
51 void
52 mowgli_pollable_destroy(mowgli_eventloop_t *eventloop, mowgli_eventloop_pollable_t *pollable)
53 {
54 return_if_fail(eventloop != NULL);
55 return_if_fail(pollable != NULL);
56 return_if_fail(!pollable->removed);
57
58 /* unregister any interest in the pollable. */
59 eventloop->eventloop_ops->destroy(eventloop, pollable);
60
61 /* we cannot safely free a pollable from within the event loop
62 * as the event processing code might still hold pointers to it;
63 * only mark it as removed in that case and have the event loop
64 * handle cleanup as needed
65 */
66 if (eventloop->processing_events)
67 {
68 pollable->removed = true;
69 mowgli_node_add(pollable, mowgli_node_create(), &eventloop->destroyed_pollable_list);
70 }
71 else
72 {
73 mowgli_pollable_free(pollable);
74 }
75 }
76
77 void
78 mowgli_pollable_setselect(mowgli_eventloop_t *eventloop, mowgli_eventloop_pollable_t *pollable, mowgli_eventloop_io_dir_t dir, mowgli_eventloop_io_cb_t *event_function)
79 {
80 return_if_fail(eventloop != NULL);
81 return_if_fail(pollable != NULL);
82 return_if_fail(eventloop->eventloop_ops != NULL);
83
84 eventloop->eventloop_ops->setselect(eventloop, pollable, dir, event_function);
85 }
86
87 void
88 mowgli_pollable_set_nonblocking(mowgli_eventloop_pollable_t *pollable, bool nonblocking)
89 {
90 #if defined(HAVE_FCNTL)
91 unsigned long flags;
92
93 return_if_fail(pollable != NULL);
94
95 flags = fcntl(pollable->fd, F_GETFL);
96
97 if (nonblocking)
98 flags |= O_NONBLOCK;
99 else
100 flags &= ~O_NONBLOCK;
101
102 fcntl(pollable->fd, F_SETFL, flags);
103 #elif defined(HAVE_WINSOCK2_H)
104 u_long mode;
105
106 return_if_fail(pollable != NULL);
107
108 mode = nonblocking;
109
110 ioctlsocket(pollable->fd, FIONBIO, &mode);
111 #endif
112 }
113
114 void
115 mowgli_pollable_set_cloexec(mowgli_eventloop_pollable_t *pollable, bool cloexec)
116 {
117 #if defined(FD_CLOEXEC)
118 unsigned long flags;
119
120 return_if_fail(pollable != NULL);
121
122 flags = fcntl(pollable->fd, F_GETFD);
123
124 if (cloexec)
125 flags |= FD_CLOEXEC;
126 else
127 flags &= ~FD_CLOEXEC;
128
129 fcntl(pollable->fd, F_SETFD, flags);
130 #elif defined(HAVE_WINSOCK2_H)
131 return_if_fail(pollable != NULL);
132
133 SetHandleInformation((HANDLE)pollable->fd, HANDLE_FLAG_INHERIT, !cloexec);
134 #endif
135 }
136
137 void
138 mowgli_pollable_trigger(mowgli_eventloop_t *eventloop, mowgli_eventloop_pollable_t *pollable, mowgli_eventloop_io_dir_t dir)
139 {
140 mowgli_eventloop_io_cb_t *event_function;
141
142 return_if_fail(eventloop != NULL);
143 return_if_fail(pollable != NULL);
144
145 switch (dir)
146 {
147 case MOWGLI_EVENTLOOP_IO_READ:
148 event_function = pollable->read_function;
149 break;
150 case MOWGLI_EVENTLOOP_IO_WRITE:
151 event_function = pollable->write_function;
152 break;
153 default:
154 event_function = NULL;
155 return;
156 }
157
158 if (event_function == NULL)
159 return;
160
161 event_function(eventloop, pollable, dir, pollable->userdata);
162 }