Changeset 7df7be2
- Timestamp:
- Jun 11, 2011, 6:31:32 PM (13 years ago)
- 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)
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
global.c
rf97c1a6 r7df7be2 106 106 owl_message_init_fmtext_cache(); 107 107 owl_list_create(&(g->io_dispatch_list)); 108 g->timerlist = NULL;109 108 g->kill_buffer = NULL; 110 109 … … 844 843 { 845 844 return &(g->io_dispatch_list); 846 }847 848 GList **owl_global_get_timerlist(owl_global *g)849 {850 return &(g->timerlist);851 845 } 852 846 -
owl.h
r6dc3757 r7df7be2 525 525 owl_list zusers; 526 526 } 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;536 527 537 528 typedef struct _owl_errqueue { … … 626 617 struct termios startup_tio; 627 618 owl_list io_dispatch_list; 628 GList *timerlist;629 619 guint aim_nop_timer; 630 620 int load_initial_subs; -
perl/lib/BarnOwl/Timer.pm
rc6adf17 r7df7be2 3 3 4 4 package BarnOwl::Timer; 5 6 use AnyEvent; 5 7 6 8 sub new { … … 13 15 my $self = {cb => $cb}; 14 16 15 my $name = $args->{name};16 $name = "(unnamed)" unless defined $name;17 18 17 bless($self, $class); 19 18 20 $self->{timer} = BarnOwl::Internal::add_timer($args->{after} || 0, 21 $args->{interval} || 0, 22 $self, 23 $name); 19 $self->{timer} = AnyEvent->timer(%$args); 24 20 return $self; 25 21 } … … 27 23 sub stop { 28 24 my $self = shift; 29 if(defined($self->{timer})) { 30 BarnOwl::Internal::remove_timer($self->{timer}); 31 undef $self->{timer}; 32 } 25 undef $self->{timer}; 33 26 } 34 27 -
perlconfig.c
r3b8a563 r7df7be2 611 611 LEAVE; 612 612 } 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 515 515 owl_select_add_perl_io_dispatch(fd, mode, newSVsv(cb)); 516 516 517 IV518 add_timer(after, interval, cb, name = NULL)519 int after520 int interval521 SV *cb522 const char *name523 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 RETVAL538 539 void540 remove_timer(timer)541 IV timer542 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 549 517 MODULE = BarnOwl PACKAGE = BarnOwl::Editwin 550 518 -
select.c
r44976fe r7df7be2 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.