ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/State.pm
Revision: 1.46
Committed: Tue Aug 30 21:32:17 2005 UTC (18 years, 9 months ago) by root
Branch: MAIN
CVS Tags: rel-1_4, rel-1_31
Changes since 1.45: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

# Content
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 (4-16k) be
42 allocated. On systems supporting mmap a 128k stack is allocated, on the
43 assumption that the OS has on-demand virtual memory. All this is very
44 system-dependent. On my i686-pc-linux-gnu system this amounts to about 10k
45 per coroutine, 5k when the experimental context sharing is enabled.
46
47 =head2 FUNCTIONS
48
49 =over 4
50
51 =cut
52
53 package Coro::State;
54
55 BEGIN { eval { require warnings } && warnings->unimport ("uninitialized") }
56
57 BEGIN {
58 $VERSION = 1.31;
59
60 require DynaLoader;
61 push @ISA, 'DynaLoader';
62 bootstrap Coro::State $VERSION;
63 }
64
65 use base 'Exporter';
66
67 @EXPORT_OK = qw(SAVE_DEFAV SAVE_DEFSV SAVE_ERRSV SAVE_CURPM SAVE_CCTXT);
68
69 =item $coro = new [$coderef] [, @args...]
70
71 Create a new coroutine and return it. The first C<transfer> call to this
72 coroutine will start execution at the given coderef. If the subroutine
73 returns it will be executed again.
74
75 If the coderef is omitted this function will create a new "empty"
76 coroutine, i.e. a coroutine that cannot be transfered to but can be used
77 to save the current coroutine in.
78
79 =cut
80
81 # this is called (or rather: goto'ed) for each and every
82 # new coroutine. IT MUST NEVER RETURN!
83 sub initialize {
84 my $proc = shift;
85 eval {
86 &$proc while 1;
87 };
88 if ($@) {
89 print STDERR "FATAL: uncaught exception\n$@";
90 }
91 _exit 255;
92 }
93
94 sub new {
95 my $class = shift;
96 my $proc = shift || sub { die "tried to transfer to an empty coroutine" };
97 bless _newprocess [$proc, @_], $class;
98 }
99
100 =item $prev->transfer ($next, $flags)
101
102 Save the state of the current subroutine in C<$prev> and switch to the
103 coroutine saved in C<$next>.
104
105 The "state" of a subroutine includes the scope, i.e. lexical variables and
106 the current execution state (subroutine, stack). The C<$flags> value can
107 be used to specify that additional state to be saved (and later restored), by
108 oring the following constants together:
109
110 Constant Effect
111 SAVE_DEFAV save/restore @_
112 SAVE_DEFSV save/restore $_
113 SAVE_ERRSV save/restore $@
114 SAVE_CCTXT save/restore C-stack (you usually want this for coroutines)
115
116 These constants are not exported by default. If you don't need any extra
117 additional state saved, use C<0> as the flags value.
118
119 If you feel that something important is missing then tell me. Also
120 remember that every function call that might call C<transfer> (such
121 as C<Coro::Channel::put>) might clobber any global and/or special
122 variables. Yes, this is by design ;) You can always create your own
123 process abstraction model that saves these variables.
124
125 The easiest way to do this is to create your own scheduling primitive like
126 this:
127
128 sub schedule {
129 local ($_, $@, ...);
130 $old->transfer ($new);
131 }
132
133 IMPLEMENTORS NOTE: all Coro::State functions/methods expect either the
134 usual Coro::State object or a hashref with a key named "_coro_state" that
135 contains the real Coro::State object. That is, you can do:
136
137 $obj->{_coro_state} = new Coro::State ...;
138 Coro::State::transfer (..., $obj);
139
140 This exists mainly to ease subclassing (wether through @ISA or not).
141
142 =cut
143
144 1;
145
146 =back
147
148 =head1 BUGS
149
150 This module has not yet been extensively tested, but works on most
151 platforms. Expect segfaults and memleaks (but please don't be surprised if
152 it works...)
153
154 This module is not thread-safe. You must only ever use this module from
155 the same thread (this requirement might be loosened in the future).
156
157 =head1 SEE ALSO
158
159 L<Coro>.
160
161 =head1 AUTHOR
162
163 Marc Lehmann <schmorp@schmorp.de>
164 http://home.schmorp.de/
165
166 =cut
167