summaryrefslogtreecommitdiff
path: root/components/rust/core/src/api/sem.rs
diff options
context:
space:
mode:
authorzhang san <[email protected]>2025-12-08 18:34:25 +0800
committerGitHub <[email protected]>2025-12-08 18:34:25 +0800
commit69980f8b9d29b38dbd4ddf6373b4b45f3a138409 (patch)
tree21dd9a8ce80e3d30192e9ffa56ea39a199bae309 /components/rust/core/src/api/sem.rs
parentcd1d47b87c5d47e131730e5506eb3ad77d357653 (diff)
Adding Rust Language Support for RT-Thread #10910
Diffstat (limited to 'components/rust/core/src/api/sem.rs')
-rw-r--r--components/rust/core/src/api/sem.rs51
1 files changed, 51 insertions, 0 deletions
diff --git a/components/rust/core/src/api/sem.rs b/components/rust/core/src/api/sem.rs
new file mode 100644
index 0000000000..43c21978d2
--- /dev/null
+++ b/components/rust/core/src/api/sem.rs
@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2006-2025, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date Author notes
+ * 2025-10-10 foxglove Semaphore API
+ */
+use crate::api::RttCResult;
+use crate::bindings::*;
+use core::ptr;
+use alloc::ffi::CString;
+
+pub type APIRawSem = rt_sem_t;
+
+ #[inline]
+pub(crate) fn semaphore_create(name: &str) -> Option<APIRawSem> {
+ let s = CString::new(name).unwrap();
+ let raw;
+ unsafe {
+ raw = rt_sem_create(s.as_ptr(), 0, 0);
+ }
+ return if raw == ptr::null_mut() {
+ None
+ } else {
+ Some(raw)
+ };
+}
+
+ #[inline]
+pub(crate) fn semaphore_try_take(handle: APIRawSem) -> RttCResult {
+ unsafe { rt_sem_trytake(handle).into() }
+}
+
+ #[inline]
+pub(crate) fn semaphore_take(handle: APIRawSem, tick: u32) -> RttCResult {
+ unsafe { rt_sem_take(handle, tick).into() }
+}
+
+ #[inline]
+pub(crate) fn semaphore_release(handle: APIRawSem) -> RttCResult {
+ unsafe { rt_sem_release(handle).into() }
+}
+
+ #[inline]
+pub(crate) fn semaphore_delete(handle: APIRawSem) {
+ unsafe {
+ let _ = rt_sem_delete(handle);
+ }
+}