ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent/Impl/Glib.pm
Revision: 1.23
Committed: Mon Jun 22 11:17:03 2009 UTC (14 years, 11 months ago) by root
Branch: MAIN
CVS Tags: rel-4_412, rel-4_8, rel-4_45, rel-4_42
Changes since 1.22: +1 -1 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.21 On the positive side, and most importantly, Glib generally works
25     correctly, no quarrels there.
26 root 1.20
27 root 1.12 If you create many watchers (as in: more than two), you might consider one
28     of the L<Glib::EV>, L<EV::Glib> or L<Glib::Event> modules that map Glib to
29     other, more efficient, event loops.
30    
31 root 1.21 This module uses the default Glib main context for all its watchers.
32 root 1.12
33     =cut
34    
35 root 1.1 package AnyEvent::Impl::Glib;
36    
37 root 1.8 no warnings;
38 root 1.10 use strict;
39 root 1.8
40 root 1.1 use Glib ();
41    
42 root 1.22 our $mainloop = Glib::MainContext->default;
43 root 1.1
44 root 1.2 sub io {
45     my ($class, %arg) = @_;
46    
47 root 1.17 my $cb = $arg{cb};
48 root 1.2
49 root 1.11 my @cond;
50 root 1.4 # some glibs need hup, others error with it, YMMV
51 root 1.17 push @cond, "in", "hup" if $arg{poll} eq "r";
52     push @cond, "out", "hup" if $arg{poll} eq "w";
53 root 1.2
54 root 1.21 my $source = add_watch Glib::IO fileno $arg{fh}, \@cond, sub { &$cb; 1 };
55 root 1.17 bless \\$source, $class
56 root 1.1 }
57    
58 root 1.2 sub timer {
59     my ($class, %arg) = @_;
60 root 1.1
61 root 1.18 my $cb = $arg{cb};
62     my $ival = $arg{interval} * 1000;
63 root 1.1
64 root 1.18 my $source; $source = add Glib::Timeout $arg{after} * 1000,
65     $ival ? sub {
66     remove Glib::Source $source;
67     $source = add Glib::Timeout $ival, sub { &$cb; 1 };
68     &$cb;
69     0
70     }
71     : sub { &$cb; 0 };
72 root 1.1
73 root 1.17 bless \\$source, $class
74 root 1.1 }
75    
76 root 1.21 sub idle {
77     my ($class, %arg) = @_;
78    
79     my $cb = $arg{cb};
80     my $source = add Glib::Idle sub { &$cb; 1 };
81     bless \\$source, $class
82     }
83    
84 root 1.6 sub DESTROY {
85 root 1.17 remove Glib::Source $${$_[0]};
86 root 1.2 }
87    
88 root 1.7 sub one_event {
89 root 1.22 $mainloop->iteration (1);
90     }
91    
92     sub loop {
93 root 1.23 $mainloop->iteration (1) while 1; # hackish, but w ehave no mainloop
94 root 1.7 }
95    
96 root 1.12 1;
97    
98     =head1 SEE ALSO
99    
100 root 1.14 L<AnyEvent>, L<Glib>.
101 root 1.12
102     =head1 AUTHOR
103    
104     Marc Lehmann <schmorp@schmorp.de>
105     http://home.schmorp.de/
106    
107     =cut
108 root 1.1