Changeset ca1fc33a
- Timestamp:
- Dec 11, 2008, 4:37:41 PM (16 years ago)
- Branches:
- master, debian, release-1.10, release-1.4, release-1.5, release-1.6, release-1.7, release-1.8, release-1.9
- Children:
- 58d1f8a
- Parents:
- 86bf047
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
select.c
r86bf047 rca1fc33a 4 4 5 5 int _owl_select_timer_cmp(owl_timer *t1, owl_timer *t2, void *data) { 6 6 return t1->time - t2->time; 7 7 } 8 8 9 9 int _owl_select_timer_eq(owl_timer *t1, owl_timer *t2, void *data) { 10 10 return t1 == t2; 11 11 } 12 12 13 13 owl_timer *owl_select_add_timer(int after, int interval, void (*cb)(struct _owl_timer *, void *), void *data) 14 14 { 15 16 17 18 19 20 21 22 23 24 25 15 owl_timer *t = owl_malloc(sizeof(owl_timer)); 16 GSequence *timers = owl_global_get_timerlist(&g); 17 18 t->time = time(NULL) + after; 19 t->interval = interval; 20 t->callback = cb; 21 t->data = data; 22 23 g_sequence_insert_sorted(timers, t, 24 (GCompareDataFunc)_owl_select_timer_cmp, NULL); 25 return t; 26 26 } 27 27 28 28 void owl_select_remove_timer(owl_timer *t) 29 29 { 30 31 32 33 34 35 36 30 GSequenceIter *it = g_sequence_search(owl_global_get_timerlist(&g), 31 t, (GCompareDataFunc)_owl_select_timer_eq, NULL); 32 if(!g_sequence_iter_is_end(it) && 33 g_sequence_get(it) == t) { 34 owl_free(t); 35 g_sequence_remove(it); 36 } 37 37 } 38 38 39 39 void owl_select_process_timers(struct timeval *timeout) 40 40 { 41 time_t now = time(NULL); 42 GSequenceIter *it = g_sequence_get_begin_iter(owl_global_get_timerlist(&g)); 43 44 while(!g_sequence_iter_is_end(it)) { 45 owl_timer *t = g_sequence_get(it); 46 void (*cb)(struct _owl_timer *, void *); 47 void *data; 48 49 if(t->time > now) 50 break; 51 52 cb = t->callback; 53 data = t->data; 54 55 /* Reschedule if appropriate */ 56 if(t->interval > 0) { 57 t->time = now + t->interval; 58 g_sequence_sort_changed(it, (GCompareDataFunc)_owl_select_timer_cmp, NULL); 59 } else { 60 owl_select_remove_timer(t); 61 t = NULL; 62 } 63 64 /* Do the callback */ 65 cb(t, data); 66 67 it = g_sequence_get_begin_iter(owl_global_get_timerlist(&g)); 68 } 69 70 if(g_sequence_iter_is_end(it)) { 71 timeout->tv_sec = 60; 41 time_t now = time(NULL); 42 GSequenceIter *it = g_sequence_get_begin_iter(owl_global_get_timerlist(&g)); 43 44 while(!g_sequence_iter_is_end(it)) { 45 owl_timer *t = g_sequence_get(it); 46 void (*cb)(struct _owl_timer *, void *); 47 void *data; 48 49 if(t->time > now) 50 break; 51 52 cb = t->callback; 53 data = t->data; 54 55 /* Reschedule if appropriate */ 56 if(t->interval > 0) { 57 t->time = now + t->interval; 58 g_sequence_sort_changed(it, (GCompareDataFunc)_owl_select_timer_cmp, NULL); 72 59 } else { 73 owl_timer *t = g_sequence_get(it); 74 timeout->tv_sec = t->time - now; 75 } 76 77 timeout->tv_usec = 0; 60 owl_select_remove_timer(t); 61 t = NULL; 62 } 63 64 /* Do the callback */ 65 cb(t, data); 66 67 it = g_sequence_get_begin_iter(owl_global_get_timerlist(&g)); 68 } 69 70 if(g_sequence_iter_is_end(it)) { 71 timeout->tv_sec = 60; 72 } else { 73 owl_timer *t = g_sequence_get(it); 74 timeout->tv_sec = t->time - now; 75 } 76 77 timeout->tv_usec = 0; 78 78 } 79 79
Note: See TracChangeset
for help on using the changeset viewer.