source: perl/lib/BarnOwl/MainLoopCompatHook.pm @ bad4496

release-1.10release-1.7release-1.8release-1.9
Last change on this file since bad4496 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: 1.2 KB
Line 
1use strict;
2use warnings;
3
4package BarnOwl::MainLoopCompatHook;
5
6use BarnOwl;
7use BarnOwl::Hook;
8use BarnOwl::Timer;
9use Scalar::Util qw(weaken);
10
11sub new {
12    my $class = shift;
13    my $hook = BarnOwl::Hook->new;
14    return bless {hook => $hook}, $class;
15}
16
17sub check_owlconf {
18  my $self = shift;
19  $self->_ensure_timer() if (*BarnOwl::mainloop_hook{CODE});
20}
21
22sub run {
23    my $self = shift;
24    return $self->{hook}->run(@_);
25}
26
27sub add {
28    my $self = shift;
29    $self->_ensure_timer();
30    $self->{hook}->add(@_);
31}
32
33sub clear {
34    my $self = shift;
35    $self->_kill_timer() unless *BarnOwl::mainloop_hook{CODE};
36    $self->{hook}->clear();
37}
38
39sub _ensure_timer {
40  my $self = shift;
41  BarnOwl::debug("Enabling backwards-compatibility \"main loop\" hook");
42  my $weak = $self;
43  weaken($weak);
44  unless ($self->{timer}) {
45    $self->{timer} = BarnOwl::Timer->new( {
46        name => "BarnOwl::MainLoopCompatHook",
47        after => 0,
48        interval => 1,
49        cb => sub {
50          $weak->run if $weak;
51          BarnOwl::mainloop_hook() if *BarnOwl::mainloop_hook{CODE};
52        },
53      } );
54  }
55}
56
57sub _kill_timer {
58  my $self = shift;
59  if ($self->{timer}) {
60    $self->{timer}->stop;
61    undef $self->{timer};
62  }
63}
64
651;
Note: See TracBrowser for help on using the repository browser.