ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/State.pm
Revision: 1.32
Committed: Tue May 27 01:15:31 2003 UTC (21 years ago) by root
Branch: MAIN
Changes since 1.31: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.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 root 1.3 print "in coroutine (called with @_), switching back\n";
11 root 1.1 $new->transfer($main);
12     print "in coroutine again, switching back\n";
13     $new->transfer($main);
14 root 1.3 }, 5;
15 root 1.1
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 root 1.9 =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 root 1.11 per coroutine, 5k when the experimental context sharing is enabled.
43 root 1.9
44 root 1.1 =over 4
45    
46     =cut
47    
48     package Coro::State;
49    
50 root 1.19 no warnings qw(uninitialized);
51 root 1.18
52 root 1.1 BEGIN {
53 root 1.32 $VERSION = 0.7;
54 root 1.1
55     require XSLoader;
56     XSLoader::load Coro::State, $VERSION;
57     }
58    
59 root 1.5 use base 'Exporter';
60    
61 root 1.22 @EXPORT_OK = qw(SAVE_DEFAV SAVE_DEFSV SAVE_ERRSV SAVE_CURPM SAVE_CCTXT);
62 root 1.5
63 root 1.3 =item $coro = new [$coderef] [, @args...]
64 root 1.1
65     Create a new coroutine and return it. The first C<transfer> call to this
66 root 1.10 coroutine will start execution at the given coderef. If the subroutine
67 root 1.1 returns it will be executed again.
68    
69     If the coderef is omitted this function will create a new "empty"
70     coroutine, i.e. a coroutine that cannot be transfered to but can be used
71     to save the current coroutine in.
72    
73     =cut
74    
75 root 1.12 # this is called (or rather: goto'ed) for each and every
76     # new coroutine. IT MUST NEVER RETURN and should not call
77     # anything that changes the stacklevel (like eval).
78 root 1.8 sub initialize {
79 root 1.3 my $proc = shift;
80 root 1.13 eval {
81     &$proc while 1;
82     };
83     if ($@) {
84     print STDERR "FATAL: uncaught exception\n$@";
85     }
86     _exit 255;
87 root 1.3 }
88    
89 root 1.1 sub new {
90 root 1.3 my $class = shift;
91 root 1.4 my $proc = shift || sub { die "tried to transfer to an empty coroutine" };
92 root 1.3 bless _newprocess [$proc, @_], $class;
93 root 1.1 }
94    
95 root 1.10 =item $prev->transfer($next,$flags)
96 root 1.1
97     Save the state of the current subroutine in C<$prev> and switch to the
98     coroutine saved in C<$next>.
99    
100 root 1.5 The "state" of a subroutine includes the scope, i.e. lexical variables and
101 root 1.23 the current execution state (subroutine, stack). The C<$flags> value can
102     be used to specify that additional state be saved (and later restored), by
103     C<||>-ing the following constants together:
104 root 1.5
105 root 1.9 Constant Effect
106     SAVE_DEFAV save/restore @_
107     SAVE_DEFSV save/restore $_
108     SAVE_ERRSV save/restore $@
109     SAVE_CCTXT save/restore C-stack (you usually want this)
110 root 1.5
111 root 1.15 These constants are not exported by default. If you don't need any extra
112     additional state saved use C<0> as the flags value.
113 root 1.2
114 root 1.5 If you feel that something important is missing then tell me. Also
115 root 1.2 remember that every function call that might call C<transfer> (such
116     as C<Coro::Channel::put>) might clobber any global and/or special
117     variables. Yes, this is by design ;) You can always create your own
118     process abstraction model that saves these variables.
119 root 1.1
120 root 1.9 The easiest way to do this is to create your own scheduling primitive like
121     this:
122 root 1.1
123     sub schedule {
124     local ($_, $@, ...);
125     $old->transfer($new);
126     }
127    
128     IMPLEMENTORS NOTE: all Coro::State functions/methods expect either the
129     usual Coro::State object or a hashref with a key named "_coro_state" that
130     contains the real Coro::State object. That is, you can do:
131    
132     $obj->{_coro_state} = new Coro::State ...;
133     Coro::State::transfer(..., $obj);
134    
135     This exists mainly to ease subclassing (wether through @ISA or not).
136    
137     =cut
138    
139 root 1.5 =item Coro::State::flush
140    
141     To be efficient (actually, to not be abysmaly slow), this module does
142     some fair amount of caching (a possibly complex structure for every
143     subroutine in use). If you don't use coroutines anymore or you want to
144     reclaim some memory then you can call this function which will flush all
145     internal caches. The caches will be rebuilt when needed so this is a safe
146     operation.
147    
148     =cut
149    
150 root 1.1 1;
151    
152     =back
153    
154     =head1 BUGS
155    
156     This module has not yet been extensively tested. Expect segfaults and
157     specially memleaks.
158 root 1.5
159     This module is not thread-safe. You must only ever use this module from
160     the same thread (this requirenmnt might be loosened in the future).
161 root 1.1
162     =head1 SEE ALSO
163    
164     L<Coro>.
165    
166     =head1 AUTHOR
167    
168     Marc Lehmann <pcg@goof.com>
169     http://www.goof.com/pcg/marc/
170    
171     =cut
172