- Timestamp:
- Jun 22, 2011, 12:37:21 AM (13 years ago)
- Branches:
- master, release-1.10, release-1.8, release-1.9
- Children:
- bcde7926
- Parents:
- 58f4fb2
- git-author:
- David Benjamin <davidben@mit.edu> (05/24/11 00:36:51)
- git-committer:
- David Benjamin <davidben@mit.edu> (06/22/11 00:37:21)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
select.c
rf0781ba r074bdaa 5 5 static int dispatch_active = 0; 6 6 7 static GSource *owl_timer_source;8 7 static GSource *owl_io_dispatch_source; 9 10 static int _owl_select_timer_cmp(const owl_timer *t1, const owl_timer *t2) {11 return t1->time - t2->time;12 }13 14 owl_timer *owl_select_add_timer(const char* name, int after, int interval, void (*cb)(owl_timer *, void *), void (*destroy)(owl_timer*), void *data)15 {16 owl_timer *t = g_new(owl_timer, 1);17 GList **timers = owl_global_get_timerlist(&g);18 19 t->time = time(NULL) + after;20 t->interval = interval;21 t->callback = cb;22 t->destroy = destroy;23 t->data = data;24 t->name = name ? g_strdup(name) : NULL;25 26 *timers = g_list_insert_sorted(*timers, t,27 (GCompareFunc)_owl_select_timer_cmp);28 return t;29 }30 31 void owl_select_remove_timer(owl_timer *t)32 {33 GList **timers = owl_global_get_timerlist(&g);34 if (t && g_list_find(*timers, t)) {35 *timers = g_list_remove(*timers, t);36 if(t->destroy) {37 t->destroy(t);38 }39 g_free(t->name);40 g_free(t);41 }42 }43 44 static gboolean owl_timer_prepare(GSource *source, int *timeout) {45 GList **timers = owl_global_get_timerlist(&g);46 GTimeVal now;47 48 /* TODO: In the far /far/ future, g_source_get_time is what the cool49 * kids use to get system monotonic time. */50 g_source_get_current_time(source, &now);51 52 /* FIXME: bother with millisecond accuracy now that we can? */53 if (*timers) {54 owl_timer *t = (*timers)->data;55 *timeout = t->time - now.tv_sec;56 if (*timeout <= 0) {57 *timeout = 0;58 return TRUE;59 }60 if (*timeout > 60 * 1000)61 *timeout = 60 * 1000;62 } else {63 *timeout = 60 * 1000;64 }65 return FALSE;66 }67 68 static gboolean owl_timer_check(GSource *source) {69 GList **timers = owl_global_get_timerlist(&g);70 GTimeVal now;71 72 /* TODO: In the far /far/ future, g_source_get_time is what the cool73 * kids use to get system monotonic time. */74 g_source_get_current_time(source, &now);75 76 /* FIXME: bother with millisecond accuracy now that we can? */77 if (*timers) {78 owl_timer *t = (*timers)->data;79 return t->time >= now.tv_sec;80 }81 return FALSE;82 }83 84 85 static gboolean owl_timer_dispatch(GSource *source, GSourceFunc callback, gpointer user_data) {86 GList **timers = owl_global_get_timerlist(&g);87 GTimeVal now;88 89 /* TODO: In the far /far/ future, g_source_get_time is what the cool90 * kids use to get system monotonic time. */91 g_source_get_current_time(source, &now);92 93 /* FIXME: bother with millisecond accuracy now that we can? */94 while(*timers) {95 owl_timer *t = (*timers)->data;96 int remove = 0;97 98 if(t->time > now.tv_sec)99 break;100 101 /* Reschedule if appropriate */102 if(t->interval > 0) {103 t->time = now.tv_sec + t->interval;104 *timers = g_list_remove(*timers, t);105 *timers = g_list_insert_sorted(*timers, t,106 (GCompareFunc)_owl_select_timer_cmp);107 } else {108 remove = 1;109 }110 111 /* Do the callback */112 t->callback(t, t->data);113 if(remove) {114 owl_select_remove_timer(t);115 }116 }117 return TRUE;118 }119 120 static GSourceFuncs owl_timer_funcs = {121 owl_timer_prepare,122 owl_timer_check,123 owl_timer_dispatch,124 NULL125 };126 8 127 9 /* Returns the valid owl_io_dispatch for a given file descriptor. */ … … 335 217 void owl_select_init(void) 336 218 { 337 owl_timer_source = g_source_new(&owl_timer_funcs, sizeof(GSource));338 g_source_attach(owl_timer_source, NULL);339 340 219 owl_io_dispatch_source = g_source_new(&owl_io_dispatch_funcs, sizeof(GSource)); 341 220 g_source_attach(owl_io_dispatch_source, NULL);
Note: See TracChangeset
for help on using the changeset viewer.