#include "zthread.hpp" #include "zoslogger.hpp" using namespace iflytop; using namespace std; static void zosthread_default_task(void const *argument) { ZThread *thread = (ZThread *)argument; ZASSERT(thread); while (true) { thread->m_threadisworking = false; // µÈ´ýtaskworking xEventGroupWaitBits(thread->m_zthreadstartworkevent, 0x01, pdTRUE, pdTRUE, portMAX_DELAY); thread->m_threadisworking = true; thread->m_taskfunction(); } }; void ZThread::init(const char *threadname, int stack_size, osPriority priority) { int r_task_create = 0; ZASSERT(threadname); m_lock = xSemaphoreCreateMutex(); ZASSERT(m_lock); m_stacksize = stack_size; m_uxPriority = osPriorityNormal; m_taskfunction = nullptr; m_zthreadstartworkevent = xEventGroupCreate(); m_name = threadname; osThreadDef(zosthread_default_task, zosthread_default_task, m_uxPriority, 0, m_stacksize); m_defaultTaskHandle = osThreadCreate(osThread(zosthread_default_task), this); ZASSERT(m_defaultTaskHandle != NULL); } void ZThread::start(zosthread_cb_t cb) { m_taskfunction = cb; ZASSERT(m_taskfunction); xSemaphoreTake(m_lock, portMAX_DELAY); xEventGroupSetBits(m_zthreadstartworkevent, 0x01); while (m_threadisworking != true) { vTaskDelay(1); } xSemaphoreGive(m_lock); } void ZThread::stop() { xSemaphoreTake(m_lock, portMAX_DELAY); m_expect_stop = true; while (m_threadisworking) { 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; if (xPortIsInsideInterrupt()) { vTaskNotifyGiveFromISR(m_defaultTaskHandle, &state); } else { xTaskNotifyGive(m_defaultTaskHandle); } }