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.

69 lines
1.9 KiB

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