[DEV] continue removing stl

This commit is contained in:
Edouard DUPIN 2017-08-28 00:05:24 +02:00
parent a4641e67a3
commit 5cabc85f98
11 changed files with 60 additions and 67 deletions

View File

@ -15,7 +15,7 @@ ethread::Pool::Pool(uint16_t _numberOfThread):
ememory::SharedPtr<ethread::PoolExecutor> tmp = ememory::makeShared<ethread::PoolExecutor>(*this); ememory::SharedPtr<ethread::PoolExecutor> tmp = ememory::makeShared<ethread::PoolExecutor>(*this);
if (tmp != nullptr) { if (tmp != nullptr) {
tmp->start(); tmp->start();
m_listThread.push_back(tmp); m_listThread.pushBack(tmp);
} }
} }
} }
@ -38,7 +38,7 @@ ethread::Future ethread::Pool::async(std::function<void()> _call, uint64_t _exec
} }
ememory::SharedPtr<ethread::Promise> promise = ememory::makeShared<ethread::Promise>(); ememory::SharedPtr<ethread::Promise> promise = ememory::makeShared<ethread::Promise>();
ememory::SharedPtr<ethread::PoolAction> action = ememory::makeShared<ethread::PoolAction>(_executionInGroupId, promise, _call); ememory::SharedPtr<ethread::PoolAction> action = ememory::makeShared<ethread::PoolAction>(_executionInGroupId, promise, _call);
m_listActions.push_back(action); m_listActions.pushBack(action);
for(auto &it : m_listThread) { for(auto &it : m_listThread) {
if (it == nullptr) { if (it == nullptr) {
continue; continue;
@ -89,7 +89,7 @@ ememory::SharedPtr<ethread::PoolAction> ethread::Pool::getAction() {
if (alreadyUsed == false) { if (alreadyUsed == false) {
ememory::SharedPtr<ethread::PoolAction> out = (*it); ememory::SharedPtr<ethread::PoolAction> out = (*it);
if (uniquId != 0) { if (uniquId != 0) {
m_listIdPool.push_back(uniquId); m_listIdPool.pushBack(uniquId);
} }
it = m_listActions.erase(it); it = m_listActions.erase(it);
return out; return out;

View File

@ -6,7 +6,7 @@
#pragma once #pragma once
#include <mutex> #include <mutex>
#include <vector> #include <etk/Vector.hpp>
#include <thread> #include <thread>
#include <ethread/Future.hpp> #include <ethread/Future.hpp>
#include <ethread/PoolAction.hpp> #include <ethread/PoolAction.hpp>
@ -19,9 +19,9 @@ namespace ethread {
class Pool { class Pool {
private: private:
std::mutex m_mutex; //!< global add and release some thread std::mutex m_mutex; //!< global add and release some thread
std::vector<ememory::SharedPtr<ethread::PoolExecutor>> m_listThread; //!< Thread pool etk::Vector<ememory::SharedPtr<ethread::PoolExecutor>> m_listThread; //!< Thread pool
std::vector<ememory::SharedPtr<ethread::PoolAction>> m_listActions; //!< Thread pool etk::Vector<ememory::SharedPtr<ethread::PoolAction>> m_listActions; //!< Thread pool
std::vector<uint64_t> m_listIdPool; //!< Thread pool etk::Vector<uint64_t> m_listIdPool; //!< Thread pool
uint32_t m_lastTrandId; //!< to group the action in a single thread uint32_t m_lastTrandId; //!< to group the action in a single thread
public: public:
/** /**

View File

@ -10,7 +10,7 @@
ethread::PoolAction::PoolAction(uint64_t _currentPoolId, ememory::SharedPtr<ethread::Promise> _promise, std::function<void()> _call) : ethread::PoolAction::PoolAction(uint64_t _currentPoolId, ememory::SharedPtr<ethread::Promise> _promise, std::function<void()> _call) :
m_currentPoolId(_currentPoolId), m_currentPoolId(_currentPoolId),
m_promise(_promise), m_promise(_promise),
m_call(std::move(_call)) { m_call(etk::move(_call)) {
} }

View File

@ -6,7 +6,7 @@
#pragma once #pragma once
#include <mutex> #include <mutex>
#include <vector> #include <etk/Vector.hpp>
#include <thread> #include <thread>
#include <ethread/Future.hpp> #include <ethread/Future.hpp>
#include <ememory/memory.hpp> #include <ememory/memory.hpp>

View File

@ -5,7 +5,7 @@
*/ */
#include <mutex> #include <mutex>
#include <vector> #include <etk/Vector.hpp>
#include <thread> #include <thread>
#include <ethread/Future.hpp> #include <ethread/Future.hpp>
#include <ethread/PoolExecutor.hpp> #include <ethread/PoolExecutor.hpp>
@ -22,7 +22,7 @@ ethread::PoolExecutor::PoolExecutor(ethread::Pool& _pool):
void ethread::PoolExecutor::threadCallback() { void ethread::PoolExecutor::threadCallback() {
ETHREAD_DEBUG("RUN: thread in Pool [START]"); ETHREAD_DEBUG("RUN: thread in Pool [START]");
ethread::setName("pool " + etk::to_string(ethread::getId())); ethread::setName("pool " + etk::toString(ethread::getId()));
// get datas: // get datas:
while (m_running == true) { while (m_running == true) {
// get an action: // get an action:

View File

@ -6,7 +6,7 @@
#pragma once #pragma once
#include <mutex> #include <mutex>
#include <vector> #include <etk/Vector.hpp>
#include <thread> #include <thread>
#include <ethread/Future.hpp> #include <ethread/Future.hpp>
#include <ethread/PoolAction.hpp> #include <ethread/PoolAction.hpp>

View File

@ -29,7 +29,7 @@ void ethread::Promise::finish() {
m_isFinished = true; m_isFinished = true;
if (m_callback != nullptr) { if (m_callback != nullptr) {
// call callbacks ... // call callbacks ...
callback = std::move(m_callback); callback = etk::move(m_callback);
} }
} }
if (callback != nullptr) { if (callback != nullptr) {
@ -59,7 +59,7 @@ bool ethread::Promise::wait(echrono::Duration _delay) {
void ethread::Promise::andThen(std::function<void()> _action) { void ethread::Promise::andThen(std::function<void()> _action) {
std::unique_lock<std::mutex> lock(m_mutex); std::unique_lock<std::mutex> lock(m_mutex);
m_callback = std::move(_action); m_callback = etk::move(_action);
if (m_isFinished == true) { if (m_isFinished == true) {
m_callback(); m_callback();
} }

View File

@ -5,12 +5,13 @@
*/ */
#include <ethread/tools.hpp> #include <ethread/tools.hpp>
#include <etk/Pair.hpp>
#include <mutex> #include <mutex>
#include <map> #include <etk/Map.hpp>
static std::mutex g_lock; static std::mutex g_lock;
static std::map<uint32_t, std::string>& getThreadList() { static etk::Map<uint32_t, etk::String>& getThreadList() {
static std::map<uint32_t, std::string> g_val; static etk::Map<uint32_t, etk::String> g_val;
return g_val; return g_val;
} }
@ -18,45 +19,40 @@ static uint32_t getThreadHumanId(std::thread::id _id) {
uint32_t out = 0; uint32_t out = 0;
uint64_t iddd = std::hash<std::thread::id>()(_id); uint64_t iddd = std::hash<std::thread::id>()(_id);
g_lock.lock(); g_lock.lock();
static std::map<uint64_t, uint32_t> g_list; static etk::Map<uint64_t, uint32_t> g_list;
std::map<uint64_t, uint32_t>::iterator it = g_list.find(iddd); etk::Map<uint64_t, uint32_t>::Iterator it = g_list.find(iddd);
if (it == g_list.end()) { if (it == g_list.end()) {
// attribute new ID : // attribute new ID :
static uint32_t tmpId = 0; static uint32_t tmpId = 0;
g_list.insert(std::pair<uint64_t, uint32_t>(iddd,tmpId)); g_list.set(iddd,tmpId);
out = tmpId; out = tmpId;
tmpId++; tmpId++;
} else { } else {
out = it->second; out = it.getValue();
} }
g_lock.unlock(); g_lock.unlock();
return out; return out;
} }
static std::string getThreadName(std::thread::id _id) { static etk::String getThreadName(std::thread::id _id) {
std::map<uint32_t,std::string>& list = getThreadList(); etk::Map<uint32_t,etk::String>& list = getThreadList();
uint32_t threadID = getThreadHumanId(_id); uint32_t threadID = getThreadHumanId(_id);
std::string out; etk::String out;
g_lock.lock(); g_lock.lock();
std::map<uint32_t,std::string>::iterator it = list.find(threadID); auto it = list.find(threadID);
if (it != list.end()) { if (it != list.end()) {
out = it->second; out = it.getValue();
} }
g_lock.unlock(); g_lock.unlock();
return out; return out;
} }
static void setThreadName(std::thread* _thread, const std::string& _name) { static void setThreadName(std::thread* _thread, const etk::String& _name) {
std::map<uint32_t,std::string>& list = getThreadList(); etk::Map<uint32_t,etk::String>& list = getThreadList();
uint32_t threadID = ethread::getId(); uint32_t threadID = ethread::getId();
g_lock.lock(); g_lock.lock();
std::map<uint32_t,std::string>::iterator it = list.find(threadID); list.set(threadID, _name);
if (it == list.end()) {
list.insert(std::pair<uint32_t, std::string>(threadID, _name));
} else {
it->second = _name;
}
g_lock.unlock(); g_lock.unlock();
// try now to set the thread name with Pthread // try now to set the thread name with Pthread
#if defined(__TARGET_OS__Linux) \ #if defined(__TARGET_OS__Linux) \
@ -67,7 +63,7 @@ static void setThreadName(std::thread* _thread, const std::string& _name) {
} else { } else {
pthreadID = (pthread_t) _thread->native_handle(); pthreadID = (pthread_t) _thread->native_handle();
} }
std::string name = _name; etk::String name = _name;
if (name.size() > 15) { if (name.size() > 15) {
name.resize(15); name.resize(15);
} }
@ -87,19 +83,19 @@ uint32_t ethread::getId(std::thread& _thread) {
return getThreadHumanId(_thread.get_id()); return getThreadHumanId(_thread.get_id());
} }
void ethread::setName(const std::string& _name) { void ethread::setName(const etk::String& _name) {
setThreadName(nullptr, _name); setThreadName(nullptr, _name);
} }
void ethread::setName(std::thread& _thread, const std::string& _name) { void ethread::setName(std::thread& _thread, const etk::String& _name) {
setThreadName(&_thread, _name); setThreadName(&_thread, _name);
} }
std::string ethread::getName() { etk::String ethread::getName() {
return getThreadName(std::this_thread::get_id()); return getThreadName(std::this_thread::get_id());
} }
std::string ethread::getName(std::thread& _thread) { etk::String ethread::getName(std::thread& _thread) {
return getThreadName(_thread.get_id()); return getThreadName(_thread.get_id());
} }
@ -184,46 +180,41 @@ int32_t ethread::getPriority(std::thread& _thread) {
} }
static std::mutex g_localMutex; static std::mutex g_localMutex;
static std::map<uint32_t, std::map<std::string, uint64_t>> g_listMetaData; static etk::Map<uint32_t, etk::Map<etk::String, uint64_t>> g_listMetaData;
void ethread::metadataSet(const std::string& _key, uint64_t _value) { void ethread::metadataSet(const etk::String& _key, uint64_t _value) {
uint32_t currentThreadId = ethread::getId(); uint32_t currentThreadId = ethread::getId();
std::unique_lock<std::mutex> lock(g_localMutex); std::unique_lock<std::mutex> lock(g_localMutex);
auto it = g_listMetaData.find(currentThreadId); auto it = g_listMetaData.find(currentThreadId);
if (it != g_listMetaData.end()) { if (it != g_listMetaData.end()) {
auto it2 = it->second.find(_key); it.getValue().set(_key, _value);
if (it2 != it->second.end()) {
it2->second = _value;
} else {
it->second.insert(std::make_pair( _key, _value));
}
} else { } else {
std::map<std::string, uint64_t> tmp; etk::Map<etk::String, uint64_t> tmp;
tmp.insert(std::make_pair( _key, _value)); tmp.set(_key, _value);
g_listMetaData.insert(std::make_pair(currentThreadId, tmp)); g_listMetaData.set(currentThreadId, tmp);
} }
} }
void ethread::metadataRemove(const std::string& _key) { void ethread::metadataRemove(const etk::String& _key) {
uint32_t currentThreadId = ethread::getId(); uint32_t currentThreadId = ethread::getId();
std::unique_lock<std::mutex> lock(g_localMutex); std::unique_lock<std::mutex> lock(g_localMutex);
auto it = g_listMetaData.find(currentThreadId); etk::Map<uint32_t, etk::Map<etk::String, uint64_t>>::Iterator it = g_listMetaData.find(currentThreadId);
if (it != g_listMetaData.end()) { if (it != g_listMetaData.end()) {
auto it2 = it->second.find(_key); auto it2 = it.getValue().find(_key);
if (it2 != it->second.end()) { if (it2 != it.getValue().end()) {
it->second.erase(it2); it.getValue().erase(it2);
} }
} }
} }
uint64_t ethread::metadataGetU64(const std::string& _key) { uint64_t ethread::metadataGetU64(const etk::String& _key) {
uint32_t currentThreadId = ethread::getId(); uint32_t currentThreadId = ethread::getId();
std::unique_lock<std::mutex> lock(g_localMutex); std::unique_lock<std::mutex> lock(g_localMutex);
auto it = g_listMetaData.find(currentThreadId); auto it = g_listMetaData.find(currentThreadId);
if (it != g_listMetaData.end()) { if (it != g_listMetaData.end()) {
auto it2 = it->second.find(_key); auto it2 = it.getValue().find(_key);
if (it2 != it->second.end()) { if (it2 != it.getValue().end()) {
return it2->second; return it2.getValue();
} }
} }
return 0; return 0;

View File

@ -6,7 +6,7 @@
#pragma once #pragma once
#include <thread> #include <thread>
#include <string> #include <etk/String.hpp>
/** /**
* @brief ethread main namespace * @brief ethread main namespace
@ -27,24 +27,24 @@ namespace ethread {
* @brief Set the Current thread name * @brief Set the Current thread name
* @param[in] _name New name of the thread * @param[in] _name New name of the thread
*/ */
void setName(const std::string& _name); void setName(const etk::String& _name);
/** /**
* @brief Set an other thread name * @brief Set an other thread name
* @param[in] _thread Thread handle * @param[in] _thread Thread handle
* @param[in] _name New name of the thread * @param[in] _name New name of the thread
*/ */
void setName(std::thread& _thread, const std::string& _name); void setName(std::thread& _thread, const etk::String& _name);
/** /**
* @brief Set the Current thread name * @brief Set the Current thread name
* @return The current name of the thread * @return The current name of the thread
*/ */
std::string getName(); etk::String getName();
/** /**
* @brief Get an other thread name * @brief Get an other thread name
* @param[in] _thread Thread handle * @param[in] _thread Thread handle
* @return The external thread name of the thread * @return The external thread name of the thread
*/ */
std::string getName(std::thread& _thread); etk::String getName(std::thread& _thread);
/** /**
* @brief Set the Current thread priority [-20..0] for RT and ]0..50] for normal priority * @brief Set the Current thread priority [-20..0] for RT and ]0..50] for normal priority
* @param[in] _priority New priority of the thread * @param[in] _priority New priority of the thread
@ -74,16 +74,16 @@ namespace ethread {
* @param[in] _key key to store the value * @param[in] _key key to store the value
* @param[in] _value Value to store * @param[in] _value Value to store
*/ */
void metadataSet(const std::string& _key, uint64_t _value); void metadataSet(const etk::String& _key, uint64_t _value);
/** /**
* @brief Remove the information with a key on the current thread * @brief Remove the information with a key on the current thread
* @param[in] _key key to remove * @param[in] _key key to remove
*/ */
void metadataRemove(const std::string& _key); void metadataRemove(const etk::String& _key);
/** /**
* @brief get the information with a key on the current thread * @brief get the information with a key on the current thread
* @param[in] _key key to store the value * @param[in] _key key to store the value
* @return the uint 64 value to stored * @return the uint 64 value to stored
*/ */
uint64_t metadataGetU64(const std::string& _key); uint64_t metadataGetU64(const etk::String& _key);
} }

View File

@ -40,6 +40,7 @@ def configure(target, my_module):
# add dependency of the generic C++ library: # add dependency of the generic C++ library:
my_module.add_depend([ my_module.add_depend([
'cxx', 'cxx',
'etk-base',
]) ])
#pthread is not availlable on Windows #pthread is not availlable on Windows
if "Linux" in target.get_type() \ if "Linux" in target.get_type() \

View File

@ -50,6 +50,7 @@ def configure(target, my_module):
my_module.add_depend([ my_module.add_depend([
'cxx', 'cxx',
'elog', 'elog',
'etk',
'ethread-tools', 'ethread-tools',
'echrono', 'echrono',
'ememory' 'ememory'