source: perl/lib/BarnOwl/Timer.pm @ ede073c

release-1.10release-1.7release-1.8release-1.9
Last change on this file since ede073c was c6adf17, checked in by David Benjamin <davidben@mit.edu>, 14 years ago
Track names along with timers, add :show timers This will help people with BarnOwls eating CPU to diagnose timer leaks.
  • Property mode set to 100644
File size: 922 bytes
Line 
1use strict;
2use warnings;
3
4package BarnOwl::Timer;
5
6sub new {
7    my $class = shift;
8    my $args = shift;
9
10    my $cb = $args->{cb};
11    die("Invalid callback pased to BarnOwl::Timer\n") unless ref($cb) eq 'CODE';
12
13    my $self = {cb => $cb};
14
15    my $name = $args->{name};
16    $name = "(unnamed)" unless defined $name;
17
18    bless($self, $class);
19
20    $self->{timer} = BarnOwl::Internal::add_timer($args->{after} || 0,
21                                                  $args->{interval} || 0,
22                                                  $self,
23                                                  $name);
24    return $self;
25}
26
27sub stop {
28    my $self = shift;
29    if(defined($self->{timer})) {
30        BarnOwl::Internal::remove_timer($self->{timer});
31        undef $self->{timer};
32    }
33}
34
35sub do_callback {
36    my $self = shift;
37    $self->{cb}->($self);
38}
39
40sub DESTROY {
41    my $self = shift;
42    $self->stop;
43}
44
45
461;
Note: See TracBrowser for help on using the repository browser.