1 |
=head1 NAME |
2 |
|
3 |
Coro::State - create and manage simple coroutines |
4 |
|
5 |
=head1 SYNOPSIS |
6 |
|
7 |
use Coro::State; |
8 |
|
9 |
$new = new Coro::State sub { |
10 |
print "in coroutine (called with @_), switching back\n"; |
11 |
$new->transfer ($main); |
12 |
print "in coroutine again, switching back\n"; |
13 |
$new->transfer ($main); |
14 |
}, 5; |
15 |
|
16 |
$main = new Coro::State; |
17 |
|
18 |
print "in main, switching to coroutine\n"; |
19 |
$main->transfer ($new); |
20 |
print "back in main, switch to coroutine again\n"; |
21 |
$main->transfer ($new); |
22 |
print "back in main\n"; |
23 |
|
24 |
=head1 DESCRIPTION |
25 |
|
26 |
This module implements coroutines. Coroutines, similar to continuations, |
27 |
allow you to run more than one "thread of execution" in parallel. Unlike |
28 |
threads, there is no parallelism and only voluntary switching is used so |
29 |
locking problems are greatly reduced. |
30 |
|
31 |
This can be used to implement non-local jumps, exception handling, |
32 |
continuations and more. |
33 |
|
34 |
This module provides only low-level functionality. See L<Coro> and related |
35 |
modules for a higher level process abstraction including scheduling. |
36 |
|
37 |
=head2 MEMORY CONSUMPTION |
38 |
|
39 |
A newly created coroutine that has not been used only allocates a |
40 |
relatively small (a few hundred bytes) structure. Only on the first |
41 |
C<transfer> will perl stacks (a few k) and optionally C stack All this |
42 |
is very system-dependent. On my i686-pc-linux-gnu system this amounts |
43 |
to about 10k per coroutine, 5k when the experimental context sharing is |
44 |
enabled. |
45 |
|
46 |
=head2 FUNCTIONS |
47 |
|
48 |
=over 4 |
49 |
|
50 |
=cut |
51 |
|
52 |
package Coro::State; |
53 |
|
54 |
use strict; |
55 |
no warnings "uninitialized"; |
56 |
|
57 |
use XSLoader; |
58 |
|
59 |
BEGIN { |
60 |
our $VERSION = '3.0'; |
61 |
|
62 |
XSLoader::load __PACKAGE__, $VERSION; |
63 |
} |
64 |
|
65 |
use Exporter; |
66 |
use base Exporter::; |
67 |
|
68 |
our @EXPORT_OK = qw(SAVE_DEFAV SAVE_DEFSV SAVE_ERRSV); |
69 |
|
70 |
=item $coro = new Coro::State [$coderef[, @args...]] |
71 |
|
72 |
Create a new coroutine and return it. The first C<transfer> call to this |
73 |
coroutine will start execution at the given coderef. If the subroutine |
74 |
returns it will be executed again. If it throws an exception the program |
75 |
will terminate. |
76 |
|
77 |
Calling C<exit> in a coroutine will not work correctly, so do not do that. |
78 |
|
79 |
If the coderef is omitted this function will create a new "empty" |
80 |
coroutine, i.e. a coroutine that cannot be transfered to but can be used |
81 |
to save the current coroutine in. |
82 |
|
83 |
The returned object is an empty hash which can be used for any purpose |
84 |
whatsoever, for example when subclassing Coro::State. |
85 |
|
86 |
=cut |
87 |
|
88 |
our $_cctx; # holds the coro_cctx pointer |
89 |
|
90 |
# this is called for each newly created C coroutine, |
91 |
# and is being artificially injected into the opcode flow |
92 |
sub _cctx_init { |
93 |
_set_stacklevel $_cctx; |
94 |
} |
95 |
|
96 |
# this is called (or rather: goto'ed) for each and every |
97 |
# new coroutine. IT MUST NEVER RETURN! |
98 |
sub _coro_init { |
99 |
eval { |
100 |
$_[0] or die "transfer() to empty coroutine $_[0]"; |
101 |
&{$_[0]} while 1; |
102 |
}; |
103 |
print STDERR $@ if $@; |
104 |
_exit 55; |
105 |
} |
106 |
|
107 |
=item $prev->transfer ($next, $flags) |
108 |
|
109 |
Save the state of the current subroutine in C<$prev> and switch to the |
110 |
coroutine saved in C<$next>. |
111 |
|
112 |
The "state" of a subroutine includes the scope, i.e. lexical variables and |
113 |
the current execution state (subroutine, stack). The C<$flags> value can |
114 |
be used to specify that additional state to be saved (and later restored), by |
115 |
oring the following constants together: |
116 |
|
117 |
Constant Effect |
118 |
SAVE_DEFAV save/restore @_ |
119 |
SAVE_DEFSV save/restore $_ |
120 |
SAVE_ERRSV save/restore $@ |
121 |
|
122 |
These constants are not exported by default. If you don't need any extra |
123 |
additional state saved, use C<0> as the flags value. |
124 |
|
125 |
If you feel that something important is missing then tell me. Also |
126 |
remember that every function call that might call C<transfer> (such |
127 |
as C<Coro::Channel::put>) might clobber any global and/or special |
128 |
variables. Yes, this is by design ;) You can always create your own |
129 |
process abstraction model that saves these variables. |
130 |
|
131 |
The easiest way to do this is to create your own scheduling primitive like |
132 |
this: |
133 |
|
134 |
sub schedule { |
135 |
local ($_, $@, ...); |
136 |
$old->transfer ($new); |
137 |
} |
138 |
|
139 |
=item Coro::State::cctx_count |
140 |
|
141 |
Returns the number of C-level coroutines allocated. If this number is |
142 |
very high (more than a dozen) it might help to identify points of C-level |
143 |
recursion in your code and moving this into a separate coroutine. |
144 |
|
145 |
=item Coro::State::cctx_idle |
146 |
|
147 |
Returns the number of allocated but idle (free for reuse) C level |
148 |
coroutines. As C level coroutines are curretly rarely being deallocated, a |
149 |
high number means that you used many C coroutines in the past. |
150 |
|
151 |
=cut |
152 |
|
153 |
1; |
154 |
|
155 |
=back |
156 |
|
157 |
=head1 BUGS |
158 |
|
159 |
This module is not thread-safe. You must only ever use this module from |
160 |
the same thread (this requirement might be loosened in the future). |
161 |
|
162 |
=head1 SEE ALSO |
163 |
|
164 |
L<Coro>. |
165 |
|
166 |
=head1 AUTHOR |
167 |
|
168 |
Marc Lehmann <schmorp@schmorp.de> |
169 |
http://home.schmorp.de/ |
170 |
|
171 |
=cut |
172 |
|