Skip to content

Commit

Permalink
Move RNG generation into the block runtime.
Browse files Browse the repository at this point in the history
  • Loading branch information
artob committed Jul 28, 2024
1 parent 25ba885 commit 378a7ac
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
8 changes: 7 additions & 1 deletion lib/protoflow/src/block_runtime.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
// This is free and unencumbered software released into the public domain.

use crate::{
prelude::{Duration, Instant},
prelude::{Duration, Instant, Range},
BlockError, Port,
};

pub trait BlockRuntime: Send + Sync {
fn is_alive(&self) -> bool;

fn sleep_for(&self, duration: Duration) -> Result<(), BlockError>;

fn sleep_until(&self, instant: Instant) -> Result<(), BlockError>; // TODO

fn wait_for(&self, port: &dyn Port) -> Result<(), BlockError>;

fn yield_now(&self) -> Result<(), BlockError>;

fn random_duration(&self, range: Range<Duration>) -> Duration;
}
16 changes: 1 addition & 15 deletions lib/protoflow/src/blocks/delay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ use protoflow::{
Block, BlockError, BlockRuntime, InputPort, Message, OutputPort, Port,
};

#[cfg(all(feature = "std", feature = "rand"))]
use rand::Rng;

/// A block that passes messages through while delaying them by a fixed or
/// random duration.
#[derive(Block, Clone)]
Expand Down Expand Up @@ -45,18 +42,7 @@ impl<T: Message> Block for Delay<T> {

let duration = match self.delay {
DelayType::Fixed(duration) => duration,
#[allow(unused_variables)]
DelayType::Random(ref range) => {
#[cfg(all(feature = "std", feature = "rand"))]
{
let mut rng = rand::thread_rng();
let low = range.start.as_nanos() as u64;
let high = range.end.as_nanos() as u64;
Duration::from_nanos(rng.gen_range(low..high))
}
#[cfg(not(all(feature = "std", feature = "rand")))]
let mut _rng = todo!();
}
DelayType::Random(ref range) => runtime.random_duration(range.clone()),
};
runtime.sleep_for(duration)?;

Expand Down
16 changes: 15 additions & 1 deletion lib/protoflow/src/runtimes/std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

use crate::{
prelude::{
Arc, AtomicBool, AtomicUsize, Box, Duration, Instant, Ordering, Rc, RefCell, ToString, Vec,
Arc, AtomicBool, AtomicUsize, Box, Duration, Instant, Ordering, Range, Rc, RefCell,
ToString, Vec,
},
transport::Transport,
Block, BlockError, BlockResult, BlockRuntime, Port, Process, ProcessID, Runtime, System,
Expand Down Expand Up @@ -107,6 +108,19 @@ impl BlockRuntime for Arc<StdRuntime> {
unimplemented!("std::thread::yield_now requires the 'std' feature");
Ok(())
}

fn random_duration(&self, range: Range<Duration>) -> Duration {
#[cfg(all(feature = "std", feature = "rand"))]
{
use rand::Rng;
let mut rng = rand::thread_rng();
let low = range.start.as_nanos() as u64;
let high = range.end.as_nanos() as u64;
Duration::from_nanos(rng.gen_range(low..high))
}
#[cfg(not(all(feature = "std", feature = "rand")))]
let mut _rng = todo!();
}
}

#[allow(unused)]
Expand Down

0 comments on commit 378a7ac

Please sign in to comment.