1 |
root |
1.1 |
=head1 NAME |
2 |
|
|
|
3 |
|
|
Coro::Multicore - make coro threads on multiple cores with specially supported modules |
4 |
|
|
|
5 |
|
|
=head1 SYNOPSIS |
6 |
|
|
|
7 |
|
|
use Coro::Multicore; |
8 |
|
|
|
9 |
root |
1.5 |
# or, if you want it disabled by default (e.g. to use it from a module) |
10 |
|
|
use Coro::Multicore (); |
11 |
|
|
|
12 |
root |
1.1 |
=head1 DESCRIPTION |
13 |
|
|
|
14 |
root |
1.2 |
While L<Coro> threads (unlike ithreads) provide real threads similar to |
15 |
root |
1.3 |
pthreads, python threads and so on, they do not run in parallel to each |
16 |
root |
1.2 |
other even on machines with multiple CPUs or multiple CPU cores. |
17 |
|
|
|
18 |
|
|
This module lifts this restriction under two very specific but useful |
19 |
|
|
conditions: firstly, the coro thread executes in XS code and does not |
20 |
|
|
touch any perl data structures, and secondly, the XS code is specially |
21 |
|
|
prepared to allow this. |
22 |
|
|
|
23 |
|
|
This means that, when you call an XS function of a module prepared for it, |
24 |
|
|
this XS function can execute in parallel to any other Coro threads. |
25 |
|
|
|
26 |
root |
1.3 |
The mechanism to support this is easily added to existing modules |
27 |
|
|
and is independent of L<Coro> or L<Coro::Multicore>, and therefore |
28 |
|
|
could be used, without changes, with other, similar, modules, or even |
29 |
|
|
the perl core, should it gain real thread support anytime soon. See |
30 |
root |
1.5 |
L<http://perlmulticore.schmorp.de/> for more info on how to prepare a |
31 |
|
|
module to allow parallel execution. Preparing an existing module is easy, |
32 |
|
|
doesn't add much overhead and no dependencies. |
33 |
root |
1.3 |
|
34 |
|
|
This module is an L<AnyEvent> user (and also, if not obvious, uses |
35 |
|
|
L<Coro>). |
36 |
|
|
|
37 |
|
|
=head1 HOW TO USE IT |
38 |
|
|
|
39 |
|
|
It could hardly be simpler - if you use coro threads, and before you call |
40 |
|
|
a supported lengthy operation implemented in XS, use this module and other |
41 |
|
|
coro threads can run in parallel: |
42 |
|
|
|
43 |
|
|
use Coro::Multicore; |
44 |
|
|
|
45 |
|
|
This module has no important API functions to learn or remember. All you |
46 |
|
|
need to do is I<load> it before you can take advantage of it. |
47 |
|
|
|
48 |
root |
1.5 |
=head2 EXPORTS |
49 |
|
|
|
50 |
|
|
This module does not (at the moment) export any symbols. It does, however, |
51 |
|
|
export "behaviour" - if you use the default import, then Coro::Multicore |
52 |
|
|
will be enabled for all threads and all callers in the whole program: |
53 |
|
|
|
54 |
|
|
use Coro::Multicore; |
55 |
|
|
|
56 |
|
|
In a module where you don't control what else might be loaded and run, you |
57 |
|
|
might want to be more conservative, and not import anything. This has the |
58 |
|
|
effect of not enabling the functionality by default, so you have to enable |
59 |
|
|
it per scope: |
60 |
|
|
|
61 |
|
|
use Coro::Multicore (); |
62 |
|
|
|
63 |
|
|
sub myfunc { |
64 |
|
|
Coro::Multicore::scoped_enable; |
65 |
|
|
|
66 |
|
|
# from here to the end of this function, and in any functions |
67 |
|
|
# callsed from this function, tasks will be executed asynchronously. |
68 |
|
|
|
69 |
|
|
} |
70 |
|
|
|
71 |
root |
1.4 |
=head1 API FUNCTIONS |
72 |
root |
1.3 |
|
73 |
root |
1.4 |
=over 4 |
74 |
|
|
|
75 |
|
|
=item $previous = Coro::Multicore::enable [$enable] |
76 |
|
|
|
77 |
|
|
This function enables (if C<$enable> is true) or disables (if C<$enable> |
78 |
|
|
is false) the multicore functionality globally. By default, it is enabled. |
79 |
|
|
|
80 |
|
|
This can be used to effectively disable this module's functionality by |
81 |
|
|
default, and enable it only for selected threads or scopes, by calling |
82 |
|
|
C<Coro::Multicore::scope_enable>. |
83 |
|
|
|
84 |
|
|
The function returns the previous value of the enable flag. |
85 |
root |
1.2 |
|
86 |
root |
1.4 |
=item Coro::Multicore::scoped_enable |
87 |
|
|
|
88 |
|
|
This function instructs Coro::Multicore to handle all requests executed |
89 |
|
|
in the current coro thread, from the call to the end of the current scope. |
90 |
|
|
|
91 |
|
|
Calls to C<scoped_enable> and C<scoped_disable> don't nest very well at |
92 |
|
|
the moment, so don't nest them. |
93 |
|
|
|
94 |
|
|
=item Coro::Multicore::scoped_disable |
95 |
|
|
|
96 |
|
|
The opposite of C<Coro::Multicore::scope_disable>: instructs Coro::Multicore to |
97 |
|
|
I<not> handle the next multicore-enabled request. |
98 |
root |
1.1 |
|
99 |
root |
1.3 |
=back |
100 |
|
|
|
101 |
root |
1.1 |
=cut |
102 |
|
|
|
103 |
|
|
package Coro::Multicore; |
104 |
|
|
|
105 |
|
|
use Coro (); |
106 |
|
|
use AnyEvent (); |
107 |
|
|
|
108 |
|
|
BEGIN { |
109 |
|
|
our $VERSION = 0.02; |
110 |
|
|
|
111 |
|
|
use XSLoader; |
112 |
|
|
XSLoader::load __PACKAGE__, $VERSION; |
113 |
|
|
} |
114 |
|
|
|
115 |
root |
1.5 |
|
116 |
|
|
sub import { |
117 |
|
|
if (@_ > 1) { |
118 |
|
|
require Carp; |
119 |
|
|
Carp::croak ("Coro::Multicore does not export any symbols"); |
120 |
|
|
} |
121 |
|
|
|
122 |
|
|
enable 1; |
123 |
|
|
} |
124 |
|
|
|
125 |
root |
1.1 |
our $WATCHER = AE::io fd, 0, \&poll; |
126 |
|
|
|
127 |
root |
1.5 |
=head1 INTERACTION WITH OTHER SOFTWARE |
128 |
|
|
|
129 |
|
|
TODO |
130 |
|
|
|
131 |
|
|
=head1 BUGS |
132 |
|
|
|
133 |
|
|
At the moment, threads that were created once will never be freed. They |
134 |
|
|
will be reused for asynchronous requests, though, so a slong as you limit |
135 |
|
|
the maximum number of concurrent asynchronous tasks, this will also limit |
136 |
|
|
the maximum number of threads created. |
137 |
|
|
|
138 |
|
|
Future versions will likely lift this limitation. |
139 |
|
|
|
140 |
root |
1.1 |
=head1 AUTHOR |
141 |
|
|
|
142 |
|
|
Marc Lehmann <schmorp@schmorp.de> |
143 |
|
|
http://software.schmorp.de/pkg/AnyEvent-XSThreadPool.html |
144 |
|
|
|
145 |
|
|
=cut |
146 |
|
|
|
147 |
|
|
1 |
148 |
|
|
|