ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro.pm
Revision: 1.1
Committed: Tue Jul 3 02:53:34 2001 UTC (22 years, 11 months ago) by root
Branch: MAIN
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 =head1 DESCRIPTION
10
11 =over 4
12
13 =cut
14
15 package Coro;
16
17 BEGIN {
18 $VERSION = 0.01;
19
20 require XSLoader;
21 XSLoader::load Coro, $VERSION;
22 }
23
24 =item $main
25
26 This coroutine represents the main program.
27
28 =item $current
29
30 The current coroutine (the last coroutine switched to). The initial value is C<$main> (of course).
31
32 =cut
33
34 $main = $current = _newprocess {
35 # never being called
36 };
37
38 =item $error, $error_msg, $error_coro
39
40 This coroutine will be called on fatal errors. C<$error_msg> and
41 C<$error_coro> return the error message and the error-causing coroutine,
42 respectively.
43
44 =cut
45
46 $error_msg =
47 $error_coro = undef;
48
49 $error = _newprocess {
50 print STDERR "FATAL: $error_msg, program aborted\n";
51 exit 250;
52 };
53
54 =item $coro = new $coderef [, @args]
55
56 Create a new coroutine and return it. The first C<resume> call to this
57 coroutine will start execution at the given coderef. If it returns it
58 should return a coroutine to switch to. If, after returning, the coroutine
59 is C<resume>d again it starts execution again at the givne coderef.
60
61 =cut
62
63 sub new {
64 my $class = $_[0];
65 my $proc = $_[1];
66 bless _newprocess {
67 do {
68 eval { &$proc->resume };
69 if ($@) {
70 ($error_msg, $error_coro) = ($@, $current);
71 $error->resume;
72 }
73 } while ();
74 }, $class;
75 }
76
77 =item $coro->resume
78
79 Resume execution at the given coroutine.
80
81 =cut
82
83 my $prev;
84
85 sub resume {
86 $prev = $current; $current = $_[0];
87 _transfer($prev, $current);
88 }
89
90 1;
91
92 =back
93
94 =head1 BUGS
95
96 This module has not yet been extensively tested.
97
98 =head1 AUTHOR
99
100 Marc Lehmann <pcg@goof.com>
101 http://www.goof.com/pcg/marc/
102
103 =cut
104