diff --git a/os/zthread.cpp b/os/zthread.cpp index 9911690..1cfb2b1 100644 --- a/os/zthread.cpp +++ b/os/zthread.cpp @@ -7,14 +7,22 @@ using namespace std; static void zosthread_default_task(void const *argument) { ZThread *thread = (ZThread *)argument; ZASSERT(thread); + thread->m_threadisworkingFlag = false; + thread->m_threadisWaitingForStopFlag = false; while (true) { - thread->m_threadisworking = false; + if (thread->m_threadisworkingFlagCallSide) { + thread->m_threadisworkingFlag = true; + thread->m_taskfunction(); - // µÈ´ýtaskworking - xEventGroupWaitBits(thread->m_zthreadstartworkevent, 0x01, pdTRUE, pdTRUE, portMAX_DELAY); - thread->m_threadisworking = true; - thread->m_taskfunction(); + thread->m_threadisWaitingForStopFlag = true; + while (thread->m_threadisworkingFlagCallSide) { + vTaskDelay(10); + } + thread->m_threadisworkingFlag = false; + thread->m_threadisWaitingForStopFlag = false; + } + vTaskDelay(10); } }; @@ -40,8 +48,10 @@ void ZThread::start(zosthread_cb_t cb) { ZASSERT(m_taskfunction); xSemaphoreTake(m_lock, portMAX_DELAY); - xEventGroupSetBits(m_zthreadstartworkevent, 0x01); - while (m_threadisworking != true) { + m_threadisworkingFlagCallSide = true; + // xEventGroupSetBits(m_zthreadstartworkevent, 0x01); + while (!m_threadisworkingFlag) { + xTaskNotifyGive(m_defaultTaskHandle); vTaskDelay(1); } xSemaphoreGive(m_lock); @@ -49,16 +59,15 @@ void ZThread::start(zosthread_cb_t cb) { void ZThread::stop() { xSemaphoreTake(m_lock, portMAX_DELAY); - m_expect_stop = true; - while (m_threadisworking) { + m_threadisworkingFlagCallSide = false; + // xEventGroupSetBits(m_zthreadstartworkevent, 0x01); + while (m_threadisworkingFlag) { xTaskNotifyGive(m_defaultTaskHandle); vTaskDelay(1); } - m_expect_stop = false; xSemaphoreGive(m_lock); } -bool ZThread::isExpectStop() { return m_expect_stop; } void ZThread::sleep(uint32_t ms) { ulTaskNotifyTake(pdFALSE, pdMS_TO_TICKS(ms)); } void ZThread::wake() { BaseType_t state; diff --git a/os/zthread.hpp b/os/zthread.hpp index d3a3f3c..4cb711f 100644 --- a/os/zthread.hpp +++ b/os/zthread.hpp @@ -10,8 +10,9 @@ class ZThread { size_t m_stacksize; osPriority m_uxPriority; - bool m_threadisworking; - bool m_expect_stop; + bool m_threadisworkingFlag = false; + bool m_threadisWaitingForStopFlag = false; + bool m_threadisworkingFlagCallSide = false; zosthread_cb_t m_taskfunction; EventGroupHandle_t m_zthreadstartworkevent; @@ -25,7 +26,6 @@ class ZThread { void start(zosthread_cb_t cb); void stop(); - bool isExpectStop(); void sleep(uint32_t ms); void wake(); };