ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro-Multicore/Multicore.pm
(Generate patch)

Comparing Coro-Multicore/Multicore.pm (file contents):
Revision 1.4 by root, Sun Jun 28 17:39:42 2015 UTC vs.
Revision 1.5 by root, Mon Jun 29 13:15:53 2015 UTC

3Coro::Multicore - make coro threads on multiple cores with specially supported modules 3Coro::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
11While L<Coro> threads (unlike ithreads) provide real threads similar to 14While L<Coro> threads (unlike ithreads) provide real threads similar to
12pthreads, python threads and so on, they do not run in parallel to each 15pthreads, python threads and so on, they do not run in parallel to each
22 25
23The mechanism to support this is easily added to existing modules 26The mechanism to support this is easily added to existing modules
24and is independent of L<Coro> or L<Coro::Multicore>, and therefore 27and is independent of L<Coro> or L<Coro::Multicore>, and therefore
25could be used, without changes, with other, similar, modules, or even 28could be used, without changes, with other, similar, modules, or even
26the perl core, should it gain real thread support anytime soon. See 29the perl core, should it gain real thread support anytime soon. See
27L<http://pod.tst.eu/http://cvs.schmorp.de/Coro-Multicore/perlmulticore.h> 30L<http://perlmulticore.schmorp.de/> for more info on how to prepare a
28for more info on how to prepare a module to allow parallel execution. 31module to allow parallel execution. Preparing an existing module is easy,
32doesn't add much overhead and no dependencies.
29 33
30This module is an L<AnyEvent> user (and also, if not obvious, uses 34This module is an L<AnyEvent> user (and also, if not obvious, uses
31L<Coro>). 35L<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
41This module has no important API functions to learn or remember. All you 45This module has no important API functions to learn or remember. All you
42need to do is I<load> it before you can take advantage of it. 46need to do is I<load> it before you can take advantage of it.
47
48=head2 EXPORTS
49
50This module does not (at the moment) export any symbols. It does, however,
51export "behaviour" - if you use the default import, then Coro::Multicore
52will be enabled for all threads and all callers in the whole program:
53
54 use Coro::Multicore;
55
56In a module where you don't control what else might be loaded and run, you
57might want to be more conservative, and not import anything. This has the
58effect of not enabling the functionality by default, so you have to enable
59it 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
69The opposite of C<Coro::Multicore::scope_disable>: instructs Coro::Multicore to 96The opposite of C<Coro::Multicore::scope_disable>: instructs Coro::Multicore to
70I<not> handle the next multicore-enabled request. 97I<not> handle the next multicore-enabled request.
71 98
72=item $previous = Coro::Multicore::max_idle_threads [$count]
73
74To run on multiple cores, this module creates I<threads>. Since thread
75creation is costly, it will keep some of them around for future uses. This
76function returns the current maximum number of threads that are being kept
77around (default: C<8>), and can be used to set a new limit, in case you
78have bigger requirements.
79
80Future implementations will also provide a timeout mechanism, for even
81better behaviour.
82
83=back 99=back
84 100
85=cut 101=cut
86 102
87package Coro::Multicore; 103package 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
116sub import {
117 if (@_ > 1) {
118 require Carp;
119 Carp::croak ("Coro::Multicore does not export any symbols");
120 }
121
122 enable 1;
123}
124
99our $WATCHER = AE::io fd, 0, \&poll; 125our $WATCHER = AE::io fd, 0, \&poll;
126
127=head1 INTERACTION WITH OTHER SOFTWARE
128
129TODO
130
131=head1 BUGS
132
133At the moment, threads that were created once will never be freed. They
134will be reused for asynchronous requests, though, so a slong as you limit
135the maximum number of concurrent asynchronous tasks, this will also limit
136the maximum number of threads created.
137
138Future 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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines