Changeset 074bdaa


Ignore:
Timestamp:
Jun 22, 2011, 12:37:21 AM (13 years ago)
Author:
David Benjamin <davidben@mit.edu>
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)
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 r074bdaa  
    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

    r6b0b4f4 r074bdaa  
    516516  owl_list zusers;
    517517} owl_zbuddylist;
    518 
    519 typedef struct _owl_timer {
    520   time_t time;
    521   int interval;
    522   void (*callback)(struct _owl_timer *, void *);
    523   void (*destroy)(struct _owl_timer *);
    524   void *data;
    525   char *name;
    526 } owl_timer;
    527518
    528519typedef struct _owl_errqueue {
     
    617608  struct termios startup_tio;
    618609  owl_list io_dispatch_list;
    619   GList *timerlist;
    620610  guint aim_nop_timer;
    621611  int load_initial_subs;
  • perl/lib/BarnOwl/Timer.pm

    rc6adf17 r074bdaa  
    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

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

    r3b8a563 r074bdaa  
    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

    rf0781ba r074bdaa  
    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.