ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro.pm
(Generate patch)

Comparing Coro/Coro.pm (file contents):
Revision 1.92 by root, Fri Dec 1 03:47:55 2006 UTC vs.
Revision 1.97 by root, Mon Dec 4 13:47:56 2006 UTC

41 41
42our $idle; # idle handler 42our $idle; # idle handler
43our $main; # main coroutine 43our $main; # main coroutine
44our $current; # current coroutine 44our $current; # current coroutine
45 45
46our $VERSION = '3.0'; 46our $VERSION = '3.01';
47 47
48our @EXPORT = qw(async cede schedule terminate current unblock_sub); 48our @EXPORT = qw(async cede schedule terminate current unblock_sub);
49our %EXPORT_TAGS = ( 49our %EXPORT_TAGS = (
50 prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)], 50 prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)],
51); 51);
52our @EXPORT_OK = @{$EXPORT_TAGS{prio}}; 52our @EXPORT_OK = (@{$EXPORT_TAGS{prio}}, qw(nready));
53 53
54{ 54{
55 my @async; 55 my @async;
56 my $init; 56 my $init;
57 57
58 # this way of handling attributes simply is NOT scalable ;() 58 # this way of handling attributes simply is NOT scalable ;()
59 sub import { 59 sub import {
60 no strict 'refs'; 60 no strict 'refs';
61 61
62 Coro->export_to_level(1, @_); 62 Coro->export_to_level (1, @_);
63 63
64 my $old = *{(caller)[0]."::MODIFY_CODE_ATTRIBUTES"}{CODE}; 64 my $old = *{(caller)[0]."::MODIFY_CODE_ATTRIBUTES"}{CODE};
65 *{(caller)[0]."::MODIFY_CODE_ATTRIBUTES"} = sub { 65 *{(caller)[0]."::MODIFY_CODE_ATTRIBUTES"} = sub {
66 my ($package, $ref) = (shift, shift); 66 my ($package, $ref) = (shift, shift);
67 my @attrs; 67 my @attrs;
105C<Coro::current> function instead. 105C<Coro::current> function instead.
106 106
107=cut 107=cut
108 108
109# maybe some other module used Coro::Specific before... 109# maybe some other module used Coro::Specific before...
110if ($current) {
111 $main->{specific} = $current->{specific}; 110$main->{specific} = $current->{specific}
112} 111 if $current;
113 112
114$current = $main; 113_set_current $main;
115 114
116sub current() { $current } 115sub current() { $current }
117 116
118=item $idle 117=item $idle
119 118
129handlers), then it must be prepared to be called recursively. 128handlers), then it must be prepared to be called recursively.
130 129
131=cut 130=cut
132 131
133$idle = sub { 132$idle = sub {
134 print STDERR "FATAL: deadlock detected\n"; 133 require Carp;
135 exit (51); 134 Carp::croak ("FATAL: deadlock detected");
136}; 135};
137 136
138# this coroutine is necessary because a coroutine 137# this coroutine is necessary because a coroutine
139# cannot destroy itself. 138# cannot destroy itself.
140my @destroy; 139my @destroy;
255 254
256Calling C<exit> in a coroutine will not work correctly, so do not do that. 255Calling C<exit> in a coroutine will not work correctly, so do not do that.
257 256
258=cut 257=cut
259 258
260sub _new_coro { 259sub _run_coro {
261 terminate &{+shift}; 260 terminate &{+shift};
262} 261}
263 262
264sub new { 263sub new {
265 my $class = shift; 264 my $class = shift;
266 265
267 $class->SUPER::new (\&_new_coro, @_) 266 $class->SUPER::new (\&_run_coro, @_)
268} 267}
269 268
270=item $success = $coroutine->ready 269=item $success = $coroutine->ready
271 270
272Put the given coroutine into the ready queue (according to it's priority) 271Put the given coroutine into the ready queue (according to it's priority)
349 $old; 348 $old;
350} 349}
351 350
352=back 351=back
353 352
354=head2 UTILITY FUNCTIONS 353=head2 GLOBAL FUNCTIONS
355 354
356=over 4 355=over 4
356
357=item Coro::nready
358
359Returns the number of coroutines that are currently in the ready state,
360i.e. that can be swicthed to. The value C<0> means that the only runnable
361coroutine is the currently running one, so C<cede> would have no effect,
362and C<schedule> would cause a deadlock unless there is an idle handler
363that wakes up some coroutines.
357 364
358=item unblock_sub { ... } 365=item unblock_sub { ... }
359 366
360This utility function takes a BLOCK or code reference and "unblocks" it, 367This utility function takes a BLOCK or code reference and "unblocks" it,
361returning the new coderef. This means that the new coderef will return 368returning the new coderef. This means that the new coderef will return

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines