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.

78 lines
2.2 KiB

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->m_threadisworkingFlag = false;
  9. thread->m_threadisWaitingForStopFlag = false;
  10. while (true) {
  11. if (thread->m_threadisworkingFlagCallSide) {
  12. thread->m_threadisworkingFlag = true;
  13. thread->m_taskfunction();
  14. thread->m_threadisWaitingForStopFlag = true;
  15. while (thread->m_threadisworkingFlagCallSide) {
  16. vTaskDelay(10);
  17. }
  18. thread->m_threadisworkingFlag = false;
  19. thread->m_threadisWaitingForStopFlag = false;
  20. }
  21. vTaskDelay(10);
  22. }
  23. };
  24. void ZThread::init(const char *threadname, int stack_size, osPriority priority) {
  25. int r_task_create = 0;
  26. ZASSERT(threadname);
  27. m_lock = xSemaphoreCreateMutex();
  28. ZASSERT(m_lock);
  29. m_stacksize = stack_size;
  30. m_uxPriority = osPriorityNormal;
  31. m_taskfunction = nullptr;
  32. m_zthreadstartworkevent = xEventGroupCreate();
  33. m_name = threadname;
  34. osThreadDef(zosthread_default_task, zosthread_default_task, m_uxPriority, 0, m_stacksize);
  35. m_defaultTaskHandle = osThreadCreate(osThread(zosthread_default_task), this);
  36. ZASSERT(m_defaultTaskHandle != NULL);
  37. }
  38. void ZThread::start(zosthread_cb_t cb) {
  39. m_taskfunction = cb;
  40. ZASSERT(m_taskfunction);
  41. xSemaphoreTake(m_lock, portMAX_DELAY);
  42. m_threadisworkingFlagCallSide = true;
  43. // xEventGroupSetBits(m_zthreadstartworkevent, 0x01);
  44. while (!m_threadisworkingFlag) {
  45. xTaskNotifyGive(m_defaultTaskHandle);
  46. vTaskDelay(1);
  47. }
  48. xSemaphoreGive(m_lock);
  49. }
  50. void ZThread::stop() {
  51. xSemaphoreTake(m_lock, portMAX_DELAY);
  52. m_threadisworkingFlagCallSide = false;
  53. // xEventGroupSetBits(m_zthreadstartworkevent, 0x01);
  54. while (m_threadisworkingFlag) {
  55. xTaskNotifyGive(m_defaultTaskHandle);
  56. vTaskDelay(1);
  57. }
  58. xSemaphoreGive(m_lock);
  59. }
  60. void ZThread::sleep(uint32_t ms) { ulTaskNotifyTake(pdFALSE, pdMS_TO_TICKS(ms)); }
  61. void ZThread::wake() {
  62. BaseType_t state;
  63. if (xPortIsInsideInterrupt()) {
  64. vTaskNotifyGiveFromISR(m_defaultTaskHandle, &state);
  65. } else {
  66. xTaskNotifyGive(m_defaultTaskHandle);
  67. }
  68. }