blib/lib/AnyEvent/Impl/EventLib.pm untested

filter: Statementstmt 0.0% 0 / 42 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 67 CC 2 SCAR 9.8
CC
2
Cov
0%
timer
6.0
BEGIN
2.0
ccb
2.0
CRAP 2.7
1 =head1 NAME
2  
3 AnyEvent::Impl::EventLib - AnyEvent adaptor for Event::Lib
4  
5 =head1 SYNOPSIS
6  
7 use AnyEvent;
8 use Event::Lib;
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 Event work with AnyEvent except by loading Event::Lib
16 before creating the first AnyEvent watcher.
17  
18 Note: the AnyEvent author has not found recent releases of Event::Lib to
19 be even remotely working (not even the examples from the manpage or the
20 testsuite work), so this event backend should be avoided (or somebody
21 should step up and maintain it, hint, hint).
22  
23 The L<Event::Lib> module suffers from the same limitations and bugs as
24 libevent, most notably it kills already-installed watchers on a file
25 descriptor and it is unable to support fork. These are not fatal issues,
26 and are worked-around by this module, but the L<Event::Lib> perl module
27 itself has many additional bugs such as taking references to file handles
28 and callbacks instead of making a copy or freeing still-allocated scalars,
29 causing memory corruption and random crashes. Only Tk rivals it in its
30 brokenness.
31  
32 This adaptor module employs the same workaround around the watcher
33 problems as Tk and should therefore be avoided. (This was done for
34 simplicity, one could in theory work around the problems with lower
35 overhead by managing our own watchers).
36  
37 Event::Lib also leaks file handles and memory and tends to just exit on
38 problems.
39  
40 It also doesn't work around the Windows bug of not signalling TCP
41 connection failures.
42  
43 It also doesn't work with many special devices on Linux (F</dev/random>
44 works, F</dev/urandom> fails, F</dev/tty> works, F</dev/null> fails and so
45 on).
46  
47 Event::Lib does not support idle watchers. They could be emulated using
48 low-priority timers but as the priority range (and availability) is not
49 queryable nor guaranteed, and the default priority is likely the lowest
50 one, this module cannot use them.
51  
52 Avoid Event::Lib if you can.
53  
54 =cut
55  
56 package AnyEvent::Impl::EventLib;
57  
58 use AnyEvent (); BEGIN { AnyEvent::common_sense }
59  
60 use Event::Lib;
61  
62 # Event::Lib doesn't always take a reference to the callback, so closures
63 # cause memory corruption and segfaults. it also has an issue actually
64 # calling callbacks, so this exists as workaround.
65 sub ccb {
66 # Event:Lib accesses $_[0] after the callback, when it might be freed,
67 # so we keep it referenced until after the callback. This still accesses
68 # a freed scalar, but at least it'll not crash.
69 my $keep_it = $_[0];
70  
71 $_[2]();
72 }
73  
74 my $ccb = \&ccb;
75  
76 sub io {
77 my (undef, %arg) = @_;
78  
79 # work around these bugs in Event::Lib:
80 # - adding a callback might destroy other callbacks
81 # - only one callback per fd/poll combination
82 my ($fh, $mode) = AnyEvent::_dupfh $arg{poll}, $arg{fh}, EV_READ, EV_WRITE;
83  
84 # event_new errornously takes a reference to fh and cb instead of making a copy
85 # fortunately, going through %arg/_dupfh already makes a copy, so it happpens to work
86 my $w = event_new $fh, $mode | EV_PERSIST, $ccb, $arg{cb};
87 event_add $w;
88 bless \\$w, __PACKAGE__
89 }
90  
91 sub timer {
92 my (undef, %arg) = @_;
93  
94 my $ival = $arg{interval};
95 my $cb = $arg{cb};
96  
97 my $w; $w = timer_new $ccb,
98 $ival
99 ? sub { event_add $w, $ival; &$cb }
100 : sub { undef $w ; &$cb };
101  
102 event_add $w, $arg{after} || 1e-10; # work around 0-bug in Event::Lib
103  
104 bless \\$w, __PACKAGE__
105 }
106  
107 sub DESTROY {
108 local $@;
109 ${${$_[0]}}->remove;
110 }
111  
112 sub signal {
113 my (undef, %arg) = @_;
114  
115 my $w = signal_new AnyEvent::Base::sig2num $arg{signal}, $ccb, $arg{cb};
116 event_add $w;
117 AnyEvent::Base::_sig_add;
118 bless \\$w, "AnyEvent::Impl::EventLib::signal"
119 }
120  
121 sub AnyEvent::Impl::EventLib::signal::DESTROY {
122 AnyEvent::Base::_sig_del;
123 local $@;
124 ${${$_[0]}}->remove;
125 }
126  
127 #sub loop {
128 # event_mainloop;
129 #}
130  
131 sub _poll {
132 event_one_loop;
133 }
134  
135 sub AnyEvent::CondVar::Base::_wait {
136 event_one_loop until exists $_[0]{_ae_sent};
137 }
138  
139 =head1 SEE ALSO
140  
141 L<AnyEvent>, L<Event::Lib>.
142  
143 =head1 AUTHOR
144  
145 Marc Lehmann <schmorp@schmorp.de>
146 http://anyevent.schmorp.de
147  
148 =cut
149  
150 1
151