|
| 1 | +=begin pod |
| 2 | +
|
| 3 | +=TITLE class Channel |
| 4 | +
|
| 5 | +=SUBTITLE A thread-safe way to send a series of values from producers to consumers |
| 6 | +
|
| 7 | + class Channel { ... } |
| 8 | +
|
| 9 | +A Channel is a thread-safe queue that helps you to a series of objects from |
| 10 | +potentially mutltiple produces to potentially multiple consumers. |
| 11 | +
|
| 12 | + my $c = Channel.new; |
| 13 | + await (^10).map: { |
| 14 | + start { |
| 15 | + my $r = rand; |
| 16 | + sleep $r; |
| 17 | + $c.send($r); |
| 18 | + } |
| 19 | + } |
| 20 | + $c.close; |
| 21 | + say $c.list; |
| 22 | +
|
| 23 | +=head1 Methods |
| 24 | +
|
| 25 | +=head2 method send |
| 26 | +
|
| 27 | + method send(Channel:D: \item) |
| 28 | +
|
| 29 | +Enqueues an into the channel. Throws an exception of type |
| 30 | +C<X::Channel::SendOnClosed> if the channel has been closed already. |
| 31 | +
|
| 32 | +=head2 method receive |
| 33 | +
|
| 34 | + method receive(Channel:D:) |
| 35 | +
|
| 36 | +Receives and removes an item from the channel. It blocks if no item is |
| 37 | +present, waiting for a C<.send> from another thread. |
| 38 | +
|
| 39 | +Throws an exception of |
| 40 | +type C<X::Channel::ReceiveOnClose> if the channel has been closed, and the |
| 41 | +last item has been removed already. |
| 42 | +
|
| 43 | +If the channel has been marked as erratic with method C<fail>, and the last |
| 44 | +item has been removed, throws the argument that was given to C<fail> as an |
| 45 | +exception. |
| 46 | +
|
| 47 | +See method C<poll> for a non-blocking version that won't throw exceptions. |
| 48 | +
|
| 49 | +=head2 method poll |
| 50 | +
|
| 51 | + method poll(Channel:D:) |
| 52 | +
|
| 53 | +Receives and removes an item from the channel. If no item is present, returns |
| 54 | +C<Nil> instead of waiting. |
| 55 | +
|
| 56 | +See method C<receive> for a blocking version that properly responds to channel |
| 57 | +closing and failure. |
| 58 | +
|
| 59 | +=head2 method close |
| 60 | +
|
| 61 | + method close(Channel:D:) |
| 62 | +
|
| 63 | +Close the channel. This makes subsequent C<send> calls die, as well as a |
| 64 | +C<receive> call past the end of the queue, |
| 65 | +and is a prerequisite for C<list> to terminate. |
| 66 | +
|
| 67 | +=head2 method list |
| 68 | +
|
| 69 | + method list(Channel:D:) returns List:D |
| 70 | +
|
| 71 | +Returns a list of all remaining items in the queue, and removes them from the |
| 72 | +channel. This can only terminate once C<.close> has been called. |
| 73 | +
|
| 74 | +=head2 method closed |
| 75 | +
|
| 76 | + method closed(Channel:D:) returns Promise:D |
| 77 | +
|
| 78 | +Returns a promise that will be kept once the channel is closed by a call to |
| 79 | +method C<close>. |
| 80 | +
|
| 81 | +=head2 method fail |
| 82 | +
|
| 83 | + method fail(Channel:D: $error) |
| 84 | +
|
| 85 | +Closes the channel (that is, makes subsequent C<send> calls die), and enqueues |
| 86 | +the error as the final element in the channel. Method C<receive> will throw |
| 87 | +the error as an exception. |
| 88 | +
|
| 89 | +=end pod |
0 commit comments