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

Comparing Coro/myhttpd/access.pl (file contents):
Revision 1.15 by root, Fri Nov 30 03:06:37 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 {
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;
17 Scalar::Util::weaken($self->{wait}[-1]); 23 Scalar::Util::weaken($self->{wait}[-1]);
18 24
19 if (--$self->{conns} >= 0) {
20 $self->wake_next; 25 $self->wake_next;
21 }
22 26
23 $trans; 27 $trans;
24} 28}
25 29
26sub wake_next { 30sub wake_next {
27 my $self = shift; 31 my $self = shift;
28 32
29 if ($self->{conns} >= 0) { 33 $self->sort;
30 while(@{$self->{wait}}) { 34
35 while($self->{slots} && @{$self->{wait}}) {
31 my $transfer = shift @{$self->{wait}}; 36 my $transfer = shift @{$self->{wait}};
32 if ($transfer) { 37 if ($transfer) {
38 $self->{lastspb} = $transfer->{spb};
33 $transfer->wake; 39 $transfer->wake;
34 last; 40 last;
35 }
36 } 41 }
37 } 42 }
38} 43}
39 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
40sub waiters { 53sub waiters {
41 map $_->[1], @{$_[0]{wait}}; 54 $_[0]->sort;
55 @{$_[0]{wait}};
42} 56}
43 57
44package transfer; 58package transfer;
45 59
46use Coro::Timer (); 60use Coro::Timer ();
47 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
48sub try { 70sub try {
49 my $self = shift; 71 my $self = shift;
50 my $timeout = Coro::Timer::timeout $_[0];
51 72
52 unless ($self->[2]) { 73 $self->{alloc} || do {
53 local $self->[1] = $Coro::current; 74 my $timeout = Coro::Timer::timeout $_[0];
75 local $self->{wake} = $self->{coro};
76
54 Coro::schedule; 77 Coro::schedule;
78
79 $self->{alloc};
55 } 80 }
56
57 return $self->[2];
58}
59
60sub wake {
61 my $self = shift;
62 $self->[2] = 1;
63 ref $self->[1] and $self->[1]->ready;
64} 81}
65 82
66sub DESTROY { 83sub DESTROY {
67 my $self = shift; 84 my $self = shift;
68 $self->[0]{conns}++; 85
86 if ($self->{alloc}) {
87 $self->{queue}{slots}++;
69 $self->[0]->wake_next; 88 $self->{queue}->wake_next;
89 }
70} 90}
71 91
72package conn; 92package conn;
73 93
74our %blockuri; 94our %blockuri;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines