ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro.pm
Revision: 1.2
Committed: Tue Jul 3 03:40:07 2001 UTC (22 years, 11 months ago) by root
Branch: MAIN
Changes since 1.1: +27 -1 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 =head1 NAME
2
3 Coro - create an manage coroutines
4
5 =head1 SYNOPSIS
6
7 use Coro;
8
9 $new = new Coro sub {
10 print "in coroutine, switching back\n";
11 $Coro::main->resume;
12 print "in coroutine again, switching back\n";
13 $Coro::main->resume;
14 };
15
16 print "in main, switching to coroutine\n";
17 $new->resume;
18 print "back in main, switch to coroutine again\n";
19 $new->resume;
20 print "back in main\n";
21
22 =head1 DESCRIPTION
23
24 This module implements coroutines. Coroutines, similar to continuations,
25 allow you to run more than one "thread of execution" in parallel. Unlike
26 threads this, only voluntary switching is used so locking problems are
27 greatly reduced.
28
29 Although this is the "main" module of the Coro family it provides only
30 low-level functionality. See L<Coro::Process> and related modules for a
31 more useful process abstraction including scheduling.
32
33 =over 4
34
35 =cut
36
37 package Coro;
38
39 BEGIN {
40 $VERSION = 0.01;
41
42 require XSLoader;
43 XSLoader::load Coro, $VERSION;
44 }
45
46 =item $main
47
48 This coroutine represents the main program.
49
50 =item $current
51
52 The current coroutine (the last coroutine switched to). The initial value is C<$main> (of course).
53
54 =cut
55
56 $main = $current = _newprocess {
57 # never being called
58 };
59
60 =item $error, $error_msg, $error_coro
61
62 This coroutine will be called on fatal errors. C<$error_msg> and
63 C<$error_coro> return the error message and the error-causing coroutine,
64 respectively.
65
66 =cut
67
68 $error_msg =
69 $error_coro = undef;
70
71 $error = _newprocess {
72 print STDERR "FATAL: $error_msg\nprogram aborted\n";
73 exit 250;
74 };
75
76 =item $coro = new $coderef [, @args]
77
78 Create a new coroutine and return it. The first C<resume> call to this
79 coroutine will start execution at the given coderef. If it returns it
80 should return a coroutine to switch to. If, after returning, the coroutine
81 is C<resume>d again it starts execution again at the givne coderef.
82
83 =cut
84
85 sub new {
86 my $class = $_[0];
87 my $proc = $_[1];
88 bless _newprocess {
89 do {
90 eval { &$proc->resume };
91 if ($@) {
92 ($error_msg, $error_coro) = ($@, $current);
93 $error->resume;
94 }
95 } while ();
96 }, $class;
97 }
98
99 =item $coro->resume
100
101 Resume execution at the given coroutine.
102
103 =cut
104
105 my $prev;
106
107 sub resume {
108 $prev = $current; $current = $_[0];
109 _transfer($prev, $current);
110 }
111
112 1;
113
114 =back
115
116 =head1 BUGS
117
118 This module has not yet been extensively tested.
119
120 =head1 SEE ALSO
121
122 L<Coro::Process>, L<Coro::Signal>.
123
124 =head1 AUTHOR
125
126 Marc Lehmann <pcg@goof.com>
127 http://www.goof.com/pcg/marc/
128
129 =cut
130