twizzler_rt_abi/
thread.rs1#![allow(unused_variables)]
4use core::time::Duration;
5
6use crate::{error::RawTwzError, Result};
7
8pub type ThreadId = crate::bindings::thread_id;
10pub type TlsIndex = crate::bindings::tls_index;
12pub type TlsDesc = crate::bindings::tls_desc;
14pub type FutexWord = crate::bindings::futex_word;
16pub type AtomicFutexWord = core::sync::atomic::AtomicU32;
18pub type ThreadSpawnArgs = crate::bindings::spawn_args;
20
21impl From<Result<ThreadId>> for crate::bindings::spawn_result {
22 fn from(value: Result<ThreadId>) -> Self {
23 match value {
24 Ok(id) => Self {
25 id,
26 err: RawTwzError::success().raw(),
27 },
28 Err(e) => Self {
29 id: 0,
30 err: e.raw(),
31 },
32 }
33 }
34}
35
36impl From<crate::bindings::spawn_result> for Result<ThreadId> {
37 fn from(value: crate::bindings::spawn_result) -> Self {
38 let raw = RawTwzError::new(value.err);
39 if raw.is_success() {
40 Ok(value.id)
41 } else {
42 Err(raw.error())
43 }
44 }
45}
46
47pub fn twz_rt_futex_wait(
51 word: &AtomicFutexWord,
52 expected: FutexWord,
53 timeout: Option<Duration>,
54) -> bool {
55 unsafe { crate::bindings::twz_rt_futex_wait(word.as_ptr().cast(), expected, timeout.into()) }
56}
57
58pub fn twz_rt_futex_wake(word: &AtomicFutexWord, max: Option<usize>) -> bool {
60 let max = match max {
61 Some(max) => max as i64,
62 None => crate::bindings::FUTEX_WAKE_ALL,
63 };
64 unsafe { crate::bindings::twz_rt_futex_wake(word.as_ptr().cast(), max) }
65}
66
67pub fn twz_rt_yield() {
69 unsafe {
70 crate::bindings::twz_rt_yield_now();
71 }
72}
73
74pub fn twz_rt_sleep(dur: Duration) {
76 unsafe {
77 crate::bindings::twz_rt_sleep(dur.into());
78 }
79}
80
81pub fn twz_rt_set_thread_name(name: &core::ffi::CStr) {
83 unsafe {
84 crate::bindings::twz_rt_set_name(name.as_ptr());
85 }
86}
87
88pub fn twz_rt_tls_get_addr(index: &TlsIndex) -> *mut u8 {
90 unsafe { crate::bindings::twz_rt_tls_get_addr(index as *const _ as *mut _).cast() }
91}
92
93pub fn twz_rt_spawn_thread(args: ThreadSpawnArgs) -> Result<ThreadId> {
96 unsafe { crate::bindings::twz_rt_spawn_thread(args).into() }
97}
98
99pub fn twz_rt_join_thread(id: ThreadId, timeout: Option<Duration>) -> Result<()> {
101 unsafe { RawTwzError::new(crate::bindings::twz_rt_join_thread(id, timeout.into())).result() }
102}