ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro.pm
Revision: 1.6
Committed: Tue Jul 10 21:19:47 2001 UTC (22 years, 11 months ago) by root
Branch: MAIN
Changes since 1.5: +42 -52 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 =head1 NAME
2
3 Coro - create and manage simple coroutines
4
5 =head1 SYNOPSIS
6
7 use Coro;
8
9 $new = new Coro sub {
10 print "in coroutine, switching back\n";
11 $new->transfer($main);
12 print "in coroutine again, switching back\n";
13 $new->transfer($main);
14 };
15
16 $main = new Coro;
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 Although this is the "main" module of the Coro family it provides only
32 low-level functionality. See L<Coro::Process> and related modules for a
33 more useful process abstraction including scheduling.
34
35 =over 4
36
37 =cut
38
39 package Coro;
40
41 BEGIN {
42 $VERSION = 0.03;
43
44 require XSLoader;
45 XSLoader::load Coro, $VERSION;
46 }
47
48 =item $coro = new [$coderef [, @args]]
49
50 Create a new coroutine and return it. The first C<transfer> call to this
51 coroutine will start execution at the given coderef. If, the subroutine
52 returns it will be executed again.
53
54 If the coderef is omitted this function will create a new "empty"
55 coroutine, i.e. a coroutine that cannot be transfered to but can be used
56 to save the current coroutine in.
57
58 =cut
59
60 sub new {
61 my $class = $_[0];
62 my $proc = $_[1] || sub { die "tried to transfer to an empty coroutine" };
63 bless _newprocess {
64 do {
65 eval { &$proc };
66 if ($@) {
67 $error_msg = $@;
68 $error_coro = _newprocess { };
69 &transfer($error_coro, $error);
70 }
71 } while (1);
72 }, $class;
73 }
74
75 =item $prev->transfer($next)
76
77 Save the state of the current subroutine in $prev and switch to the
78 coroutine saved in $next.
79
80 =cut
81
82 # I call the _transfer function from a perl function
83 # because that way perl saves all important things on
84 # the stack.
85 sub transfer {
86 _transfer($_[0], $_[1]);
87 }
88
89 =item $error, $error_msg, $error_coro
90
91 This coroutine will be called on fatal errors. C<$error_msg> and
92 C<$error_coro> return the error message and the error-causing coroutine
93 (NOT an object) respectively. This API might change.
94
95 =cut
96
97 $error_msg =
98 $error_coro = undef;
99
100 $error = _newprocess {
101 print STDERR "FATAL: $error_msg\nprogram aborted\n";
102 exit 50;
103 };
104
105 1;
106
107 =back
108
109 =head1 BUGS
110
111 This module has not yet been extensively tested.
112
113 =head1 SEE ALSO
114
115 L<Coro::Process>, L<Coro::Signal>.
116
117 =head1 AUTHOR
118
119 Marc Lehmann <pcg@goof.com>
120 http://www.goof.com/pcg/marc/
121
122 =cut
123