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.2 by root, Thu Dec 1 18:56:18 2005 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;
6 43
7my %RWE = ( 44my %io_cond = (
8 in => 'r', 45 r => ["in" , "hup"],
9 out => 'w', 46 w => ["out", "hup"],
10 pri => 'e',
11); 47);
12 48
13sub io { 49sub io {
14 my ($class, %arg) = @_; 50 my ($class, %arg) = @_;
15 51
16 my $self = \%arg, $class;
17 my $rcb = \$self->{cb}; 52 my $cb = $arg{cb};
53 my $fd = fileno $arg{fh};
54 defined $fd or $fd = $arg{fh};
18 55
19 my @cond; 56 my $source = add_watch Glib::IO
20 push @cond, "in" if $self->{poll} =~ /r/i; 57 $fd,
21 push @cond, "out" if $self->{poll} =~ /w/i; 58 $io_cond{$arg{poll}},
22 push @cond, "pri" if $self->{poll} =~ /e/i; 59 sub { &$cb; 1 };
23 60
24 $self->{source} = add_watch Glib::IO fileno $self->{fh}, \@cond, sub { 61 bless \\$source, $class
25 $$rcb->(join "", map $RWE{$_}, @{ $_[1] });
26 ! ! $$rcb
27 };
28
29 $self
30} 62}
31 63
32sub timer { 64sub timer {
33 my ($class, %arg) = @_; 65 my ($class, %arg) = @_;
34 66
35 my $self = \%arg, $class;
36 my $cb = $self->{cb}; 67 my $cb = $arg{cb};
68 my $ival = $arg{interval} * 1000;
37 69
38 $self->{source} = add Glib::Timeout $self->{after} * 1000, sub { 70 my $source; $source = add Glib::Timeout $arg{after} * 1000,
39 $cb->(); 71 $ival ? sub {
40 0 72 remove Glib::Source $source;
41 }; 73 $source = add Glib::Timeout $ival, sub { &$cb; 1 };
74 &$cb;
75 0
76 }
77 : sub { &$cb; 0 };
42 78
43 $self 79 bless \\$source, $class
44} 80}
45 81
46sub cancel { 82sub idle {
47 my ($self) = @_; 83 my ($class, %arg) = @_;
48 84
49 return unless HASH:: eq ref $self; 85 my $cb = $arg{cb};
50 86 my $source = add Glib::Idle sub { &$cb; 1 };
51 remove Glib::Source delete $self->{source} if $self->{source}; 87 bless \\$source, $class
52 $self->{cb} = undef;
53 delete $self->{cb};
54} 88}
55 89
56sub DESTROY { 90sub DESTROY {
57 my ($self) = @_; 91 remove Glib::Source $${$_[0]};
58
59 $self->cancel;
60} 92}
61 93
62sub condvar { 94sub one_event {
63 my $class = shift; 95 $mainloop->iteration (1);
64
65 bless \my $x, $class;
66} 96}
67 97
68sub broadcast { 98sub loop {
69 ${$_[0]}++ 99 # hackish, but we do not have a mainloop, just a maincontext
100 $mainloop->iteration (1) while 1;
70} 101}
71
72sub wait {
73 $maincontext->iteration (1) while !${$_[0]};
74}
75
76$AnyEvent::MODEL = __PACKAGE__;
77 102
781; 1031;
79 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