You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.2 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. #include "zthread.hpp"
  2. #include "zoslogger.hpp"
  3. using namespace iflytop;
  4. using namespace std;
  5. static void zosthread_default_task(void const *argument) {
  6. ZThread *thread = (ZThread *)argument;
  7. ZASSERT(thread);
  8. thread->threadcb();
  9. };
  10. void ZThread::threadcb() {
  11. m_status = kidle;
  12. while (true) {
  13. if (m_threadisworkingFlagCallSide) {
  14. m_status = kworking;
  15. if (m_taskfunction) m_taskfunction();
  16. if (m_taskfunction_exitcb) m_taskfunction_exitcb();
  17. m_status = kdead;
  18. while (m_threadisworkingFlagCallSide) {
  19. vTaskDelay(10);
  20. }
  21. m_status = kidle;
  22. }
  23. vTaskDelay(10);
  24. }
  25. }
  26. void ZThread::init(const char *threadname, int stack_size, osPriority priority) {
  27. int r_task_create = 0;
  28. ZASSERT(threadname);
  29. m_lock = xSemaphoreCreateMutex();
  30. ZASSERT(m_lock);
  31. m_stacksize = stack_size;
  32. m_uxPriority = osPriorityNormal;
  33. m_taskfunction = nullptr;
  34. m_zthreadstartworkevent = xEventGroupCreate();
  35. m_name = threadname;
  36. osThreadDef(zosthread_default_task, zosthread_default_task, m_uxPriority, 0, m_stacksize);
  37. m_defaultTaskHandle = osThreadCreate(osThread(zosthread_default_task), this);
  38. ZASSERT(m_defaultTaskHandle != NULL);
  39. }
  40. void ZThread::start(zosthread_cb_t cb) { start(cb, nullptr); }
  41. void ZThread::start(zosthread_cb_t cb, zosthread_cb_t exitcb) {
  42. stop();
  43. m_taskfunction = cb;
  44. m_taskfunction_exitcb = exitcb;
  45. ZASSERT(m_taskfunction);
  46. xSemaphoreTake(m_lock, portMAX_DELAY);
  47. m_threadisworkingFlagCallSide = true;
  48. // xEventGroupSetBits(m_zthreadstartworkevent, 0x01);
  49. while (m_status != kidle) {
  50. xTaskNotifyGive(m_defaultTaskHandle);
  51. vTaskDelay(1);
  52. }
  53. xSemaphoreGive(m_lock);
  54. }
  55. void ZThread::stop() {
  56. xSemaphoreTake(m_lock, portMAX_DELAY);
  57. m_threadisworkingFlagCallSide = false;
  58. // xEventGroupSetBits(m_zthreadstartworkevent, 0x01);
  59. while (m_status != kidle) {
  60. xTaskNotifyGive(m_defaultTaskHandle);
  61. vTaskDelay(1);
  62. }
  63. xSemaphoreGive(m_lock);
  64. }
  65. void ZThread::sleep(uint32_t ms) { ulTaskNotifyTake(pdFALSE, pdMS_TO_TICKS(ms)); }
  66. void ZThread::wake() {
  67. BaseType_t state;
  68. if (xPortIsInsideInterrupt()) {
  69. vTaskNotifyGiveFromISR(m_defaultTaskHandle, &state);
  70. } else {
  71. xTaskNotifyGive(m_defaultTaskHandle);
  72. }
  73. }