Skip to content

Commit 31ca7c5

Browse files
maciejbacalextvinodreddy-g
authored andcommitted
orch: Add an async select macro
Added a basic implementation of the async select macro. No conditionals or a default case for now.
1 parent 8d89591 commit 31ca7c5

4 files changed

Lines changed: 769 additions & 0 deletions

File tree

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
//
2+
// Copyright (c) 2025 Contributors to the Eclipse Foundation
3+
//
4+
// See the NOTICE file(s) distributed with this work for additional
5+
// information regarding copyright ownership.
6+
//
7+
// This program and the accompanying materials are made available under the
8+
// terms of the Apache License Version 2.0 which is available at
9+
// <https://www.apache.org/licenses/LICENSE-2.0>
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
14+
use async_runtime::{
15+
prelude::*,
16+
runtime::async_runtime::AsyncRuntimeBuilder,
17+
safety::{ensure_safety_enabled, spawn, SafetyResult},
18+
scheduler::execution_engine::*,
19+
select,
20+
};
21+
use foundation::prelude::*;
22+
23+
fn main() {
24+
tracing_subscriber::fmt()
25+
// .with_span_events(FmtSpan::FULL) // Ensures span open/close events are logged
26+
.with_target(false) // Optional: Remove module path
27+
.with_max_level(Level::DEBUG)
28+
.with_thread_ids(true)
29+
.with_thread_names(true)
30+
.init();
31+
32+
let (builder, _engine_id) = AsyncRuntimeBuilder::new().with_engine(
33+
ExecutionEngineBuilder::new()
34+
.task_queue_size(256)
35+
.workers(4)
36+
.with_dedicated_worker("dedicated".into(), ThreadParameters::default())
37+
.enable_safety_worker(ThreadParameters::default()),
38+
);
39+
let mut runtime = builder.build().unwrap();
40+
41+
let _ = runtime.block_on(async {
42+
ensure_safety_enabled();
43+
44+
async fn short_job(result: i32) -> SafetyResult<i32, ()> {
45+
for _ in 0..1_000_usize {}
46+
Ok(result)
47+
}
48+
49+
async fn long_job(result: i32) -> SafetyResult<i32, ()> {
50+
for _ in 0..1_000_000_usize {}
51+
Ok(result)
52+
}
53+
54+
async fn very_long_job(result: i32) -> SafetyResult<i32, ()> {
55+
for _ in 0..1_000_000_000_usize {}
56+
Ok(result)
57+
}
58+
59+
// First job should finish first.
60+
{
61+
info!("Test 1");
62+
63+
let mut fut1 = spawn(short_job(111));
64+
let mut fut2 = spawn(long_job(222));
65+
let mut fut3 = spawn(long_job(333));
66+
let mut result = 0;
67+
68+
select! {
69+
Ok(Ok(var1)) = fut1 => {
70+
result = var1;
71+
}
72+
Ok(Ok(var2)) = fut2 => {
73+
result = var2;
74+
}
75+
Ok(Ok(var3)) = fut3 => {
76+
result = var3;
77+
}
78+
}
79+
80+
assert_eq!(result, 111);
81+
}
82+
83+
// Second job should finish first.
84+
{
85+
info!("Test 2");
86+
87+
let mut fut1 = spawn(long_job(111));
88+
let mut fut2 = spawn(short_job(222));
89+
let mut fut3 = spawn(long_job(333));
90+
let mut result = 0;
91+
92+
select! {
93+
Ok(Ok(var1)) = fut1 => {
94+
result = var1;
95+
}
96+
Ok(Ok(var2)) = fut2 => {
97+
result = var2;
98+
}
99+
Ok(Ok(var3)) = fut3 => {
100+
result = var3;
101+
}
102+
}
103+
104+
assert_eq!(result, 222);
105+
}
106+
107+
// First job should finish first, but it won't match, so the third job should match first.
108+
{
109+
info!("Test 3");
110+
111+
let mut fut1 = spawn(short_job(111));
112+
let mut fut2 = spawn(very_long_job(222));
113+
let mut fut3 = spawn(long_job(333));
114+
let mut result = 0;
115+
116+
select! {
117+
Ok(Err(())) = fut1 => {
118+
result = 111;
119+
}
120+
Ok(Ok(var2)) = fut2 => {
121+
result = var2;
122+
}
123+
Ok(Ok(var3)) = fut3 => {
124+
result = var3;
125+
}
126+
}
127+
128+
assert_eq!(result, 333);
129+
}
130+
131+
Ok(0)
132+
});
133+
}

src/async_runtime/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ pub mod core;
5858
pub mod futures;
5959
pub mod io;
6060
pub mod ipc;
61+
pub mod macros;
6162
pub mod mio;
6263
pub mod net;
6364
pub mod prelude;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// Copyright (c) 2025 Contributors to the Eclipse Foundation
3+
//
4+
// See the NOTICE file(s) distributed with this work for additional
5+
// information regarding copyright ownership.
6+
//
7+
// This program and the accompanying materials are made available under the
8+
// terms of the Apache License Version 2.0 which is available at
9+
// <https://www.apache.org/licenses/LICENSE-2.0>
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
14+
pub mod select;

0 commit comments

Comments
 (0)