ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/Channel.pm
Revision: 1.46
Committed: Sun Aug 19 08:46:46 2007 UTC (16 years, 10 months ago) by root
Branch: MAIN
CVS Tags: rel-4_22, rel-4_21, rel-4_0, rel-4_3, rel-4_13, rel-4_11, rel-4_01, rel-4_03, rel-4_02, rel-4_1, rel-4_2, rel-4_31, rel-4_32, rel-4_33, rel-4_34, rel-4_35, rel-4_36, rel-4_37
Changes since 1.45: +3 -3 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 root 1.46 $q1->put ("xxx");
12 root 1.1 print $q1->get;
13    
14     die unless $q1->size;
15    
16     =head1 DESCRIPTION
17    
18 root 1.8 A Coro::Channel is the equivalent of a pipe: you can put things into it on
19     one end end read things out of it from the other hand. If the capacity of
20     the Channel is maxed out writers will block. Both ends of a Channel can be
21     read/written from as many coroutines as you want.
22    
23 root 1.1 =over 4
24    
25     =cut
26    
27     package Coro::Channel;
28    
29 root 1.45 no warnings;
30    
31 root 1.3 use Coro ();
32 root 1.1
33 root 1.43 $VERSION = 1.9;
34 root 1.1
35     =item $q = new Coro:Channel $maxsize
36    
37     Create a new channel with the given maximum size (unlimited if C<maxsize>
38 root 1.8 is omitted). Giving a size of one gives you a traditional channel, i.e. a
39     queue that can store only a single element.
40 root 1.1
41     =cut
42    
43     sub new {
44 root 1.7 # [\@contents, [$getwait], $maxsize, [$putwait]];
45 root 1.8 bless [[], [], $_[1] || (1e30),[]], $_[0];
46 root 1.1 }
47    
48 root 1.44 =item $q->put ($scalar)
49 root 1.1
50     Put the given scalar into the queue.
51    
52     =cut
53    
54     sub put {
55     push @{$_[0][0]}, $_[1];
56 root 1.7
57 root 1.1 (pop @{$_[0][1]})->ready if @{$_[0][1]};
58 root 1.7
59 root 1.8 while (@{$_[0][0]} >= $_[0][2]) {
60     push @{$_[0][3]}, $Coro::current;
61     &Coro::schedule;
62 root 1.7 }
63 root 1.1 }
64    
65     =item $q->get
66    
67     Return the next element from the queue, waiting if necessary.
68    
69     =cut
70    
71     sub get {
72 root 1.8 (pop @{$_[0][3]})->ready if @{$_[0][3]};
73    
74 root 1.1 while (!@{$_[0][0]}) {
75 root 1.3 push @{$_[0][1]}, $Coro::current;
76     &Coro::schedule;
77 root 1.1 }
78 root 1.7
79 root 1.46 shift @{$_[0][0]}
80 root 1.1 }
81    
82     =item $q->size
83    
84     Return the number of elements waiting to be consumed. Please note that:
85    
86     if ($q->size) {
87     my $data = $q->get;
88     }
89    
90     is NOT a race condition but works fine.
91    
92     =cut
93    
94     sub size {
95 root 1.46 scalar @{$_[0][0]}
96 root 1.1 }
97    
98     1;
99    
100     =back
101    
102     =head1 AUTHOR
103    
104 root 1.35 Marc Lehmann <schmorp@schmorp.de>
105 root 1.33 http://home.schmorp.de/
106 root 1.1
107     =cut
108