1 |
root |
1.1 |
=head1 NAME |
2 |
|
|
|
3 |
|
|
AnyEvent::XSThreadPool - manage xs-level thread pools to do stuff asynchronously |
4 |
|
|
|
5 |
|
|
=head1 SYNOPSIS |
6 |
|
|
|
7 |
|
|
use AnyEvent::XSThreadPool; |
8 |
|
|
|
9 |
|
|
=head1 DESCRIPTION |
10 |
|
|
|
11 |
|
|
=over 4 |
12 |
|
|
|
13 |
|
|
=cut |
14 |
|
|
|
15 |
|
|
package AnyEvent::XSThreadPool; |
16 |
|
|
|
17 |
|
|
BEGIN { |
18 |
|
|
our $VERSION = 0.02; |
19 |
|
|
|
20 |
|
|
use XSLoader; |
21 |
|
|
XSLoader::load __PACKAGE__, $VERSION; |
22 |
|
|
} |
23 |
|
|
|
24 |
|
|
use AnyEvent; |
25 |
|
|
|
26 |
|
|
=item $pool = new AnyEvent::XSThreadPool |
27 |
|
|
|
28 |
|
|
=cut |
29 |
|
|
|
30 |
|
|
sub new { |
31 |
|
|
my ($class) = @_; |
32 |
|
|
|
33 |
|
|
my $self = bless [""], $class; |
34 |
|
|
|
35 |
|
|
my $poll_cb = _init $self->[0]; |
36 |
|
|
$self->[1] = AE::io $self->fileno, 0, $poll_cb; |
37 |
|
|
|
38 |
|
|
$self |
39 |
|
|
} |
40 |
|
|
|
41 |
|
|
sub DESTROY { |
42 |
|
|
&_destroy; |
43 |
|
|
} |
44 |
|
|
|
45 |
root |
1.2 |
=item $pool->req (REQUEST_TYPE, [request-args], $callback) |
46 |
|
|
|
47 |
root |
1.3 |
=item AnyEvent::XSThreadPool::req $pool, REQUEST_TYPE, [request-args], $callback |
48 |
|
|
|
49 |
|
|
This is the same as the C<< ->req >> method, above, except as a function |
50 |
|
|
call. If you submit many small requests, the function syntax executes much |
51 |
|
|
faster, but is less convenient than the method syntax. |
52 |
|
|
|
53 |
root |
1.2 |
=item $fd = $pool->fileno |
54 |
|
|
|
55 |
|
|
=item $nreqs = $pool->nreqs |
56 |
|
|
|
57 |
|
|
=item $nready = $pool->nready |
58 |
|
|
|
59 |
|
|
=item $npending = $pool->npending |
60 |
|
|
|
61 |
|
|
=item $threadcount = $pool->nthreads |
62 |
|
|
|
63 |
|
|
=item $pool->max_poll_time ($seconds) |
64 |
|
|
|
65 |
|
|
=item $pool->max_poll_reqs ($maxreqs) |
66 |
|
|
|
67 |
|
|
=item $pool->idle_timeout ($seconds) |
68 |
|
|
|
69 |
|
|
=item $pool->max_idle ($threads) |
70 |
|
|
|
71 |
|
|
=item $pool->min_parallel ($nthreads) |
72 |
|
|
|
73 |
|
|
=item $pool->max_parallel ($nthreads) |
74 |
|
|
|
75 |
root |
1.1 |
=back |
76 |
|
|
|
77 |
root |
1.3 |
=head2 AnyEvent::XSThreadPool REQUEST TYPES DEFINED BY THIS MODULE |
78 |
|
|
|
79 |
|
|
This module defines only two rather lame request types, which can nevertheless be used to |
80 |
|
|
test out the module. |
81 |
|
|
|
82 |
|
|
=over 4 |
83 |
|
|
|
84 |
|
|
=item AnyEvent::XSThreadPool::sleep $seconds, $callback->() |
85 |
|
|
|
86 |
|
|
This request type simply puts the worker thread to sleep for the given |
87 |
|
|
number of (fractional) seconds. This can be used to simulate a very slow |
88 |
|
|
request, without actually putting stress on the machine. |
89 |
|
|
|
90 |
|
|
Example: execute two 1.2 second sleeps and wait for them. Normally this |
91 |
|
|
should take only about 1.2 seconds overall, as the sleeps execute in |
92 |
|
|
parallel. |
93 |
|
|
|
94 |
|
|
AnyEvent::XSThreadPool::cpupool->req (AnyEvent::XSThreadPool::sleep, 1.2, my $cv1 = AE::cv); |
95 |
|
|
AnyEvent::XSThreadPool::cpupool->req (AnyEvent::XSThreadPool::sleep, 1.2, my $cv2 = AE::cv); |
96 |
|
|
$cv1->recv; |
97 |
|
|
$cv2->recv; |
98 |
|
|
|
99 |
|
|
=item AnyEvent::XSThreadPool::burn $seconds, $callback->($iterations) |
100 |
|
|
|
101 |
|
|
This request type burns cpu cycles by calling C<gettimeofday> (or an |
102 |
|
|
equivalent function) in a loop, for the given number of (fractional) |
103 |
|
|
seconds, counting how many times it looped. |
104 |
|
|
|
105 |
|
|
This can be used to simulate actual cpu load on your machine, or it could |
106 |
|
|
be used as a primitive benchmark (although this is not advised). |
107 |
|
|
|
108 |
|
|
Example: see how often the module can call gettimeofday within 0.1 seconds. |
109 |
|
|
|
110 |
|
|
my $cv = AE::cv; |
111 |
|
|
AnyEvent::XSThreadPool::cpupool->req (AnyEvent::XSThreadPool::burn => 0.1, sub { |
112 |
|
|
printf "executed %d cycles in 0.1s\n", $_[0]; |
113 |
|
|
$cv->(); |
114 |
|
|
}); |
115 |
|
|
$cv->recv; |
116 |
|
|
|
117 |
|
|
# outputs, on my current development box: |
118 |
|
|
# executed 3900480 cycles in 0.1s |
119 |
|
|
|
120 |
|
|
=back |
121 |
|
|
|
122 |
root |
1.1 |
=head1 AUTHOR |
123 |
|
|
|
124 |
|
|
Marc Lehmann <schmorp@schmorp.de> |
125 |
|
|
http://software.schmorp.de/pkg/AnyEvent-XSThreadPool.html |
126 |
|
|
|
127 |
|
|
=cut |
128 |
|
|
|
129 |
|
|
1 |
130 |
|
|
|