summaryrefslogtreecommitdiffstats
path: root/tokio/src/util
diff options
context:
space:
mode:
authorCarl Lerche <me@carllerche.com>2020-01-22 18:59:22 -0800
committerGitHub <noreply@github.com>2020-01-22 18:59:22 -0800
commit8cf98d694663e8397bfc337e7a4676567287bfae (patch)
tree50b214c2e29a257633494a64eed35c5b6f568eba /tokio/src/util
parentf9ea576ccae5beffeaa2f2c48c2c0d2f9449673b (diff)
Provide `select!` macro (#2152)
Provides a `select!` macro for concurrently waiting on multiple async expressions. The macro has similar goals and syntax as the one provided by the `futures` crate, but differs significantly in implementation. First, this implementation does not require special traits to be implemented on futures or streams (i.e., no `FuseFuture`). A design goal is to be able to pass a "plain" async fn result into the select! macro. Even without `FuseFuture`, this `select!` implementation is able to handle all cases the `futures::select!` macro can handle. It does this by supporting pre-poll conditions on branches and result pattern matching. For pre-conditions, each branch is able to include a condition that disables the branch if it evaluates to false. This allows the user to guard futures that have already been polled, preventing double polling. Pattern matching can be used to disable streams that complete. A second big difference is the macro is implemented almost entirely as a declarative macro. The biggest advantage to using this strategy is that the user will not need to alter the rustc recursion limit except in the most extreme cases. The resulting future also tends to be smaller in many cases.
Diffstat (limited to 'tokio/src/util')
-rw-r--r--tokio/src/util/mod.rs8
-rw-r--r--tokio/src/util/rand.rs13
2 files changed, 20 insertions, 1 deletions
diff --git a/tokio/src/util/mod.rs b/tokio/src/util/mod.rs
index 7b4a12e5..fa904c3d 100644
--- a/tokio/src/util/mod.rs
+++ b/tokio/src/util/mod.rs
@@ -3,13 +3,19 @@ cfg_io_driver! {
pub(crate) mod slab;
}
+#[cfg(any(feature = "rt-threaded", feature = "macros"))]
+mod rand;
+
cfg_rt_threaded! {
mod pad;
pub(crate) use pad::CachePadded;
- mod rand;
pub(crate) use rand::FastRand;
mod try_lock;
pub(crate) use try_lock::TryLock;
}
+
+cfg_macros! {
+ pub use rand::thread_rng_n;
+}
diff --git a/tokio/src/util/rand.rs b/tokio/src/util/rand.rs
index 11617e26..101f5bb6 100644
--- a/tokio/src/util/rand.rs
+++ b/tokio/src/util/rand.rs
@@ -50,3 +50,16 @@ impl FastRand {
s0.wrapping_add(s1)
}
}
+
+// Used by the select macro
+cfg_macros! {
+ thread_local! {
+ static THREAD_RNG: FastRand = FastRand::new(crate::loom::rand::seed());
+ }
+
+ // Used by macros
+ #[doc(hidden)]
+ pub fn thread_rng_n(n: u32) -> u32 {
+ THREAD_RNG.with(|rng| rng.fastrand_n(n))
+ }
+}