… | |
… | |
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; |
40 | |
43 | |
41 | sub io { |
44 | sub io { |
42 | my ($class, %arg) = @_; |
45 | my ($class, %arg) = @_; |
43 | |
46 | |
44 | my $fh = $arg{fh}; |
|
|
45 | my $cb = $arg{cb}; |
47 | my $cb = $arg{cb}; |
46 | |
48 | |
47 | my @cond; |
49 | my @cond; |
48 | # some glibs need hup, others error with it, YMMV |
50 | # some glibs need hup, others error with it, YMMV |
49 | push @cond, "in", "hup" if $arg{poll} eq "r"; |
51 | push @cond, "in", "hup" if $arg{poll} eq "r"; |
50 | push @cond, "out", "hup" if $arg{poll} eq "w"; |
52 | push @cond, "out", "hup" if $arg{poll} eq "w"; |
51 | |
53 | |
52 | my $source = add_watch Glib::IO fileno $arg{fh}, \@cond, sub { |
54 | my $source = add_watch Glib::IO fileno $arg{fh}, \@cond, sub { &$cb; 1 }; |
53 | &$cb; |
|
|
54 | $fh; # mention it here to keep it from being destroyed |
|
|
55 | 1 |
|
|
56 | }; |
|
|
57 | |
|
|
58 | bless \\$source, $class |
55 | bless \\$source, $class |
59 | } |
56 | } |
60 | |
57 | |
61 | sub timer { |
58 | sub timer { |
62 | my ($class, %arg) = @_; |
59 | my ($class, %arg) = @_; |
… | |
… | |
74 | : sub { &$cb; 0 }; |
71 | : sub { &$cb; 0 }; |
75 | |
72 | |
76 | bless \\$source, $class |
73 | bless \\$source, $class |
77 | } |
74 | } |
78 | |
75 | |
|
|
76 | sub idle { |
|
|
77 | my ($class, %arg) = @_; |
|
|
78 | |
|
|
79 | my $cb = $arg{cb}; |
|
|
80 | my $source = add Glib::Idle sub { &$cb; 1 }; |
|
|
81 | bless \\$source, $class |
|
|
82 | } |
|
|
83 | |
79 | sub DESTROY { |
84 | sub DESTROY { |
80 | remove Glib::Source $${$_[0]}; |
85 | remove Glib::Source $${$_[0]}; |
81 | } |
86 | } |
82 | |
87 | |
83 | sub one_event { |
88 | sub one_event { |
84 | $maincontext->iteration (1); |
89 | $mainloop->iteration (1); |
|
|
90 | } |
|
|
91 | |
|
|
92 | sub loop { |
|
|
93 | $mainloop->iteration (1) while 1; # hackish, but w ehave no mainloop |
85 | } |
94 | } |
86 | |
95 | |
87 | 1; |
96 | 1; |
88 | |
97 | |
89 | =head1 SEE ALSO |
98 | =head1 SEE ALSO |