… | |
… | |
6 | |
6 | |
7 | #ifndef PERL_MULTICORE_H |
7 | #ifndef PERL_MULTICORE_H |
8 | #define PERL_MULTICORE_H |
8 | #define PERL_MULTICORE_H |
9 | |
9 | |
10 | /* |
10 | /* |
|
|
11 | |
11 | =head1 NAME |
12 | =head1 NAME |
12 | |
13 | |
13 | perlmulticore.h - release the perl interpreter for other uses while doing hard work |
14 | perlmulticore.h - the Perl Multicore Specification and Implementation |
14 | |
15 | |
15 | =head1 SYNOPSIS |
16 | =head1 SYNOPSIS |
16 | |
17 | |
17 | #include "perlmultiore.h" |
18 | #include "perlmultiore.h" |
18 | |
19 | |
… | |
… | |
33 | applicability. |
34 | applicability. |
34 | |
35 | |
35 | The newest version of this document can be found at |
36 | The newest version of this document can be found at |
36 | L<http://pod.tst.eu/http://cvs.schmorp.de/Coro-Multicore/perlmulticore.h>. |
37 | L<http://pod.tst.eu/http://cvs.schmorp.de/Coro-Multicore/perlmulticore.h>. |
37 | |
38 | |
38 | The nwest version of the header fgile itself, which |
39 | The newest version of the header file itself, which |
39 | includes this documentation, can be downloaded from |
40 | includes this documentation, can be downloaded from |
40 | L<http://cvs.schmorp.de/Coro-Multicore/perlmulticore.h>. |
41 | L<http://cvs.schmorp.de/Coro-Multicore/perlmulticore.h>. |
41 | |
42 | |
42 | =head1 HOW DO I USE THIS IN MY MODULES? |
43 | =head1 HOW DO I USE THIS IN MY MODULES? |
43 | |
44 | |
… | |
… | |
117 | if (res) |
118 | if (res) |
118 | { |
119 | { |
119 | // error, assume lock is held by another process and do it the slow way |
120 | // error, assume lock is held by another process and do it the slow way |
120 | perlinterp_release (); |
121 | perlinterp_release (); |
121 | res = fcntl (fd, F_SETLKW, &flock); |
122 | res = fcntl (fd, F_SETLKW, &flock); |
122 | perlinterp_release (); |
123 | perlinterp_acquire (); |
123 | } |
124 | } |
124 | |
125 | |
125 | =head1 THE HARD AND FAST RULES |
126 | =head1 THE HARD AND FAST RULES |
126 | |
127 | |
127 | As with everything, there are a number of rules to follow. |
128 | As with everything, there are a number of rules to follow. |
… | |
… | |
197 | pthread_mutex_lock (&my_mutex); |
198 | pthread_mutex_lock (&my_mutex); |
198 | do_your_non_thread_safe_thing (); |
199 | do_your_non_thread_safe_thing (); |
199 | pthread_mutex_unlock (&my_mutex); |
200 | pthread_mutex_unlock (&my_mutex); |
200 | perlinterp_acquire (); |
201 | perlinterp_acquire (); |
201 | |
202 | |
202 | This isn't as trivial as it looks though, as you need to find out which |
|
|
203 | threading system is in use (with L<Coro::Multicore>, it currently is |
|
|
204 | always pthreads). |
|
|
205 | |
|
|
206 | =item I<Don't> get confused by having to release first. |
203 | =item I<Don't> get confused by having to release first. |
207 | |
204 | |
208 | In many real world scenarios, you acquire a resource, do something, then |
205 | In many real world scenarios, you acquire a resource, do something, then |
209 | release it again. Don't let this confuse you, with this, you already own |
206 | release it again. Don't let this confuse you, with this, you already own |
210 | the resource (the perl interpreter) so you have to I<release> first, and |
207 | the resource (the perl interpreter) so you have to I<release> first, and |
… | |
… | |
255 | first are two memory accesses and a predictable function call of an empty |
252 | first are two memory accesses and a predictable function call of an empty |
256 | function. |
253 | function. |
257 | |
254 | |
258 | Of course, the overhead is much higher when these functions actually |
255 | Of course, the overhead is much higher when these functions actually |
259 | implement anything useful, but you always get what you pay for. |
256 | implement anything useful, but you always get what you pay for. |
|
|
257 | |
|
|
258 | With L<Coro::Multicore>, every release/acquire involves two pthread |
|
|
259 | switches, two coro thread switches, a bunch of syscalls, and sometimes |
|
|
260 | interacting with the event loop. |
|
|
261 | |
|
|
262 | A dedicated thread pool such as the one L<IO::AIO> uses could reduce |
|
|
263 | these overheads, and would also reduce the dependencies (L<AnyEvent> is a |
|
|
264 | smaller and more portable dependency than L<Coro>), but it would require a |
|
|
265 | lot more work on the side of the module author wanting to support it than |
|
|
266 | this solution. |
260 | |
267 | |
261 | =item Low Code and Data Size Overhead |
268 | =item Low Code and Data Size Overhead |
262 | |
269 | |
263 | On a 64 bit system, F<perlmulticore.h> uses exactly C<8> octets (one |
270 | On a 64 bit system, F<perlmulticore.h> uses exactly C<8> octets (one |
264 | pointer) of your data segment, to store the C<perl_multicore_api> |
271 | pointer) of your data segment, to store the C<perl_multicore_api> |
… | |
… | |
293 | efficient when not needed, low code and data size overhead and broad |
300 | efficient when not needed, low code and data size overhead and broad |
294 | applicability. |
301 | applicability. |
295 | |
302 | |
296 | =back |
303 | =back |
297 | |
304 | |
|
|
305 | |
|
|
306 | =head1 DISABLING PERL MULTICORE AT COMPILE TIME |
|
|
307 | |
|
|
308 | You can disable the complete perl multicore API by defining the |
|
|
309 | symbol C<PERL_MULTICORE_DISABLE> to C<1> (e.g. by specifying |
|
|
310 | F<-DPERL_MULTICORE_DISABLE> as compiler argument). |
|
|
311 | |
|
|
312 | This will leave no traces of the API in the compiled code, suitable |
|
|
313 | "empty" C<perl_release> and C<perl_acquire> definitions will be provided. |
|
|
314 | |
|
|
315 | This could be added to perl's C<CPPFLAGS> when configuring perl on |
|
|
316 | platforms that do not support threading at all for example. |
|
|
317 | |
|
|
318 | |
298 | =head1 AUTHOR |
319 | =head1 AUTHOR |
299 | |
320 | |
300 | Marc A. Lehmann <perlmulticore@schmorp.de> |
321 | Marc A. Lehmann <perlmulticore@schmorp.de> |
|
|
322 | http://perlmulticore.schmorp.de/ |
301 | |
323 | |
302 | =head1 LICENSE |
324 | =head1 LICENSE |
303 | |
325 | |
304 | The F<perlmulticore.h> is put into the public domain. Where this is legally |
326 | The F<perlmulticore.h> header file is put into the public |
|
|
327 | domain. Where this is legally not possible, or at your |
305 | not possible, or at your option, it can be licensed under creativecommons |
328 | option, it can be licensed under creativecommons CC0 |
306 | CC0 license: L<https://creativecommons.org/publicdomain/zero/1.0/>. |
329 | license: L<https://creativecommons.org/publicdomain/zero/1.0/>. |
307 | |
330 | |
308 | =cut |
331 | =cut |
|
|
332 | |
309 | */ |
333 | */ |
310 | |
334 | |
|
|
335 | #define PERL_MULTICORE_MAJOR 1 /* bumped on incompatible changes */ |
|
|
336 | #define PERL_MULTICORE_MINOR 0 /* bumped on every change */ |
|
|
337 | |
|
|
338 | #if PERL_MULTICORE_DISABLE |
|
|
339 | |
|
|
340 | #define perlinterp_release() do { } while (0) |
|
|
341 | #define perlinterp_acquire() do { } while (0) |
|
|
342 | |
|
|
343 | #else |
|
|
344 | |
|
|
345 | /* this struct is shared between all modules, and currently */ |
|
|
346 | /* contain only the two function pointers for release/acquire */ |
311 | struct perl_multicore_api |
347 | struct perl_multicore_api |
312 | { |
348 | { |
313 | void (*pmapi_release)(void); |
349 | void (*pmapi_release)(void); |
314 | void (*pmapi_acquire)(void); |
350 | void (*pmapi_acquire)(void); |
315 | }; |
351 | }; |
… | |
… | |
322 | = (struct perl_multicore_api *)&perl_multicore_api_init; |
358 | = (struct perl_multicore_api *)&perl_multicore_api_init; |
323 | |
359 | |
324 | #define perlinterp_release() perl_multicore_api->pmapi_release () |
360 | #define perlinterp_release() perl_multicore_api->pmapi_release () |
325 | #define perlinterp_acquire() perl_multicore_api->pmapi_acquire () |
361 | #define perlinterp_acquire() perl_multicore_api->pmapi_acquire () |
326 | |
362 | |
|
|
363 | /* this is the release/acquire implementation used as fallback */ |
327 | static void |
364 | static void |
328 | perl_multicore_nop (void) |
365 | perl_multicore_nop (void) |
329 | { |
366 | { |
330 | } |
367 | } |
331 | |
368 | |
|
|
369 | /* this is the initial implementation of "release" - it initialises */ |
|
|
370 | /* the api and then calls the real release function */ |
332 | static void |
371 | static void |
333 | perl_multicore_init (void) |
372 | perl_multicore_init (void) |
334 | { |
373 | { |
335 | dTHX; |
374 | dTHX; |
336 | |
375 | |
… | |
… | |
354 | /* call the real (or dummy) implementation now */ |
393 | /* call the real (or dummy) implementation now */ |
355 | perlinterp_release (); |
394 | perlinterp_release (); |
356 | } |
395 | } |
357 | |
396 | |
358 | #endif |
397 | #endif |
|
|
398 | |
|
|
399 | #endif |