cxx_Thread.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. */
  9. #include "cxx_thread.h"
  10. using namespace rtthread;
  11. /**
  12. * @brief Thread class constructor with parameters for stack size, priority, tick, and name.
  13. * @param stack_size Stack size in bytes
  14. * @param priority Thread priority
  15. * @param tick Time slice in ticks
  16. * @param name Thread name
  17. */
  18. Thread::Thread(rt_uint32_t stack_size,
  19. rt_uint8_t priority,
  20. rt_uint32_t tick,
  21. const char *name)
  22. : _entry(RT_NULL), _param(RT_NULL), started(false)
  23. {
  24. rt_event_init(&_event, name, 0);
  25. _thread = rt_thread_create(name,
  26. (thread_func_t)func,
  27. this,
  28. stack_size,
  29. priority,
  30. tick);
  31. }
  32. /**
  33. * @brief Thread class constructor with entry function and parameters.
  34. * @param entry The entry function pointer for the thread.
  35. * @param p The parameter to pass to the entry function.
  36. * @param stack_size The size of the thread stack in bytes.
  37. * @param priority The priority of the thread.
  38. * @param tick The time slice (tick) for the thread.
  39. * @param name The name of the thread.
  40. */
  41. Thread::Thread(void (*entry)(void *p),
  42. void *p,
  43. rt_uint32_t stack_size,
  44. rt_uint8_t priority,
  45. rt_uint32_t tick,
  46. const char *name)
  47. : _entry(entry), _param(p), started(false)
  48. {
  49. rt_event_init(&_event, name, 0);
  50. _thread = rt_thread_create(name,
  51. (thread_func_t)func,
  52. this,
  53. stack_size,
  54. priority,
  55. tick);
  56. }
  57. /**
  58. * @brief Detach the event and delete the thread when the object is destroyed.
  59. */
  60. Thread::~Thread()
  61. {
  62. rt_event_detach(&_event);
  63. rt_thread_delete(_thread);
  64. }
  65. /**
  66. * @brief Start the thread execution.
  67. * @return true if the thread was successfully started.
  68. */
  69. bool Thread::start()
  70. {
  71. if (rt_thread_startup(_thread) == RT_EOK)
  72. {
  73. started = true;
  74. }
  75. return started;
  76. }
  77. /**
  78. * @brief Make the thread sleep for a specified duration.
  79. * @param millisec Duration in milliseconds.
  80. */
  81. void Thread::sleep(int32_t millisec)
  82. {
  83. rt_int32_t tick;
  84. if (millisec < 0)
  85. tick = 1;
  86. else
  87. tick = rt_tick_from_millisecond(millisec);
  88. rt_thread_delay(tick);
  89. }
  90. /**
  91. * @brief Function to run the thread's entry function.
  92. */
  93. void Thread::func(Thread *pThis)
  94. {
  95. if (pThis->_entry != RT_NULL)
  96. {
  97. pThis->_entry(pThis->_param);
  98. }
  99. else
  100. {
  101. pThis->run(pThis->_param);
  102. }
  103. rt_event_send(&pThis->_event, 1);
  104. }
  105. /**
  106. * @brief Default run function that can be overridden by subclasses.
  107. */
  108. void Thread::run(void *parameter)
  109. {
  110. /* please overload this method */
  111. }
  112. /**
  113. * @brief Wait for the thread to complete with a timeout.
  114. * @param millisec Timeout in milliseconds.
  115. * @return RT_EOK if the thread completed within the timeout, error code otherwise.
  116. */
  117. rt_err_t Thread::wait(int32_t millisec)
  118. {
  119. return join(millisec);
  120. }
  121. /**
  122. * @brief Join the thread with a timeout.
  123. * @param millisec Timeout in milliseconds.
  124. * @return RT_EOK if the thread completed within the timeout, error code otherwise.
  125. */
  126. rt_err_t Thread::join(int32_t millisec)
  127. {
  128. if (started)
  129. {
  130. rt_int32_t tick;
  131. if (millisec < 0)
  132. tick = -1;
  133. else
  134. tick = rt_tick_from_millisecond(millisec);
  135. return rt_event_recv(&_event, 1, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, tick, RT_NULL);
  136. }
  137. else
  138. {
  139. return -RT_ENOSYS;
  140. }
  141. }