ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent/Impl/Glib.pm
Revision: 1.14
Committed: Sat Apr 19 03:57:10 2008 UTC (16 years, 1 month ago) by root
Branch: MAIN
CVS Tags: rel-3_2, rel-3_12, rel-3_11
Changes since 1.13: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 =head1 NAME
2
3 AnyEvent::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
14 This module provides transparent support for AnyEvent. You don't have to
15 do anything to make Glib work with AnyEvent except by loading Glib before
16 creating the first AnyEvent watcher.
17
18 Glib is probably the most inefficient event loop that has ever seen the
19 light of the world: Glib not only scans all its watchers (really, ALL
20 of them, whether I/O-related, timer-related or not) during each loop
21 iteration, it also does so multiple times and rebuilds the poll list for
22 the kernel each time again, dynamically even.
23
24 If you create many watchers (as in: more than two), you might consider one
25 of the L<Glib::EV>, L<EV::Glib> or L<Glib::Event> modules that map Glib to
26 other, more efficient, event loops.
27
28 This module uses the default Glib main context for all it's watchers.
29
30 =cut
31
32 package AnyEvent::Impl::Glib;
33
34 no warnings;
35 use strict;
36
37 use Glib ();
38
39 our $maincontext = Glib::MainContext->default;
40
41 sub io {
42 my ($class, %arg) = @_;
43
44 my $self = bless \%arg, $class;
45 my $rcb = \$self->{cb};
46
47 my @cond;
48 # some glibs need hup, others error with it, YMMV
49 push @cond, "in", "hup" if $self->{poll} eq "r";
50 push @cond, "out", "hup" if $self->{poll} eq "w";
51
52 $self->{source} = add_watch Glib::IO fileno $self->{fh}, \@cond, sub {
53 $$rcb->();
54 ! ! $$rcb
55 };
56
57 $self
58 }
59
60 sub timer {
61 my ($class, %arg) = @_;
62
63 my $self = bless \%arg, $class;
64 my $cb = $self->{cb};
65
66 $self->{source} = add Glib::Timeout $self->{after} * 1000, sub {
67 $cb->();
68 0
69 };
70
71 $self
72 }
73
74 sub DESTROY {
75 my ($self) = @_;
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 %$self = ();
81 }
82
83 sub one_event {
84 $maincontext->iteration (1);
85 }
86
87 1;
88
89 =head1 SEE ALSO
90
91 L<AnyEvent>, L<Glib>.
92
93 =head1 AUTHOR
94
95 Marc Lehmann <schmorp@schmorp.de>
96 http://home.schmorp.de/
97
98 =cut
99