From 9d6f76219f7c8cc909248d7b08e77e65b4ffaa17 Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Sun, 17 Sep 2017 00:12:49 +0200 Subject: [PATCH] [DEV/DEBUG] No stl start to work (add manual type declaration (NO RTTI)) --- ethread/Future.cpp | 3 + ethread/Mutex.Windows.cpp | 4 + ethread/Mutex.pthread.cpp | 42 +++++++++- ethread/Pool.cpp | 2 + ethread/PoolAction.cpp | 2 + ethread/PoolExecutor.cpp | 2 + ethread/Promise.cpp | 2 + ethread/Semaphore.Windows.cpp | 2 + ethread/Semaphore.pthread.cpp | 12 +-- ethread/Thread.hpp | 9 ++- ethread/Thread.pthread.cpp | 22 +++-- ethread/tools.cpp | 147 +++++++++++++++++----------------- lutin_ethread-tools.py | 2 + lutin_ethread.py | 11 --- 14 files changed, 162 insertions(+), 100 deletions(-) diff --git a/ethread/Future.cpp b/ethread/Future.cpp index a0e6a59..db0166e 100644 --- a/ethread/Future.cpp +++ b/ethread/Future.cpp @@ -7,6 +7,9 @@ #include "debug.hpp" #include +#include +ETK_DECLARE_TYPE(ethread::Future); + ethread::Future::Future(ememory::SharedPtr _promise): m_promise(_promise) { diff --git a/ethread/Mutex.Windows.cpp b/ethread/Mutex.Windows.cpp index 3055296..a89f8dc 100644 --- a/ethread/Mutex.Windows.cpp +++ b/ethread/Mutex.Windows.cpp @@ -7,6 +7,10 @@ #include +#include +ETK_DECLARE_TYPE(ethread::Mutex); +ETK_DECLARE_TYPE(ethread::UniqueLock); + ethread::Mutex::Mutex() { InitializeCriticalSection(&m_mutex); } diff --git a/ethread/Mutex.pthread.cpp b/ethread/Mutex.pthread.cpp index 200160d..5a54ac1 100644 --- a/ethread/Mutex.pthread.cpp +++ b/ethread/Mutex.pthread.cpp @@ -6,6 +6,13 @@ #include #include +#include +extern "C" { + #include +} +#include +ETK_DECLARE_TYPE(ethread::Mutex); +ETK_DECLARE_TYPE(ethread::UniqueLock); //#include ethread::Mutex::Mutex() { @@ -28,7 +35,40 @@ void ethread::Mutex::lock() { bool ethread::Mutex::tryLock() { - return pthread_mutex_trylock(&m_mutex) != 0; + int ret = pthread_mutex_trylock(&m_mutex); + if (ret == 0) { + return true; + } + if (ret == EINVAL) { + printf("trylock error: EINVAL\n"); + // The mutex was created with the protocol attribute having the value PTHREAD_PRIO_PROTECT and the calling thread's + // priority is higher than the mutex's current priority ceiling. + // The pthread_mutex_trylock() function shall fail if: + } + if (ret == EBUSY) { + printf("trylock error: EBUSY\n"); + // The mutex could not be acquired because it was already locked. + // The pthread_mutex_lock(), pthread_mutex_trylock(), and pthread_mutex_unlock() functions may fail if: + } + if (ret == EINVAL) { + printf("trylock error: EINVAL\n"); + // The value specified by mutex does not refer to an initialized mutex object. + } + if (ret == EAGAIN) { + printf("trylock error: EAGAIN\n"); + // The mutex could not be acquired because the maximum number of recursive locks for mutex has been exceeded. + // The pthread_mutex_lock() function may fail if: + } + if (ret == EDEADLK) { + printf("trylock error: EDEADLK\n"); + // The current thread already owns the mutex. + // The pthread_mutex_unlock() function may fail if: + } + if (ret == EPERM) { + printf("trylock error: EPERM\n"); + //The current thread does not own the mutex. + } + return false; } diff --git a/ethread/Pool.cpp b/ethread/Pool.cpp index cae31cd..665e355 100644 --- a/ethread/Pool.cpp +++ b/ethread/Pool.cpp @@ -7,6 +7,8 @@ #include #include #include "debug.hpp" +#include +ETK_DECLARE_TYPE(ethread::Pool); ethread::Pool::Pool(uint16_t _numberOfThread): m_lastTrandId(1) { diff --git a/ethread/PoolAction.cpp b/ethread/PoolAction.cpp index 2ea8199..cd83f14 100644 --- a/ethread/PoolAction.cpp +++ b/ethread/PoolAction.cpp @@ -6,6 +6,8 @@ #include #include "debug.hpp" +#include +ETK_DECLARE_TYPE(ethread::PoolAction); ethread::PoolAction::PoolAction(uint64_t _currentPoolId, ememory::SharedPtr _promise, etk::Function _call) : m_currentPoolId(_currentPoolId), diff --git a/ethread/PoolExecutor.cpp b/ethread/PoolExecutor.cpp index 0188da8..e6393f9 100644 --- a/ethread/PoolExecutor.cpp +++ b/ethread/PoolExecutor.cpp @@ -11,6 +11,8 @@ #include #include #include "debug.hpp" +#include +ETK_DECLARE_TYPE(ethread::PoolExecutor); ethread::PoolExecutor::PoolExecutor(ethread::Pool& _pool): m_needProcess(false), diff --git a/ethread/Promise.cpp b/ethread/Promise.cpp index 389279d..d2881b8 100644 --- a/ethread/Promise.cpp +++ b/ethread/Promise.cpp @@ -8,6 +8,8 @@ #include #include #include +#include +ETK_DECLARE_TYPE(ethread::Promise); ethread::Promise::Promise(): m_isFinished(false) { diff --git a/ethread/Semaphore.Windows.cpp b/ethread/Semaphore.Windows.cpp index 4fe4bce..db09f1e 100644 --- a/ethread/Semaphore.Windows.cpp +++ b/ethread/Semaphore.Windows.cpp @@ -6,6 +6,8 @@ #include #include #include +#include +ETK_DECLARE_TYPE(ethread::Semaphore); etk::Semaphore::Semaphore(uint32_t _nbBasicElement, uint32_t _nbMessageMax) { // create interface mutex : diff --git a/ethread/Semaphore.pthread.cpp b/ethread/Semaphore.pthread.cpp index b7618c2..ba16220 100644 --- a/ethread/Semaphore.pthread.cpp +++ b/ethread/Semaphore.pthread.cpp @@ -7,17 +7,19 @@ #include //#include #include +#include +ETK_DECLARE_TYPE(ethread::Semaphore); ethread::Semaphore::Semaphore(uint32_t _nbBasicElement, uint32_t _nbMessageMax) { // create interface mutex : int ret = pthread_mutex_init(&m_mutex, nullptr); - TK_ASSERT(ret == 0, "Error creating Mutex ..."); + //TK_ASSERT(ret == 0, "Error creating Mutex ..."); // create contition : ret = pthread_cond_init(&m_condition, nullptr); - TK_ASSERT(ret == 0, "Error creating Condition ..."); + //TK_ASSERT(ret == 0, "Error creating Condition ..."); if (ret != 0) { ret = pthread_mutex_destroy(&m_mutex); - TK_ASSERT(ret == 0, "Error destroying Mutex ..."); + //TK_ASSERT(ret == 0, "Error destroying Mutex ..."); } m_maximum = _nbMessageMax; m_data = _nbBasicElement; @@ -27,10 +29,10 @@ ethread::Semaphore::Semaphore(uint32_t _nbBasicElement, uint32_t _nbMessageMax) ethread::Semaphore::~Semaphore() { // Remove condition int ret = pthread_cond_destroy(&m_condition); - TK_ASSERT(ret == 0, "Error destroying Condition ..."); + //TK_ASSERT(ret == 0, "Error destroying Condition ..."); // Remove Mutex ret = pthread_mutex_destroy(&m_mutex); - TK_ASSERT(ret == 0, "Error destroying Mutex ..."); + //TK_ASSERT(ret == 0, "Error destroying Mutex ..."); } uint32_t ethread::Semaphore::getCount() { diff --git a/ethread/Thread.hpp b/ethread/Thread.hpp index b6b6484..fd71038 100644 --- a/ethread/Thread.hpp +++ b/ethread/Thread.hpp @@ -38,8 +38,15 @@ namespace ethread { bool detach(); void threadCall(); void setName(const etk::String& _name); - const etk::String& setName() const; + const etk::String& getName() const; uint64_t getId() const; + #ifdef __TARGET_OS__Windows + + #else + pthread_t getNativeHandle() { + return m_thread; + } + #endif }; } diff --git a/ethread/Thread.pthread.cpp b/ethread/Thread.pthread.cpp index 8e0e89a..8e7ebba 100644 --- a/ethread/Thread.pthread.cpp +++ b/ethread/Thread.pthread.cpp @@ -4,7 +4,16 @@ * @license MPL v2.0 (see license file) */ #include +#include +#include +ETK_DECLARE_TYPE(ethread::Thread); + +namespace ethread { + uint32_t getThreadHumanId(uint64_t _id); + etk::String getThreadName(uint64_t _id); + void setThreadName(ethread::Thread* _thread, const etk::String& _name); +} void* ethread::Thread::threadCallback(void* _userData) { ethread::Thread* threadHandle = static_cast(_userData); @@ -20,13 +29,10 @@ ethread::Thread::Thread(etk::Function&& _call, const etk::String& _name) m_uid(-1), m_name(_name), m_function(etk::move(_call)) { + uint32_t iii = ethread::getId(); pthread_create(&m_thread, nullptr, ðread::Thread::threadCallback, this); - /* - pthread_id_np_t tid; - pthread_getunique_np(&m_thread, &tid); - m_uid = uint64_t(tid); - */ - m_uid = *(uint64_t*)(m_thread); + m_uid = ethread::getThreadHumanId(uint64_t(m_thread)); + printf("New thread: %d from %d", m_uid, iii); } ethread::Thread::~Thread() { @@ -40,13 +46,15 @@ void ethread::Thread::join() { bool ethread::Thread::detach() { + return true; } void ethread::Thread::setName(const etk::String& _name) { m_name = _name; + ethread::setThreadName(this, m_name); } -const etk::String& ethread::Thread::setName() const { +const etk::String& ethread::Thread::getName() const { return m_name; } diff --git a/ethread/tools.cpp b/ethread/tools.cpp index 4d2b8f2..9ab9d78 100644 --- a/ethread/tools.cpp +++ b/ethread/tools.cpp @@ -6,7 +6,7 @@ #include #include -//#include +#include // TODO: set mutex back ... #include extern "C" { @@ -19,104 +19,101 @@ extern "C" { #include } #endif -//static ethread::Mutex g_lock; -static etk::Map& getThreadList() { - static etk::Map g_val; +static ethread::Mutex g_lock; +static etk::Map& getThreadList() { + static etk::Map g_val; return g_val; } -/* -static uint32_t getThreadHumanId(ethread::Thread::id _id) { - return 0; - uint32_t out = 0; - uint64_t iddd = std::hash()(_id); - // TODO: g_lock.lock(); - static etk::Map g_list; - etk::Map::Iterator it = g_list.find(iddd); - if (it == g_list.end()) { - // attribute new ID : - static uint32_t tmpId = 0; - g_list.set(iddd,tmpId); - out = tmpId; - tmpId++; - } else { - out = it.getValue(); - } - // TODO: g_lock.unlock(); - return out; -} - -static etk::String getThreadName(ethread::Thread::id _id) { - etk::Map& list = getThreadList(); - uint32_t threadID = getThreadHumanId(_id); - etk::String out; - // TODO: g_lock.lock(); - auto it = list.find(threadID); - if (it != list.end()) { - out = it.getValue(); - } - // TODO: g_lock.unlock(); - return out; - return "TODO"; -} - -static void setThreadName(ethread::Thread* _thread, const etk::String& _name) { - etk::Map& list = getThreadList(); - uint32_t threadID = ethread::getId(); - // TODO: g_lock.lock(); - list.set(threadID, _name); - vg_lock.unlock(); - // try now to set the thread name with Pthread - #if defined(__TARGET_OS__Linux) \ - && !defined(__TARGET_OS__Web) - pthread_t pthreadID; - if (_thread == nullptr) { - pthreadID = pthread_self(); +namespace ethread { + // Note: Declared in Thread.cpp + uint32_t getThreadHumanId(uint64_t _id) { + uint32_t out = 0; + g_lock.lock(); + static etk::Map g_list; + auto it = g_list.find(_id); + if (it == g_list.end()) { + // attribute new ID : + static uint32_t tmpId = 0; + g_list.set(_id, tmpId); + out = tmpId; + tmpId++; } else { - pthreadID = (pthread_t) _thread->native_handle(); + out = it.getValue(); } - etk::String name = _name; - if (name.size() > 15) { - name.resize(15); + g_lock.unLock(); + return out; + } + + etk::String getThreadName(uint64_t _id) { + etk::Map& list = getThreadList(); + uint32_t threadID = getThreadHumanId(_id); + etk::String out; + // TODO: g_lock.lock(); + auto it = list.find(threadID); + if (it != list.end()) { + out = it.getValue(); } - if (pthread_setname_np(pthreadID, name.c_str()) < 0) { - //TODO: TK_ERROR("Error when setting the Name in the OS thread naming"); - } - #else - //TODO: TK_INFO("Can not set the thread name in this OS (local set)"); - #endif + // TODO: g_lock.unlock(); + return out; + } + + void setThreadName(ethread::Thread* _thread, const etk::String& _name) { + etk::Map& list = getThreadList(); + uint32_t threadID = ethread::getId(); + // TODO: g_lock.lock(); + list.set(threadID, _name); + // TODO: g_lock.unlock(); + // try now to set the thread name with Pthread + #if ( defined(__TARGET_OS__Linux) \ + || defined(__TARGET_OS__Android) \ + ) \ + && !defined(__TARGET_OS__Web) + pthread_t pthreadID; + if (_thread == nullptr) { + pthreadID = pthread_self(); + } else { + pthreadID = _thread->getNativeHandle(); + } + etk::String name = _name; + if (name.size() > 15) { + name.resize(15); + } + if (pthread_setname_np(pthreadID, name.c_str()) < 0) { + //TODO: TK_ERROR("Error when setting the Name in the OS thread naming"); + } + #else + //TODO: TK_INFO("Can not set the thread name in this OS (local set)"); + #endif + } } -*/ + + uint32_t ethread::getId() { pthread_t self; self = pthread_self(); - /* - pthread_id_np_t tid; - pthread_getunique_np(&self, &tid); - return uint64_t(tid); - */ - return *(uint64_t*)(self); + return ethread::getThreadHumanId(uint64_t(self)); } uint32_t ethread::getId(ethread::Thread& _thread) { - return _thread.getId(); + return ethread::getThreadHumanId(_thread.getId()); } void ethread::setName(const etk::String& _name) { - //setThreadName(nullptr, _name); + setThreadName(nullptr, _name); } void ethread::setName(ethread::Thread& _thread, const etk::String& _name) { - //setThreadName(&_thread, _name); + _thread.setName(_name); } etk::String ethread::getName() { - //return getThreadName(std::this_thread::get_id()); - return ""; + pthread_t self; + self = pthread_self(); + return getThreadName(uint64_t(self)); } etk::String ethread::getName(ethread::Thread& _thread) { - //return getThreadName(_thread.get_id()); - return ""; + return _thread.getName(); } #if defined(__TARGET_OS__Linux) \ diff --git a/lutin_ethread-tools.py b/lutin_ethread-tools.py index 77f3639..e509d1b 100644 --- a/lutin_ethread-tools.py +++ b/lutin_ethread-tools.py @@ -44,12 +44,14 @@ def configure(target, my_module): 'ethread/Mutex.Windows.cpp', 'ethread/MutexRecursive.Windows.cpp', 'ethread/Thread.Windows.cpp', + 'ethread/Semaphore.Windows.cpp', ]) else: my_module.add_src_file([ 'ethread/Mutex.pthread.cpp', 'ethread/MutexRecursive.pthread.cpp', 'ethread/Thread.pthread.cpp', + 'ethread/Semaphore.pthread.cpp', ]) my_module.add_depend([ 'pthread', diff --git a/lutin_ethread.py b/lutin_ethread.py index 0a9e57e..665ac4b 100644 --- a/lutin_ethread.py +++ b/lutin_ethread.py @@ -44,17 +44,6 @@ def configure(target, my_module): 'ethread/PoolExecutor.hpp', ]) - if "Windows" in target.get_type(): - my_module.add_src_file([ - 'ethread/Semaphore.Windows.cpp', - ]) - else: - my_module.add_src_file([ - 'ethread/Semaphore.pthread.cpp', - ]) - my_module.add_depend([ - 'pthread', - ]) # build in C++ mode my_module.compile_version("c++", 2011) # add dependency of the generic C++ library: