twizzler_rt_abi/
thread.rs1#![allow(unused_variables)]
4use core::{ffi::c_void, time::Duration};
5
6use crate::{error::RawTwzError, nk, 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, *mut c_void)>> for crate::bindings::spawn_result {
22 fn from(value: Result<(ThreadId, *mut c_void)>) -> Self {
23 match value {
24 Ok((id, tcb)) => Self {
25 id,
26 tcb,
27 err: RawTwzError::success().raw(),
28 },
29 Err(e) => Self {
30 id: 0,
31 tcb: core::ptr::null_mut(),
32 err: e.raw(),
33 },
34 }
35 }
36}
37
38impl From<crate::bindings::spawn_result> for Result<ThreadId> {
39 fn from(value: crate::bindings::spawn_result) -> Self {
40 let raw = RawTwzError::new(value.err);
41 if raw.is_success() {
42 Ok(value.id)
43 } else {
44 Err(raw.error())
45 }
46 }
47}
48
49pub fn twz_rt_futex_wait(
53 word: &AtomicFutexWord,
54 expected: FutexWord,
55 timeout: Option<Duration>,
56) -> Result<()> {
57 unsafe {
58 match nk!(crate::bindings::twz_rt_futex_wait(
59 word.as_ptr().cast(),
60 expected,
61 timeout.into()
62 )) {
63 0 => Ok(()),
64 e => Err(RawTwzError::new(e).error()),
65 }
66 }
67}
68
69pub fn twz_rt_futex_wake(word: &AtomicFutexWord, max: Option<usize>) -> Result<()> {
71 let max = match max {
72 Some(max) => max as i64,
73 None => crate::bindings::FUTEX_WAKE_ALL,
74 };
75 unsafe {
76 match nk!(crate::bindings::twz_rt_futex_wake(
77 word.as_ptr().cast(),
78 max
79 )) {
80 0 => Ok(()),
81 e => Err(RawTwzError::new(e).error()),
82 }
83 }
84}
85
86pub fn twz_rt_yield() {
88 unsafe {
89 nk!(crate::bindings::twz_rt_yield_now());
90 }
91}
92
93pub fn twz_rt_sleep(dur: Duration) {
95 unsafe {
96 nk!(crate::bindings::twz_rt_sleep(dur.into()));
97 }
98}
99
100pub fn twz_rt_set_thread_name(name: &core::ffi::CStr) {
102 unsafe {
103 nk!(crate::bindings::twz_rt_set_name(name.as_ptr()));
104 }
105}
106
107pub fn twz_rt_tls_get_addr(index: &TlsIndex) -> *mut u8 {
109 unsafe { nk!(crate::bindings::twz_rt_tls_get_addr(index as *const _ as *mut _).cast()) }
110}
111
112pub fn twz_rt_spawn_thread(args: ThreadSpawnArgs) -> Result<ThreadId> {
115 unsafe { nk!(crate::bindings::twz_rt_spawn_thread(args).into()) }
116}
117
118pub fn twz_rt_join_thread(id: ThreadId, timeout: Option<Duration>) -> Result<()> {
120 unsafe {
121 RawTwzError::new(nk!(crate::bindings::twz_rt_join_thread(id, timeout.into()))).result()
122 }
123}