ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/Channel.pm
Revision: 1.7
Committed: Sat Jul 21 03:44:06 2001 UTC (22 years, 10 months ago) by root
Branch: MAIN
Changes since 1.6: +9 -2 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 =head1 NAME
2    
3     Coro::Channel - message queues
4    
5     =head1 SYNOPSIS
6    
7     use Coro::Channel;
8    
9     $q1 = new Coro::Channel <maxsize>;
10    
11     $q1->put("xxx");
12     print $q1->get;
13    
14     die unless $q1->size;
15    
16     =head1 DESCRIPTION
17    
18     =over 4
19    
20     =cut
21    
22     package Coro::Channel;
23    
24 root 1.3 use Coro ();
25 root 1.1
26 root 1.6 $VERSION = 0.09;
27 root 1.1
28     =item $q = new Coro:Channel $maxsize
29    
30     Create a new channel with the given maximum size (unlimited if C<maxsize>
31     is omitted). Stating a size of zero gives you a traditional channel, i.e.
32     a queue that can store only a single element.
33    
34     =cut
35    
36     sub new {
37 root 1.7 # [\@contents, [$getwait], $maxsize, [$putwait]];
38 root 1.1 bless [[], [], $_[1]], $_[0];
39     }
40    
41     =item $q->put($scalar)
42    
43     Put the given scalar into the queue.
44    
45     =cut
46    
47     sub put {
48     push @{$_[0][0]}, $_[1];
49 root 1.7
50 root 1.1 (pop @{$_[0][1]})->ready if @{$_[0][1]};
51 root 1.7
52     while (defined $_[0][2] && @{$_[0][0]} >= $_[0][2]) {
53     push @{$_[0][3]}, $Coro::current; &Coro::schedule;
54     }
55 root 1.1 }
56    
57     =item $q->get
58    
59     Return the next element from the queue, waiting if necessary.
60    
61     =cut
62    
63     sub get {
64     while (!@{$_[0][0]}) {
65 root 1.3 push @{$_[0][1]}, $Coro::current;
66     &Coro::schedule;
67 root 1.1 }
68 root 1.7
69     (pop @{$_[0][3]})->ready if @{$_[0][3]};
70    
71 root 1.1 shift @{$_[0][0]};
72     }
73    
74     =item $q->size
75    
76     Return the number of elements waiting to be consumed. Please note that:
77    
78     if ($q->size) {
79     my $data = $q->get;
80     }
81    
82     is NOT a race condition but works fine.
83    
84     =cut
85    
86     sub size {
87     scalar @{$_[0][0]};
88     }
89    
90     1;
91    
92     =back
93    
94     =head1 AUTHOR
95    
96     Marc Lehmann <pcg@goof.com>
97     http://www.goof.com/pcg/marc/
98    
99     =cut
100