ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/Channel.pm
Revision: 1.74
Committed: Thu Nov 20 09:37:21 2008 UTC (15 years, 6 months ago) by root
Branch: MAIN
CVS Tags: rel-5_0
Changes since 1.73: +1 -1 lines
Log Message:
5.0

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 root 1.71 one end and read things out of it from the other hand. If the capacity of
20 root 1.8 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.60 use strict qw(vars subs);
30 root 1.45 no warnings;
31    
32 root 1.3 use Coro ();
33 root 1.71 use Coro::Semaphore ();
34 root 1.1
35 root 1.74 our $VERSION = "5.0";
36 root 1.1
37 root 1.70 sub DATA (){ 0 }
38 root 1.71 sub SGET (){ 1 }
39     sub SPUT (){ 2 }
40 root 1.70
41 root 1.1 =item $q = new Coro:Channel $maxsize
42    
43     Create a new channel with the given maximum size (unlimited if C<maxsize>
44 root 1.71 is omitted). Giving a size of one gives you a traditional channel, i.e.
45     a queue that can store only a single element (which means there will be
46     no buffering, and C<put> will wait until there is a corresponding C<get>
47     call). To buffer one element you have to specify C<2>, and so on.
48 root 1.1
49     =cut
50    
51     sub new {
52 root 1.71 # we cheat and set infinity == 10**9
53     bless [
54     [],
55 root 1.73 (Coro::Semaphore::_alloc 0),
56     (Coro::Semaphore::_alloc +($_[1] || 1_000_000_000) - 1),
57 root 1.71 ]
58 root 1.1 }
59    
60 root 1.44 =item $q->put ($scalar)
61 root 1.1
62     Put the given scalar into the queue.
63    
64     =cut
65    
66     sub put {
67 root 1.70 push @{$_[0][DATA]}, $_[1];
68 root 1.71 Coro::Semaphore::up $_[0][SGET];
69     Coro::Semaphore::down $_[0][SPUT];
70 root 1.1 }
71    
72     =item $q->get
73    
74     Return the next element from the queue, waiting if necessary.
75    
76     =cut
77    
78     sub get {
79 root 1.71 Coro::Semaphore::down $_[0][SGET];
80     Coro::Semaphore::up $_[0][SPUT];
81 root 1.70 shift @{$_[0][DATA]}
82 root 1.47 }
83    
84 root 1.1 =item $q->size
85    
86     Return the number of elements waiting to be consumed. Please note that:
87    
88     if ($q->size) {
89     my $data = $q->get;
90     }
91    
92     is NOT a race condition but works fine.
93    
94     =cut
95    
96     sub size {
97 root 1.70 scalar @{$_[0][DATA]}
98 root 1.1 }
99    
100     1;
101    
102     =back
103    
104     =head1 AUTHOR
105    
106 root 1.35 Marc Lehmann <schmorp@schmorp.de>
107 root 1.33 http://home.schmorp.de/
108 root 1.1
109     =cut
110