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