ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent/Impl/Glib.pm
Revision: 1.17
Committed: Tue Jul 8 18:56:13 2008 UTC (15 years, 10 months ago) by root
Branch: MAIN
Changes since 1.16: +16 -19 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 $fh = $arg{fh};
45 my $cb = $arg{cb};
46
47 my @cond;
48 # some glibs need hup, others error with it, YMMV
49 push @cond, "in", "hup" if $arg{poll} eq "r";
50 push @cond, "out", "hup" if $arg{poll} eq "w";
51
52 my $source = add_watch Glib::IO fileno $arg{fh}, \@cond, sub {
53 &$cb;
54 $fh; # mention it here to keep it from being destroyed
55 1
56 };
57
58 bless \\$source, $class
59 }
60
61 sub timer {
62 my ($class, %arg) = @_;
63
64 my $cb = $arg{cb};
65 my $rp = $arg{repeat};
66
67 my $source = add Glib::Timeout 1000 * delete $arg{after}, sub {
68 &$cb;
69 $rp
70 };
71
72 bless \\$source, $class
73 }
74
75 sub DESTROY {
76 remove Glib::Source $${$_[0]};
77 }
78
79 sub one_event {
80 $maincontext->iteration (1);
81 }
82
83 1;
84
85 =head1 SEE ALSO
86
87 L<AnyEvent>, L<Glib>.
88
89 =head1 AUTHOR
90
91 Marc Lehmann <schmorp@schmorp.de>
92 http://home.schmorp.de/
93
94 =cut
95