| 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 |
static mowgli_heap_t *eventloop_heap = NULL; |
| 25 |
|
| 26 |
mowgli_eventloop_t * |
| 27 |
mowgli_eventloop_create(void) |
| 28 |
{ |
| 29 |
mowgli_eventloop_t *eventloop; |
| 30 |
|
| 31 |
if (eventloop_heap == NULL) |
| 32 |
eventloop_heap = mowgli_heap_create(sizeof(mowgli_eventloop_t), 16, BH_NOW); |
| 33 |
|
| 34 |
eventloop = mowgli_heap_alloc(eventloop_heap); |
| 35 |
|
| 36 |
eventloop->eventloop_ops = &_mowgli_null_pollops; |
| 37 |
|
| 38 |
#ifdef HAVE_SELECT |
| 39 |
eventloop->eventloop_ops = &_mowgli_select_pollops; |
| 40 |
#endif |
| 41 |
#ifdef HAVE_POLL_H |
| 42 |
eventloop->eventloop_ops = &_mowgli_poll_pollops; |
| 43 |
#endif |
| 44 |
#ifdef HAVE_SYS_EPOLL_H |
| 45 |
eventloop->eventloop_ops = &_mowgli_epoll_pollops; |
| 46 |
#endif |
| 47 |
#ifdef HAVE_KQUEUE |
| 48 |
eventloop->eventloop_ops = &_mowgli_kqueue_pollops; |
| 49 |
#endif |
| 50 |
#ifdef HAVE_DISPATCH_BLOCK |
| 51 |
eventloop->eventloop_ops = &_mowgli_qnx_pollops; |
| 52 |
#endif |
| 53 |
#ifdef HAVE_PORT_CREATE |
| 54 |
eventloop->eventloop_ops = &_mowgli_ports_pollops; |
| 55 |
#endif |
| 56 |
#if 0 |
| 57 |
eventloop->eventloop_ops = &_mowgli_winsock_pollops; |
| 58 |
#endif |
| 59 |
|
| 60 |
if (mowgli_mutex_init(&eventloop->mutex) != 0) |
| 61 |
{ |
| 62 |
mowgli_log("couldn't create mutex for eventloop %p, aborting...", (void *) eventloop); |
| 63 |
abort(); |
| 64 |
} |
| 65 |
|
| 66 |
eventloop->eventloop_ops->pollsetup(eventloop); |
| 67 |
|
| 68 |
eventloop->deadline = -1; |
| 69 |
|
| 70 |
mowgli_eventloop_calibrate(eventloop); |
| 71 |
|
| 72 |
return eventloop; |
| 73 |
} |
| 74 |
|
| 75 |
void |
| 76 |
mowgli_eventloop_destroy(mowgli_eventloop_t *eventloop) |
| 77 |
{ |
| 78 |
eventloop->eventloop_ops->pollshutdown(eventloop); |
| 79 |
|
| 80 |
mowgli_mutex_uninit(&eventloop->mutex); |
| 81 |
mowgli_heap_free(eventloop_heap, eventloop); |
| 82 |
} |
| 83 |
|
| 84 |
static void |
| 85 |
mowgli_eventloop_reap_pollables(mowgli_eventloop_t *eventloop) |
| 86 |
{ |
| 87 |
mowgli_node_t *n, *tn; |
| 88 |
MOWGLI_ITER_FOREACH_SAFE(n, tn, eventloop->destroyed_pollable_list.head) |
| 89 |
{ |
| 90 |
mowgli_pollable_free(n->data); |
| 91 |
mowgli_node_delete(n, &eventloop->destroyed_pollable_list); |
| 92 |
mowgli_node_free(n); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
void |
| 97 |
mowgli_eventloop_run(mowgli_eventloop_t *eventloop) |
| 98 |
{ |
| 99 |
return_if_fail(eventloop != NULL); |
| 100 |
|
| 101 |
mowgli_mutex_lock(&eventloop->mutex); |
| 102 |
|
| 103 |
eventloop->death_requested = false; |
| 104 |
|
| 105 |
eventloop->processing_events = true; |
| 106 |
|
| 107 |
while (!eventloop->death_requested) |
| 108 |
{ |
| 109 |
eventloop->eventloop_ops->run_once(eventloop); |
| 110 |
mowgli_eventloop_reap_pollables(eventloop); |
| 111 |
} |
| 112 |
|
| 113 |
eventloop->processing_events = false; |
| 114 |
|
| 115 |
mowgli_mutex_unlock(&eventloop->mutex); |
| 116 |
} |
| 117 |
|
| 118 |
void |
| 119 |
mowgli_eventloop_run_once(mowgli_eventloop_t *eventloop) |
| 120 |
{ |
| 121 |
return_if_fail(eventloop != NULL); |
| 122 |
|
| 123 |
mowgli_mutex_lock(&eventloop->mutex); |
| 124 |
|
| 125 |
eventloop->processing_events = true; |
| 126 |
eventloop->eventloop_ops->run_once(eventloop); |
| 127 |
eventloop->processing_events = false; |
| 128 |
|
| 129 |
mowgli_eventloop_reap_pollables(eventloop); |
| 130 |
|
| 131 |
mowgli_mutex_unlock(&eventloop->mutex); |
| 132 |
} |
| 133 |
|
| 134 |
void |
| 135 |
mowgli_eventloop_timeout_once(mowgli_eventloop_t *eventloop, int timeout) |
| 136 |
{ |
| 137 |
return_if_fail(eventloop != NULL); |
| 138 |
|
| 139 |
mowgli_mutex_lock(&eventloop->mutex); |
| 140 |
|
| 141 |
eventloop->processing_events = true; |
| 142 |
|
| 143 |
if (timeout >= 0) |
| 144 |
eventloop->eventloop_ops->timeout_once(eventloop, timeout); |
| 145 |
else |
| 146 |
eventloop->eventloop_ops->run_once(eventloop); |
| 147 |
|
| 148 |
eventloop->processing_events = false; |
| 149 |
|
| 150 |
mowgli_eventloop_reap_pollables(eventloop); |
| 151 |
|
| 152 |
mowgli_mutex_unlock(&eventloop->mutex); |
| 153 |
} |
| 154 |
|
| 155 |
void |
| 156 |
mowgli_eventloop_break(mowgli_eventloop_t *eventloop) |
| 157 |
{ |
| 158 |
return_if_fail(eventloop != NULL); |
| 159 |
|
| 160 |
eventloop->death_requested = true; |
| 161 |
} |
| 162 |
|
| 163 |
/* convenience function to request null pollops */ |
| 164 |
void |
| 165 |
mowgli_eventloop_timers_only(mowgli_eventloop_t *eventloop) |
| 166 |
{ |
| 167 |
return_if_fail(eventloop != NULL); |
| 168 |
|
| 169 |
eventloop->eventloop_ops = &_mowgli_null_pollops; |
| 170 |
} |
| 171 |
|
| 172 |
/* userdata setting/getting functions (for bindings) */ |
| 173 |
void * |
| 174 |
mowgli_eventloop_get_data(mowgli_eventloop_t *eventloop) |
| 175 |
{ |
| 176 |
return_val_if_fail(eventloop != NULL, NULL); |
| 177 |
|
| 178 |
return eventloop->data; |
| 179 |
} |
| 180 |
|
| 181 |
void |
| 182 |
mowgli_eventloop_set_data(mowgli_eventloop_t *eventloop, void *data) |
| 183 |
{ |
| 184 |
return_if_fail(eventloop != NULL); |
| 185 |
|
| 186 |
eventloop->data = data; |
| 187 |
} |