summaryrefslogtreecommitdiff
path: root/components/rust/core/src/api/thread.rs
blob: d8b1ed295ba191ee26fcb5d6c9894e8be3fdb5f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
 * Copyright (c) 2006-2025, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       notes
 * 2025-10-10     foxglove     Thread API
 */
use super::base::*;
use crate::bindings::*;
use core::ptr;
use core::ffi::c_void;
use alloc::ffi::CString;

/* Thread handle, defined by RT-Thread C code */
pub type APIRawThread = rt_thread_t;
/* Thread entry function, defined by RT-Thread C code */
pub type ThreadEntry = extern "C" fn(parameter: *mut c_void);

#[inline]
pub fn thread_create(
    name: &str,
    entry: ThreadEntry,
    param: *mut c_void,
    stack_size: u32,
    priority: u8,
    tick: u32,
) -> Option<APIRawThread> {
    let name = CString::new(name).unwrap();
    let raw;
    unsafe {
        raw = rt_thread_create(
            name.as_ptr(),
            entry,
            param,
            stack_size as crate::bindings::librt::rt_size_t,
            priority,
            tick,
        );
    }
    if raw == ptr::null_mut() {
        None
    } else {
        Some(raw)
    }
}

#[inline]
pub fn thread_delete(th: APIRawThread) -> RttCResult {
    unsafe { rt_thread_delete(th).into() }
}

#[inline]
pub fn thread_self() -> Option<APIRawThread> {
    let ret;
    unsafe {
        ret = rt_thread_self();
    }
    if ret == ptr::null_mut() {
        None
    } else {
        Some(ret)
    }
}

#[inline]
pub fn thread_startup(th: APIRawThread) -> RttCResult {
    unsafe { rt_thread_startup(th).into() }
}

#[inline]
pub fn thread_delay(ticks: usize) -> RttCResult {
    unsafe { rt_thread_delay(ticks as _).into() }
}

#[inline]
pub fn thread_m_delay(ms: i32) -> RttCResult {
    unsafe { rt_thread_mdelay(ms as _).into() }
}

#[inline]
pub fn thread_yield() {
    unsafe {
        rt_thread_yield();
    }
}

#[inline]
pub fn thread_suspend(th: APIRawThread) -> RttCResult {
    unsafe { rt_thread_suspend(th).into() }
}

#[inline]
pub fn thread_resume(th: APIRawThread) -> RttCResult {
    unsafe { rt_thread_resume(th).into() }
}