| 1 |
NAME |
| 2 |
Coro::Multicore - make coro threads on multiple cores with specially |
| 3 |
supported modules |
| 4 |
|
| 5 |
SYNOPSIS |
| 6 |
use Coro::Multicore; |
| 7 |
|
| 8 |
# or, if you want it disabled by default (e.g. to use it from a module) |
| 9 |
use Coro::Multicore (); |
| 10 |
|
| 11 |
DESCRIPTION |
| 12 |
EXPERIMENTAL WARNING: This module is in its early stages of development. |
| 13 |
It's fine to try out, but it didn't receive the normal amount of testing |
| 14 |
and real-world usage that my other modules have gone through. |
| 15 |
|
| 16 |
While Coro threads (unlike ithreads) provide real threads similar to |
| 17 |
pthreads, python threads and so on, they do not run in parallel to each |
| 18 |
other even on machines with multiple CPUs or multiple CPU cores. |
| 19 |
|
| 20 |
This module lifts this restriction under two very specific but useful |
| 21 |
conditions: firstly, the coro thread executes in XS code and does not |
| 22 |
touch any perl data structures, and secondly, the XS code is specially |
| 23 |
prepared to allow this. |
| 24 |
|
| 25 |
This means that, when you call an XS function of a module prepared for |
| 26 |
it, this XS function can execute in parallel to any other Coro threads. |
| 27 |
|
| 28 |
The mechanism to support this is easily added to existing modules and is |
| 29 |
independent of Coro or Coro::Multicore, and therefore could be used, |
| 30 |
without changes, with other, similar, modules, or even the perl core, |
| 31 |
should it gain real thread support anytime soon. See |
| 32 |
<http://perlmulticore.schmorp.de/> for more info on how to prepare a |
| 33 |
module to allow parallel execution. Preparing an existing module is |
| 34 |
easy, doesn't add much overhead and no dependencies. |
| 35 |
|
| 36 |
This module is an AnyEvent user (and also, if not obvious, uses Coro). |
| 37 |
|
| 38 |
HOW TO USE IT |
| 39 |
It could hardly be simpler - if you use coro threads, and before you |
| 40 |
call a supported lengthy operation implemented in XS, use this module |
| 41 |
and other 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 *load* it before you can take advantage of it. |
| 47 |
|
| 48 |
EXPORTS |
| 49 |
This module does not (at the moment) export any symbols. It does, |
| 50 |
however, export "behaviour" - if you use the default import, then |
| 51 |
Coro::Multicore will be enabled for all threads and all callers in the |
| 52 |
whole program: |
| 53 |
|
| 54 |
use Coro::Multicore; |
| 55 |
|
| 56 |
In a module where you don't control what else might be loaded and run, |
| 57 |
you might want to be more conservative, and not import anything. This |
| 58 |
has the effect of not enabling the functionality by default, so you have |
| 59 |
to enable 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 |
# called from this function, tasks will be executed asynchronously. |
| 68 |
} |
| 69 |
|
| 70 |
API FUNCTIONS |
| 71 |
$previous = Coro::Multicore::enable [$enable] |
| 72 |
This function enables (if $enable is true) or disables (if $enable |
| 73 |
is false) the multicore functionality globally. By default, it is |
| 74 |
enabled. |
| 75 |
|
| 76 |
This can be used to effectively disable this module's functionality |
| 77 |
by default, and enable it only for selected threads or scopes, by |
| 78 |
calling "Coro::Multicore::scope_enable". |
| 79 |
|
| 80 |
The function returns the previous value of the enable flag. |
| 81 |
|
| 82 |
Coro::Multicore::scoped_enable |
| 83 |
This function instructs Coro::Multicore to handle all requests |
| 84 |
executed in the current coro thread, from the call to the end of the |
| 85 |
current scope. |
| 86 |
|
| 87 |
Calls to "scoped_enable" and "scoped_disable" don't nest very well |
| 88 |
at the moment, so don't nest them. |
| 89 |
|
| 90 |
Coro::Multicore::scoped_disable |
| 91 |
The opposite of "Coro::Multicore::scope_disable": instructs |
| 92 |
Coro::Multicore to *not* handle the next multicore-enabled request. |
| 93 |
|
| 94 |
INTERACTION WITH OTHER SOFTWARE |
| 95 |
This module is very similar to other environments where perl |
| 96 |
interpreters are moved between threads, such as mod_perl2, and the same |
| 97 |
caveats apply. |
| 98 |
|
| 99 |
I want to spell out the most important ones: |
| 100 |
|
| 101 |
pthreads usage |
| 102 |
Any creation of pthreads make it impossible to fork portably from a |
| 103 |
perl program, as forking from within a threaded program will leave |
| 104 |
the program in a state similar to a signal handler. While it might |
| 105 |
work on some platforms (as an extension), this might also result in |
| 106 |
silent data corruption. It also seems to work most of the time, so |
| 107 |
it's hard to test for this. |
| 108 |
|
| 109 |
I recommend using something like AnyEvent::Fork, which can create |
| 110 |
subprocesses safely (via Proc::FastSpawn). |
| 111 |
|
| 112 |
Similar issues exist for signal handlers, although this module works |
| 113 |
hard to keep safe perl signals safe. |
| 114 |
|
| 115 |
module support |
| 116 |
This module moves the same perl interpreter between different |
| 117 |
threads. Some modules might get confused by that (although this can |
| 118 |
usually be considered a bug). This is a rare case though. |
| 119 |
|
| 120 |
event loop reliance |
| 121 |
To be able to wake up programs waiting for results, this module |
| 122 |
relies on an active event loop (via AnyEvent). This is used to |
| 123 |
notify the perl interpreter when the asynchronous task is done. |
| 124 |
|
| 125 |
Since event loops typically fail to work properly after a fork, this |
| 126 |
means that some operations that were formerly working will now hang |
| 127 |
after fork. |
| 128 |
|
| 129 |
A workaround is to call "Coro::Multicore::enable 0" after a fork to |
| 130 |
disable the module. |
| 131 |
|
| 132 |
Future versions of this module might do this automatically. |
| 133 |
|
| 134 |
BUGS |
| 135 |
(OS-) threads are never released |
| 136 |
At the moment, threads that were created once will never be freed. |
| 137 |
They will be reused for asynchronous requests, though, so a slong as |
| 138 |
you limit the maximum number of concurrent asynchronous tasks, this |
| 139 |
will also limit the maximum number of threads created. |
| 140 |
|
| 141 |
Future versions will likely lift this limitation. |
| 142 |
|
| 143 |
AnyEvent is initalised at module load time |
| 144 |
AnyEvent is initialised on module load, as opposed to at a later |
| 145 |
time. |
| 146 |
|
| 147 |
Future versions will likely change this. |
| 148 |
|
| 149 |
AUTHOR |
| 150 |
Marc Lehmann <schmorp@schmorp.de> |
| 151 |
http://software.schmorp.de/pkg/AnyEvent-XSThreadPool.html |
| 152 |
|
| 153 |
Additional thanks to Zsbán Ambrus, who gave considerable desing input |
| 154 |
for this module and the perl multicore specification. |
| 155 |
|