Changeset b7bb454 for select.c


Ignore:
Timestamp:
Dec 6, 2008, 5:39:49 PM (13 years ago)
Author:
Nelson Elhage <nelhage@mit.edu>
Branches:
master, debian, release-1.4, release-1.5, release-1.6, release-1.7, release-1.8, release-1.9
Children:
7ce4003
Parents:
3ea31b6
git-author:
Nelson Elhage <nelhage@mit.edu> (12/06/08 17:36:19)
git-committer:
Nelson Elhage <nelhage@mit.edu> (12/06/08 17:39:49)
Message:
Make owl_timer have a callback and integrate into the select() loop.

Soon we should add support for registering timers from perl, and then
we can eventually remove the perl mainloop hook.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • select.c

    r2f69081 rb7bb454  
    22
    33static const char fileIdent[] = "$Id: select.c 894 2008-01-17 07:13:44Z asedeno $";
     4
     5int _owl_select_timer_cmp(owl_timer *t1, owl_timer *t2, void *data) {
     6    return t1->time - t2->time;
     7}
     8
     9int _owl_select_timer_eq(owl_timer *t1, owl_timer *t2, void *data) {
     10    return t1 == t2;
     11}
     12
     13owl_timer *owl_select_add_timer(int after, int interval, void (*cb)(struct _owl_timer *, void *), void *data)
     14{
     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}
     27
     28void owl_select_remove_timer(owl_timer *t)
     29{
     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}
     38
     39void owl_select_process_timers(struct timeval *timeout)
     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        int remove = 0;
     47
     48        if(t->time > now)
     49            break;
     50
     51        /* Reschedule if appropriate */
     52        if(t->interval > 0) {
     53            t->time = now + t->interval;
     54            g_sequence_sort_changed(it, (GCompareDataFunc)_owl_select_timer_cmp, NULL);
     55        } else {
     56            g_sequence_remove(it);
     57            remove = 1;
     58        }
     59
     60        /* Do the callback */
     61        t->callback(t, t->data);
     62
     63        if (remove) {
     64            owl_free(t);
     65        }
     66        it = g_sequence_get_begin_iter(owl_global_get_timerlist(&g));
     67    }
     68
     69    if(g_sequence_iter_is_end(it)) {
     70        timeout->tv_sec = 60;
     71    } else {
     72        owl_timer *t = g_sequence_get(it);
     73        timeout->tv_sec = t->time - now;
     74    }
     75
     76    timeout->tv_usec = 0;
     77}
    478
    579/* Returns the index of the dispatch for the file descriptor. */
     
    182256  struct timeval timeout;
    183257
    184   timeout.tv_sec = 1;
    185   timeout.tv_usec = 0;
     258  owl_select_process_timers(&timeout);
    186259
    187260  max_fd = owl_select_dispatch_prepare_fd_sets(&r, &e);
Note: See TracChangeset for help on using the changeset viewer.