1 // Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Communication primitives for concurrent tasks
13 //! Rust makes it very difficult to share data among tasks to prevent race
14 //! conditions and to improve parallelism, but there is often a need for
15 //! communication between concurrent tasks. The primitives defined in this
16 //! module are the building blocks for synchronization in rust.
18 //! This module provides message-based communication over channels, concretely
19 //! defined among three types:
25 //! A `Sender` or `SyncSender` is used to send data to a `Receiver`. Both
26 //! senders are clone-able such that many tasks can send simultaneously to one
27 //! receiver. These channels are *task blocking*, not *thread blocking*. This
28 //! means that if one task is blocked on a channel, other tasks can continue to
31 //! Rust channels come in one of two flavors:
33 //! 1. An asynchronous, infinitely buffered channel. The `channel()` function
34 //! will return a `(Sender, Receiver)` tuple where all sends will be
35 //! **asynchronous** (they never block). The channel conceptually has an
38 //! 2. A synchronous, bounded channel. The `sync_channel()` function will return
39 //! a `(SyncSender, Receiver)` tuple where the storage for pending messages
40 //! is a pre-allocated buffer of a fixed size. All sends will be
41 //! **synchronous** by blocking until there is buffer space available. Note
42 //! that a bound of 0 is allowed, causing the channel to become a
43 //! "rendezvous" channel where each sender atomically hands off a message to
46 //! ## Failure Propagation
48 //! In addition to being a core primitive for communicating in rust, channels
49 //! are the points at which panics are propagated among tasks. Whenever the one
50 //! half of channel is closed, the other half will have its next operation
51 //! `panic!`. The purpose of this is to allow propagation of panics among tasks
52 //! that are linked to one another via channels.
54 //! There are methods on both of senders and receivers to perform their
55 //! respective operations without panicking, however.
57 //! ## Runtime Requirements
59 //! The channel types defined in this module generally have very few runtime
60 //! requirements in order to operate. The major requirement they have is for a
61 //! local rust `Task` to be available if any *blocking* operation is performed.
63 //! If a local `Task` is not available (for example an FFI callback), then the
64 //! `send` operation is safe on a `Sender` (as well as a `send_opt`) as well as
65 //! the `try_send` method on a `SyncSender`, but no other operations are
66 //! guaranteed to be safe.
68 //! Additionally, channels can interoperate between runtimes. If one task in a
69 //! program is running on libnative and another is running on libgreen, they can
70 //! still communicate with one another using channels.
77 //! // Create a simple streaming channel
78 //! let (tx, rx) = channel();
82 //! assert_eq!(rx.recv(), 10i);
88 //! // Create a shared channel which can be sent along from many tasks
89 //! // where tx is the sending half (tx for transmission), and rx is the receiving
90 //! // half (rx for receiving).
91 //! let (tx, rx) = channel();
92 //! for i in range(0i, 10i) {
93 //! let tx = tx.clone();
99 //! for _ in range(0i, 10i) {
100 //! let j = rx.recv();
101 //! assert!(0 <= j && j < 10);
105 //! Propagating panics:
108 //! // The call to recv() will panic!() because the channel has already hung
109 //! // up (or been deallocated)
110 //! let (tx, rx) = channel::<int>();
115 //! Synchronous channels:
118 //! let (tx, rx) = sync_channel::<int>(0);
120 //! // This will wait for the parent task to start receiving
126 //! Reading from a channel with a timeout requires to use a Timer together
127 //! with the channel. You can use the select! macro to select either and
128 //! handle the timeout case. This first example will break out of the loop
129 //! after 10 seconds no matter what:
132 //! use std::io::timer::Timer;
133 //! use std::time::Duration;
135 //! let (tx, rx) = channel::<int>();
136 //! let mut timer = Timer::new().unwrap();
137 //! let timeout = timer.oneshot(Duration::seconds(10));
141 //! val = rx.recv() => println!("Received {}", val),
142 //! () = timeout.recv() => {
143 //! println!("timed out, total time was more than 10 seconds")
150 //! This second example is more costly since it allocates a new timer every
151 //! time a message is received, but it allows you to timeout after the channel
152 //! has been inactive for 5 seconds:
155 //! use std::io::timer::Timer;
156 //! use std::time::Duration;
158 //! let (tx, rx) = channel::<int>();
159 //! let mut timer = Timer::new().unwrap();
162 //! let timeout = timer.oneshot(Duration::seconds(5));
165 //! val = rx.recv() => println!("Received {}", val),
166 //! () = timeout.recv() => {
167 //! println!("timed out, no message received in 5 seconds")
174 // A description of how Rust's channel implementation works
176 // Channels are supposed to be the basic building block for all other
177 // concurrent primitives that are used in Rust. As a result, the channel type
178 // needs to be highly optimized, flexible, and broad enough for use everywhere.
180 // The choice of implementation of all channels is to be built on lock-free data
181 // structures. The channels themselves are then consequently also lock-free data
182 // structures. As always with lock-free code, this is a very "here be dragons"
183 // territory, especially because I'm unaware of any academic papers which have
184 // gone into great length about channels of these flavors.
186 // ## Flavors of channels
188 // From the perspective of a consumer of this library, there is only one flavor
189 // of channel. This channel can be used as a stream and cloned to allow multiple
190 // senders. Under the hood, however, there are actually three flavors of
193 // * Oneshots - these channels are highly optimized for the one-send use case.
194 // They contain as few atomics as possible and involve one and
195 // exactly one allocation.
196 // * Streams - these channels are optimized for the non-shared use case. They
197 // use a different concurrent queue which is more tailored for this
198 // use case. The initial allocation of this flavor of channel is not
200 // * Shared - this is the most general form of channel that this module offers,
201 // a channel with multiple senders. This type is as optimized as it
202 // can be, but the previous two types mentioned are much faster for
205 // ## Concurrent queues
207 // The basic idea of Rust's Sender/Receiver types is that send() never blocks, but
208 // recv() obviously blocks. This means that under the hood there must be some
209 // shared and concurrent queue holding all of the actual data.
211 // With two flavors of channels, two flavors of queues are also used. We have
212 // chosen to use queues from a well-known author which are abbreviated as SPSC
213 // and MPSC (single producer, single consumer and multiple producer, single
214 // consumer). SPSC queues are used for streams while MPSC queues are used for
217 // ### SPSC optimizations
219 // The SPSC queue found online is essentially a linked list of nodes where one
220 // half of the nodes are the "queue of data" and the other half of nodes are a
221 // cache of unused nodes. The unused nodes are used such that an allocation is
222 // not required on every push() and a free doesn't need to happen on every
225 // As found online, however, the cache of nodes is of an infinite size. This
226 // means that if a channel at one point in its life had 50k items in the queue,
227 // then the queue will always have the capacity for 50k items. I believed that
228 // this was an unnecessary limitation of the implementation, so I have altered
229 // the queue to optionally have a bound on the cache size.
231 // By default, streams will have an unbounded SPSC queue with a small-ish cache
232 // size. The hope is that the cache is still large enough to have very fast
233 // send() operations while not too large such that millions of channels can
236 // ### MPSC optimizations
238 // Right now the MPSC queue has not been optimized. Like the SPSC queue, it uses
239 // a linked list under the hood to earn its unboundedness, but I have not put
240 // forth much effort into having a cache of nodes similar to the SPSC queue.
242 // For now, I believe that this is "ok" because shared channels are not the most
243 // common type, but soon we may wish to revisit this queue choice and determine
244 // another candidate for backend storage of shared channels.
246 // ## Overview of the Implementation
248 // Now that there's a little background on the concurrent queues used, it's
249 // worth going into much more detail about the channels themselves. The basic
250 // pseudocode for a send/recv are:
254 // queue.push(t) return if queue.pop()
255 // if increment() == -1 deschedule {
256 // wakeup() if decrement() > 0
257 // cancel_deschedule()
261 // As mentioned before, there are no locks in this implementation, only atomic
262 // instructions are used.
264 // ### The internal atomic counter
266 // Every channel has a shared counter with each half to keep track of the size
267 // of the queue. This counter is used to abort descheduling by the receiver and
268 // to know when to wake up on the sending side.
270 // As seen in the pseudocode, senders will increment this count and receivers
271 // will decrement the count. The theory behind this is that if a sender sees a
272 // -1 count, it will wake up the receiver, and if the receiver sees a 1+ count,
273 // then it doesn't need to block.
275 // The recv() method has a beginning call to pop(), and if successful, it needs
276 // to decrement the count. It is a crucial implementation detail that this
277 // decrement does *not* happen to the shared counter. If this were the case,
278 // then it would be possible for the counter to be very negative when there were
279 // no receivers waiting, in which case the senders would have to determine when
280 // it was actually appropriate to wake up a receiver.
282 // Instead, the "steal count" is kept track of separately (not atomically
283 // because it's only used by receivers), and then the decrement() call when
284 // descheduling will lump in all of the recent steals into one large decrement.
286 // The implication of this is that if a sender sees a -1 count, then there's
287 // guaranteed to be a waiter waiting!
289 // ## Native Implementation
291 // A major goal of these channels is to work seamlessly on and off the runtime.
292 // All of the previous race conditions have been worded in terms of
293 // scheduler-isms (which is obviously not available without the runtime).
295 // For now, native usage of channels (off the runtime) will fall back onto
296 // mutexes/cond vars for descheduling/atomic decisions. The no-contention path
297 // is still entirely lock-free, the "deschedule" blocks above are surrounded by
298 // a mutex and the "wakeup" blocks involve grabbing a mutex and signaling on a
299 // condition variable.
303 // Being able to support selection over channels has greatly influenced this
304 // design, and not only does selection need to work inside the runtime, but also
305 // outside the runtime.
307 // The implementation is fairly straightforward. The goal of select() is not to
308 // return some data, but only to return which channel can receive data without
309 // blocking. The implementation is essentially the entire blocking procedure
310 // followed by an increment as soon as its woken up. The cancellation procedure
311 // involves an increment and swapping out of to_wake to acquire ownership of the
314 // Sadly this current implementation requires multiple allocations, so I have
315 // seen the throughput of select() be much worse than it should be. I do not
316 // believe that there is anything fundamental which needs to change about these
317 // channels, however, in order to support a more efficient select().
321 // And now that you've seen all the races that I found and attempted to fix,
322 // here's the code for you to find some more!
324 use core::prelude::*;
327 use alloc::boxed::Box;
328 use core::cell::Cell;
329 use core::kinds::marker;
331 use core::cell::UnsafeCell;
332 use rustrt::local::Local;
333 use rustrt::task::{Task, BlockedTask};
335 pub use comm::select::{Select, Handle};
338 { fn $name:ident() $b:block $(#[$a:meta])*} => (
340 #![allow(unused_imports)]
352 $(#[$a])* #[test] fn uv() { f() }
353 $(#[$a])* #[test] fn native() {
355 let (tx, rx) = channel();
356 spawn(proc() { tx.send(f()) });
369 // Use a power of 2 to allow LLVM to optimize to something that's not a
370 // division, this is hit pretty regularly.
371 static RESCHED_FREQ: int = 256;
373 /// The receiving-half of Rust's channel type. This half can only be owned by
376 pub struct Receiver<T> {
377 inner: UnsafeCell<Flavor<T>>,
378 receives: Cell<uint>,
379 // can't share in an arc
380 _marker: marker::NoSync,
383 /// An iterator over messages on a receiver, this iterator will block
384 /// whenever `next` is called, waiting for a new message, and `None` will be
385 /// returned when the corresponding channel has hung up.
387 pub struct Messages<'a, T:'a> {
391 /// The sending-half of Rust's asynchronous channel type. This half can only be
392 /// owned by one task, but it can be cloned to send to other tasks.
394 pub struct Sender<T> {
395 inner: UnsafeCell<Flavor<T>>,
397 // can't share in an arc
398 _marker: marker::NoSync,
401 /// The sending-half of Rust's synchronous channel type. This half can only be
402 /// owned by one task, but it can be cloned to send to other tasks.
403 #[unstable = "this type may be renamed, but it will always exist"]
404 pub struct SyncSender<T> {
405 inner: Arc<UnsafeCell<sync::Packet<T>>>,
406 // can't share in an arc
407 _marker: marker::NoSync,
410 /// This enumeration is the list of the possible reasons that try_recv could not
411 /// return data when called.
412 #[deriving(PartialEq, Clone, Show)]
413 #[experimental = "this is likely to be removed in changing try_recv()"]
414 pub enum TryRecvError {
415 /// This channel is currently empty, but the sender(s) have not yet
416 /// disconnected, so data may yet become available.
418 /// This channel's sending half has become disconnected, and there will
419 /// never be any more data received on this channel
423 /// This enumeration is the list of the possible error outcomes for the
424 /// `SyncSender::try_send` method.
425 #[deriving(PartialEq, Clone, Show)]
426 #[experimental = "this is likely to be removed in changing try_send()"]
427 pub enum TrySendError<T> {
428 /// The data could not be sent on the channel because it would require that
429 /// the callee block to send the data.
431 /// If this is a buffered channel, then the buffer is full at this time. If
432 /// this is not a buffered channel, then there is no receiver available to
433 /// acquire the data.
435 /// This channel's receiving half has disconnected, so the data could not be
436 /// sent. The data is returned back to the callee in this case.
441 Oneshot(Arc<UnsafeCell<oneshot::Packet<T>>>),
442 Stream(Arc<UnsafeCell<stream::Packet<T>>>),
443 Shared(Arc<UnsafeCell<shared::Packet<T>>>),
444 Sync(Arc<UnsafeCell<sync::Packet<T>>>),
448 trait UnsafeFlavor<T> {
449 fn inner_unsafe<'a>(&'a self) -> &'a UnsafeCell<Flavor<T>>;
450 unsafe fn inner_mut<'a>(&'a self) -> &'a mut Flavor<T> {
451 &mut *self.inner_unsafe().get()
453 unsafe fn inner<'a>(&'a self) -> &'a Flavor<T> {
454 &*self.inner_unsafe().get()
457 impl<T> UnsafeFlavor<T> for Sender<T> {
458 fn inner_unsafe<'a>(&'a self) -> &'a UnsafeCell<Flavor<T>> {
462 impl<T> UnsafeFlavor<T> for Receiver<T> {
463 fn inner_unsafe<'a>(&'a self) -> &'a UnsafeCell<Flavor<T>> {
468 /// Creates a new asynchronous channel, returning the sender/receiver halves.
470 /// All data sent on the sender will become available on the receiver, and no
471 /// send will block the calling task (this channel has an "infinite buffer").
476 /// // tx is is the sending half (tx for transmission), and rx is the receiving
477 /// // half (rx for receiving).
478 /// let (tx, rx) = channel();
480 /// // Spawn off an expensive computation
482 /// # fn expensive_computation() {}
483 /// tx.send(expensive_computation());
486 /// // Do some useful work for awhile
488 /// // Let's see what that answer was
489 /// println!("{}", rx.recv());
492 pub fn channel<T: Send>() -> (Sender<T>, Receiver<T>) {
493 let a = Arc::new(UnsafeCell::new(oneshot::Packet::new()));
494 (Sender::new(Oneshot(a.clone())), Receiver::new(Oneshot(a)))
497 /// Creates a new synchronous, bounded channel.
499 /// Like asynchronous channels, the `Receiver` will block until a message
500 /// becomes available. These channels differ greatly in the semantics of the
501 /// sender from asynchronous channels, however.
503 /// This channel has an internal buffer on which messages will be queued. When
504 /// the internal buffer becomes full, future sends will *block* waiting for the
505 /// buffer to open up. Note that a buffer size of 0 is valid, in which case this
506 /// becomes "rendezvous channel" where each send will not return until a recv
507 /// is paired with it.
509 /// As with asynchronous channels, all senders will panic in `send` if the
510 /// `Receiver` has been destroyed.
515 /// let (tx, rx) = sync_channel(1);
517 /// // this returns immediately
521 /// // this will block until the previous message has been received
525 /// assert_eq!(rx.recv(), 1i);
526 /// assert_eq!(rx.recv(), 2i);
528 #[unstable = "this function may be renamed to more accurately reflect the type \
529 of channel that is is creating"]
530 pub fn sync_channel<T: Send>(bound: uint) -> (SyncSender<T>, Receiver<T>) {
531 let a = Arc::new(UnsafeCell::new(sync::Packet::new(bound)));
532 (SyncSender::new(a.clone()), Receiver::new(Sync(a)))
535 ////////////////////////////////////////////////////////////////////////////////
537 ////////////////////////////////////////////////////////////////////////////////
539 impl<T: Send> Sender<T> {
540 fn new(inner: Flavor<T>) -> Sender<T> {
542 inner: UnsafeCell::new(inner),
544 _marker: marker::NoSync,
548 /// Sends a value along this channel to be received by the corresponding
551 /// Rust channels are infinitely buffered so this method will never block.
555 /// This function will panic if the other end of the channel has hung up.
556 /// This means that if the corresponding receiver has fallen out of scope,
557 /// this function will trigger a panic message saying that a message is
558 /// being sent on a closed channel.
560 /// Note that if this function does *not* panic, it does not mean that the
561 /// data will be successfully received. All sends are placed into a queue,
562 /// so it is possible for a send to succeed (the other end is alive), but
563 /// then the other end could immediately disconnect.
565 /// The purpose of this functionality is to propagate panicks among tasks.
566 /// If a panic is not desired, then consider using the `send_opt` method
567 #[experimental = "this function is being considered candidate for removal \
568 to adhere to the general guidelines of rust"]
569 pub fn send(&self, t: T) {
570 if self.send_opt(t).is_err() {
571 panic!("sending on a closed channel");
575 /// Attempts to send a value on this channel, returning it back if it could
578 /// A successful send occurs when it is determined that the other end of
579 /// the channel has not hung up already. An unsuccessful send would be one
580 /// where the corresponding receiver has already been deallocated. Note
581 /// that a return value of `Err` means that the data will never be
582 /// received, but a return value of `Ok` does *not* mean that the data
583 /// will be received. It is possible for the corresponding receiver to
584 /// hang up immediately after this function returns `Ok`.
586 /// Like `send`, this method will never block.
590 /// This method will never panic, it will return the message back to the
591 /// caller if the other end is disconnected
596 /// let (tx, rx) = channel();
598 /// // This send is always successful
599 /// assert_eq!(tx.send_opt(1i), Ok(()));
601 /// // This send will fail because the receiver is gone
603 /// assert_eq!(tx.send_opt(1i), Err(1));
605 #[unstable = "this function may be renamed to send() in the future"]
606 pub fn send_opt(&self, t: T) -> Result<(), T> {
607 // In order to prevent starvation of other tasks in situations where
608 // a task sends repeatedly without ever receiving, we occasionally
609 // yield instead of doing a send immediately.
611 // Don't unconditionally attempt to yield because the TLS overhead can
612 // be a bit much, and also use `try_take` instead of `take` because
613 // there's no reason that this send shouldn't be usable off the
615 let cnt = self.sends.get() + 1;
617 if cnt % (RESCHED_FREQ as uint) == 0 {
618 let task: Option<Box<Task>> = Local::try_take();
619 task.map(|t| t.maybe_yield());
622 let (new_inner, ret) = match *unsafe { self.inner() } {
629 let a = Arc::new(UnsafeCell::new(stream::Packet::new()));
630 match (*p).upgrade(Receiver::new(Stream(a.clone()))) {
631 oneshot::UpSuccess => {
632 let ret = (*a.get()).send(t);
635 oneshot::UpDisconnected => (a, Err(t)),
636 oneshot::UpWoke(task) => {
637 // This send cannot panic because the task is
638 // asleep (we're looking at it), so the receiver
640 (*a.get()).send(t).ok().unwrap();
641 task.wake().map(|t| t.reawaken());
648 Stream(ref p) => return unsafe { (*p.get()).send(t) },
649 Shared(ref p) => return unsafe { (*p.get()).send(t) },
650 Sync(..) => unreachable!(),
654 let tmp = Sender::new(Stream(new_inner));
655 mem::swap(self.inner_mut(), tmp.inner_mut());
662 impl<T: Send> Clone for Sender<T> {
663 fn clone(&self) -> Sender<T> {
664 let (packet, sleeper) = match *unsafe { self.inner() } {
666 let a = Arc::new(UnsafeCell::new(shared::Packet::new()));
668 (*a.get()).postinit_lock();
669 match (*p.get()).upgrade(Receiver::new(Shared(a.clone()))) {
670 oneshot::UpSuccess | oneshot::UpDisconnected => (a, None),
671 oneshot::UpWoke(task) => (a, Some(task))
676 let a = Arc::new(UnsafeCell::new(shared::Packet::new()));
678 (*a.get()).postinit_lock();
679 match (*p.get()).upgrade(Receiver::new(Shared(a.clone()))) {
680 stream::UpSuccess | stream::UpDisconnected => (a, None),
681 stream::UpWoke(task) => (a, Some(task)),
686 unsafe { (*p.get()).clone_chan(); }
687 return Sender::new(Shared(p.clone()));
689 Sync(..) => unreachable!(),
693 (*packet.get()).inherit_blocker(sleeper);
695 let tmp = Sender::new(Shared(packet.clone()));
696 mem::swap(self.inner_mut(), tmp.inner_mut());
698 Sender::new(Shared(packet))
703 impl<T: Send> Drop for Sender<T> {
705 match *unsafe { self.inner_mut() } {
706 Oneshot(ref mut p) => unsafe { (*p.get()).drop_chan(); },
707 Stream(ref mut p) => unsafe { (*p.get()).drop_chan(); },
708 Shared(ref mut p) => unsafe { (*p.get()).drop_chan(); },
709 Sync(..) => unreachable!(),
714 ////////////////////////////////////////////////////////////////////////////////
716 ////////////////////////////////////////////////////////////////////////////////
718 impl<T: Send> SyncSender<T> {
719 fn new(inner: Arc<UnsafeCell<sync::Packet<T>>>) -> SyncSender<T> {
720 SyncSender { inner: inner, _marker: marker::NoSync }
723 /// Sends a value on this synchronous channel.
725 /// This function will *block* until space in the internal buffer becomes
726 /// available or a receiver is available to hand off the message to.
728 /// Note that a successful send does *not* guarantee that the receiver will
729 /// ever see the data if there is a buffer on this channel. Messages may be
730 /// enqueued in the internal buffer for the receiver to receive at a later
731 /// time. If the buffer size is 0, however, it can be guaranteed that the
732 /// receiver has indeed received the data if this function returns success.
736 /// Similarly to `Sender::send`, this function will panic if the
737 /// corresponding `Receiver` for this channel has disconnected. This
738 /// behavior is used to propagate panics among tasks.
740 /// If a panic is not desired, you can achieve the same semantics with the
741 /// `SyncSender::send_opt` method which will not panic if the receiver
743 #[experimental = "this function is being considered candidate for removal \
744 to adhere to the general guidelines of rust"]
745 pub fn send(&self, t: T) {
746 if self.send_opt(t).is_err() {
747 panic!("sending on a closed channel");
751 /// Send a value on a channel, returning it back if the receiver
754 /// This method will *block* to send the value `t` on the channel, but if
755 /// the value could not be sent due to the receiver disconnecting, the value
756 /// is returned back to the callee. This function is similar to `try_send`,
757 /// except that it will block if the channel is currently full.
761 /// This function cannot panic.
762 #[unstable = "this function may be renamed to send() in the future"]
763 pub fn send_opt(&self, t: T) -> Result<(), T> {
764 unsafe { (*self.inner.get()).send(t) }
767 /// Attempts to send a value on this channel without blocking.
769 /// This method differs from `send_opt` by returning immediately if the
770 /// channel's buffer is full or no receiver is waiting to acquire some
771 /// data. Compared with `send_opt`, this function has two failure cases
772 /// instead of one (one for disconnection, one for a full buffer).
774 /// See `SyncSender::send` for notes about guarantees of whether the
775 /// receiver has received the data or not if this function is successful.
779 /// This function cannot panic
780 #[unstable = "the return type of this function is candidate for \
782 pub fn try_send(&self, t: T) -> Result<(), TrySendError<T>> {
783 unsafe { (*self.inner.get()).try_send(t) }
788 impl<T: Send> Clone for SyncSender<T> {
789 fn clone(&self) -> SyncSender<T> {
790 unsafe { (*self.inner.get()).clone_chan(); }
791 return SyncSender::new(self.inner.clone());
796 impl<T: Send> Drop for SyncSender<T> {
798 unsafe { (*self.inner.get()).drop_chan(); }
802 ////////////////////////////////////////////////////////////////////////////////
804 ////////////////////////////////////////////////////////////////////////////////
806 impl<T: Send> Receiver<T> {
807 fn new(inner: Flavor<T>) -> Receiver<T> {
808 Receiver { inner: UnsafeCell::new(inner), receives: Cell::new(0), _marker: marker::NoSync }
811 /// Blocks waiting for a value on this receiver
813 /// This function will block if necessary to wait for a corresponding send
814 /// on the channel from its paired `Sender` structure. This receiver will
815 /// be woken up when data is ready, and the data will be returned.
819 /// Similar to channels, this method will trigger a task panic if the
820 /// other end of the channel has hung up (been deallocated). The purpose of
821 /// this is to propagate panicks among tasks.
823 /// If a panic is not desired, then there are two options:
825 /// * If blocking is still desired, the `recv_opt` method will return `None`
826 /// when the other end hangs up
828 /// * If blocking is not desired, then the `try_recv` method will attempt to
829 /// peek at a value on this receiver.
830 #[experimental = "this function is being considered candidate for removal \
831 to adhere to the general guidelines of rust"]
832 pub fn recv(&self) -> T {
833 match self.recv_opt() {
835 Err(()) => panic!("receiving on a closed channel"),
839 /// Attempts to return a pending value on this receiver without blocking
841 /// This method will never block the caller in order to wait for data to
842 /// become available. Instead, this will always return immediately with a
843 /// possible option of pending data on the channel.
845 /// This is useful for a flavor of "optimistic check" before deciding to
846 /// block on a receiver.
850 /// This function cannot panic.
851 #[unstable = "the return type of this function may be altered"]
852 pub fn try_recv(&self) -> Result<T, TryRecvError> {
853 // If a thread is spinning in try_recv, we should take the opportunity
854 // to reschedule things occasionally. See notes above in scheduling on
855 // sends for why this doesn't always hit TLS, and also for why this uses
856 // `try_take` instead of `take`.
857 let cnt = self.receives.get() + 1;
858 self.receives.set(cnt);
859 if cnt % (RESCHED_FREQ as uint) == 0 {
860 let task: Option<Box<Task>> = Local::try_take();
861 task.map(|t| t.maybe_yield());
865 let new_port = match *unsafe { self.inner() } {
867 match unsafe { (*p.get()).try_recv() } {
868 Ok(t) => return Ok(t),
869 Err(oneshot::Empty) => return Err(Empty),
870 Err(oneshot::Disconnected) => return Err(Disconnected),
871 Err(oneshot::Upgraded(rx)) => rx,
875 match unsafe { (*p.get()).try_recv() } {
876 Ok(t) => return Ok(t),
877 Err(stream::Empty) => return Err(Empty),
878 Err(stream::Disconnected) => return Err(Disconnected),
879 Err(stream::Upgraded(rx)) => rx,
883 match unsafe { (*p.get()).try_recv() } {
884 Ok(t) => return Ok(t),
885 Err(shared::Empty) => return Err(Empty),
886 Err(shared::Disconnected) => return Err(Disconnected),
890 match unsafe { (*p.get()).try_recv() } {
891 Ok(t) => return Ok(t),
892 Err(sync::Empty) => return Err(Empty),
893 Err(sync::Disconnected) => return Err(Disconnected),
898 mem::swap(self.inner_mut(),
899 new_port.inner_mut());
904 /// Attempt to wait for a value on this receiver, but does not panic if the
905 /// corresponding channel has hung up.
907 /// This implementation of iterators for ports will always block if there is
908 /// not data available on the receiver, but it will not panic in the case
909 /// that the channel has been deallocated.
911 /// In other words, this function has the same semantics as the `recv`
912 /// method except for the panic aspect.
914 /// If the channel has hung up, then `Err` is returned. Otherwise `Ok` of
915 /// the value found on the receiver is returned.
916 #[unstable = "this function may be renamed to recv()"]
917 pub fn recv_opt(&self) -> Result<T, ()> {
919 let new_port = match *unsafe { self.inner() } {
921 match unsafe { (*p.get()).recv() } {
922 Ok(t) => return Ok(t),
923 Err(oneshot::Empty) => return unreachable!(),
924 Err(oneshot::Disconnected) => return Err(()),
925 Err(oneshot::Upgraded(rx)) => rx,
929 match unsafe { (*p.get()).recv() } {
930 Ok(t) => return Ok(t),
931 Err(stream::Empty) => return unreachable!(),
932 Err(stream::Disconnected) => return Err(()),
933 Err(stream::Upgraded(rx)) => rx,
937 match unsafe { (*p.get()).recv() } {
938 Ok(t) => return Ok(t),
939 Err(shared::Empty) => return unreachable!(),
940 Err(shared::Disconnected) => return Err(()),
943 Sync(ref p) => return unsafe { (*p.get()).recv() }
946 mem::swap(self.inner_mut(), new_port.inner_mut());
951 /// Returns an iterator which will block waiting for messages, but never
952 /// `panic!`. It will return `None` when the channel has hung up.
954 pub fn iter<'a>(&'a self) -> Messages<'a, T> {
955 Messages { rx: self }
959 impl<T: Send> select::Packet for Receiver<T> {
960 fn can_recv(&self) -> bool {
962 let new_port = match *unsafe { self.inner() } {
964 match unsafe { (*p.get()).can_recv() } {
965 Ok(ret) => return ret,
966 Err(upgrade) => upgrade,
970 match unsafe { (*p.get()).can_recv() } {
971 Ok(ret) => return ret,
972 Err(upgrade) => upgrade,
976 return unsafe { (*p.get()).can_recv() };
979 return unsafe { (*p.get()).can_recv() };
983 mem::swap(self.inner_mut(),
984 new_port.inner_mut());
989 fn start_selection(&self, mut task: BlockedTask) -> Result<(), BlockedTask>{
991 let (t, new_port) = match *unsafe { self.inner() } {
993 match unsafe { (*p.get()).start_selection(task) } {
994 oneshot::SelSuccess => return Ok(()),
995 oneshot::SelCanceled(task) => return Err(task),
996 oneshot::SelUpgraded(t, rx) => (t, rx),
1000 match unsafe { (*p.get()).start_selection(task) } {
1001 stream::SelSuccess => return Ok(()),
1002 stream::SelCanceled(task) => return Err(task),
1003 stream::SelUpgraded(t, rx) => (t, rx),
1007 return unsafe { (*p.get()).start_selection(task) };
1010 return unsafe { (*p.get()).start_selection(task) };
1015 mem::swap(self.inner_mut(),
1016 new_port.inner_mut());
1021 fn abort_selection(&self) -> bool {
1022 let mut was_upgrade = false;
1024 let result = match *unsafe { self.inner() } {
1025 Oneshot(ref p) => unsafe { (*p.get()).abort_selection() },
1026 Stream(ref p) => unsafe {
1027 (*p.get()).abort_selection(was_upgrade)
1029 Shared(ref p) => return unsafe {
1030 (*p.get()).abort_selection(was_upgrade)
1032 Sync(ref p) => return unsafe {
1033 (*p.get()).abort_selection()
1036 let new_port = match result { Ok(b) => return b, Err(p) => p };
1039 mem::swap(self.inner_mut(),
1040 new_port.inner_mut());
1047 impl<'a, T: Send> Iterator<T> for Messages<'a, T> {
1048 fn next(&mut self) -> Option<T> { self.rx.recv_opt().ok() }
1051 #[unsafe_destructor]
1052 impl<T: Send> Drop for Receiver<T> {
1053 fn drop(&mut self) {
1054 match *unsafe { self.inner_mut() } {
1055 Oneshot(ref mut p) => unsafe { (*p.get()).drop_port(); },
1056 Stream(ref mut p) => unsafe { (*p.get()).drop_port(); },
1057 Shared(ref mut p) => unsafe { (*p.get()).drop_port(); },
1058 Sync(ref mut p) => unsafe { (*p.get()).drop_port(); },
1065 use std::prelude::*;
1070 pub fn stress_factor() -> uint {
1071 match os::getenv("RUST_TEST_STRESS") {
1072 Some(val) => from_str::<uint>(val.as_slice()).unwrap(),
1078 let (tx, rx) = channel::<int>();
1080 assert_eq!(rx.recv(), 1);
1083 test!(fn drop_full() {
1084 let (tx, _rx) = channel();
1088 test!(fn drop_full_shared() {
1089 let (tx, _rx) = channel();
1095 test!(fn smoke_shared() {
1096 let (tx, rx) = channel::<int>();
1098 assert_eq!(rx.recv(), 1);
1099 let tx = tx.clone();
1101 assert_eq!(rx.recv(), 1);
1104 test!(fn smoke_threads() {
1105 let (tx, rx) = channel::<int>();
1109 assert_eq!(rx.recv(), 1);
1112 test!(fn smoke_port_gone() {
1113 let (tx, rx) = channel::<int>();
1118 test!(fn smoke_shared_port_gone() {
1119 let (tx, rx) = channel::<int>();
1124 test!(fn smoke_shared_port_gone2() {
1125 let (tx, rx) = channel::<int>();
1127 let tx2 = tx.clone();
1132 test!(fn port_gone_concurrent() {
1133 let (tx, rx) = channel::<int>();
1140 test!(fn port_gone_concurrent_shared() {
1141 let (tx, rx) = channel::<int>();
1142 let tx2 = tx.clone();
1152 test!(fn smoke_chan_gone() {
1153 let (tx, rx) = channel::<int>();
1158 test!(fn smoke_chan_gone_shared() {
1159 let (tx, rx) = channel::<()>();
1160 let tx2 = tx.clone();
1166 test!(fn chan_gone_concurrent() {
1167 let (tx, rx) = channel::<int>();
1176 let (tx, rx) = channel::<int>();
1178 for _ in range(0u, 10000) { tx.send(1i); }
1180 for _ in range(0u, 10000) {
1181 assert_eq!(rx.recv(), 1);
1185 test!(fn stress_shared() {
1186 static AMT: uint = 10000;
1187 static NTHREADS: uint = 8;
1188 let (tx, rx) = channel::<int>();
1189 let (dtx, drx) = channel::<()>();
1192 for _ in range(0, AMT * NTHREADS) {
1193 assert_eq!(rx.recv(), 1);
1195 match rx.try_recv() {
1202 for _ in range(0, NTHREADS) {
1203 let tx = tx.clone();
1205 for _ in range(0, AMT) { tx.send(1); }
1213 fn send_from_outside_runtime() {
1214 let (tx1, rx1) = channel::<()>();
1215 let (tx2, rx2) = channel::<int>();
1216 let (tx3, rx3) = channel::<()>();
1217 let tx4 = tx3.clone();
1220 for _ in range(0i, 40) {
1221 assert_eq!(rx2.recv(), 1);
1227 for _ in range(0i, 40) {
1237 fn recv_from_outside_runtime() {
1238 let (tx, rx) = channel::<int>();
1239 let (dtx, drx) = channel();
1241 for _ in range(0i, 40) {
1242 assert_eq!(rx.recv(), 1);
1246 for _ in range(0u, 40) {
1254 let (tx1, rx1) = channel::<int>();
1255 let (tx2, rx2) = channel::<int>();
1256 let (tx3, rx3) = channel::<()>();
1257 let tx4 = tx3.clone();
1259 assert_eq!(rx1.recv(), 1);
1265 assert_eq!(rx2.recv(), 2);
1272 test!(fn oneshot_single_thread_close_port_first() {
1273 // Simple test of closing without sending
1274 let (_tx, rx) = channel::<int>();
1278 test!(fn oneshot_single_thread_close_chan_first() {
1279 // Simple test of closing without sending
1280 let (tx, _rx) = channel::<int>();
1284 test!(fn oneshot_single_thread_send_port_close() {
1285 // Testing that the sender cleans up the payload if receiver is closed
1286 let (tx, rx) = channel::<Box<int>>();
1291 test!(fn oneshot_single_thread_recv_chan_close() {
1292 // Receiving on a closed chan will panic
1293 let res = task::try(proc() {
1294 let (tx, rx) = channel::<int>();
1299 assert!(res.is_err());
1302 test!(fn oneshot_single_thread_send_then_recv() {
1303 let (tx, rx) = channel::<Box<int>>();
1305 assert!(rx.recv() == box 10);
1308 test!(fn oneshot_single_thread_try_send_open() {
1309 let (tx, rx) = channel::<int>();
1310 assert!(tx.send_opt(10).is_ok());
1311 assert!(rx.recv() == 10);
1314 test!(fn oneshot_single_thread_try_send_closed() {
1315 let (tx, rx) = channel::<int>();
1317 assert!(tx.send_opt(10).is_err());
1320 test!(fn oneshot_single_thread_try_recv_open() {
1321 let (tx, rx) = channel::<int>();
1323 assert!(rx.recv_opt() == Ok(10));
1326 test!(fn oneshot_single_thread_try_recv_closed() {
1327 let (tx, rx) = channel::<int>();
1329 assert!(rx.recv_opt() == Err(()));
1332 test!(fn oneshot_single_thread_peek_data() {
1333 let (tx, rx) = channel::<int>();
1334 assert_eq!(rx.try_recv(), Err(Empty))
1336 assert_eq!(rx.try_recv(), Ok(10));
1339 test!(fn oneshot_single_thread_peek_close() {
1340 let (tx, rx) = channel::<int>();
1342 assert_eq!(rx.try_recv(), Err(Disconnected));
1343 assert_eq!(rx.try_recv(), Err(Disconnected));
1346 test!(fn oneshot_single_thread_peek_open() {
1347 let (_tx, rx) = channel::<int>();
1348 assert_eq!(rx.try_recv(), Err(Empty));
1351 test!(fn oneshot_multi_task_recv_then_send() {
1352 let (tx, rx) = channel::<Box<int>>();
1354 assert!(rx.recv() == box 10);
1360 test!(fn oneshot_multi_task_recv_then_close() {
1361 let (tx, rx) = channel::<Box<int>>();
1365 let res = task::try(proc() {
1366 assert!(rx.recv() == box 10);
1368 assert!(res.is_err());
1371 test!(fn oneshot_multi_thread_close_stress() {
1372 for _ in range(0, stress_factor()) {
1373 let (tx, rx) = channel::<int>();
1381 test!(fn oneshot_multi_thread_send_close_stress() {
1382 for _ in range(0, stress_factor()) {
1383 let (tx, rx) = channel::<int>();
1387 let _ = task::try(proc() {
1393 test!(fn oneshot_multi_thread_recv_close_stress() {
1394 for _ in range(0, stress_factor()) {
1395 let (tx, rx) = channel::<int>();
1397 let res = task::try(proc() {
1400 assert!(res.is_err());
1410 test!(fn oneshot_multi_thread_send_recv_stress() {
1411 for _ in range(0, stress_factor()) {
1412 let (tx, rx) = channel();
1417 assert!(rx.recv() == box 10i);
1422 test!(fn stream_send_recv_stress() {
1423 for _ in range(0, stress_factor()) {
1424 let (tx, rx) = channel();
1429 fn send(tx: Sender<Box<int>>, i: int) {
1430 if i == 10 { return }
1438 fn recv(rx: Receiver<Box<int>>, i: int) {
1439 if i == 10 { return }
1442 assert!(rx.recv() == box i);
1449 test!(fn recv_a_lot() {
1450 // Regression test that we don't run out of stack in scheduler context
1451 let (tx, rx) = channel();
1452 for _ in range(0i, 10000) { tx.send(()); }
1453 for _ in range(0i, 10000) { rx.recv(); }
1456 test!(fn shared_chan_stress() {
1457 let (tx, rx) = channel();
1458 let total = stress_factor() + 100;
1459 for _ in range(0, total) {
1460 let tx = tx.clone();
1466 for _ in range(0, total) {
1471 test!(fn test_nested_recv_iter() {
1472 let (tx, rx) = channel::<int>();
1473 let (total_tx, total_rx) = channel::<int>();
1477 for x in rx.iter() {
1487 assert_eq!(total_rx.recv(), 6);
1490 test!(fn test_recv_iter_break() {
1491 let (tx, rx) = channel::<int>();
1492 let (count_tx, count_rx) = channel();
1496 for x in rx.iter() {
1503 count_tx.send(count);
1509 let _ = tx.send_opt(2);
1511 assert_eq!(count_rx.recv(), 4);
1514 test!(fn try_recv_states() {
1515 let (tx1, rx1) = channel::<int>();
1516 let (tx2, rx2) = channel::<()>();
1517 let (tx3, rx3) = channel::<()>();
1527 assert_eq!(rx1.try_recv(), Err(Empty));
1530 assert_eq!(rx1.try_recv(), Ok(1));
1531 assert_eq!(rx1.try_recv(), Err(Empty));
1534 assert_eq!(rx1.try_recv(), Err(Disconnected));
1537 // This bug used to end up in a livelock inside of the Receiver destructor
1538 // because the internal state of the Shared packet was corrupted
1539 test!(fn destroy_upgraded_shared_port_when_sender_still_active() {
1540 let (tx, rx) = channel();
1541 let (tx2, rx2) = channel();
1543 rx.recv(); // wait on a oneshot
1544 drop(rx); // destroy a shared
1547 // make sure the other task has gone to sleep
1548 for _ in range(0u, 5000) { task::deschedule(); }
1550 // upgrade to a shared chan and send a message
1555 // wait for the child task to exit before we exit
1559 test!(fn sends_off_the_runtime() {
1560 use std::rt::thread::Thread;
1562 let (tx, rx) = channel();
1563 let t = Thread::start(proc() {
1564 for _ in range(0u, 1000) {
1568 for _ in range(0u, 1000) {
1574 test!(fn try_recvs_off_the_runtime() {
1575 use std::rt::thread::Thread;
1577 let (tx, rx) = channel();
1578 let (cdone, pdone) = channel();
1579 let t = Thread::start(proc() {
1582 match rx.try_recv() {
1583 Ok(()) => { hits += 1; }
1584 Err(Empty) => { Thread::yield_now(); }
1585 Err(Disconnected) => return,
1590 for _ in range(0u, 10) {
1600 use std::prelude::*;
1603 pub fn stress_factor() -> uint {
1604 match os::getenv("RUST_TEST_STRESS") {
1605 Some(val) => from_str::<uint>(val.as_slice()).unwrap(),
1611 let (tx, rx) = sync_channel::<int>(1);
1613 assert_eq!(rx.recv(), 1);
1616 test!(fn drop_full() {
1617 let (tx, _rx) = sync_channel(1);
1621 test!(fn smoke_shared() {
1622 let (tx, rx) = sync_channel::<int>(1);
1624 assert_eq!(rx.recv(), 1);
1625 let tx = tx.clone();
1627 assert_eq!(rx.recv(), 1);
1630 test!(fn smoke_threads() {
1631 let (tx, rx) = sync_channel::<int>(0);
1635 assert_eq!(rx.recv(), 1);
1638 test!(fn smoke_port_gone() {
1639 let (tx, rx) = sync_channel::<int>(0);
1644 test!(fn smoke_shared_port_gone2() {
1645 let (tx, rx) = sync_channel::<int>(0);
1647 let tx2 = tx.clone();
1652 test!(fn port_gone_concurrent() {
1653 let (tx, rx) = sync_channel::<int>(0);
1660 test!(fn port_gone_concurrent_shared() {
1661 let (tx, rx) = sync_channel::<int>(0);
1662 let tx2 = tx.clone();
1672 test!(fn smoke_chan_gone() {
1673 let (tx, rx) = sync_channel::<int>(0);
1678 test!(fn smoke_chan_gone_shared() {
1679 let (tx, rx) = sync_channel::<()>(0);
1680 let tx2 = tx.clone();
1686 test!(fn chan_gone_concurrent() {
1687 let (tx, rx) = sync_channel::<int>(0);
1696 let (tx, rx) = sync_channel::<int>(0);
1698 for _ in range(0u, 10000) { tx.send(1); }
1700 for _ in range(0u, 10000) {
1701 assert_eq!(rx.recv(), 1);
1705 test!(fn stress_shared() {
1706 static AMT: uint = 1000;
1707 static NTHREADS: uint = 8;
1708 let (tx, rx) = sync_channel::<int>(0);
1709 let (dtx, drx) = sync_channel::<()>(0);
1712 for _ in range(0, AMT * NTHREADS) {
1713 assert_eq!(rx.recv(), 1);
1715 match rx.try_recv() {
1722 for _ in range(0, NTHREADS) {
1723 let tx = tx.clone();
1725 for _ in range(0, AMT) { tx.send(1); }
1732 test!(fn oneshot_single_thread_close_port_first() {
1733 // Simple test of closing without sending
1734 let (_tx, rx) = sync_channel::<int>(0);
1738 test!(fn oneshot_single_thread_close_chan_first() {
1739 // Simple test of closing without sending
1740 let (tx, _rx) = sync_channel::<int>(0);
1744 test!(fn oneshot_single_thread_send_port_close() {
1745 // Testing that the sender cleans up the payload if receiver is closed
1746 let (tx, rx) = sync_channel::<Box<int>>(0);
1751 test!(fn oneshot_single_thread_recv_chan_close() {
1752 // Receiving on a closed chan will panic
1753 let res = task::try(proc() {
1754 let (tx, rx) = sync_channel::<int>(0);
1759 assert!(res.is_err());
1762 test!(fn oneshot_single_thread_send_then_recv() {
1763 let (tx, rx) = sync_channel::<Box<int>>(1);
1765 assert!(rx.recv() == box 10);
1768 test!(fn oneshot_single_thread_try_send_open() {
1769 let (tx, rx) = sync_channel::<int>(1);
1770 assert_eq!(tx.try_send(10), Ok(()));
1771 assert!(rx.recv() == 10);
1774 test!(fn oneshot_single_thread_try_send_closed() {
1775 let (tx, rx) = sync_channel::<int>(0);
1777 assert_eq!(tx.try_send(10), Err(RecvDisconnected(10)));
1780 test!(fn oneshot_single_thread_try_send_closed2() {
1781 let (tx, _rx) = sync_channel::<int>(0);
1782 assert_eq!(tx.try_send(10), Err(Full(10)));
1785 test!(fn oneshot_single_thread_try_recv_open() {
1786 let (tx, rx) = sync_channel::<int>(1);
1788 assert!(rx.recv_opt() == Ok(10));
1791 test!(fn oneshot_single_thread_try_recv_closed() {
1792 let (tx, rx) = sync_channel::<int>(0);
1794 assert!(rx.recv_opt() == Err(()));
1797 test!(fn oneshot_single_thread_peek_data() {
1798 let (tx, rx) = sync_channel::<int>(1);
1799 assert_eq!(rx.try_recv(), Err(Empty))
1801 assert_eq!(rx.try_recv(), Ok(10));
1804 test!(fn oneshot_single_thread_peek_close() {
1805 let (tx, rx) = sync_channel::<int>(0);
1807 assert_eq!(rx.try_recv(), Err(Disconnected));
1808 assert_eq!(rx.try_recv(), Err(Disconnected));
1811 test!(fn oneshot_single_thread_peek_open() {
1812 let (_tx, rx) = sync_channel::<int>(0);
1813 assert_eq!(rx.try_recv(), Err(Empty));
1816 test!(fn oneshot_multi_task_recv_then_send() {
1817 let (tx, rx) = sync_channel::<Box<int>>(0);
1819 assert!(rx.recv() == box 10);
1825 test!(fn oneshot_multi_task_recv_then_close() {
1826 let (tx, rx) = sync_channel::<Box<int>>(0);
1830 let res = task::try(proc() {
1831 assert!(rx.recv() == box 10);
1833 assert!(res.is_err());
1836 test!(fn oneshot_multi_thread_close_stress() {
1837 for _ in range(0, stress_factor()) {
1838 let (tx, rx) = sync_channel::<int>(0);
1846 test!(fn oneshot_multi_thread_send_close_stress() {
1847 for _ in range(0, stress_factor()) {
1848 let (tx, rx) = sync_channel::<int>(0);
1852 let _ = task::try(proc() {
1858 test!(fn oneshot_multi_thread_recv_close_stress() {
1859 for _ in range(0, stress_factor()) {
1860 let (tx, rx) = sync_channel::<int>(0);
1862 let res = task::try(proc() {
1865 assert!(res.is_err());
1875 test!(fn oneshot_multi_thread_send_recv_stress() {
1876 for _ in range(0, stress_factor()) {
1877 let (tx, rx) = sync_channel::<Box<int>>(0);
1882 assert!(rx.recv() == box 10i);
1887 test!(fn stream_send_recv_stress() {
1888 for _ in range(0, stress_factor()) {
1889 let (tx, rx) = sync_channel::<Box<int>>(0);
1894 fn send(tx: SyncSender<Box<int>>, i: int) {
1895 if i == 10 { return }
1903 fn recv(rx: Receiver<Box<int>>, i: int) {
1904 if i == 10 { return }
1907 assert!(rx.recv() == box i);
1914 test!(fn recv_a_lot() {
1915 // Regression test that we don't run out of stack in scheduler context
1916 let (tx, rx) = sync_channel(10000);
1917 for _ in range(0u, 10000) { tx.send(()); }
1918 for _ in range(0u, 10000) { rx.recv(); }
1921 test!(fn shared_chan_stress() {
1922 let (tx, rx) = sync_channel(0);
1923 let total = stress_factor() + 100;
1924 for _ in range(0, total) {
1925 let tx = tx.clone();
1931 for _ in range(0, total) {
1936 test!(fn test_nested_recv_iter() {
1937 let (tx, rx) = sync_channel::<int>(0);
1938 let (total_tx, total_rx) = sync_channel::<int>(0);
1942 for x in rx.iter() {
1952 assert_eq!(total_rx.recv(), 6);
1955 test!(fn test_recv_iter_break() {
1956 let (tx, rx) = sync_channel::<int>(0);
1957 let (count_tx, count_rx) = sync_channel(0);
1961 for x in rx.iter() {
1968 count_tx.send(count);
1974 let _ = tx.try_send(2);
1976 assert_eq!(count_rx.recv(), 4);
1979 test!(fn try_recv_states() {
1980 let (tx1, rx1) = sync_channel::<int>(1);
1981 let (tx2, rx2) = sync_channel::<()>(1);
1982 let (tx3, rx3) = sync_channel::<()>(1);
1992 assert_eq!(rx1.try_recv(), Err(Empty));
1995 assert_eq!(rx1.try_recv(), Ok(1));
1996 assert_eq!(rx1.try_recv(), Err(Empty));
1999 assert_eq!(rx1.try_recv(), Err(Disconnected));
2002 // This bug used to end up in a livelock inside of the Receiver destructor
2003 // because the internal state of the Shared packet was corrupted
2004 test!(fn destroy_upgraded_shared_port_when_sender_still_active() {
2005 let (tx, rx) = sync_channel::<()>(0);
2006 let (tx2, rx2) = sync_channel::<()>(0);
2008 rx.recv(); // wait on a oneshot
2009 drop(rx); // destroy a shared
2012 // make sure the other task has gone to sleep
2013 for _ in range(0u, 5000) { task::deschedule(); }
2015 // upgrade to a shared chan and send a message
2020 // wait for the child task to exit before we exit
2024 test!(fn try_recvs_off_the_runtime() {
2025 use std::rt::thread::Thread;
2027 let (tx, rx) = sync_channel::<()>(0);
2028 let (cdone, pdone) = channel();
2029 let t = Thread::start(proc() {
2032 match rx.try_recv() {
2033 Ok(()) => { hits += 1; }
2034 Err(Empty) => { Thread::yield_now(); }
2035 Err(Disconnected) => return,
2040 for _ in range(0u, 10) {
2047 test!(fn send_opt1() {
2048 let (tx, rx) = sync_channel::<int>(0);
2049 spawn(proc() { rx.recv(); });
2050 assert_eq!(tx.send_opt(1), Ok(()));
2053 test!(fn send_opt2() {
2054 let (tx, rx) = sync_channel::<int>(0);
2055 spawn(proc() { drop(rx); });
2056 assert_eq!(tx.send_opt(1), Err(1));
2059 test!(fn send_opt3() {
2060 let (tx, rx) = sync_channel::<int>(1);
2061 assert_eq!(tx.send_opt(1), Ok(()));
2062 spawn(proc() { drop(rx); });
2063 assert_eq!(tx.send_opt(1), Err(1));
2066 test!(fn send_opt4() {
2067 let (tx, rx) = sync_channel::<int>(0);
2068 let tx2 = tx.clone();
2069 let (done, donerx) = channel();
2070 let done2 = done.clone();
2072 assert_eq!(tx.send_opt(1), Err(1));
2076 assert_eq!(tx2.send_opt(2), Err(2));
2084 test!(fn try_send1() {
2085 let (tx, _rx) = sync_channel::<int>(0);
2086 assert_eq!(tx.try_send(1), Err(Full(1)));
2089 test!(fn try_send2() {
2090 let (tx, _rx) = sync_channel::<int>(1);
2091 assert_eq!(tx.try_send(1), Ok(()));
2092 assert_eq!(tx.try_send(1), Err(Full(1)));
2095 test!(fn try_send3() {
2096 let (tx, rx) = sync_channel::<int>(1);
2097 assert_eq!(tx.try_send(1), Ok(()));
2099 assert_eq!(tx.try_send(1), Err(RecvDisconnected(1)));
2102 test!(fn try_send4() {
2103 let (tx, rx) = sync_channel::<int>(0);
2105 for _ in range(0u, 1000) { task::deschedule(); }
2106 assert_eq!(tx.try_send(1), Ok(()));
2108 assert_eq!(rx.recv(), 1);
2109 } #[ignore(reason = "flaky on libnative")])
2111 test!(fn issue_15761() {
2113 let (tx1, rx1) = sync_channel::<()>(3);
2114 let (tx2, rx2) = sync_channel::<()>(3);
2118 tx2.try_send(()).unwrap();
2121 tx1.try_send(()).unwrap();
2125 for _ in range(0u, 100) {