ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent/Impl/Glib.pm
Revision: 1.16
Committed: Sat May 31 13:38:01 2008 UTC (15 years, 11 months ago) by root
Branch: MAIN
CVS Tags: rel-4_151, rel-4_152, rel-4_14, rel-4_15, rel-4_13, rel-4_12, rel-4_161, rel-4_160
Changes since 1.15: +4 -4 lines
Log Message:
indent 3

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     light of the world: Glib not only scans all its watchers (really, ALL
20 root 1.13 of them, whether I/O-related, timer-related or 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     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 root 1.1 package AnyEvent::Impl::Glib;
33    
34 root 1.8 no warnings;
35 root 1.10 use strict;
36 root 1.8
37 root 1.1 use Glib ();
38    
39 root 1.12 our $maincontext = Glib::MainContext->default;
40 root 1.1
41 root 1.2 sub io {
42     my ($class, %arg) = @_;
43    
44 root 1.3 my $self = bless \%arg, $class;
45 root 1.2 my $rcb = \$self->{cb};
46    
47 root 1.11 my @cond;
48 root 1.4 # some glibs need hup, others error with it, YMMV
49 root 1.6 push @cond, "in", "hup" if $self->{poll} eq "r";
50     push @cond, "out", "hup" if $self->{poll} eq "w";
51 root 1.2
52     $self->{source} = add_watch Glib::IO fileno $self->{fh}, \@cond, sub {
53 root 1.6 $$rcb->();
54 root 1.2 ! ! $$rcb
55     };
56 root 1.1
57 root 1.2 $self
58 root 1.1 }
59    
60 root 1.2 sub timer {
61     my ($class, %arg) = @_;
62 root 1.1
63 root 1.3 my $self = bless \%arg, $class;
64 root 1.2 my $cb = $self->{cb};
65 root 1.1
66 root 1.2 $self->{source} = add Glib::Timeout $self->{after} * 1000, sub {
67     $cb->();
68     0
69 root 1.1 };
70    
71 root 1.2 $self
72 root 1.1 }
73    
74 root 1.6 sub DESTROY {
75 root 1.1 my ($self) = @_;
76    
77     remove Glib::Source delete $self->{source} if $self->{source};
78 root 1.6 # need to undef $cb because we hold references to it
79 root 1.2 $self->{cb} = undef;
80     }
81    
82 root 1.7 sub one_event {
83     $maincontext->iteration (1);
84     }
85    
86 root 1.12 1;
87    
88     =head1 SEE ALSO
89    
90 root 1.14 L<AnyEvent>, L<Glib>.
91 root 1.12
92     =head1 AUTHOR
93    
94     Marc Lehmann <schmorp@schmorp.de>
95     http://home.schmorp.de/
96    
97     =cut
98 root 1.1