Skip to content

Publish version 0.2 #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
[package]
name = "ticked_async_executor"
version = "0.1.0"
version = "0.2.0"
authors = ["coder137"]
edition = "2021"
description = "Local executor that runs woken async tasks when it is ticked"
license = "Apache-2.0"
repository = "https://github.com/coder137/ticked-async-executor"
categories = ["asynchronous", "concurrency", "game-development", "simulation"]
readme = "README.md"

[dependencies]
async-task = "4.7"
pin-project = "1"

# For timer only
# TODO, Add this under a feature gate
# TODO, Only tokio::sync::watch channel is used (find individual dependency)
tokio = { version = "1.0", default-features = false, features = ["sync"] }
tokio = { version = "1", default-features = false, features = ["sync"] }

[dev-dependencies]
tokio = { version = "1", features = ["full"] }
65 changes: 59 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,71 @@
# Ticked Async Executor

Rust based Async Executor which executes woken tasks only when it is ticked
Async Local Executor which executes woken tasks only when it is ticked

# Example
# Usage

## Default Local Executor

```rust
use ticked_async_executor::*;

const DELTA: f64 = 1000.0 / 60.0;

let executor = TickedAsyncExecutor::default();

executor.spawn_local("MyIdentifier", async move {}).detach();

// Make sure to tick your executor to run the tasks
executor.tick(DELTA, LIMIT);
// Tick your executor to run the tasks
assert_eq!(executor.num_tasks(), 1);
executor.tick(DELTA, None);
assert_eq!(executor.num_tasks(), 0);
```

## Split Local Executor

```rust
use ticked_async_executor::*;

const DELTA: f64 = 1000.0 / 60.0;

let (spawner, ticker) = SplitTickedAsyncExecutor::default();

spawner.spawn_local("MyIdentifier", async move {}).detach();

// Tick your ticker to run the tasks
assert_eq!(spawner.num_tasks(), 1);
ticker.tick(DELTA, None);
assert_eq!(spawner.num_tasks(), 0);
```

# Limitation
## Limit the number of woken tasks run per tick

```rust
use ticked_async_executor::*;

const DELTA: f64 = 1000.0 / 60.0;

let executor = TickedAsyncExecutor::default();

executor.spawn_local("MyIdentifier1", async move {}).detach();
executor.spawn_local("MyIdentifier2", async move {}).detach();

// Runs upto 1 woken tasks per tick
assert_eq!(executor.num_tasks(), 2);
executor.tick(DELTA, Some(1));
assert_eq!(executor.num_tasks(), 1);
executor.tick(DELTA, Some(1));
assert_eq!(executor.num_tasks(), 0);
```

# Caveats

- Uses the `smol` ecosystem
- Ensure that tasks are spawned on the same thread as the one that initializes the executor

# Roadmap

- Does not work with the tokio runtime and async constructs that use the tokio runtime internally
- [x] TickedAsyncExecutor
- [x] SplitTickedAsyncExecutor
- Similar to the channel API, but spawner and ticker cannot be moved to different threads
- [ ] Tracing
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![doc = include_str!("../README.md")]

mod droppable_future;
use droppable_future::*;

Expand Down
59 changes: 34 additions & 25 deletions src/split_ticked_async_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,40 @@
pub type Task<T> = async_task::Task<T>;
type Payload = (TaskIdentifier, async_task::Runnable);

pub fn new_split_ticked_async_executor<O>(
observer: O,
) -> (TickedAsyncExecutorSpawner<O>, TickedAsyncExecutorTicker<O>)
where
O: Fn(TaskState) + Clone + Send + Sync + 'static,
{
let (tx_channel, rx_channel) = mpsc::channel();
let num_woken_tasks = Arc::new(AtomicUsize::new(0));
let num_spawned_tasks = Arc::new(AtomicUsize::new(0));
let (tx_tick_event, rx_tick_event) = tokio::sync::watch::channel(1.0);
let spawner = TickedAsyncExecutorSpawner {
tx_channel,
num_woken_tasks: num_woken_tasks.clone(),
num_spawned_tasks: num_spawned_tasks.clone(),
observer: observer.clone(),
rx_tick_event,
};
let ticker = TickedAsyncExecutorTicker {
rx_channel,
num_woken_tasks,
num_spawned_tasks,
observer,
tx_tick_event,
};
(spawner, ticker)
pub struct SplitTickedAsyncExecutor;

impl SplitTickedAsyncExecutor {
pub fn default() -> (

Check warning on line 25 in src/split_ticked_async_executor.rs

View check run for this annotation

Codecov / codecov/patch

src/split_ticked_async_executor.rs#L25

Added line #L25 was not covered by tests
TickedAsyncExecutorSpawner<fn(TaskState)>,
TickedAsyncExecutorTicker<fn(TaskState)>,
) {
Self::new(|_state| {})

Check warning on line 29 in src/split_ticked_async_executor.rs

View check run for this annotation

Codecov / codecov/patch

src/split_ticked_async_executor.rs#L29

Added line #L29 was not covered by tests
}

pub fn new<O>(observer: O) -> (TickedAsyncExecutorSpawner<O>, TickedAsyncExecutorTicker<O>)
where
O: Fn(TaskState) + Clone + Send + Sync + 'static,
{
let (tx_channel, rx_channel) = mpsc::channel();
let num_woken_tasks = Arc::new(AtomicUsize::new(0));
let num_spawned_tasks = Arc::new(AtomicUsize::new(0));
let (tx_tick_event, rx_tick_event) = tokio::sync::watch::channel(1.0);
let spawner = TickedAsyncExecutorSpawner {
tx_channel,
num_woken_tasks: num_woken_tasks.clone(),
num_spawned_tasks: num_spawned_tasks.clone(),
observer: observer.clone(),
rx_tick_event,
};
let ticker = TickedAsyncExecutorTicker {
rx_channel,
num_woken_tasks,
num_spawned_tasks,
observer,
tx_tick_event,
};
(spawner, ticker)
}
}

pub struct TickedAsyncExecutorSpawner<O> {
Expand Down
4 changes: 2 additions & 2 deletions src/ticked_async_executor.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::future::Future;

use crate::{
new_split_ticked_async_executor, Task, TaskIdentifier, TaskState, TickedAsyncExecutorSpawner,
SplitTickedAsyncExecutor, Task, TaskIdentifier, TaskState, TickedAsyncExecutorSpawner,
TickedAsyncExecutorTicker, TickedTimer,
};

Expand All @@ -21,7 +21,7 @@ where
O: Fn(TaskState) + Clone + Send + Sync + 'static,
{
pub fn new(observer: O) -> Self {
let (spawner, ticker) = new_split_ticked_async_executor(observer);
let (spawner, ticker) = SplitTickedAsyncExecutor::new(observer);
Self { spawner, ticker }
}

Expand Down