File Coverage

blib/lib/IO/Async/Loop/KQueue.pm
Criterion Covered Total %
statement 11 13 84.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 16 18 88.8


line stmt bran cond sub pod time code
1             package IO::Async::Loop::KQueue;
2             BEGIN {
3 2     2   1996 $IO::Async::Loop::KQueue::VERSION = '0.02';
4             }
5              
6 2     2   16 use strict;
  2         3  
  2         55  
7 2     2   9 use warnings;
  2         3  
  2         64  
8 2     2   8 use Carp;
  2         4  
  2         133  
9              
10 2     2   849 use IO::KQueue;
  0            
  0            
11              
12             use base qw( IO::Async::Loop );
13              
14             use constant API_VERSION => '0.33';
15              
16             =head1 NAME
17              
18             IO::Async::Loop::KQueue - use C with C
19              
20             =head1 VERSION
21              
22             Version 0.02
23              
24             =head1 SYNOPSIS
25              
26             Like L for Linux, This module provides native loop management for
27             BSD like operating systems that have KQueue present, using C.
28              
29             use IO::Async::Loop::KQueue;
30            
31             my $loop = IO::Async::Loop::KQueue->new();
32            
33             $loop->add( IO::Async::Signal->new(
34             name => '',
35             on_receipt => sub { ... },
36             ) );
37              
38             =head1 METHODS
39              
40             =head2 new
41              
42             =cut
43              
44             sub new
45             {
46             my $class = shift;
47             my ( %args ) = @_;
48              
49             my $kq = IO::KQueue->new() or croak "Cannot create kqueue handle - $!";
50              
51             my $self = $class->SUPER::__new( %args );
52             $self->{kqueue} = $kq;
53              
54             return $self;
55             }
56              
57             =head2 $count = $loop->loop_once( $timeout )
58              
59             This method calls the kevent method, using the given timeout and processes
60             the results of that call. It returns the total number of C
61             callbacks invoked.
62              
63             =cut
64              
65             sub loop_once
66             {
67             my $self = shift;
68             my ( $timeout ) = @_;
69              
70             $self->_adjust_timeout( \$timeout );
71              
72             my $msec = defined $timeout ? $timeout * 1000 : -1;
73              
74             my @events = $self->{kqueue}->kevent($msec);
75              
76             my $count = 0;
77             local $self->{cancellations} = \my %cancellations;
78              
79             foreach my $ev ( @events )
80             {
81             next if $cancellations{$ev->[KQ_FILTER]."/".$ev->[KQ_IDENT]};
82              
83             $ev->[KQ_UDATA]->();
84              
85             $count++;
86             }
87              
88             $count += $self->_manage_queues;
89              
90             return $count;
91             }
92              
93             sub watch_io
94             {
95             my $self = shift;
96             my %params = @_;
97              
98             my $kqueue = $self->{kqueue};
99              
100             my $handle = $params{handle};
101             my $fd = $handle->fileno;
102              
103             if( my $cb = $params{on_read_ready} ) {
104             $kqueue->EV_SET($fd, EVFILT_READ, EV_ADD, 0, 0, $cb);
105             }
106              
107             if( my $cb = $params{on_write_ready} ) {
108             $kqueue->EV_SET($fd, EVFILT_WRITE, EV_ADD, 0, 0, $cb);
109             }
110             }
111              
112             sub unwatch_io
113             {
114             my $self = shift;
115             my %params = @_;
116              
117             my $kqueue = $self->{kqueue};
118              
119             my $handle = $params{handle};
120             my $fd = $handle->fileno;
121              
122             # Just ignore errors from EV_SET; doesn't matter if we fail to delete
123             # because it wasn't there
124             if( $params{on_read_ready} ) {
125             eval { $kqueue->EV_SET($fd, EVFILT_READ, EV_DELETE) };
126             $self->{cancellations}{EVFILT_READ."/$fd"}++ if $self->{cancellations};
127             }
128              
129             if( $params{on_write_ready} ) {
130             eval { $kqueue->EV_SET($fd, EVFILT_WRITE, EV_DELETE) };
131             $self->{cancellations}{EVFILT_WRITE."/$fd"}++ if $self->{cancellations};
132             }
133             }
134              
135             =head1 AUTHOR
136              
137             Squeeks, C<< >>
138              
139             =head1 BUGS
140              
141             Please report any bugs or feature requests to C, or through
142             the web interface at L. I will be notified, and then you'll
143             automatically be notified of progress on your bug as I make changes.
144              
145             =head1 SUPPORT
146              
147             You can find documentation for this module with the perldoc command.
148              
149             perldoc IO::Async::Loop::KQueue
150              
151             You can also look for information at:
152              
153             =over 4
154              
155             =item * RT: CPAN's request tracker
156              
157             L
158              
159             =item * AnnoCPAN: Annotated CPAN documentation
160              
161             L
162              
163             =item * CPAN Ratings
164              
165             L
166              
167             =item * Search CPAN
168              
169             L
170              
171             =back
172              
173              
174             =head1 ACKNOWLEDGEMENTS
175              
176             Paul Evans (LeoNerd) for doing all the hard work.
177              
178             =head1 LICENSE AND COPYRIGHT
179              
180             Copyright 2010 Squeeks.
181              
182             This program is free software; you can redistribute it and/or modify it
183             under the terms of either: the GNU General Public License as published
184             by the Free Software Foundation; or the Artistic License.
185              
186             See http://dev.perl.org/licenses/ for more information.
187              
188              
189             =cut
190              
191             1; # End of IO::Async::Loop::KQueue