ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/State.pm
Revision: 1.50
Committed: Mon Dec 26 18:48:11 2005 UTC (18 years, 5 months ago) by root
Branch: MAIN
CVS Tags: rel-1_7
Changes since 1.49: +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 use strict;
56 no warnings "uninitialized";
57
58 use XSLoader;
59
60 BEGIN {
61 our $VERSION = 1.7;
62
63 XSLoader::load __PACKAGE__, $VERSION;
64 }
65
66 use base Exporter::;
67
68 our @EXPORT_OK = qw(SAVE_DEFAV SAVE_DEFSV SAVE_ERRSV SAVE_CURPM SAVE_CCTXT);
69
70 =item $coro = new [$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.
75
76 If the coderef is omitted this function will create a new "empty"
77 coroutine, i.e. a coroutine that cannot be transfered to but can be used
78 to save the current coroutine in.
79
80 =cut
81
82 # this is called (or rather: goto'ed) for each and every
83 # new coroutine. IT MUST NEVER RETURN!
84 sub initialize {
85 my $proc = shift;
86 eval {
87 &$proc while 1;
88 };
89 if ($@) {
90 print STDERR "FATAL: uncaught exception\n$@";
91 }
92 _exit 255;
93 }
94
95 sub new {
96 my $class = shift;
97 my $proc = shift || sub { die "tried to transfer to an empty coroutine" };
98 bless _newprocess [$proc, @_], $class;
99 }
100
101 =item $prev->transfer ($next, $flags)
102
103 Save the state of the current subroutine in C<$prev> and switch to the
104 coroutine saved in C<$next>.
105
106 The "state" of a subroutine includes the scope, i.e. lexical variables and
107 the current execution state (subroutine, stack). The C<$flags> value can
108 be used to specify that additional state to be saved (and later restored), by
109 oring the following constants together:
110
111 Constant Effect
112 SAVE_DEFAV save/restore @_
113 SAVE_DEFSV save/restore $_
114 SAVE_ERRSV save/restore $@
115 SAVE_CCTXT save/restore C-stack (you usually want this for coroutines)
116
117 These constants are not exported by default. If you don't need any extra
118 additional state saved, use C<0> as the flags value.
119
120 If you feel that something important is missing then tell me. Also
121 remember that every function call that might call C<transfer> (such
122 as C<Coro::Channel::put>) might clobber any global and/or special
123 variables. Yes, this is by design ;) You can always create your own
124 process abstraction model that saves these variables.
125
126 The easiest way to do this is to create your own scheduling primitive like
127 this:
128
129 sub schedule {
130 local ($_, $@, ...);
131 $old->transfer ($new);
132 }
133
134 IMPLEMENTORS NOTE: all Coro::State functions/methods expect either the
135 usual Coro::State object or a hashref with a key named "_coro_state" that
136 contains the real Coro::State object. That is, you can do:
137
138 $obj->{_coro_state} = new Coro::State ...;
139 Coro::State::transfer (..., $obj);
140
141 This exists mainly to ease subclassing (wether through @ISA or not).
142
143 =cut
144
145 1;
146
147 =back
148
149 =head1 BUGS
150
151 This module has not yet been extensively tested, but works on most
152 platforms. Expect segfaults and memleaks (but please don't be surprised if
153 it works...)
154
155 This module is not thread-safe. You must only ever use this module from
156 the same thread (this requirement might be loosened in the future).
157
158 =head1 SEE ALSO
159
160 L<Coro>.
161
162 =head1 AUTHOR
163
164 Marc Lehmann <schmorp@schmorp.de>
165 http://home.schmorp.de/
166
167 =cut
168