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.

80 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. stop();
  40. m_taskfunction = cb;
  41. ZASSERT(m_taskfunction);
  42. xSemaphoreTake(m_lock, portMAX_DELAY);
  43. m_threadisworkingFlagCallSide = true;
  44. // xEventGroupSetBits(m_zthreadstartworkevent, 0x01);
  45. while (!m_threadisworkingFlag) {
  46. xTaskNotifyGive(m_defaultTaskHandle);
  47. vTaskDelay(1);
  48. }
  49. xSemaphoreGive(m_lock);
  50. }
  51. void ZThread::stop() {
  52. xSemaphoreTake(m_lock, portMAX_DELAY);
  53. m_threadisworkingFlagCallSide = false;
  54. // xEventGroupSetBits(m_zthreadstartworkevent, 0x01);
  55. while (m_threadisworkingFlag) {
  56. xTaskNotifyGive(m_defaultTaskHandle);
  57. vTaskDelay(1);
  58. }
  59. xSemaphoreGive(m_lock);
  60. }
  61. void ZThread::sleep(uint32_t ms) { ulTaskNotifyTake(pdFALSE, pdMS_TO_TICKS(ms)); }
  62. void ZThread::wake() {
  63. BaseType_t state;
  64. if (xPortIsInsideInterrupt()) {
  65. vTaskNotifyGiveFromISR(m_defaultTaskHandle, &state);
  66. } else {
  67. xTaskNotifyGive(m_defaultTaskHandle);
  68. }
  69. }