| 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 |
#include "eventloop/eventloop_internal.h" |
| 23 |
|
| 24 |
void |
| 25 |
mowgli_simple_eventloop_timeout_once(mowgli_eventloop_t *eventloop, int timeout) |
| 26 |
{ |
| 27 |
time_t delay, currtime; |
| 28 |
int t; |
| 29 |
|
| 30 |
return_if_fail(eventloop != NULL); |
| 31 |
return_if_fail(eventloop->eventloop_ops != NULL); |
| 32 |
|
| 33 |
mowgli_eventloop_synchronize(eventloop); |
| 34 |
|
| 35 |
currtime = mowgli_eventloop_get_time(eventloop); |
| 36 |
delay = mowgli_eventloop_next_timer(eventloop); |
| 37 |
|
| 38 |
while (delay != -1 && delay <= currtime) |
| 39 |
{ |
| 40 |
mowgli_eventloop_run_timers(eventloop); |
| 41 |
mowgli_eventloop_synchronize(eventloop); |
| 42 |
|
| 43 |
currtime = mowgli_eventloop_get_time(eventloop); |
| 44 |
delay = mowgli_eventloop_next_timer(eventloop); |
| 45 |
} |
| 46 |
|
| 47 |
if (timeout) |
| 48 |
t = timeout; |
| 49 |
else if (delay == -1) |
| 50 |
t = 5000; /* arbitrary 5 second default timeout */ |
| 51 |
else |
| 52 |
t = (delay - currtime) * 1000; |
| 53 |
|
| 54 |
#ifdef DEBUG |
| 55 |
mowgli_log("delay: %ld, currtime: %ld, select period: %d", delay, currtime, t); |
| 56 |
#endif |
| 57 |
|
| 58 |
eventloop->eventloop_ops->select(eventloop, t); |
| 59 |
} |
| 60 |
|
| 61 |
void |
| 62 |
mowgli_simple_eventloop_run_once(mowgli_eventloop_t *eventloop) |
| 63 |
{ |
| 64 |
eventloop->eventloop_ops->timeout_once(eventloop, 0); |
| 65 |
} |
| 66 |
|
| 67 |
void |
| 68 |
mowgli_simple_eventloop_error_handler(mowgli_eventloop_t *eventloop, mowgli_eventloop_io_t *io, mowgli_eventloop_io_dir_t dir, void *userdata) |
| 69 |
{ |
| 70 |
mowgli_eventloop_pollable_t *pollable = mowgli_eventloop_io_pollable(io); |
| 71 |
|
| 72 |
if (pollable != NULL) |
| 73 |
mowgli_pollable_destroy(eventloop, pollable); |
| 74 |
} |
| 75 |
|
| 76 |
static void |
| 77 |
mowgli_null_eventloop_pollsetup(mowgli_eventloop_t *eventloop) |
| 78 |
{ |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
static void |
| 83 |
mowgli_null_eventloop_pollshutdown(mowgli_eventloop_t *eventloop) |
| 84 |
{ |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
static void |
| 89 |
mowgli_null_eventloop_destroy(mowgli_eventloop_t *eventloop, mowgli_eventloop_pollable_t *pollable) |
| 90 |
{ |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
static void |
| 95 |
mowgli_null_eventloop_setselect(mowgli_eventloop_t *eventloop, mowgli_eventloop_pollable_t *pollable, mowgli_eventloop_io_dir_t dir, mowgli_eventloop_io_cb_t *event_function) |
| 96 |
{ |
| 97 |
mowgli_log("null eventloop does not really do polling, events for pollable<%p> will be ignored", (void *) pollable); |
| 98 |
|
| 99 |
switch (dir) |
| 100 |
{ |
| 101 |
case MOWGLI_EVENTLOOP_IO_READ: |
| 102 |
pollable->read_function = event_function; |
| 103 |
break; |
| 104 |
case MOWGLI_EVENTLOOP_IO_WRITE: |
| 105 |
pollable->write_function = event_function; |
| 106 |
break; |
| 107 |
default: |
| 108 |
mowgli_log("unhandled pollable direction %d", dir); |
| 109 |
break; |
| 110 |
} |
| 111 |
|
| 112 |
return; |
| 113 |
} |
| 114 |
|
| 115 |
static void |
| 116 |
mowgli_null_eventloop_select(mowgli_eventloop_t *eventloop, int time) |
| 117 |
{ |
| 118 |
for (; time > 999999; time -= 999999) |
| 119 |
usleep(999999); |
| 120 |
usleep(time); |
| 121 |
} |
| 122 |
|
| 123 |
mowgli_eventloop_ops_t _mowgli_null_pollops = |
| 124 |
{ |
| 125 |
.timeout_once = mowgli_simple_eventloop_timeout_once, |
| 126 |
.run_once = mowgli_simple_eventloop_run_once, |
| 127 |
.pollsetup = mowgli_null_eventloop_pollsetup, |
| 128 |
.pollshutdown = mowgli_null_eventloop_pollshutdown, |
| 129 |
.setselect = mowgli_null_eventloop_setselect, |
| 130 |
.select = mowgli_null_eventloop_select, |
| 131 |
.destroy = mowgli_null_eventloop_destroy, |
| 132 |
}; |