… | |
… | |
3 | Coro::Multicore - make coro threads on multiple cores with specially supported modules |
3 | Coro::Multicore - make coro threads on multiple cores with specially supported modules |
4 | |
4 | |
5 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
6 | |
6 | |
7 | use Coro::Multicore; |
7 | use Coro::Multicore; |
|
|
8 | |
|
|
9 | # or, if you want it disabled by default (e.g. to use it from a module) |
|
|
10 | use Coro::Multicore (); |
8 | |
11 | |
9 | =head1 DESCRIPTION |
12 | =head1 DESCRIPTION |
10 | |
13 | |
11 | While L<Coro> threads (unlike ithreads) provide real threads similar to |
14 | While L<Coro> threads (unlike ithreads) provide real threads similar to |
12 | pthreads, python threads and so on, they do not run in parallel to each |
15 | pthreads, python threads and so on, they do not run in parallel to each |
… | |
… | |
22 | |
25 | |
23 | The mechanism to support this is easily added to existing modules |
26 | The mechanism to support this is easily added to existing modules |
24 | and is independent of L<Coro> or L<Coro::Multicore>, and therefore |
27 | and is independent of L<Coro> or L<Coro::Multicore>, and therefore |
25 | could be used, without changes, with other, similar, modules, or even |
28 | could be used, without changes, with other, similar, modules, or even |
26 | the perl core, should it gain real thread support anytime soon. See |
29 | the perl core, should it gain real thread support anytime soon. See |
27 | L<http://pod.tst.eu/http://cvs.schmorp.de/Coro-Multicore/perlmulticore.h> |
30 | L<http://perlmulticore.schmorp.de/> for more info on how to prepare a |
28 | for more info on how to prepare a module to allow parallel execution. |
31 | module to allow parallel execution. Preparing an existing module is easy, |
|
|
32 | doesn't add much overhead and no dependencies. |
29 | |
33 | |
30 | This module is an L<AnyEvent> user (and also, if not obvious, uses |
34 | This module is an L<AnyEvent> user (and also, if not obvious, uses |
31 | L<Coro>). |
35 | L<Coro>). |
32 | |
36 | |
33 | =head1 HOW TO USE IT |
37 | =head1 HOW TO USE IT |
… | |
… | |
38 | |
42 | |
39 | use Coro::Multicore; |
43 | use Coro::Multicore; |
40 | |
44 | |
41 | This module has no important API functions to learn or remember. All you |
45 | This module has no important API functions to learn or remember. All you |
42 | need to do is I<load> it before you can take advantage of it. |
46 | need to do is I<load> it before you can take advantage of it. |
|
|
47 | |
|
|
48 | =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 | } |
43 | |
70 | |
44 | =head1 API FUNCTIONS |
71 | =head1 API FUNCTIONS |
45 | |
72 | |
46 | =over 4 |
73 | =over 4 |
47 | |
74 | |
… | |
… | |
67 | =item Coro::Multicore::scoped_disable |
94 | =item Coro::Multicore::scoped_disable |
68 | |
95 | |
69 | The opposite of C<Coro::Multicore::scope_disable>: instructs Coro::Multicore to |
96 | The opposite of C<Coro::Multicore::scope_disable>: instructs Coro::Multicore to |
70 | I<not> handle the next multicore-enabled request. |
97 | I<not> handle the next multicore-enabled request. |
71 | |
98 | |
72 | =item $previous = Coro::Multicore::max_idle_threads [$count] |
|
|
73 | |
|
|
74 | To run on multiple cores, this module creates I<threads>. Since thread |
|
|
75 | creation is costly, it will keep some of them around for future uses. This |
|
|
76 | function returns the current maximum number of threads that are being kept |
|
|
77 | around (default: C<8>), and can be used to set a new limit, in case you |
|
|
78 | have bigger requirements. |
|
|
79 | |
|
|
80 | Future implementations will also provide a timeout mechanism, for even |
|
|
81 | better behaviour. |
|
|
82 | |
|
|
83 | =back |
99 | =back |
84 | |
100 | |
85 | =cut |
101 | =cut |
86 | |
102 | |
87 | package Coro::Multicore; |
103 | package Coro::Multicore; |
… | |
… | |
94 | |
110 | |
95 | use XSLoader; |
111 | use XSLoader; |
96 | XSLoader::load __PACKAGE__, $VERSION; |
112 | XSLoader::load __PACKAGE__, $VERSION; |
97 | } |
113 | } |
98 | |
114 | |
|
|
115 | |
|
|
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 | |
99 | our $WATCHER = AE::io fd, 0, \&poll; |
125 | our $WATCHER = AE::io fd, 0, \&poll; |
|
|
126 | |
|
|
127 | =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. |
100 | |
139 | |
101 | =head1 AUTHOR |
140 | =head1 AUTHOR |
102 | |
141 | |
103 | Marc Lehmann <schmorp@schmorp.de> |
142 | Marc Lehmann <schmorp@schmorp.de> |
104 | http://software.schmorp.de/pkg/AnyEvent-XSThreadPool.html |
143 | http://software.schmorp.de/pkg/AnyEvent-XSThreadPool.html |