thread.rs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (c) 2006-2025, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author notes
  8. * 2025-10-10 foxglove Thread API
  9. */
  10. use super::base::*;
  11. use crate::bindings::*;
  12. use core::ptr;
  13. use core::ffi::c_void;
  14. use alloc::ffi::CString;
  15. /* Thread handle, defined by RT-Thread C code */
  16. pub type APIRawThread = rt_thread_t;
  17. /* Thread entry function, defined by RT-Thread C code */
  18. pub type ThreadEntry = extern "C" fn(parameter: *mut c_void);
  19. #[inline]
  20. pub fn thread_create(
  21. name: &str,
  22. entry: ThreadEntry,
  23. param: *mut c_void,
  24. stack_size: u32,
  25. priority: u8,
  26. tick: u32,
  27. ) -> Option<APIRawThread> {
  28. let name = CString::new(name).unwrap();
  29. let raw;
  30. unsafe {
  31. raw = rt_thread_create(
  32. name.as_ptr(),
  33. entry,
  34. param,
  35. stack_size as crate::bindings::librt::rt_size_t,
  36. priority,
  37. tick,
  38. );
  39. }
  40. if raw == ptr::null_mut() {
  41. None
  42. } else {
  43. Some(raw)
  44. }
  45. }
  46. #[inline]
  47. pub fn thread_delete(th: APIRawThread) -> RttCResult {
  48. unsafe { rt_thread_delete(th).into() }
  49. }
  50. #[inline]
  51. pub fn thread_self() -> Option<APIRawThread> {
  52. let ret;
  53. unsafe {
  54. ret = rt_thread_self();
  55. }
  56. if ret == ptr::null_mut() {
  57. None
  58. } else {
  59. Some(ret)
  60. }
  61. }
  62. #[inline]
  63. pub fn thread_startup(th: APIRawThread) -> RttCResult {
  64. unsafe { rt_thread_startup(th).into() }
  65. }
  66. #[inline]
  67. pub fn thread_delay(ticks: usize) -> RttCResult {
  68. unsafe { rt_thread_delay(ticks as _).into() }
  69. }
  70. #[inline]
  71. pub fn thread_m_delay(ms: i32) -> RttCResult {
  72. unsafe { rt_thread_mdelay(ms as _).into() }
  73. }
  74. #[inline]
  75. pub fn thread_yield() {
  76. unsafe {
  77. rt_thread_yield();
  78. }
  79. }
  80. #[inline]
  81. pub fn thread_suspend(th: APIRawThread) -> RttCResult {
  82. unsafe { rt_thread_suspend(th).into() }
  83. }
  84. #[inline]
  85. pub fn thread_resume(th: APIRawThread) -> RttCResult {
  86. unsafe { rt_thread_resume(th).into() }
  87. }