ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent/Impl/Glib.pm
Revision: 1.20
Committed: Thu Jul 24 06:31:37 2008 UTC (15 years, 10 months ago) by root
Branch: MAIN
CVS Tags: rel-4_23, rel-4_352, rel-4_351, rel-4_331, rel-4_231, rel-4_233, rel-4_232, rel-4_234, rel-4_3, rel-4_31, rel-4_32, rel-4_33, rel-4_34, rel-4_35
Changes since 1.19: +4 -2 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.12 =head1 NAME
2    
3     AnyEvent::Impl::Glib - AnyEvent adaptor for Glib
4    
5     =head1 SYNOPSIS
6    
7 root 1.16 use AnyEvent;
8     use Glib;
9    
10     # this module gets loaded automatically as required
11 root 1.12
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 root 1.20 light of the world: Glib not only scans all its watchers (really, ALL of
20     them, whether I/O-related, timer-related or what not) during each loop
21 root 1.12 iteration, it also does so multiple times and rebuilds the poll list for
22     the kernel each time again, dynamically even.
23    
24 root 1.20 On the positive side, Glib generally works correctly, no quarrels there.
25    
26 root 1.12 If you create many watchers (as in: more than two), you might consider one
27     of the L<Glib::EV>, L<EV::Glib> or L<Glib::Event> modules that map Glib to
28     other, more efficient, event loops.
29    
30     This module uses the default Glib main context for all it's watchers.
31    
32     =cut
33    
34 root 1.1 package AnyEvent::Impl::Glib;
35    
36 root 1.8 no warnings;
37 root 1.10 use strict;
38 root 1.8
39 root 1.1 use Glib ();
40    
41 root 1.12 our $maincontext = Glib::MainContext->default;
42 root 1.1
43 root 1.2 sub io {
44     my ($class, %arg) = @_;
45    
46 root 1.17 my $cb = $arg{cb};
47 root 1.2
48 root 1.11 my @cond;
49 root 1.4 # some glibs need hup, others error with it, YMMV
50 root 1.17 push @cond, "in", "hup" if $arg{poll} eq "r";
51     push @cond, "out", "hup" if $arg{poll} eq "w";
52 root 1.2
53 root 1.17 my $source = add_watch Glib::IO fileno $arg{fh}, \@cond, sub {
54     &$cb;
55     1
56 root 1.2 };
57 root 1.1
58 root 1.17 bless \\$source, $class
59 root 1.1 }
60    
61 root 1.2 sub timer {
62     my ($class, %arg) = @_;
63 root 1.1
64 root 1.18 my $cb = $arg{cb};
65     my $ival = $arg{interval} * 1000;
66 root 1.1
67 root 1.18 my $source; $source = add Glib::Timeout $arg{after} * 1000,
68     $ival ? sub {
69     remove Glib::Source $source;
70     $source = add Glib::Timeout $ival, sub { &$cb; 1 };
71     &$cb;
72     0
73     }
74     : sub { &$cb; 0 };
75 root 1.1
76 root 1.17 bless \\$source, $class
77 root 1.1 }
78    
79 root 1.6 sub DESTROY {
80 root 1.17 remove Glib::Source $${$_[0]};
81 root 1.2 }
82    
83 root 1.7 sub one_event {
84     $maincontext->iteration (1);
85     }
86    
87 root 1.12 1;
88    
89     =head1 SEE ALSO
90    
91 root 1.14 L<AnyEvent>, L<Glib>.
92 root 1.12
93     =head1 AUTHOR
94    
95     Marc Lehmann <schmorp@schmorp.de>
96     http://home.schmorp.de/
97    
98     =cut
99 root 1.1