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.

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