[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:
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<ethread::Thread>([&](void *){ this->threadCallback();}, nullptr);
m_semaphore.post();
m_thread = ememory::makeShared<ethread::Thread>([&](){ 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;
}

View File

@ -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:

View File

@ -7,6 +7,7 @@
#include "debug.hpp"
#include <ethread/Future.hpp>
#include <ethread/Promise.hpp>
#include <ethread/tools.hpp>
#include <echrono/Steady.hpp>
#include <etk/typeInfo.hpp>
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::Function<void()> m_function; //!< Function to call every cycle of the thead running
public:
Thread(etk::Function<void()>&& _call, const etk::String& _name);
Thread(etk::Function<void()>&& _call, const etk::String& _name = "");
~Thread();
void join();
bool detach();

View File

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