Changeset 7df7be2


Ignore:
Timestamp:
Jun 11, 2011, 6:31:32 PM (13 years ago)
Author:
David Benjamin <davidben@mit.edu>
Children:
f21bc36
Parents:
b9d22f7
git-author:
David Benjamin <davidben@mit.edu> (05/24/11 00:36:51)
git-committer:
David Benjamin <davidben@mit.edu> (06/11/11 18:31:32)
Message:
Replace BarnOwl::Timer with a perl wrapper over AnyEvent

This also allows us to kill owl_timer altogether.
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • global.c

    rf97c1a6 r7df7be2  
    106106  owl_message_init_fmtext_cache();
    107107  owl_list_create(&(g->io_dispatch_list));
    108   g->timerlist = NULL;
    109108  g->kill_buffer = NULL;
    110109
     
    844843{
    845844  return &(g->io_dispatch_list);
    846 }
    847 
    848 GList **owl_global_get_timerlist(owl_global *g)
    849 {
    850   return &(g->timerlist);
    851845}
    852846
  • owl.h

    r6dc3757 r7df7be2  
    525525  owl_list zusers;
    526526} owl_zbuddylist;
    527 
    528 typedef struct _owl_timer {
    529   time_t time;
    530   int interval;
    531   void (*callback)(struct _owl_timer *, void *);
    532   void (*destroy)(struct _owl_timer *);
    533   void *data;
    534   char *name;
    535 } owl_timer;
    536527
    537528typedef struct _owl_errqueue {
     
    626617  struct termios startup_tio;
    627618  owl_list io_dispatch_list;
    628   GList *timerlist;
    629619  guint aim_nop_timer;
    630620  int load_initial_subs;
  • perl/lib/BarnOwl/Timer.pm

    rc6adf17 r7df7be2  
    33
    44package BarnOwl::Timer;
     5
     6use AnyEvent;
    57
    68sub new {
     
    1315    my $self = {cb => $cb};
    1416
    15     my $name = $args->{name};
    16     $name = "(unnamed)" unless defined $name;
    17 
    1817    bless($self, $class);
    1918
    20     $self->{timer} = BarnOwl::Internal::add_timer($args->{after} || 0,
    21                                                   $args->{interval} || 0,
    22                                                   $self,
    23                                                   $name);
     19    $self->{timer} = AnyEvent->timer(%$args);
    2420    return $self;
    2521}
     
    2723sub stop {
    2824    my $self = shift;
    29     if(defined($self->{timer})) {
    30         BarnOwl::Internal::remove_timer($self->{timer});
    31         undef $self->{timer};
    32     }
     25    undef $self->{timer};
    3326}
    3427
  • perlconfig.c

    r3b8a563 r7df7be2  
    611611  LEAVE;
    612612}
    613 
    614 void owl_perlconfig_perl_timer(owl_timer *t, void *data)
    615 {
    616   dSP;
    617   SV *obj = data;
    618 
    619   if(!SvROK(obj)) {
    620     return;
    621   }
    622 
    623   ENTER;
    624   SAVETMPS;
    625 
    626   PUSHMARK(SP);
    627   XPUSHs(obj);
    628   PUTBACK;
    629 
    630   call_method("do_callback", G_DISCARD|G_EVAL);
    631 
    632   SPAGAIN;
    633 
    634   if (SvTRUE(ERRSV)) {
    635     owl_function_error("Error in callback: '%s'", SvPV_nolen(ERRSV));
    636     sv_setsv (ERRSV, &PL_sv_undef);
    637   }
    638 
    639   PUTBACK;
    640   FREETMPS;
    641   LEAVE;
    642 }
    643 
    644 void owl_perlconfig_perl_timer_destroy(owl_timer *t)
    645 {
    646   if(SvOK((SV*)t->data)) {
    647     SvREFCNT_dec((SV*)t->data);
    648   }
    649 }
  • perlglue.xs

    r3b8a563 r7df7be2  
    515515        owl_select_add_perl_io_dispatch(fd, mode, newSVsv(cb));
    516516
    517 IV
    518 add_timer(after, interval, cb, name = NULL)
    519         int after
    520         int interval
    521         SV *cb
    522         const char *name
    523         PREINIT:
    524                 SV *ref;
    525                 owl_timer *t;
    526         CODE:
    527                 ref = sv_rvweaken(newSVsv(cb));
    528                 t = owl_select_add_timer(name,
    529                                          after,
    530                                          interval,
    531                                          owl_perlconfig_perl_timer,
    532                                          owl_perlconfig_perl_timer_destroy,
    533                                          ref);
    534                 owl_function_debugmsg("Created timer %s: %p", t->name ? t->name : "(unnamed)", t);
    535         RETVAL = (IV)t;
    536         OUTPUT:
    537                 RETVAL
    538 
    539 void
    540 remove_timer(timer)
    541         IV timer
    542         PREINIT:
    543                 owl_timer *t;
    544         CODE:
    545                 t = (owl_timer*)timer;
    546                 owl_function_debugmsg("Freeing timer %s: %p", t->name ? t->name : "(unnamed)", t);
    547                 owl_select_remove_timer(t);
    548 
    549517MODULE = BarnOwl                PACKAGE = BarnOwl::Editwin
    550518
  • select.c

    r44976fe r7df7be2  
    55static int dispatch_active = 0;
    66
    7 static GSource *owl_timer_source;
    87static 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 cool
    49    * 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 cool
    73    * 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 cool
    90    * 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   NULL
    125 };
    1268
    1279/* Returns the valid owl_io_dispatch for a given file descriptor. */
     
    335217void owl_select_init(void)
    336218{
    337   owl_timer_source = g_source_new(&owl_timer_funcs, sizeof(GSource));
    338   g_source_attach(owl_timer_source, NULL);
    339 
    340219  owl_io_dispatch_source = g_source_new(&owl_io_dispatch_funcs, sizeof(GSource));
    341220  g_source_attach(owl_io_dispatch_source, NULL);
Note: See TracChangeset for help on using the changeset viewer.