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.1 by root, Wed Apr 27 01:26:44 2005 UTC vs.
Revision 1.23 by root, Mon Jun 22 11:17:03 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
7sub new_from_fh { 44sub io {
8 my ($class, $fh) = @_; 45 my ($class, %arg) = @_;
9 bless { fh => $fh }, $class; 46
47 my $cb = $arg{cb};
48
49 my @cond;
50 # some glibs need hup, others error with it, YMMV
51 push @cond, "in", "hup" if $arg{poll} eq "r";
52 push @cond, "out", "hup" if $arg{poll} eq "w";
53
54 my $source = add_watch Glib::IO fileno $arg{fh}, \@cond, sub { &$cb; 1 };
55 bless \\$source, $class
10} 56}
11 57
12sub cb { 58sub timer {
13 my ($self, $cb) = @_; 59 my ($class, %arg) = @_;
14 $self->{cb} = $cb; 60
15 $self; 61 my $cb = $arg{cb};
62 my $ival = $arg{interval} * 1000;
63
64 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
73 bless \\$source, $class
16} 74}
17 75
18sub poll { 76sub idle {
19 my ($self, $r, $w, $e) = @_; 77 my ($class, %arg) = @_;
20
21 remove Glib::Source delete $self->{source} if $self->{source};
22 78
23 my @cond; 79 my $cb = $arg{cb};
24 push @cond, "in" if $r; 80 my $source = add Glib::Idle sub { &$cb; 1 };
25 push @cond, "out" if $w; 81 bless \\$source, $class
26 push @cond, "err" if $e;
27
28 my $cb = \$self->{cb}; # avoid $self-reference
29
30 $self->{source} = add_watch Glib::IO fileno $self->{fh}, \@cond, sub {
31 $$cb->(); 1;
32 };
33
34 $self;
35} 82}
36 83
37sub DESTROY { 84sub DESTROY {
38 my ($self) = @_; 85 remove Glib::Source $${$_[0]};
39
40 remove Glib::Source delete $self->{source} if $self->{source};
41} 86}
42 87
43############# 88sub one_event {
44 89 $mainloop->iteration (1);
45sub new_signal {
46 my $class = shift;
47
48 bless \my $x, $class;
49} 90}
50 91
51sub send { 92sub loop {
52 ${$_[0]}++; 93 $mainloop->iteration (1) while 1; # hackish, but w ehave no mainloop
53}
54
55sub wait {
56 $maincontext->iteration (1) while !${$_[0]};
57} 94}
58 95
591; 961;
60 97
98=head1 SEE ALSO
99
100L<AnyEvent>, L<Glib>.
101
102=head1 AUTHOR
103
104 Marc Lehmann <schmorp@schmorp.de>
105 http://home.schmorp.de/
106
107=cut
108

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines