ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/Coro/myhttpd/access.pl
(Generate patch)

Comparing cvsroot/Coro/myhttpd/access.pl (file contents):
Revision 1.12 by root, Thu Nov 29 01:53:40 2001 UTC vs.
Revision 1.19 by root, Mon Dec 3 04:57:22 2001 UTC

1package transferqueue; 1package transferqueue;
2 2
3sub new { 3sub new {
4 my $class = shift; 4 my $class = shift;
5 bless { 5 bless {
6 conns => $_[0], 6 slots => $_[0],
7 lastspb => 0,
7 }, $class; 8 }, $class;
8} 9}
9 10
10sub start_transfer { 11sub start_transfer {
11 my $self = shift; 12 my $self = shift;
13 my $size = $_[0];
12 14
13 my $trans = bless [ $self, $Coro::current ], transfer::; 15 my $trans = bless {
14 Scalar::Util::weaken($trans->[0]); 16 queue => $self,
17 time => $::NOW,
18 size => $size,
19 coro => $Coro::current,
20 }, transfer::;
15 21
16 push @{$self->{wait}}, $trans; 22 push @{$self->{wait}}, $trans;
23 Scalar::Util::weaken($self->{wait}[-1]);
17 24
18 if (--$self->{conns} >= 0) {
19 $self->wake_next; 25 $self->wake_next;
20 }
21 26
22 $trans; 27 $trans;
23} 28}
24 29
25sub wake_next { 30sub wake_next {
26 my $self = shift; 31 my $self = shift;
27 32
28 return unless $self->{conns} >= 0; 33 $self->sort;
29 34
30 (pop @{$self->{wait}})->wake if @{$self->{wait}}; 35 while($self->{slots} && @{$self->{wait}}) {
36 my $transfer = shift @{$self->{wait}};
37 if ($transfer) {
38 $self->{lastspb} = $transfer->{spb};
39 $transfer->wake;
40 last;
41 }
42 }
43}
44
45sub sort {
46 $_[0]{wait} = [
47 sort { $b->{spb} <=> $a->{spb} }
48 grep { $_ && ($_->{spb} = ($::NOW-$_->{time})/($_->{size}||1)), $_ }
49 @{$_[0]{wait}}
50 ];
31} 51}
32 52
33sub waiters { 53sub waiters {
34 map $_->[1], @{$_[0]{wait}}; 54 $_[0]->sort;
55 @{$_[0]{wait}};
35} 56}
36 57
37package transfer; 58package transfer;
38 59
39use Coro::Timer (); 60use Coro::Timer ();
40 61
62sub wake {
63 my $self = shift;
64
65 $self->{alloc} = 1;
66 $self->{queue}{slots}--;
67 $self->{wake} and $self->{wake}->ready;
68}
69
41sub try { 70sub try {
42 my $self = shift; 71 my $self = shift;
43 my $timeout = Coro::Timer::timeout $_[0];
44 72
45 Coro::schedule; 73 $self->{alloc} || do {
74 my $timeout = Coro::Timer::timeout $_[0];
75 local $self->{wake} = $self->{coro};
46 76
47 return $self->[2]; 77 Coro::schedule;
48}
49 78
50sub wake { 79 $self->{alloc};
51 my $self = shift; 80 }
52 $self->[2] = 1;
53 $self->[1]->ready;
54} 81}
55 82
56sub DESTROY { 83sub DESTROY {
57 my $self = shift; 84 my $self = shift;
58 $self->[0]{conns}++; 85
86 if ($self->{alloc}) {
87 $self->{queue}{slots}++;
59 $self->[0]->wake_next; 88 $self->{queue}->wake_next;
89 }
60} 90}
61 91
62package conn; 92package conn;
63 93
64our %blockuri; 94our %blockuri;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines