[DEV] update new API ETHREADÃ-base

This commit is contained in:
Edouard DUPIN 2017-09-26 15:50:53 +02:00
parent 9d6f76219f
commit d5ef0aa31f
5 changed files with 10 additions and 21 deletions

View File

@ -30,12 +30,11 @@ void ethread::PoolExecutor::threadCallback() {
// get an action: // get an action:
m_action = m_pool.getAction(); m_action = m_pool.getAction();
if (m_action == nullptr) { if (m_action == nullptr) {
ethread::UniqueLock lock(m_mutex);
// If no action availlable and not requested to check, just sleep ... // If no action availlable and not requested to check, just sleep ...
if (m_needProcess == false) { if (m_needProcess == false) {
m_isWaiting = true; m_isWaiting = true;
ETHREAD_VERBOSE("RUN: Jump in sleep"); 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"); ETHREAD_VERBOSE("RUN: time-out");
continue; continue;
} }
@ -56,11 +55,8 @@ void ethread::PoolExecutor::threadCallback() {
void ethread::PoolExecutor::start() { void ethread::PoolExecutor::start() {
ETHREAD_DEBUG("START: thread in Pool [START]"); ETHREAD_DEBUG("START: thread in Pool [START]");
m_running = true; m_running = true;
{ m_semaphore.post();
ethread::UniqueLock lock(m_mutex); m_thread = ememory::makeShared<ethread::Thread>([&](){ threadCallback();});
m_condition.notify_all();
}
m_thread = ememory::makeShared<ethread::Thread>([&](void *){ this->threadCallback();}, nullptr);
if (m_thread == nullptr) { if (m_thread == nullptr) {
m_running = false; m_running = false;
ETHREAD_ERROR("START: thread in Pool [STOP] can not intanciate THREAD!"); ETHREAD_ERROR("START: thread in Pool [STOP] can not intanciate THREAD!");
@ -72,20 +68,14 @@ void ethread::PoolExecutor::start() {
void ethread::PoolExecutor::stop() { void ethread::PoolExecutor::stop() {
ETHREAD_DEBUG("STOP: thread in Pool [START]"); ETHREAD_DEBUG("STOP: thread in Pool [START]");
{ m_semaphore.post();
ethread::UniqueLock lock(m_mutex);
m_condition.notify_all();
}
m_running = false; m_running = false;
ETHREAD_DEBUG("STOP: thread in Pool [STOP]"); ETHREAD_DEBUG("STOP: thread in Pool [STOP]");
} }
void ethread::PoolExecutor::join() { void ethread::PoolExecutor::join() {
ETHREAD_DEBUG("JOIN: thread in Pool [START]"); ETHREAD_DEBUG("JOIN: thread in Pool [START]");
{ m_semaphore.post();
ethread::UniqueLock lock(m_mutex);
m_condition.notify_all();
}
if (m_thread != nullptr) { if (m_thread != nullptr) {
ETHREAD_DEBUG("JOIN: waiting ..."); ETHREAD_DEBUG("JOIN: waiting ...");
m_thread->join(); m_thread->join();
@ -95,14 +85,13 @@ void ethread::PoolExecutor::join() {
} }
bool ethread::PoolExecutor::touch() { bool ethread::PoolExecutor::touch() {
ethread::UniqueLock lock(m_mutex);
bool ret = false; bool ret = false;
if ( m_needProcess == false if ( m_needProcess == false
&& m_isWaiting == true) { && m_isWaiting == true) {
ETHREAD_VERBOSE("Touch ..."); ETHREAD_VERBOSE("Touch ...");
m_needProcess = true; m_needProcess = true;
ret = true; ret = true;
m_condition.notify_all(); m_semaphore.post();
} }
return ret; return ret;
} }

View File

@ -20,8 +20,7 @@ namespace ethread {
*/ */
class PoolExecutor { class PoolExecutor {
private: //section to permit to optimize CPU: private: //section to permit to optimize CPU:
ethread::Mutex m_mutex; //!< protection of the internal data. ethread::Semaphore m_semaphore; //!< protection of the internal data.
std::condition_variable m_condition; //!< Message system to send event on an other thread.
bool m_needProcess; //!< Need to do action (no need to wait condition). bool m_needProcess; //!< Need to do action (no need to wait condition).
bool m_isWaiting; //!< The executor is waiting to some action to do. bool m_isWaiting; //!< The executor is waiting to some action to do.
private: private:

View File

@ -7,6 +7,7 @@
#include "debug.hpp" #include "debug.hpp"
#include <ethread/Future.hpp> #include <ethread/Future.hpp>
#include <ethread/Promise.hpp> #include <ethread/Promise.hpp>
#include <ethread/tools.hpp>
#include <echrono/Steady.hpp> #include <echrono/Steady.hpp>
#include <etk/typeInfo.hpp> #include <etk/typeInfo.hpp>
ETK_DECLARE_TYPE(ethread::Promise); ETK_DECLARE_TYPE(ethread::Promise);

View File

@ -32,7 +32,7 @@ namespace ethread {
etk::String m_name; //!< Name of the thread (do not get it on the system ==> more portable) etk::String m_name; //!< Name of the thread (do not get it on the system ==> more portable)
etk::Function<void()> m_function; //!< Function to call every cycle of the thead running etk::Function<void()> m_function; //!< Function to call every cycle of the thead running
public: public:
Thread(etk::Function<void()>&& _call, const etk::String& _name); Thread(etk::Function<void()>&& _call, const etk::String& _name = "");
~Thread(); ~Thread();
void join(); void join();
bool detach(); bool detach();

View File

@ -32,7 +32,7 @@ ethread::Thread::Thread(etk::Function<void()>&& _call, const etk::String& _name)
uint32_t iii = ethread::getId(); uint32_t iii = ethread::getId();
pthread_create(&m_thread, nullptr, &ethread::Thread::threadCallback, this); pthread_create(&m_thread, nullptr, &ethread::Thread::threadCallback, this);
m_uid = ethread::getThreadHumanId(uint64_t(m_thread)); 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() { ethread::Thread::~Thread() {