ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/State.pm
Revision: 1.35
Committed: Fri Feb 13 23:17:41 2004 UTC (20 years, 4 months ago) by pcg
Branch: MAIN
Changes since 1.34: +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 this, only voluntary switching is used so locking problems are
29 greatly reduced.
30
31 This module provides only low-level functionality. See L<Coro> and related
32 modules for a more useful process abstraction including scheduling.
33
34 =head2 MEMORY CONSUMPTION
35
36 A newly created coroutine that has not been used only allocates a
37 relatively small (a few hundred bytes) structure. Only on the first
38 C<transfer> will perl stacks (a few k) and optionally C stack (4-16k) be
39 allocated. On systems supporting mmap a 128k stack is allocated, on the
40 assumption that the OS has on-demand virtual memory. All this is very
41 system-dependent. On my i686-pc-linux-gnu system this amounts to about 10k
42 per coroutine, 5k when the experimental context sharing is enabled.
43
44 =over 4
45
46 =cut
47
48 package Coro::State;
49
50 BEGIN { eval { require warnings } && warnings->unimport ("uninitialized") }
51
52 BEGIN {
53 $VERSION = 0.95;
54
55 require DynaLoader;
56 push @ISA, 'DynaLoader';
57 bootstrap Coro::State $VERSION;
58 }
59
60 use base 'Exporter';
61
62 @EXPORT_OK = qw(SAVE_DEFAV SAVE_DEFSV SAVE_ERRSV SAVE_CURPM SAVE_CCTXT);
63
64 =item $coro = new [$coderef] [, @args...]
65
66 Create a new coroutine and return it. The first C<transfer> call to this
67 coroutine will start execution at the given coderef. If the subroutine
68 returns it will be executed again.
69
70 If the coderef is omitted this function will create a new "empty"
71 coroutine, i.e. a coroutine that cannot be transfered to but can be used
72 to save the current coroutine in.
73
74 =cut
75
76 # this is called (or rather: goto'ed) for each and every
77 # new coroutine. IT MUST NEVER RETURN and should not call
78 # anything that changes the stacklevel (like eval).
79 sub initialize {
80 my $proc = shift;
81 eval {
82 &$proc while 1;
83 };
84 if ($@) {
85 print STDERR "FATAL: uncaught exception\n$@";
86 }
87 _exit 255;
88 }
89
90 sub new {
91 my $class = shift;
92 my $proc = shift || sub { die "tried to transfer to an empty coroutine" };
93 bless _newprocess [$proc, @_], $class;
94 }
95
96 =item $prev->transfer($next,$flags)
97
98 Save the state of the current subroutine in C<$prev> and switch to the
99 coroutine saved in C<$next>.
100
101 The "state" of a subroutine includes the scope, i.e. lexical variables and
102 the current execution state (subroutine, stack). The C<$flags> value can
103 be used to specify that additional state be saved (and later restored), by
104 C<||>-ing the following constants together:
105
106 Constant Effect
107 SAVE_DEFAV save/restore @_
108 SAVE_DEFSV save/restore $_
109 SAVE_ERRSV save/restore $@
110 SAVE_CCTXT save/restore C-stack (you usually want this)
111
112 These constants are not exported by default. If you don't need any extra
113 additional state saved use C<0> as the flags value.
114
115 If you feel that something important is missing then tell me. Also
116 remember that every function call that might call C<transfer> (such
117 as C<Coro::Channel::put>) might clobber any global and/or special
118 variables. Yes, this is by design ;) You can always create your own
119 process abstraction model that saves these variables.
120
121 The easiest way to do this is to create your own scheduling primitive like
122 this:
123
124 sub schedule {
125 local ($_, $@, ...);
126 $old->transfer($new);
127 }
128
129 IMPLEMENTORS NOTE: all Coro::State functions/methods expect either the
130 usual Coro::State object or a hashref with a key named "_coro_state" that
131 contains the real Coro::State object. That is, you can do:
132
133 $obj->{_coro_state} = new Coro::State ...;
134 Coro::State::transfer(..., $obj);
135
136 This exists mainly to ease subclassing (wether through @ISA or not).
137
138 =cut
139
140 =item Coro::State::flush
141
142 To be efficient (actually, to not be abysmaly slow), this module does
143 some fair amount of caching (a possibly complex structure for every
144 subroutine in use). If you don't use coroutines anymore or you want to
145 reclaim some memory then you can call this function which will flush all
146 internal caches. The caches will be rebuilt when needed so this is a safe
147 operation.
148
149 =cut
150
151 1;
152
153 =back
154
155 =head1 BUGS
156
157 This module has not yet been extensively tested. Expect segfaults and
158 specially memleaks.
159
160 This module is not thread-safe. You must only ever use this module from
161 the same thread (this requirenmnt might be loosened in the future).
162
163 =head1 SEE ALSO
164
165 L<Coro>.
166
167 =head1 AUTHOR
168
169 Marc Lehmann <pcg@goof.com>
170 http://www.goof.com/pcg/marc/
171
172 =cut
173