/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /* * SPITHandler lists runnable instances and processes them in order. * File: SPIT_Handler.cpp * Author: Hagi * * Created on 24. April 2019, 11:18 */ #include "SPIT_Handler.h" #include "esp_log.h" fos::Mutex SPIT_Handler::s_MutexLock; fos::Semaphore SPIT_Handler::s_ConditionVariable(1000); list SPIT_Handler::s_Runnables; bool SPIT_Handler::s_Close = false; fos::Task* SPIT_Handler::s_HandlerThread = nullptr; SPIT_Handler::Runnable::~Runnable() {} void SPIT_Handler::addRunnable(Runnable* p_Runnable) { if(s_HandlerThread == nullptr) { s_HandlerThread = new fos::Task(SPIT_Handler::run, "spitHandler"); } if (p_Runnable == NULL) return; { fos::MutexGuard o_Unique_Lock(s_MutexLock); if (s_Close == true) return; s_Runnables.push_back(p_Runnable); } s_ConditionVariable.give(); } void SPIT_Handler::closeHandler() { if (s_Close == true) return; { fos::MutexGuard o_Unique_Lock(s_MutexLock); if (s_Close == true) return; s_Close = true; } s_ConditionVariable.give(); } void SPIT_Handler::run() { Runnable* o_Runnable; bool boolWait = false; while (s_Close == false) { boolWait = false; { fos::MutexGuard o_Lock(s_MutexLock); if (s_Runnables.empty()) { s_ConditionVariable.take(); } if (s_Close == true) { return; } if (s_Runnables.empty()) { continue; } else { o_Runnable = s_Runnables.front(); s_Runnables.erase(s_Runnables.begin()); } } if (o_Runnable != NULL) { try { o_Runnable->run(); delete o_Runnable; } catch(...) { ESP_LOGW("SPIT", "ERROR SPITHandler o_Runable->run()"); } } } }