forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio_ev.c
373 lines (337 loc) · 9.14 KB
/
io_ev.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/**
* @file
*
* @brief I/O EV binding.
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*
*/
#include <stdlib.h>
#include <ev.h>
#include <kdbassert.h>
#include <kdbhelper.h>
#include <kdbio.h>
#include <kdblogger.h>
typedef struct ev_loop ev_loop_t;
/**
* Container for required additional information for
* I/O binding operations & libev Handles
*/
typedef struct EvBindingData
{
union
{
ElektraIoFdOperation * fd;
ElektraIoTimerOperation * timer;
ElektraIoIdleOperation * idle;
} operation;
union
{
ev_io fd;
ev_timer timer;
ev_idle idle;
} handle;
} EvBindingData;
/**
* Convert I/O flags to libuv event bit mask
* @param flags I/O flags bit mask
* @return libuv events bit mask
*/
static int flagsToEvents (int flags)
{
int events = 0;
if (flags & ELEKTRA_IO_READABLE)
{
events |= EV_READ;
}
if (flags & ELEKTRA_IO_WRITABLE)
{
events |= EV_WRITE;
}
return events;
}
/**
* Convert libuv event bit mask to I/O flags
* @param events libev events bit mask
* @return I/O flags bit mask
*/
static int eventsToFlags (int events)
{
int flags = 0;
if (events & EV_READ)
{
flags |= ELEKTRA_IO_READABLE;
}
if (events & EV_WRITE)
{
flags |= ELEKTRA_IO_WRITABLE;
}
return flags;
}
static EvBindingData * newBindingData (void)
{
EvBindingData * bindingData = elektraCalloc (sizeof (*bindingData));
if (bindingData == NULL)
{
ELEKTRA_LOG_WARNING ("elektraCalloc failed");
return NULL;
}
return bindingData;
}
/**
* Converts the events bit mask and calls the associated callback
* Called by libuv whenever a file descriptor status has changed.
*
* @param handle libuv poll handle
* @param events events bit mask
*/
static void ioEvBindingFdCallback (ev_loop_t * loop ELEKTRA_UNUSED, ev_io * handle, int revents ELEKTRA_UNUSED)
{
ELEKTRA_NOT_NULL (handle->data);
EvBindingData * bindingData = (EvBindingData *) handle->data;
ElektraIoFdOperation * fdOp = (ElektraIoFdOperation *) bindingData->operation.fd;
elektraIoFdGetCallback (fdOp) (fdOp, eventsToFlags (revents));
}
/**
* Calls the associated callback.
* Called by libuv whenever a timer has elapsed.
*
* @param handle libuv timer handle
*/
static void ioEvBindingTimerCallback (ev_loop_t * loop ELEKTRA_UNUSED, ev_timer * handle, int revents ELEKTRA_UNUSED)
{
ELEKTRA_NOT_NULL (handle->data);
EvBindingData * bindingData = (EvBindingData *) handle->data;
ElektraIoTimerOperation * timerOp = (ElektraIoTimerOperation *) bindingData->operation.timer;
elektraIoTimerGetCallback (timerOp) (timerOp);
}
/**
* Calls the associated callback.
* Called by libuv whenever an idle operation can perform its operations.
*
* @param handle libuv idle handle
*/
static void ioEvBindingIdleCallback (ev_loop_t * loop ELEKTRA_UNUSED, ev_idle * handle, int revents ELEKTRA_UNUSED)
{
ELEKTRA_NOT_NULL (handle->data);
EvBindingData * bindingData = (EvBindingData *) handle->data;
ElektraIoIdleOperation * idleOp = (ElektraIoIdleOperation *) bindingData->operation.idle;
elektraIoIdleGetCallback (idleOp) (idleOp);
}
/**
* Update information about a file descriptor watched by I/O binding.
* @see kdbio.h ::ElektraIoBindingUpdateFd
*/
static int ioEvBindingUpdateFd (ElektraIoFdOperation * fdOp)
{
EvBindingData * bindingData = (EvBindingData *) elektraIoFdGetBindingData (fdOp);
ELEKTRA_NOT_NULL (bindingData);
ev_loop_t * loop = elektraIoBindingGetData (elektraIoFdGetBinding (fdOp));
ev_io * fd = &bindingData->handle.fd;
if (elektraIoFdIsEnabled (fdOp))
{
ev_io_stop (loop, fd);
ev_io_set (fd, elektraIoFdGetFd (fdOp), flagsToEvents (elektraIoFdGetFlags (fdOp)));
ev_io_start (loop, fd);
}
else
{
ev_io_stop (loop, fd);
}
return 1;
}
/**
* Add file descriptor to I/O binding
* @see kdbio.h ::ElektraIoBindingAddFd
*/
static int ioEvBindingAddFd (ElektraIoInterface * binding ELEKTRA_UNUSED, ElektraIoFdOperation * fdOp)
{
EvBindingData * bindingData = newBindingData ();
if (bindingData == NULL)
{
return 0;
}
elektraIoFdSetBindingData (fdOp, bindingData);
bindingData->operation.fd = fdOp;
bindingData->handle.fd.data = bindingData;
ev_io * fd = &bindingData->handle.fd;
ev_io_init (fd, ioEvBindingFdCallback, elektraIoFdGetFd (fdOp), flagsToEvents (elektraIoFdGetFlags (fdOp)));
// Start polling if enabled
if (elektraIoFdIsEnabled (fdOp))
{
ioEvBindingUpdateFd (fdOp);
}
return 1;
}
/**
* Remove file descriptor from I/O binding.
* @see kdbio.h ::ElektraIoBindingRemoveFd
*/
static int ioEvBindingRemoveFd (ElektraIoFdOperation * fdOp)
{
EvBindingData * bindingData = (EvBindingData *) elektraIoFdGetBindingData (fdOp);
ELEKTRA_NOT_NULL (bindingData);
ev_loop_t * loop = elektraIoBindingGetData (elektraIoFdGetBinding (fdOp));
ev_io * fd = &bindingData->handle.fd;
ev_io_stop (loop, fd);
elektraFree (bindingData);
return 1;
}
/**
* Update timer in I/O binding.
* @see kdbio.h ::ElektraIoBindingUpdateTimer
*/
static int ioEvBindingUpdateTimer (ElektraIoTimerOperation * timerOp)
{
EvBindingData * bindingData = (EvBindingData *) elektraIoTimerGetBindingData (timerOp);
ELEKTRA_NOT_NULL (bindingData);
ev_loop_t * loop = elektraIoBindingGetData (elektraIoTimerGetBinding (timerOp));
ev_timer * timer = &bindingData->handle.timer;
if (elektraIoTimerIsEnabled (timerOp))
{
double interval = elektraIoTimerGetInterval (timerOp) / 1000.0;
ev_timer_set (timer, interval, interval);
ev_timer_start (loop, timer);
}
else
{
ev_timer_stop (loop, timer);
}
return 1;
}
/**
* Add timer for I/O binding.
* @see kdbio.h ::ElektraIoBindingAddTimer
*/
static int ioEvBindingAddTimer (ElektraIoInterface * binding ELEKTRA_UNUSED, ElektraIoTimerOperation * timerOp)
{
EvBindingData * bindingData = newBindingData ();
if (bindingData == NULL)
{
return 0;
}
elektraIoTimerSetBindingData (timerOp, bindingData);
bindingData->operation.timer = timerOp;
bindingData->handle.timer.data = bindingData;
ev_timer * timer = &bindingData->handle.timer;
ev_timer_init (timer, ioEvBindingTimerCallback, 0, 0);
// Start timer if enabled
if (elektraIoTimerIsEnabled (timerOp))
{
ioEvBindingUpdateTimer (timerOp);
}
return 1;
}
/**
* Remove timer from I/O binding
* @see kdbio.h ::ElektraIoBindingRemoveTimer
*/
static int ioEvBindingRemoveTimer (ElektraIoTimerOperation * timerOp)
{
EvBindingData * bindingData = (EvBindingData *) elektraIoTimerGetBindingData (timerOp);
ELEKTRA_NOT_NULL (bindingData);
ev_loop_t * loop = elektraIoBindingGetData (elektraIoTimerGetBinding (timerOp));
ev_timer * timer = &bindingData->handle.timer;
ev_timer_stop (loop, timer);
elektraFree (bindingData);
return 1;
}
/**
* Update idle operation in I/O binding
* @see kdbio.h ::ElektraIoBindingUpdateIdle
*/
static int ioEvBindingUpdateIdle (ElektraIoIdleOperation * idleOp)
{
EvBindingData * bindingData = elektraIoIdleGetBindingData (idleOp);
ELEKTRA_NOT_NULL (bindingData);
ev_loop_t * loop = elektraIoBindingGetData (elektraIoIdleGetBinding (idleOp));
ev_idle * idle = &bindingData->handle.idle;
if (elektraIoIdleIsEnabled (idleOp))
{
ev_idle_start (loop, idle);
}
else
{
ev_idle_stop (loop, idle);
}
return 1;
}
/**
* Add idle operation to I/O binding
* @see kdbio.h ::ElektraIoBindingAddIdle
*/
static int ioEvBindingAddIdle (ElektraIoInterface * binding ELEKTRA_UNUSED, ElektraIoIdleOperation * idleOp)
{
EvBindingData * bindingData = newBindingData ();
if (bindingData == NULL)
{
return 0;
}
elektraIoIdleSetBindingData (idleOp, bindingData);
bindingData->operation.idle = idleOp;
bindingData->handle.idle.data = bindingData;
ev_idle * idle = &bindingData->handle.idle;
ev_idle_init (idle, ioEvBindingIdleCallback);
// Add idle to loop if enabled
if (elektraIoIdleIsEnabled (idleOp))
{
ioEvBindingUpdateIdle (idleOp);
}
return 1;
}
/**
* Remove idle operation from I/O binding
* @see kdbio.h ::ElektraIoBindingRemoveIdle
*/
static int ioEvBindingRemoveIdle (ElektraIoIdleOperation * idleOp)
{
EvBindingData * bindingData = elektraIoIdleGetBindingData (idleOp);
ELEKTRA_NOT_NULL (bindingData);
ev_loop_t * loop = elektraIoBindingGetData (elektraIoIdleGetBinding (idleOp));
ev_idle * idle = &bindingData->handle.idle;
ev_idle_stop (loop, idle);
elektraFree (bindingData);
return 1;
}
/**
* Cleanup
* @param binding I/O binding
* @see kdbio.h ::ElektraIoBindingCleanup
*/
static int ioEvBindingCleanup (ElektraIoInterface * binding)
{
ELEKTRA_NOT_NULL (binding);
elektraFree (binding);
return 1;
}
/**
* Create and initialize a new I/O binding.
* @param loop Loop to use for I/O operations
* @return Populated I/O interface
*/
ElektraIoInterface * elektraIoEvNew (struct ev_loop * loop)
{
if (loop == NULL)
{
ELEKTRA_LOG_WARNING ("loop was NULL");
return NULL;
}
// Initialize I/O interface
ElektraIoInterface * binding = elektraIoNewBinding (
// file descriptors
ioEvBindingAddFd, ioEvBindingUpdateFd, ioEvBindingRemoveFd,
// timers
ioEvBindingAddTimer, ioEvBindingUpdateTimer, ioEvBindingRemoveTimer,
// idle
ioEvBindingAddIdle, ioEvBindingUpdateIdle, ioEvBindingRemoveIdle,
// cleanup
ioEvBindingCleanup);
if (binding == NULL)
{
ELEKTRA_LOG_WARNING ("elektraIoNewBinding failed");
return NULL;
}
// Save the libuv loop we are using
elektraIoBindingSetData (binding, loop);
return binding;
}