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.17 by root, Sat Dec 1 01:09:56 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 ], transfer::; 15 my $trans = bless {
16 queue => $self,
17 time => $::NOW,
18 size => $size,
19 coro => $Coro::current,
20 }, transfer::;
14 21
15 push @{$self->{wait}}, $trans; 22 push @{$self->{wait}}, $trans;
16 Scalar::Util::weaken($self->{wait}[-1]); 23 Scalar::Util::weaken($self->{wait}[-1]);
17 24
18 print "ALLOC $Coro::current\n";#d#
19 --$self->{conns};
20 $self->wake_next; 25 $self->wake_next;
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
33 $self->sort;
34
28 while ($self->{conns} >= 0 && @{$self->{wait}}) { 35 while($self->{slots} && @{$self->{wait}}) {
29 print "WAKING some\n";#d#
30 while(@{$self->{wait}}) {
31 my $transfer = shift @{$self->{wait}}; 36 my $transfer = shift @{$self->{wait}};
32 if ($transfer) { 37 if ($transfer) {
33 print "WAKING $transfer\n";#d# 38 $self->{lastspb} = $transfer->{spb};
34 $transfer->wake; 39 $transfer->wake;
35 last; 40 last;
36 }
37 } 41 }
38 } 42 }
39} 43}
40 44
45sub sort {
46 $_[0]{wait} = [
47 sort { $b->{spb} <=> $a->{spb} }
48 grep { $_ && ($_->{spb} = ($::NOW-$_->{time})/($_->{size}||1)), $_ }
49 @{$_[0]{wait}}
50 ];
51}
52
41sub waiters { 53sub waiters {
42 map $_->[1], @{$_[0]{wait}}; 54 $_[0]->sort;
55 @{$_[0]{wait}};
43} 56}
44 57
45package transfer; 58package transfer;
46 59
47use Coro::Timer (); 60use Coro::Timer ();
48 61
49sub wake { 62sub wake {
50 my $self = shift; 63 my $self = shift;
64
51 $self->[2] = 1; 65 $self->{alloc} = 1;
52 ref $self->[1] and $self->[1]->ready; 66 $self->{queue}{slots}--;
67 $self->{wake} and $self->{wake}->ready;
53} 68}
54 69
55sub try { 70sub try {
56 my $self = shift; 71 my $self = shift;
57 72
58 unless ($self->[2]) { 73 $self->{alloc} || do {
59 my $timeout = Coro::Timer::timeout $_[0]; 74 my $timeout = Coro::Timer::timeout $_[0];
60 local $self->[1] = $Coro::current; 75 local $self->{wake} = $self->{coro};
61 76
62 Coro::schedule; 77 Coro::schedule;
63 print "WOKE $Coro::current\n" if $self->[2]; 78
79 $self->{alloc};
64 } 80 }
65
66 return $self->[2];
67} 81}
68 82
69sub DESTROY { 83sub DESTROY {
70 my $self = shift; 84 my $self = shift;
71 eval { 85
72 $self->[0]{conns}++; 86 if ($self->{alloc}) {
87 $self->{queue}{slots}++;
73 $self->[0]->wake_next; 88 $self->{queue}->wake_next;
74 }; 89 }
75 print "DESTROY $Coro::current $@\n";#d#
76} 90}
77 91
78package conn; 92package conn;
79 93
80our %blockuri; 94our %blockuri;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines