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.10 by root, Thu Aug 30 02:58:17 2001 UTC vs.
Revision 1.19 by root, Mon Dec 3 04:57:22 2001 UTC

1package transferqueue;
2
3sub new {
4 my $class = shift;
5 bless {
6 slots => $_[0],
7 lastspb => 0,
8 }, $class;
9}
10
11sub start_transfer {
12 my $self = shift;
13 my $size = $_[0];
14
15 my $trans = bless {
16 queue => $self,
17 time => $::NOW,
18 size => $size,
19 coro => $Coro::current,
20 }, transfer::;
21
22 push @{$self->{wait}}, $trans;
23 Scalar::Util::weaken($self->{wait}[-1]);
24
25 $self->wake_next;
26
27 $trans;
28}
29
30sub wake_next {
31 my $self = shift;
32
33 $self->sort;
34
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 ];
51}
52
53sub waiters {
54 $_[0]->sort;
55 @{$_[0]{wait}};
56}
57
58package transfer;
59
60use Coro::Timer ();
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
70sub try {
71 my $self = shift;
72
73 $self->{alloc} || do {
74 my $timeout = Coro::Timer::timeout $_[0];
75 local $self->{wake} = $self->{coro};
76
77 Coro::schedule;
78
79 $self->{alloc};
80 }
81}
82
83sub DESTROY {
84 my $self = shift;
85
86 if ($self->{alloc}) {
87 $self->{queue}{slots}++;
88 $self->{queue}->wake_next;
89 }
90}
91
1package conn; 92package conn;
2 93
3our %blockuri; 94our %blockuri;
4our $blockref; 95our $blockref;
5 96

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines