ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent/Impl/Glib.pm
(Generate patch)

Comparing AnyEvent/lib/AnyEvent/Impl/Glib.pm (file contents):
Revision 1.16 by root, Sat May 31 13:38:01 2008 UTC vs.
Revision 1.24 by root, Wed Jul 8 02:01:12 2009 UTC

14This module provides transparent support for AnyEvent. You don't have to 14This module provides transparent support for AnyEvent. You don't have to
15do anything to make Glib work with AnyEvent except by loading Glib before 15do anything to make Glib work with AnyEvent except by loading Glib before
16creating the first AnyEvent watcher. 16creating the first AnyEvent watcher.
17 17
18Glib is probably the most inefficient event loop that has ever seen the 18Glib is probably the most inefficient event loop that has ever seen the
19light of the world: Glib not only scans all its watchers (really, ALL 19light of the world: Glib not only scans all its watchers (really, ALL of
20of them, whether I/O-related, timer-related or not) during each loop 20them, whether I/O-related, timer-related or what not) during each loop
21iteration, it also does so multiple times and rebuilds the poll list for 21iteration, it also does so multiple times and rebuilds the poll list for
22the kernel each time again, dynamically even. 22the kernel each time again, dynamically even.
23
24On the positive side, and most importantly, Glib generally works
25correctly, no quarrels there.
23 26
24If you create many watchers (as in: more than two), you might consider one 27If you create many watchers (as in: more than two), you might consider one
25of the L<Glib::EV>, L<EV::Glib> or L<Glib::Event> modules that map Glib to 28of the L<Glib::EV>, L<EV::Glib> or L<Glib::Event> modules that map Glib to
26other, more efficient, event loops. 29other, more efficient, event loops.
27 30
28This module uses the default Glib main context for all it's watchers. 31This module uses the default Glib main context for all its watchers.
29 32
30=cut 33=cut
31 34
32package AnyEvent::Impl::Glib; 35package AnyEvent::Impl::Glib;
33 36
34no warnings; 37no warnings;
35use strict; 38use strict;
36 39
37use Glib (); 40use Glib ();
38 41
39our $maincontext = Glib::MainContext->default; 42our $mainloop = Glib::MainContext->default;
43
44my %io_cond = (
45 r => ["in" , "hup"],
46 w => ["out", "hup"],
47);
40 48
41sub io { 49sub io {
42 my ($class, %arg) = @_; 50 my ($class, %arg) = @_;
43 51
44 my $self = bless \%arg, $class;
45 my $rcb = \$self->{cb}; 52 my $cb = $arg{cb};
53 my $fd = fileno $arg{fh};
54 defined $fd or $fd = $arg{fh};
46 55
47 my @cond; 56 my $source = add_watch Glib::IO
48 # some glibs need hup, others error with it, YMMV 57 $fd,
49 push @cond, "in", "hup" if $self->{poll} eq "r"; 58 $io_cond{$arg{poll}},
50 push @cond, "out", "hup" if $self->{poll} eq "w"; 59 sub { &$cb; 1 };
51 60
52 $self->{source} = add_watch Glib::IO fileno $self->{fh}, \@cond, sub { 61 bless \\$source, $class
53 $$rcb->();
54 ! ! $$rcb
55 };
56
57 $self
58} 62}
59 63
60sub timer { 64sub timer {
61 my ($class, %arg) = @_; 65 my ($class, %arg) = @_;
62 66
63 my $self = bless \%arg, $class;
64 my $cb = $self->{cb}; 67 my $cb = $arg{cb};
68 my $ival = $arg{interval} * 1000;
65 69
66 $self->{source} = add Glib::Timeout $self->{after} * 1000, sub { 70 my $source; $source = add Glib::Timeout $arg{after} * 1000,
67 $cb->(); 71 $ival ? sub {
68 0 72 remove Glib::Source $source;
69 }; 73 $source = add Glib::Timeout $ival, sub { &$cb; 1 };
74 &$cb;
75 0
76 }
77 : sub { &$cb; 0 };
70 78
71 $self 79 bless \\$source, $class
80}
81
82sub idle {
83 my ($class, %arg) = @_;
84
85 my $cb = $arg{cb};
86 my $source = add Glib::Idle sub { &$cb; 1 };
87 bless \\$source, $class
72} 88}
73 89
74sub DESTROY { 90sub DESTROY {
75 my ($self) = @_; 91 remove Glib::Source $${$_[0]};
76
77 remove Glib::Source delete $self->{source} if $self->{source};
78 # need to undef $cb because we hold references to it
79 $self->{cb} = undef;
80} 92}
81 93
82sub one_event { 94sub one_event {
83 $maincontext->iteration (1); 95 $mainloop->iteration (1);
96}
97
98sub loop {
99 # hackish, but we do not have a mainloop, just a maincontext
100 $mainloop->iteration (1) while 1;
84} 101}
85 102
861; 1031;
87 104
88=head1 SEE ALSO 105=head1 SEE ALSO

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines