ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent/Impl/Glib.pm
(Generate patch)

Comparing AnyEvent/lib/AnyEvent/Impl/Glib.pm (file contents):
Revision 1.7 by root, Fri Nov 24 14:40:13 2006 UTC vs.
Revision 1.24 by root, Wed Jul 8 02:01:12 2009 UTC

1=head1 NAME
2
3AnyEvent::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
14This module provides transparent support for AnyEvent. You don't have to
15do anything to make Glib work with AnyEvent except by loading Glib before
16creating the first AnyEvent watcher.
17
18Glib is probably the most inefficient event loop that has ever seen the
19light of the world: Glib not only scans all its watchers (really, ALL of
20them, whether I/O-related, timer-related or what not) during each loop
21iteration, it also does so multiple times and rebuilds the poll list for
22the kernel each time again, dynamically even.
23
24On the positive side, and most importantly, Glib generally works
25correctly, no quarrels there.
26
27If you create many watchers (as in: more than two), you might consider one
28of the L<Glib::EV>, L<EV::Glib> or L<Glib::Event> modules that map Glib to
29other, more efficient, event loops.
30
31This module uses the default Glib main context for all its watchers.
32
33=cut
34
1package AnyEvent::Impl::Glib; 35package AnyEvent::Impl::Glib;
36
37no warnings;
38use strict;
2 39
3use Glib (); 40use Glib ();
4 41
5my $maincontext = Glib::MainContext->default; 42our $mainloop = Glib::MainContext->default;
43
44my %io_cond = (
45 r => ["in" , "hup"],
46 w => ["out", "hup"],
47);
6 48
7sub io { 49sub io {
8 my ($class, %arg) = @_; 50 my ($class, %arg) = @_;
9 51
10 my $self = bless \%arg, $class;
11 my $rcb = \$self->{cb}; 52 my $cb = $arg{cb};
53 my $fd = fileno $arg{fh};
54 defined $fd or $fd = $arg{fh};
12 55
13 # some glibs need hup, others error with it, YMMV 56 my $source = add_watch Glib::IO
14 push @cond, "in", "hup" if $self->{poll} eq "r"; 57 $fd,
15 push @cond, "out", "hup" if $self->{poll} eq "w"; 58 $io_cond{$arg{poll}},
59 sub { &$cb; 1 };
16 60
17 $self->{source} = add_watch Glib::IO fileno $self->{fh}, \@cond, sub { 61 bless \\$source, $class
18 $$rcb->();
19 ! ! $$rcb
20 };
21
22 $self
23} 62}
24 63
25sub timer { 64sub timer {
26 my ($class, %arg) = @_; 65 my ($class, %arg) = @_;
27 66
28 my $self = bless \%arg, $class;
29 my $cb = $self->{cb}; 67 my $cb = $arg{cb};
68 my $ival = $arg{interval} * 1000;
30 69
31 $self->{source} = add Glib::Timeout $self->{after} * 1000, sub { 70 my $source; $source = add Glib::Timeout $arg{after} * 1000,
32 $cb->(); 71 $ival ? sub {
33 0 72 remove Glib::Source $source;
34 }; 73 $source = add Glib::Timeout $ival, sub { &$cb; 1 };
74 &$cb;
75 0
76 }
77 : sub { &$cb; 0 };
35 78
36 $self 79 bless \\$source, $class
80}
81
82sub idle {
83 my ($class, %arg) = @_;
84
85 my $cb = $arg{cb};
86 my $source = add Glib::Idle sub { &$cb; 1 };
87 bless \\$source, $class
37} 88}
38 89
39sub DESTROY { 90sub DESTROY {
40 my ($self) = @_; 91 remove Glib::Source $${$_[0]};
41
42 remove Glib::Source delete $self->{source} if $self->{source};
43 # need to undef $cb because we hold references to it
44 $self->{cb} = undef;
45 %$self = ();
46}
47
48sub condvar {
49 my $class = shift;
50
51 bless \my $flag, $class
52}
53
54sub broadcast {
55 ${$_[0]}++;
56}
57
58sub wait {
59 $maincontext->iteration (1) while !${$_[0]};
60} 92}
61 93
62sub one_event { 94sub one_event {
63 $maincontext->iteration (1); 95 $mainloop->iteration (1);
64} 96}
65 97
661 98sub loop {
99 # hackish, but we do not have a mainloop, just a maincontext
100 $mainloop->iteration (1) while 1;
101}
67 102
1031;
104
105=head1 SEE ALSO
106
107L<AnyEvent>, L<Glib>.
108
109=head1 AUTHOR
110
111 Marc Lehmann <schmorp@schmorp.de>
112 http://home.schmorp.de/
113
114=cut
115

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines