[9c7a701] | 1 | #include "owl.h" |
---|
| 2 | |
---|
[2c79eae] | 3 | static GMainLoop *loop = NULL; |
---|
[1895c29] | 4 | static int dispatch_active = 0; |
---|
[2c79eae] | 5 | |
---|
| 6 | static GSource *owl_timer_source; |
---|
| 7 | static GSource *owl_io_dispatch_source; |
---|
[1895c29] | 8 | |
---|
[ebb8498] | 9 | static int _owl_select_timer_cmp(const owl_timer *t1, const owl_timer *t2) { |
---|
[ca1fc33a] | 10 | return t1->time - t2->time; |
---|
[b7bb454] | 11 | } |
---|
| 12 | |
---|
[c6adf17] | 13 | owl_timer *owl_select_add_timer(const char* name, int after, int interval, void (*cb)(owl_timer *, void *), void (*destroy)(owl_timer*), void *data) |
---|
[b7bb454] | 14 | { |
---|
[96828e4] | 15 | owl_timer *t = g_new(owl_timer, 1); |
---|
[58d1f8a] | 16 | GList **timers = owl_global_get_timerlist(&g); |
---|
[b7bb454] | 17 | |
---|
[ca1fc33a] | 18 | t->time = time(NULL) + after; |
---|
| 19 | t->interval = interval; |
---|
| 20 | t->callback = cb; |
---|
[c675b39] | 21 | t->destroy = destroy; |
---|
[ca1fc33a] | 22 | t->data = data; |
---|
[d4927a7] | 23 | t->name = name ? g_strdup(name) : NULL; |
---|
[b7bb454] | 24 | |
---|
[58d1f8a] | 25 | *timers = g_list_insert_sorted(*timers, t, |
---|
| 26 | (GCompareFunc)_owl_select_timer_cmp); |
---|
[ca1fc33a] | 27 | return t; |
---|
[b7bb454] | 28 | } |
---|
| 29 | |
---|
| 30 | void owl_select_remove_timer(owl_timer *t) |
---|
| 31 | { |
---|
[58d1f8a] | 32 | GList **timers = owl_global_get_timerlist(&g); |
---|
| 33 | if (t && g_list_find(*timers, t)) { |
---|
| 34 | *timers = g_list_remove(*timers, t); |
---|
[c675b39] | 35 | if(t->destroy) { |
---|
| 36 | t->destroy(t); |
---|
| 37 | } |
---|
[ddbbcffa] | 38 | g_free(t->name); |
---|
| 39 | g_free(t); |
---|
[ca1fc33a] | 40 | } |
---|
[b7bb454] | 41 | } |
---|
| 42 | |
---|
[2c79eae] | 43 | static gboolean owl_timer_prepare(GSource *source, int *timeout) { |
---|
| 44 | GList **timers = owl_global_get_timerlist(&g); |
---|
| 45 | GTimeVal now; |
---|
| 46 | |
---|
| 47 | /* TODO: In the far /far/ future, g_source_get_time is what the cool |
---|
| 48 | * kids use to get system monotonic time. */ |
---|
| 49 | g_source_get_current_time(source, &now); |
---|
| 50 | |
---|
| 51 | /* FIXME: bother with millisecond accuracy now that we can? */ |
---|
| 52 | if (*timers) { |
---|
| 53 | owl_timer *t = (*timers)->data; |
---|
| 54 | *timeout = t->time - now.tv_sec; |
---|
| 55 | if (*timeout <= 0) { |
---|
| 56 | *timeout = 0; |
---|
| 57 | return TRUE; |
---|
| 58 | } |
---|
| 59 | if (*timeout > 60 * 1000) |
---|
| 60 | *timeout = 60 * 1000; |
---|
| 61 | } else { |
---|
| 62 | *timeout = 60 * 1000; |
---|
| 63 | } |
---|
| 64 | return FALSE; |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | static gboolean owl_timer_check(GSource *source) { |
---|
[58d1f8a] | 68 | GList **timers = owl_global_get_timerlist(&g); |
---|
[2c79eae] | 69 | GTimeVal now; |
---|
[b7bb454] | 70 | |
---|
[2c79eae] | 71 | /* TODO: In the far /far/ future, g_source_get_time is what the cool |
---|
| 72 | * kids use to get system monotonic time. */ |
---|
| 73 | g_source_get_current_time(source, &now); |
---|
| 74 | |
---|
| 75 | /* FIXME: bother with millisecond accuracy now that we can? */ |
---|
| 76 | if (*timers) { |
---|
| 77 | owl_timer *t = (*timers)->data; |
---|
| 78 | return t->time >= now.tv_sec; |
---|
| 79 | } |
---|
| 80 | return FALSE; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | |
---|
| 84 | static gboolean owl_timer_dispatch(GSource *source, GSourceFunc callback, gpointer user_data) { |
---|
| 85 | GList **timers = owl_global_get_timerlist(&g); |
---|
| 86 | GTimeVal now; |
---|
| 87 | |
---|
| 88 | /* TODO: In the far /far/ future, g_source_get_time is what the cool |
---|
| 89 | * kids use to get system monotonic time. */ |
---|
| 90 | g_source_get_current_time(source, &now); |
---|
| 91 | |
---|
| 92 | /* FIXME: bother with millisecond accuracy now that we can? */ |
---|
[58d1f8a] | 93 | while(*timers) { |
---|
| 94 | owl_timer *t = (*timers)->data; |
---|
[c675b39] | 95 | int remove = 0; |
---|
[b7bb454] | 96 | |
---|
[2c79eae] | 97 | if(t->time > now.tv_sec) |
---|
[ca1fc33a] | 98 | break; |
---|
[b7bb454] | 99 | |
---|
[ca1fc33a] | 100 | /* Reschedule if appropriate */ |
---|
| 101 | if(t->interval > 0) { |
---|
[2c79eae] | 102 | t->time = now.tv_sec + t->interval; |
---|
[58d1f8a] | 103 | *timers = g_list_remove(*timers, t); |
---|
| 104 | *timers = g_list_insert_sorted(*timers, t, |
---|
| 105 | (GCompareFunc)_owl_select_timer_cmp); |
---|
[ca1fc33a] | 106 | } else { |
---|
[c675b39] | 107 | remove = 1; |
---|
[ca1fc33a] | 108 | } |
---|
[b7bb454] | 109 | |
---|
[ca1fc33a] | 110 | /* Do the callback */ |
---|
[c675b39] | 111 | t->callback(t, t->data); |
---|
| 112 | if(remove) { |
---|
| 113 | owl_select_remove_timer(t); |
---|
| 114 | } |
---|
[ca1fc33a] | 115 | } |
---|
[2c79eae] | 116 | return TRUE; |
---|
| 117 | } |
---|
[b7bb454] | 118 | |
---|
[2c79eae] | 119 | static GSourceFuncs owl_timer_funcs = { |
---|
| 120 | owl_timer_prepare, |
---|
| 121 | owl_timer_check, |
---|
| 122 | owl_timer_dispatch, |
---|
| 123 | NULL |
---|
| 124 | }; |
---|
[b7bb454] | 125 | |
---|
| 126 | |
---|
[df0138f] | 127 | static const owl_io_dispatch *owl_select_find_io_dispatch_by_fd(const int fd) |
---|
| 128 | { |
---|
| 129 | int i, len; |
---|
| 130 | const owl_list *dl; |
---|
| 131 | owl_io_dispatch *d; |
---|
| 132 | dl = owl_global_get_io_dispatch_list(&g); |
---|
| 133 | len = owl_list_get_size(dl); |
---|
| 134 | for(i = 0; i < len; i++) { |
---|
| 135 | d = owl_list_get_element(dl, i); |
---|
| 136 | if (d->fd == fd) return d; |
---|
| 137 | } |
---|
| 138 | return NULL; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | static int owl_select_find_io_dispatch(const owl_io_dispatch *in) |
---|
| 142 | { |
---|
| 143 | int i, len; |
---|
| 144 | const owl_list *dl; |
---|
| 145 | |
---|
| 146 | if (in != NULL) { |
---|
| 147 | dl = owl_global_get_io_dispatch_list(&g); |
---|
| 148 | len = owl_list_get_size(dl); |
---|
| 149 | for(i = 0; i < len; i++) { |
---|
| 150 | const owl_io_dispatch *d = owl_list_get_element(dl, i); |
---|
| 151 | if (d == in) return i; |
---|
| 152 | } |
---|
| 153 | } |
---|
| 154 | return -1; |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | void owl_select_remove_io_dispatch(const owl_io_dispatch *in) |
---|
| 158 | { |
---|
| 159 | int elt; |
---|
| 160 | if (in != NULL) { |
---|
| 161 | elt = owl_select_find_io_dispatch(in); |
---|
| 162 | if (elt != -1) { |
---|
| 163 | owl_list *dl = owl_global_get_io_dispatch_list(&g); |
---|
| 164 | owl_io_dispatch *d = owl_list_get_element(dl, elt); |
---|
| 165 | if (dispatch_active) |
---|
| 166 | d->needs_gc = 1; |
---|
| 167 | else { |
---|
| 168 | owl_list_remove_element(dl, elt); |
---|
| 169 | if (d->destroy) |
---|
| 170 | d->destroy(d); |
---|
[2c79eae] | 171 | g_source_remove_poll(owl_io_dispatch_source, &d->pollfd); |
---|
[ddbbcffa] | 172 | g_free(d); |
---|
[df0138f] | 173 | } |
---|
| 174 | } |
---|
| 175 | } |
---|
| 176 | } |
---|
| 177 | |
---|
[ebb8498] | 178 | static void owl_select_io_dispatch_gc(void) |
---|
[df0138f] | 179 | { |
---|
| 180 | int i; |
---|
| 181 | owl_list *dl; |
---|
| 182 | |
---|
| 183 | dl = owl_global_get_io_dispatch_list(&g); |
---|
| 184 | /* |
---|
| 185 | * Count down so we aren't set off by removing items from the list |
---|
| 186 | * during the iteration. |
---|
| 187 | */ |
---|
| 188 | for(i = owl_list_get_size(dl) - 1; i >= 0; i--) { |
---|
| 189 | owl_io_dispatch *d = owl_list_get_element(dl, i); |
---|
| 190 | if(d->needs_gc) { |
---|
| 191 | owl_select_remove_io_dispatch(d); |
---|
| 192 | } |
---|
| 193 | } |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | /* Each FD may have at most one dispatcher. |
---|
| 197 | * If a new dispatch is added for an FD, the old one is removed. |
---|
| 198 | * mode determines what types of events are watched for, and may be any combination of: |
---|
| 199 | * OWL_IO_READ, OWL_IO_WRITE, OWL_IO_EXCEPT |
---|
| 200 | */ |
---|
| 201 | const owl_io_dispatch *owl_select_add_io_dispatch(int fd, int mode, void (*cb)(const owl_io_dispatch *, void *), void (*destroy)(const owl_io_dispatch *), void *data) |
---|
| 202 | { |
---|
[96828e4] | 203 | owl_io_dispatch *d = g_new(owl_io_dispatch, 1); |
---|
[df0138f] | 204 | owl_list *dl = owl_global_get_io_dispatch_list(&g); |
---|
| 205 | |
---|
| 206 | d->fd = fd; |
---|
| 207 | d->needs_gc = 0; |
---|
| 208 | d->mode = mode; |
---|
| 209 | d->callback = cb; |
---|
| 210 | d->destroy = destroy; |
---|
| 211 | d->data = data; |
---|
| 212 | |
---|
[2c79eae] | 213 | /* TODO: Allow changing fd and mode in the middle? Probably don't care... */ |
---|
| 214 | d->pollfd.fd = fd; |
---|
| 215 | d->pollfd.events = 0; |
---|
| 216 | if (d->mode & OWL_IO_READ) |
---|
| 217 | d->pollfd.events |= G_IO_IN | G_IO_HUP | G_IO_ERR; |
---|
| 218 | if (d->mode & OWL_IO_WRITE) |
---|
| 219 | d->pollfd.events |= G_IO_OUT | G_IO_ERR; |
---|
| 220 | if (d->mode & OWL_IO_EXCEPT) |
---|
| 221 | d->pollfd.events |= G_IO_PRI | G_IO_ERR; |
---|
| 222 | g_source_add_poll(owl_io_dispatch_source, &d->pollfd); |
---|
| 223 | |
---|
| 224 | |
---|
[df0138f] | 225 | owl_select_remove_io_dispatch(owl_select_find_io_dispatch_by_fd(fd)); |
---|
| 226 | owl_list_append_element(dl, d); |
---|
| 227 | |
---|
| 228 | return d; |
---|
| 229 | } |
---|
| 230 | |
---|
[2c79eae] | 231 | static gboolean owl_io_dispatch_prepare(GSource *source, int *timeout) { |
---|
| 232 | *timeout = -1; |
---|
| 233 | return FALSE; |
---|
| 234 | } |
---|
[df0138f] | 235 | |
---|
[2c79eae] | 236 | static gboolean owl_io_dispatch_check(GSource *source) { |
---|
| 237 | int i, len; |
---|
| 238 | const owl_list *dl; |
---|
| 239 | |
---|
| 240 | dl = owl_global_get_io_dispatch_list(&g); |
---|
[df0138f] | 241 | len = owl_list_get_size(dl); |
---|
[2c79eae] | 242 | for(i = 0; i < len; i++) { |
---|
| 243 | const owl_io_dispatch *d = owl_list_get_element(dl, i); |
---|
| 244 | if (d->pollfd.revents & d->pollfd.events) |
---|
| 245 | return TRUE; |
---|
[df0138f] | 246 | } |
---|
[2c79eae] | 247 | return FALSE; |
---|
[df0138f] | 248 | } |
---|
| 249 | |
---|
[2c79eae] | 250 | static gboolean owl_io_dispatch_dispatch(GSource *source, GSourceFunc callback, gpointer user_data) { |
---|
[df0138f] | 251 | int i, len; |
---|
[2c79eae] | 252 | const owl_list *dl; |
---|
[df0138f] | 253 | |
---|
| 254 | dispatch_active = 1; |
---|
[2c79eae] | 255 | dl = owl_global_get_io_dispatch_list(&g); |
---|
[df0138f] | 256 | len = owl_list_get_size(dl); |
---|
| 257 | for (i = 0; i < len; i++) { |
---|
[2c79eae] | 258 | owl_io_dispatch *d = owl_list_get_element(dl, i); |
---|
| 259 | if ((d->pollfd.revents & d->pollfd.events) && d->callback != NULL) { |
---|
[df0138f] | 260 | d->callback(d, d->data); |
---|
| 261 | } |
---|
| 262 | } |
---|
| 263 | dispatch_active = 0; |
---|
| 264 | owl_select_io_dispatch_gc(); |
---|
[2c79eae] | 265 | |
---|
| 266 | return TRUE; |
---|
[df0138f] | 267 | } |
---|
| 268 | |
---|
[2c79eae] | 269 | static GSourceFuncs owl_io_dispatch_funcs = { |
---|
| 270 | owl_io_dispatch_prepare, |
---|
| 271 | owl_io_dispatch_check, |
---|
| 272 | owl_io_dispatch_dispatch, |
---|
| 273 | NULL |
---|
| 274 | }; |
---|
| 275 | |
---|
[df0138f] | 276 | int owl_select_add_perl_io_dispatch(int fd, int mode, SV *cb) |
---|
| 277 | { |
---|
| 278 | const owl_io_dispatch *d = owl_select_find_io_dispatch_by_fd(fd); |
---|
| 279 | if (d != NULL && d->callback != owl_perlconfig_io_dispatch) { |
---|
| 280 | /* Don't mess with non-perl dispatch functions from here. */ |
---|
| 281 | return 1; |
---|
| 282 | } |
---|
| 283 | owl_select_add_io_dispatch(fd, mode, owl_perlconfig_io_dispatch, owl_perlconfig_io_dispatch_destroy, cb); |
---|
| 284 | return 0; |
---|
| 285 | } |
---|
| 286 | |
---|
| 287 | int owl_select_remove_perl_io_dispatch(int fd) |
---|
| 288 | { |
---|
| 289 | const owl_io_dispatch *d = owl_select_find_io_dispatch_by_fd(fd); |
---|
| 290 | if (d != NULL && d->callback == owl_perlconfig_io_dispatch) { |
---|
| 291 | /* Only remove perl io dispatchers from here. */ |
---|
| 292 | owl_select_remove_io_dispatch(d); |
---|
| 293 | return 0; |
---|
| 294 | } |
---|
| 295 | return 1; |
---|
| 296 | } |
---|
| 297 | |
---|
[2c79eae] | 298 | void owl_select_init(void) |
---|
| 299 | { |
---|
| 300 | owl_timer_source = g_source_new(&owl_timer_funcs, sizeof(GSource)); |
---|
| 301 | g_source_attach(owl_timer_source, NULL); |
---|
| 302 | |
---|
| 303 | owl_io_dispatch_source = g_source_new(&owl_io_dispatch_funcs, sizeof(GSource)); |
---|
| 304 | g_source_attach(owl_io_dispatch_source, NULL); |
---|
| 305 | } |
---|
[3ecd78b] | 306 | |
---|
| 307 | void owl_select_run_loop(void) |
---|
| 308 | { |
---|
[2c79eae] | 309 | loop = g_main_loop_new(NULL, FALSE); |
---|
| 310 | g_main_loop_run(loop); |
---|
[3ecd78b] | 311 | } |
---|
| 312 | |
---|
| 313 | void owl_select_quit_loop(void) |
---|
| 314 | { |
---|
[2c79eae] | 315 | if (loop) { |
---|
| 316 | g_main_loop_quit(loop); |
---|
| 317 | loop = NULL; |
---|
| 318 | } |
---|
[3ecd78b] | 319 | } |
---|