… | |
… | |
33 | =cut |
33 | =cut |
34 | |
34 | |
35 | package AnyEvent::Impl::Glib; |
35 | package AnyEvent::Impl::Glib; |
36 | |
36 | |
37 | use AnyEvent (); BEGIN { AnyEvent::common_sense } |
37 | use AnyEvent (); BEGIN { AnyEvent::common_sense } |
38 | use Glib (); |
38 | use Glib 1.210 (); # (stable 1.220 2009, also Glib 2.4+ required, 2004) |
39 | |
39 | |
40 | our $mainloop = Glib::MainContext->default; |
40 | our $mainloop = Glib::MainContext->default; |
41 | |
41 | |
42 | my %io_cond = ( |
42 | my %io_cond = ( |
43 | r => ["in" , "hup"], |
43 | r => ["in" , "hup"], |
… | |
… | |
80 | sub idle { |
80 | sub idle { |
81 | my ($class, %arg) = @_; |
81 | my ($class, %arg) = @_; |
82 | |
82 | |
83 | my $cb = $arg{cb}; |
83 | my $cb = $arg{cb}; |
84 | my $source = add Glib::Idle sub { &$cb; 1 }; |
84 | my $source = add Glib::Idle sub { &$cb; 1 }; |
|
|
85 | |
85 | bless \\$source, $class |
86 | bless \\$source, $class |
86 | } |
87 | } |
87 | |
88 | |
88 | sub DESTROY { |
89 | sub DESTROY { |
89 | remove Glib::Source $${$_[0]}; |
90 | remove Glib::Source $${$_[0]}; |
|
|
91 | } |
|
|
92 | |
|
|
93 | our %pid_w; |
|
|
94 | our %pid_cb; |
|
|
95 | |
|
|
96 | sub child { |
|
|
97 | my ($class, %arg) = @_; |
|
|
98 | |
|
|
99 | $arg{pid} > 0 |
|
|
100 | or Carp::croak "Glib does not support watching for all pids (pid == 0) as attempted"; |
|
|
101 | |
|
|
102 | my $pid = $arg{pid}; |
|
|
103 | my $cb = $arg{cb}; |
|
|
104 | |
|
|
105 | $pid_cb{$pid}{$cb+0} = $cb; |
|
|
106 | |
|
|
107 | $pid_w{$pid} ||= Glib::Child->watch_add ($pid, sub { |
|
|
108 | $_->($_[0], $_[1]) |
|
|
109 | for values %{ $pid_cb{$pid} }; |
|
|
110 | |
|
|
111 | 1 |
|
|
112 | }); |
|
|
113 | |
|
|
114 | bless [$pid, $cb+0], "AnyEvent::Impl::Glib::child" |
|
|
115 | } |
|
|
116 | |
|
|
117 | sub AnyEvent::Impl::Glib::child::DESTROY { |
|
|
118 | my ($pid, $icb) = @{ $_[0] }; |
|
|
119 | |
|
|
120 | delete $pid_cb{$pid}{$icb}; |
|
|
121 | unless (%{ $pid_cb{$pid} }) { |
|
|
122 | delete $pid_cb{$pid}; |
|
|
123 | remove Glib::Source delete $pid_w{$pid}; |
|
|
124 | } |
90 | } |
125 | } |
91 | |
126 | |
92 | sub AnyEvent::CondVar::_wait { |
127 | sub AnyEvent::CondVar::_wait { |
93 | $mainloop->iteration (1) until $_[0]{_ae_sent}; |
128 | $mainloop->iteration (1) until $_[0]{_ae_sent}; |
94 | } |
129 | } |