diff --git a/ethread/PoolExecutor.cpp b/ethread/PoolExecutor.cpp index e6393f9..cd1e2c5 100644 --- a/ethread/PoolExecutor.cpp +++ b/ethread/PoolExecutor.cpp @@ -30,12 +30,11 @@ void ethread::PoolExecutor::threadCallback() { // get an action: m_action = m_pool.getAction(); if (m_action == nullptr) { - ethread::UniqueLock lock(m_mutex); // If no action availlable and not requested to check, just sleep ... if (m_needProcess == false) { m_isWaiting = true; ETHREAD_VERBOSE("RUN: Jump in sleep"); - if (m_condition.wait_for(lock, std::chrono::seconds(60)) == std::cv_status::timeout) { + if (m_semaphore.wait(60000000) == false) { ETHREAD_VERBOSE("RUN: time-out"); continue; } @@ -56,11 +55,8 @@ void ethread::PoolExecutor::threadCallback() { void ethread::PoolExecutor::start() { ETHREAD_DEBUG("START: thread in Pool [START]"); m_running = true; - { - ethread::UniqueLock lock(m_mutex); - m_condition.notify_all(); - } - m_thread = ememory::makeShared([&](void *){ this->threadCallback();}, nullptr); + m_semaphore.post(); + m_thread = ememory::makeShared([&](){ threadCallback();}); if (m_thread == nullptr) { m_running = false; ETHREAD_ERROR("START: thread in Pool [STOP] can not intanciate THREAD!"); @@ -72,20 +68,14 @@ void ethread::PoolExecutor::start() { void ethread::PoolExecutor::stop() { ETHREAD_DEBUG("STOP: thread in Pool [START]"); - { - ethread::UniqueLock lock(m_mutex); - m_condition.notify_all(); - } + m_semaphore.post(); m_running = false; ETHREAD_DEBUG("STOP: thread in Pool [STOP]"); } void ethread::PoolExecutor::join() { ETHREAD_DEBUG("JOIN: thread in Pool [START]"); - { - ethread::UniqueLock lock(m_mutex); - m_condition.notify_all(); - } + m_semaphore.post(); if (m_thread != nullptr) { ETHREAD_DEBUG("JOIN: waiting ..."); m_thread->join(); @@ -95,14 +85,13 @@ void ethread::PoolExecutor::join() { } bool ethread::PoolExecutor::touch() { - ethread::UniqueLock lock(m_mutex); bool ret = false; if ( m_needProcess == false && m_isWaiting == true) { ETHREAD_VERBOSE("Touch ..."); m_needProcess = true; ret = true; - m_condition.notify_all(); + m_semaphore.post(); } return ret; } diff --git a/ethread/PoolExecutor.hpp b/ethread/PoolExecutor.hpp index fdaa6cf..95e762d 100644 --- a/ethread/PoolExecutor.hpp +++ b/ethread/PoolExecutor.hpp @@ -20,8 +20,7 @@ namespace ethread { */ class PoolExecutor { private: //section to permit to optimize CPU: - ethread::Mutex m_mutex; //!< protection of the internal data. - std::condition_variable m_condition; //!< Message system to send event on an other thread. + ethread::Semaphore m_semaphore; //!< protection of the internal data. bool m_needProcess; //!< Need to do action (no need to wait condition). bool m_isWaiting; //!< The executor is waiting to some action to do. private: diff --git a/ethread/Promise.cpp b/ethread/Promise.cpp index d2881b8..4311a3e 100644 --- a/ethread/Promise.cpp +++ b/ethread/Promise.cpp @@ -7,6 +7,7 @@ #include "debug.hpp" #include #include +#include #include #include ETK_DECLARE_TYPE(ethread::Promise); diff --git a/ethread/Thread.hpp b/ethread/Thread.hpp index fd71038..ef8a22e 100644 --- a/ethread/Thread.hpp +++ b/ethread/Thread.hpp @@ -32,7 +32,7 @@ namespace ethread { etk::String m_name; //!< Name of the thread (do not get it on the system ==> more portable) etk::Function m_function; //!< Function to call every cycle of the thead running public: - Thread(etk::Function&& _call, const etk::String& _name); + Thread(etk::Function&& _call, const etk::String& _name = ""); ~Thread(); void join(); bool detach(); diff --git a/ethread/Thread.pthread.cpp b/ethread/Thread.pthread.cpp index 8e7ebba..39353d2 100644 --- a/ethread/Thread.pthread.cpp +++ b/ethread/Thread.pthread.cpp @@ -32,7 +32,7 @@ ethread::Thread::Thread(etk::Function&& _call, const etk::String& _name) uint32_t iii = ethread::getId(); pthread_create(&m_thread, nullptr, ðread::Thread::threadCallback, this); m_uid = ethread::getThreadHumanId(uint64_t(m_thread)); - printf("New thread: %d from %d", m_uid, iii); + printf("New thread: %ld from %d\n", m_uid, iii); } ethread::Thread::~Thread() {