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