|
|
1 | =head1 NAME |
|
|
2 | |
|
|
3 | AnyEvent::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 | |
|
|
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 of |
|
|
20 | them, whether I/O-related, timer-related or what not) during each loop |
|
|
21 | iteration, it also does so multiple times and rebuilds the poll list for |
|
|
22 | the kernel each time again, dynamically even. |
|
|
23 | |
|
|
24 | On the positive side, and most importantly, Glib generally works |
|
|
25 | correctly, no quarrels there. |
|
|
26 | |
|
|
27 | 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 | This module uses the default Glib main context for all its watchers. |
|
|
32 | |
|
|
33 | =cut |
|
|
34 | |
1 | package AnyEvent::Impl::Glib; |
35 | package AnyEvent::Impl::Glib; |
|
|
36 | |
|
|
37 | no warnings; |
|
|
38 | use strict; |
2 | |
39 | |
3 | use Glib (); |
40 | use Glib (); |
4 | |
41 | |
5 | my $maincontext = Glib::MainContext->default; |
42 | our $mainloop = Glib::MainContext->default; |
6 | |
43 | |
7 | my %RWE = ( |
44 | my %io_cond = ( |
8 | in => 'r', |
45 | r => ["in" , "hup"], |
9 | out => 'w', |
46 | w => ["out", "hup"], |
10 | pri => 'e', |
|
|
11 | ); |
47 | ); |
12 | |
48 | |
13 | sub io { |
49 | sub 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 | |
32 | sub timer { |
64 | sub 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 | |
46 | sub cancel { |
82 | sub 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 | |
56 | sub DESTROY { |
90 | sub DESTROY { |
57 | my ($self) = @_; |
91 | remove Glib::Source $${$_[0]}; |
58 | |
|
|
59 | $self->cancel; |
|
|
60 | } |
92 | } |
61 | |
93 | |
62 | sub condvar { |
94 | sub one_event { |
63 | my $class = shift; |
95 | $mainloop->iteration (1); |
64 | |
|
|
65 | bless \my $x, $class; |
|
|
66 | } |
96 | } |
67 | |
97 | |
68 | sub broadcast { |
98 | sub loop { |
69 | ${$_[0]}++ |
99 | # hackish, but we do not have a mainloop, just a maincontext |
|
|
100 | $mainloop->iteration (1) while 1; |
70 | } |
101 | } |
71 | |
|
|
72 | sub wait { |
|
|
73 | $maincontext->iteration (1) while !${$_[0]}; |
|
|
74 | } |
|
|
75 | |
|
|
76 | $AnyEvent::MODEL = __PACKAGE__; |
|
|
77 | |
102 | |
78 | 1; |
103 | 1; |
79 | |
104 | |
|
|
105 | =head1 SEE ALSO |
|
|
106 | |
|
|
107 | L<AnyEvent>, L<Glib>. |
|
|
108 | |
|
|
109 | =head1 AUTHOR |
|
|
110 | |
|
|
111 | Marc Lehmann <schmorp@schmorp.de> |
|
|
112 | http://home.schmorp.de/ |
|
|
113 | |
|
|
114 | =cut |
|
|
115 | |