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.23 by root, Mon Jun 22 11:17:03 2009 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 of
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
22the kernel each time again, dynamically even.
23
24On the positive side, and most importantly, Glib generally works
25correctly, no quarrels there.
26
27If you create many watchers (as in: more than two), you might consider one
28of the L<Glib::EV>, L<EV::Glib> or L<Glib::Event> modules that map Glib to
29other, more efficient, event loops.
30
31This module uses the default Glib main context for all its watchers.
32
33=cut
34
1package AnyEvent::Impl::Glib; 35package AnyEvent::Impl::Glib;
2 36
3no warnings; 37no warnings;
4use strict; 38use strict;
5 39
6use Glib (); 40use Glib ();
7 41
8my $maincontext = Glib::MainContext->default; 42our $mainloop = Glib::MainContext->default;
9 43
10sub io { 44sub io {
11 my ($class, %arg) = @_; 45 my ($class, %arg) = @_;
12 46
13 my $self = bless \%arg, $class;
14 my $rcb = \$self->{cb}; 47 my $cb = $arg{cb};
15 48
16 my @cond; 49 my @cond;
17 # some glibs need hup, others error with it, YMMV 50 # some glibs need hup, others error with it, YMMV
18 push @cond, "in", "hup" if $self->{poll} eq "r"; 51 push @cond, "in", "hup" if $arg{poll} eq "r";
19 push @cond, "out", "hup" if $self->{poll} eq "w"; 52 push @cond, "out", "hup" if $arg{poll} eq "w";
20 53
21 $self->{source} = add_watch Glib::IO fileno $self->{fh}, \@cond, sub { 54 my $source = add_watch Glib::IO fileno $arg{fh}, \@cond, sub { &$cb; 1 };
22 $$rcb->(); 55 bless \\$source, $class
23 ! ! $$rcb
24 };
25
26 $self
27} 56}
28 57
29sub timer { 58sub timer {
30 my ($class, %arg) = @_; 59 my ($class, %arg) = @_;
31 60
32 my $self = bless \%arg, $class;
33 my $cb = $self->{cb}; 61 my $cb = $arg{cb};
62 my $ival = $arg{interval} * 1000;
34 63
35 $self->{source} = add Glib::Timeout $self->{after} * 1000, sub { 64 my $source; $source = add Glib::Timeout $arg{after} * 1000,
36 $cb->(); 65 $ival ? sub {
37 0 66 remove Glib::Source $source;
38 }; 67 $source = add Glib::Timeout $ival, sub { &$cb; 1 };
68 &$cb;
69 0
70 }
71 : sub { &$cb; 0 };
39 72
40 $self 73 bless \\$source, $class
74}
75
76sub idle {
77 my ($class, %arg) = @_;
78
79 my $cb = $arg{cb};
80 my $source = add Glib::Idle sub { &$cb; 1 };
81 bless \\$source, $class
41} 82}
42 83
43sub DESTROY { 84sub DESTROY {
44 my ($self) = @_; 85 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} 86}
51 87
52sub one_event { 88sub one_event {
53 $maincontext->iteration (1); 89 $mainloop->iteration (1);
54} 90}
55 91
561 92sub loop {
93 $mainloop->iteration (1) while 1; # hackish, but w ehave no mainloop
94}
57 95
961;
97
98=head1 SEE ALSO
99
100L<AnyEvent>, L<Glib>.
101
102=head1 AUTHOR
103
104 Marc Lehmann <schmorp@schmorp.de>
105 http://home.schmorp.de/
106
107=cut
108

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines