… | |
… | |
19 | light of the world: Glib not only scans all its watchers (really, ALL of |
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 |
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 | |
23 | |
24 | On the positive side, Glib generally works correctly, no quarrels there. |
24 | On the positive side, and most importantly, Glib generally works |
|
|
25 | correctly, no quarrels there. |
25 | |
26 | |
26 | 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 |
27 | 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 |
28 | other, more efficient, event loops. |
29 | other, more efficient, event loops. |
29 | |
30 | |
30 | 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. |
31 | |
32 | |
32 | =cut |
33 | =cut |
33 | |
34 | |
34 | package AnyEvent::Impl::Glib; |
35 | package AnyEvent::Impl::Glib; |
35 | |
36 | |
36 | no warnings; |
37 | no warnings; |
37 | use strict; |
38 | use strict; |
38 | |
39 | |
39 | use Glib (); |
40 | use Glib (); |
40 | |
41 | |
41 | 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 | ); |
42 | |
48 | |
43 | sub io { |
49 | sub io { |
44 | my ($class, %arg) = @_; |
50 | my ($class, %arg) = @_; |
45 | |
51 | |
46 | my $cb = $arg{cb}; |
52 | my $cb = $arg{cb}; |
|
|
53 | my $fd = fileno $arg{fh}; |
|
|
54 | defined $fd or $fd = $arg{fh}; |
47 | |
55 | |
48 | my @cond; |
56 | my $source = add_watch Glib::IO |
49 | # some glibs need hup, others error with it, YMMV |
57 | $fd, |
50 | push @cond, "in", "hup" if $arg{poll} eq "r"; |
58 | $io_cond{$arg{poll}}, |
51 | push @cond, "out", "hup" if $arg{poll} eq "w"; |
59 | sub { &$cb; 1 }; |
52 | |
|
|
53 | my $source = add_watch Glib::IO fileno $arg{fh}, \@cond, sub { |
|
|
54 | &$cb; |
|
|
55 | 1 |
|
|
56 | }; |
|
|
57 | |
60 | |
58 | bless \\$source, $class |
61 | bless \\$source, $class |
59 | } |
62 | } |
60 | |
63 | |
61 | sub timer { |
64 | sub timer { |
… | |
… | |
74 | : sub { &$cb; 0 }; |
77 | : sub { &$cb; 0 }; |
75 | |
78 | |
76 | bless \\$source, $class |
79 | bless \\$source, $class |
77 | } |
80 | } |
78 | |
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 |
|
|
88 | } |
|
|
89 | |
79 | sub DESTROY { |
90 | sub DESTROY { |
80 | remove Glib::Source $${$_[0]}; |
91 | remove Glib::Source $${$_[0]}; |
81 | } |
92 | } |
82 | |
93 | |
83 | sub one_event { |
94 | sub one_event { |
84 | $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; |
85 | } |
101 | } |
86 | |
102 | |
87 | 1; |
103 | 1; |
88 | |
104 | |
89 | =head1 SEE ALSO |
105 | =head1 SEE ALSO |