|
|
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; |
|
|
43 | |
|
|
44 | my %io_cond = ( |
|
|
45 | r => ["in" , "hup"], |
|
|
46 | w => ["out", "hup"], |
|
|
47 | ); |
6 | |
48 | |
7 | sub io { |
49 | sub 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 | |
25 | sub timer { |
64 | sub 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 | |
|
|
82 | sub 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 | |
39 | sub DESTROY { |
90 | sub 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 | |
|
|
48 | sub condvar { |
|
|
49 | my $class = shift; |
|
|
50 | |
|
|
51 | bless \my $flag, $class |
|
|
52 | } |
|
|
53 | |
|
|
54 | sub broadcast { |
|
|
55 | ${$_[0]}++; |
|
|
56 | } |
|
|
57 | |
|
|
58 | sub wait { |
|
|
59 | $maincontext->iteration (1) while !${$_[0]}; |
|
|
60 | } |
92 | } |
61 | |
93 | |
62 | sub one_event { |
94 | sub one_event { |
63 | $maincontext->iteration (1); |
95 | $mainloop->iteration (1); |
64 | } |
96 | } |
65 | |
97 | |
66 | 1 |
98 | sub loop { |
|
|
99 | # hackish, but we do not have a mainloop, just a maincontext |
|
|
100 | $mainloop->iteration (1) while 1; |
|
|
101 | } |
67 | |
102 | |
|
|
103 | 1; |
|
|
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 | |