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.11 by root, Sun Jul 8 08:52:10 2007 UTC vs.
Revision 1.17 by root, Tue Jul 8 18:56:13 2008 UTC

1=head1 NAME
2
3AnyEvent::Impl::Glib - AnyEvent adaptor for Glib
4
5=head1 SYNOPSIS
6
7 use AnyEvent;
8 use Glib;
9
10 # this module gets loaded automatically as required
11
12=head1 DESCRIPTION
13
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
16creating the first AnyEvent watcher.
17
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
20of them, whether I/O-related, timer-related or not) during each loop
21iteration, it also does so multiple times and rebuilds the poll list for
22the kernel each time again, dynamically even.
23
24If 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
26other, more efficient, event loops.
27
28This module uses the default Glib main context for all it's watchers.
29
30=cut
31
1package AnyEvent::Impl::Glib; 32package AnyEvent::Impl::Glib;
2 33
3no warnings; 34no warnings;
4use strict; 35use strict;
5 36
6use Glib (); 37use Glib ();
7 38
8my $maincontext = Glib::MainContext->default; 39our $maincontext = Glib::MainContext->default;
9 40
10sub io { 41sub io {
11 my ($class, %arg) = @_; 42 my ($class, %arg) = @_;
12 43
13 my $self = bless \%arg, $class; 44 my $fh = $arg{fh};
14 my $rcb = \$self->{cb}; 45 my $cb = $arg{cb};
15 46
16 my @cond; 47 my @cond;
17 # some glibs need hup, others error with it, YMMV 48 # some glibs need hup, others error with it, YMMV
18 push @cond, "in", "hup" if $self->{poll} eq "r"; 49 push @cond, "in", "hup" if $arg{poll} eq "r";
19 push @cond, "out", "hup" if $self->{poll} eq "w"; 50 push @cond, "out", "hup" if $arg{poll} eq "w";
20 51
21 $self->{source} = add_watch Glib::IO fileno $self->{fh}, \@cond, sub { 52 my $source = add_watch Glib::IO fileno $arg{fh}, \@cond, sub {
22 $$rcb->(); 53 &$cb;
23 ! ! $$rcb 54 $fh; # mention it here to keep it from being destroyed
55 1
24 }; 56 };
25 57
26 $self 58 bless \\$source, $class
27} 59}
28 60
29sub timer { 61sub timer {
30 my ($class, %arg) = @_; 62 my ($class, %arg) = @_;
31 63
32 my $self = bless \%arg, $class;
33 my $cb = $self->{cb}; 64 my $cb = $arg{cb};
65 my $rp = $arg{repeat};
34 66
35 $self->{source} = add Glib::Timeout $self->{after} * 1000, sub { 67 my $source = add Glib::Timeout 1000 * delete $arg{after}, sub {
36 $cb->(); 68 &$cb;
37 0 69 $rp
38 }; 70 };
39 71
40 $self 72 bless \\$source, $class
41} 73}
42 74
43sub DESTROY { 75sub DESTROY {
44 my ($self) = @_; 76 remove Glib::Source $${$_[0]};
45
46 remove Glib::Source delete $self->{source} if $self->{source};
47 # need to undef $cb because we hold references to it
48 $self->{cb} = undef;
49 %$self = ();
50} 77}
51 78
52sub one_event { 79sub one_event {
53 $maincontext->iteration (1); 80 $maincontext->iteration (1);
54} 81}
55 82
561 831;
57 84
85=head1 SEE ALSO
86
87L<AnyEvent>, L<Glib>.
88
89=head1 AUTHOR
90
91 Marc Lehmann <schmorp@schmorp.de>
92 http://home.schmorp.de/
93
94=cut
95

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines