Skip to content

Commit

Permalink
refactor: remove static_context as a convenient method (#11)
Browse files Browse the repository at this point in the history
Signed-off-by: tison <[email protected]>
  • Loading branch information
tisonkun authored Feb 1, 2025
1 parent ed2496d commit b7e9557
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 25 deletions.
12 changes: 9 additions & 3 deletions fastimer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

[package]
name = "fastimer"
version = "0.4.1"
version = "0.5.0"

description = "This crate implements runtime-agnostic driver for async timers and scheduled tasks."

Expand Down Expand Up @@ -48,8 +48,8 @@ tokio = { version = "1.42.0", optional = true }

[dev-dependencies]
log = { version = "0.4.22", features = ["kv"] }
logforth = { version = "0.19.0" }
mea = { version = "0.1.2" }
logforth = { version = "0.21.0" }
mea = { version = "0.2.0" }
tokio = { version = "1.42.0", features = ["full"] }

[lints]
Expand Down Expand Up @@ -79,6 +79,12 @@ name = "schedule_with_fixed_delay"
path = "examples/schedule_with_fixed_delay.rs"
required-features = ["tokio", "logging"]

[[example]]
doc-scrape-examples = true
name = "static_time_driver"
path = "examples/static_time_driver.rs"
required-features = ["driver"]

[[example]]
doc-scrape-examples = true
name = "time_driver"
Expand Down
59 changes: 59 additions & 0 deletions fastimer/examples/static_time_driver.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2024 FastLabs Developers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::sync::LazyLock;
use std::time::Duration;
use std::time::Instant;

use fastimer::driver::TimeContext;
use fastimer::make_instant_from_now;

fn static_context() -> &'static TimeContext {
static CONTEXT: LazyLock<TimeContext> = LazyLock::new(|| {
let (mut driver, context, _) = fastimer::driver::driver();
std::thread::Builder::new()
.name("fastimer-global".to_string())
.spawn(move || loop {
if driver.turn() {
break;
}
})
.expect("cannot spawn fastimer-global thread");
context
});

&CONTEXT
}

fn assert_duration_eq(actual: Duration, expected: Duration) {
if expected.abs_diff(expected) > Duration::from_millis(5) {
panic!("expected: {:?}, actual: {:?}", expected, actual);
}
}

fn main() {
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async move {
let now = Instant::now();

static_context().delay(Duration::from_secs(2)).await;
assert_duration_eq(now.elapsed(), Duration::from_secs(2));

let future = make_instant_from_now(Duration::from_secs(3));
let f1 = static_context().delay_until(future);
let f2 = static_context().delay_until(future);
tokio::join!(f1, f2);
assert_duration_eq(now.elapsed(), Duration::from_secs(3));
});
}
22 changes: 0 additions & 22 deletions fastimer/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use std::pin::Pin;
use std::sync::atomic;
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
use std::sync::LazyLock;
use std::task::Context;
use std::task::Poll;
use std::time::Duration;
Expand Down Expand Up @@ -92,27 +91,6 @@ impl Drop for Delay {
}
}

/// Returns the global time context and shutdown handle.
///
/// The first call to this function initializes the global time driver and spawns a thread to drive
/// it. Subsequent calls return the same context and shutdown handle.
pub fn static_context() -> &'static TimeContext {
static CONTEXT: LazyLock<TimeContext> = LazyLock::new(|| {
let (mut driver, context, _) = driver();
std::thread::Builder::new()
.name("fastimer-global".to_string())
.spawn(move || loop {
if driver.turn() {
break;
}
})
.expect("cannot spawn fastimer-global thread");
context
});

&CONTEXT
}

/// Returns a new time driver, its time context and the shutdown handle.
pub fn driver() -> (TimeDriver, TimeContext, TimeDriverShutdown) {
let (parker, unparker) = parking::pair();
Expand Down

0 comments on commit b7e9557

Please sign in to comment.