[DEV] set a correct port of the thread priority and name
This commit is contained in:
parent
12848e4646
commit
a84ec0fcb1
@ -44,6 +44,7 @@ add_library(${PROJECT_NAME}
|
|||||||
../${PROJECT_NAME}/tool.cpp
|
../${PROJECT_NAME}/tool.cpp
|
||||||
../${PROJECT_NAME}/Noise.cpp
|
../${PROJECT_NAME}/Noise.cpp
|
||||||
../${PROJECT_NAME}/Color.cpp
|
../${PROJECT_NAME}/Color.cpp
|
||||||
|
../${PROJECT_NAME}/thread/tools.cpp
|
||||||
../${PROJECT_NAME}/math/Matrix4.cpp
|
../${PROJECT_NAME}/math/Matrix4.cpp
|
||||||
../${PROJECT_NAME}/math/Vector2D.cpp
|
../${PROJECT_NAME}/math/Vector2D.cpp
|
||||||
../${PROJECT_NAME}/math/Vector3D.cpp
|
../${PROJECT_NAME}/math/Vector3D.cpp
|
||||||
|
58
etk/log.cpp
58
etk/log.cpp
@ -12,6 +12,7 @@
|
|||||||
#include <etk/thread.h>
|
#include <etk/thread.h>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
|
#include <etk/thread/tools.h>
|
||||||
|
|
||||||
#if defined(__TARGET_OS__Android)
|
#if defined(__TARGET_OS__Android)
|
||||||
# include <android/log.h>
|
# include <android/log.h>
|
||||||
@ -220,60 +221,7 @@ static bool& getFunction() {
|
|||||||
void etk::log::setFunction(bool _status) {
|
void etk::log::setFunction(bool _status) {
|
||||||
getFunction() = _status;
|
getFunction() = _status;
|
||||||
}
|
}
|
||||||
// TODO : add a generic lock ...
|
|
||||||
static std::map<uint32_t, std::string>& getThreadList() {
|
|
||||||
static std::map<uint32_t, std::string> g_val;
|
|
||||||
return g_val;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string etk::log::getThreadName() {
|
|
||||||
std::map<uint32_t,std::string>& list = getThreadList();
|
|
||||||
uint32_t threadID = getThreadID();
|
|
||||||
std::string out;
|
|
||||||
static std11::mutex g_lock;
|
|
||||||
g_lock.lock();
|
|
||||||
std::map<uint32_t,std::string>::iterator it = list.find(threadID);
|
|
||||||
if (it != list.end()) {
|
|
||||||
out = it->second;
|
|
||||||
}
|
|
||||||
g_lock.unlock();
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
|
|
||||||
void etk::log::setThreadName(const std::string& _name) {
|
|
||||||
std::map<uint32_t,std::string>& list = getThreadList();
|
|
||||||
uint32_t threadID = getThreadID();
|
|
||||||
static std11::mutex g_lock;
|
|
||||||
g_lock.lock();
|
|
||||||
std::map<uint32_t,std::string>::iterator it = list.find(threadID);
|
|
||||||
if (it == list.end()) {
|
|
||||||
list.insert(std::pair<uint32_t, std::string>(threadID,_name));
|
|
||||||
} else {
|
|
||||||
it->second = _name;
|
|
||||||
}
|
|
||||||
g_lock.unlock();
|
|
||||||
}
|
|
||||||
|
|
||||||
uint32_t etk::log::getThreadID() {
|
|
||||||
uint32_t out = 0;
|
|
||||||
std11::thread::id this_id = std11::this_thread::get_id();
|
|
||||||
uint64_t iddd = std11::hash<std11::thread::id>()(this_id);
|
|
||||||
static std11::mutex g_lock;
|
|
||||||
g_lock.lock();
|
|
||||||
static std::map<uint64_t, uint32_t> g_list;
|
|
||||||
std::map<uint64_t, uint32_t>::iterator it = g_list.find(iddd);
|
|
||||||
if (it == g_list.end()) {
|
|
||||||
// attribute new ID :
|
|
||||||
static uint32_t tmpId = 0;
|
|
||||||
g_list.insert(std::pair<uint64_t, uint32_t>(iddd,tmpId));
|
|
||||||
out = tmpId;
|
|
||||||
tmpId++;
|
|
||||||
} else {
|
|
||||||
out = it->second;
|
|
||||||
}
|
|
||||||
g_lock.unlock();
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void getDisplayTime(char* data) {
|
static void getDisplayTime(char* data) {
|
||||||
#ifdef __TARGET_OS__Android
|
#ifdef __TARGET_OS__Android
|
||||||
@ -400,7 +348,7 @@ void etk::log::logChar(int32_t _id, int32_t _level, int32_t _ligne, const char*
|
|||||||
}
|
}
|
||||||
if(getThreadId() == true) {
|
if(getThreadId() == true) {
|
||||||
// display thread ID
|
// display thread ID
|
||||||
uint32_t iddd = etk::log::getThreadID();
|
uint32_t iddd = etk::thread::getId();
|
||||||
sprintf(pointer, "%3d", iddd);
|
sprintf(pointer, "%3d", iddd);
|
||||||
pointer = handle+strlen(handle);
|
pointer = handle+strlen(handle);
|
||||||
*pointer++ = ' ';
|
*pointer++ = ' ';
|
||||||
@ -410,7 +358,7 @@ void etk::log::logChar(int32_t _id, int32_t _level, int32_t _ligne, const char*
|
|||||||
}
|
}
|
||||||
if(getThreadNameEnable() == true) {
|
if(getThreadNameEnable() == true) {
|
||||||
// display thread ID
|
// display thread ID
|
||||||
std::string name = etk::log::getThreadName();
|
std::string name = etk::thread::getName();
|
||||||
int32_t len = strlen(handle);
|
int32_t len = strlen(handle);
|
||||||
snprintf(pointer, 20, "%s", name.c_str());
|
snprintf(pointer, 20, "%s", name.c_str());
|
||||||
pointer = handle+strlen(handle);
|
pointer = handle+strlen(handle);
|
||||||
|
180
etk/thread/tools.cpp
Normal file
180
etk/thread/tools.cpp
Normal file
@ -0,0 +1,180 @@
|
|||||||
|
/**
|
||||||
|
* @author Edouard DUPIN
|
||||||
|
*
|
||||||
|
* @copyright 2011, Edouard DUPIN, all right reserved
|
||||||
|
*
|
||||||
|
* @license APACHE v2.0 (see license file)
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <etk/debug.h>
|
||||||
|
#include <etk/thread/tools.h>
|
||||||
|
#include <etk/mutex.h>
|
||||||
|
#include <map>
|
||||||
|
|
||||||
|
static std11::mutex g_lock;
|
||||||
|
static std::map<uint32_t, std::string>& getThreadList() {
|
||||||
|
static std::map<uint32_t, std::string> g_val;
|
||||||
|
return g_val;
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint32_t getThreadHumanId(std11::thread::id _id) {
|
||||||
|
uint32_t out = 0;
|
||||||
|
uint64_t iddd = std11::hash<std11::thread::id>()(_id);
|
||||||
|
g_lock.lock();
|
||||||
|
static std::map<uint64_t, uint32_t> g_list;
|
||||||
|
std::map<uint64_t, uint32_t>::iterator it = g_list.find(iddd);
|
||||||
|
if (it == g_list.end()) {
|
||||||
|
// attribute new ID :
|
||||||
|
static uint32_t tmpId = 0;
|
||||||
|
g_list.insert(std::pair<uint64_t, uint32_t>(iddd,tmpId));
|
||||||
|
out = tmpId;
|
||||||
|
tmpId++;
|
||||||
|
} else {
|
||||||
|
out = it->second;
|
||||||
|
}
|
||||||
|
g_lock.unlock();
|
||||||
|
return out;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
static std::string getThreadName(std11::thread::id _id) {
|
||||||
|
std::map<uint32_t,std::string>& list = getThreadList();
|
||||||
|
uint32_t threadID = getThreadHumanId(_id);
|
||||||
|
std::string out;
|
||||||
|
g_lock.lock();
|
||||||
|
std::map<uint32_t,std::string>::iterator it = list.find(threadID);
|
||||||
|
if (it != list.end()) {
|
||||||
|
out = it->second;
|
||||||
|
}
|
||||||
|
g_lock.unlock();
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void setThreadName(std11::thread* _thread, const std::string& _name) {
|
||||||
|
std::map<uint32_t,std::string>& list = getThreadList();
|
||||||
|
uint32_t threadID = etk::thread::getId();
|
||||||
|
g_lock.lock();
|
||||||
|
std::map<uint32_t,std::string>::iterator it = list.find(threadID);
|
||||||
|
if (it == list.end()) {
|
||||||
|
list.insert(std::pair<uint32_t, std::string>(threadID, _name));
|
||||||
|
} else {
|
||||||
|
it->second = _name;
|
||||||
|
}
|
||||||
|
g_lock.unlock();
|
||||||
|
// try now to set the thread name with Pthread
|
||||||
|
#if defined(__TARGET_OS__Linux)
|
||||||
|
pthread_t pthreadID;
|
||||||
|
if (_thread == nullptr) {
|
||||||
|
pthreadID = pthread_self();
|
||||||
|
} else {
|
||||||
|
pthreadID = (pthread_t) _thread->native_handle();
|
||||||
|
}
|
||||||
|
std::string name = _name;
|
||||||
|
if (name.size() > 15) {
|
||||||
|
name.resize(15);
|
||||||
|
}
|
||||||
|
if (pthread_setname_np(pthreadID, name.c_str()) < 0) {
|
||||||
|
TK_ERROR("Error when setting the Name in the OS thread naming");
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
TK_DEBUG("Can not set the thread name in this OS (local set)");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t etk::thread::getId() {
|
||||||
|
return getThreadHumanId(std11::this_thread::get_id());
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t etk::thread::getId(std11::thread& _thread) {
|
||||||
|
return getThreadHumanId(_thread.get_id());
|
||||||
|
}
|
||||||
|
|
||||||
|
void etk::thread::setName(const std::string& _name) {
|
||||||
|
setThreadName(nullptr, _name);
|
||||||
|
}
|
||||||
|
|
||||||
|
void etk::thread::setName(std11::thread& _thread, const std::string& _name) {
|
||||||
|
setThreadName(&_thread, _name);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string etk::thread::getName() {
|
||||||
|
return getThreadName(std11::this_thread::get_id());
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string etk::thread::getName(std11::thread& _thread) {
|
||||||
|
return getThreadName(_thread.get_id());
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(__TARGET_OS__Linux)
|
||||||
|
static void setThreadPriority(pthread_t _threadID, int32_t _priority) {
|
||||||
|
int retcode;
|
||||||
|
int policy;
|
||||||
|
struct sched_param param;
|
||||||
|
retcode = pthread_getschedparam(_threadID, &policy, ¶m);
|
||||||
|
if (retcode != 0) {
|
||||||
|
TK_ERROR("Can not get prioriry " << ((retcode == ESRCH) ? "WRONG THREAD ID (ESRCH)" :"???") );
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
TK_INFO("Try to set the thread proiority at :" << _priority);
|
||||||
|
policy = SCHED_OTHER;
|
||||||
|
if (_priority < 0) {
|
||||||
|
_priority *= -1;
|
||||||
|
policy = SCHED_FIFO;
|
||||||
|
}
|
||||||
|
param.sched_priority = _priority;
|
||||||
|
retcode = pthread_setschedparam(_threadID, policy, ¶m);
|
||||||
|
if (retcode != 0) {
|
||||||
|
TK_ERROR("Can not set prioriry " << ((retcode == ESRCH) ? "WRONG THREAD ID (ESRCH)" :
|
||||||
|
(retcode == EINVAL) ? "WRONG POLICY (EINVAL)" :
|
||||||
|
(retcode == EPERM) ? "NO PRIVILEGE (EPERM)" :
|
||||||
|
"???") );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
static int32_t getThreadPriority(pthread_t _threadID) {
|
||||||
|
int retcode;
|
||||||
|
int policy;
|
||||||
|
struct sched_param param;
|
||||||
|
retcode = pthread_getschedparam(_threadID, &policy, ¶m);
|
||||||
|
if (retcode != 0) {
|
||||||
|
TK_ERROR("Can not get prioriry " << ((retcode == ESRCH) ? "WRONG THREAD ID (ESRCH)" : "???") );
|
||||||
|
return 20;
|
||||||
|
}
|
||||||
|
if (policy != SCHED_OTHER) {
|
||||||
|
return -param.sched_priority;
|
||||||
|
}
|
||||||
|
return param.sched_priority;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
void etk::thread::setPriority(int32_t _priority) {
|
||||||
|
#if defined(__TARGET_OS__Linux)
|
||||||
|
pthread_t threadID = pthread_self();
|
||||||
|
setThreadPriority(threadID, _priority);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void etk::thread::setPriority(std11::thread& _thread, int32_t _priority) {
|
||||||
|
#if defined(__TARGET_OS__Linux)
|
||||||
|
pthread_t threadID = (pthread_t) _thread.native_handle();
|
||||||
|
setThreadPriority(threadID, _priority);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t etk::thread::getPriority() {
|
||||||
|
#if defined(__TARGET_OS__Linux)
|
||||||
|
pthread_t threadID = pthread_self();
|
||||||
|
return getThreadPriority(threadID);
|
||||||
|
#else
|
||||||
|
return 20;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t etk::thread::getPriority(std11::thread& _thread) {
|
||||||
|
#if defined(__TARGET_OS__Linux)
|
||||||
|
pthread_t threadID = static_cast<pthread_t>(_thread.native_handle());
|
||||||
|
return getThreadPriority(threadID);
|
||||||
|
#else
|
||||||
|
return 20;
|
||||||
|
#endif
|
||||||
|
}
|
78
etk/thread/tools.h
Normal file
78
etk/thread/tools.h
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
/**
|
||||||
|
* @author Edouard DUPIN
|
||||||
|
*
|
||||||
|
* @copyright 2011, Edouard DUPIN, all right reserved
|
||||||
|
*
|
||||||
|
* @license APACHE v2.0 (see license file)
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <etk/types.h>
|
||||||
|
|
||||||
|
#ifndef __ETK_THREAD_TOOLS_H__
|
||||||
|
#define __ETK_THREAD_TOOLS_H__
|
||||||
|
|
||||||
|
#include <etk/thread.h>
|
||||||
|
|
||||||
|
namespace etk {
|
||||||
|
namespace thread {
|
||||||
|
/**
|
||||||
|
* @brief get human readable thread ID. (not the std::thread::get_id())
|
||||||
|
* @return the ID of the thread.
|
||||||
|
*/
|
||||||
|
uint32_t getId();
|
||||||
|
/**
|
||||||
|
* @brief get human readable thread ID. (not the std::thread::get_id())
|
||||||
|
* @param[in] _thread Thread handle
|
||||||
|
* @return the ID of the thread.
|
||||||
|
*/
|
||||||
|
uint32_t getId(std11::thread& _thread);
|
||||||
|
/**
|
||||||
|
* @brief Set the Current thread name
|
||||||
|
* @param[in] _name New name of the thread
|
||||||
|
*/
|
||||||
|
void setName(const std::string& _name);
|
||||||
|
/**
|
||||||
|
* @brief Set an other thread name
|
||||||
|
* @param[in] _thread Thread handle
|
||||||
|
* @param[in] _name New name of the thread
|
||||||
|
*/
|
||||||
|
void setName(std11::thread& _thread, const std::string& _name);
|
||||||
|
/**
|
||||||
|
* @brief Set the Current thread name
|
||||||
|
* @return The current name of the thread
|
||||||
|
*/
|
||||||
|
std::string getName();
|
||||||
|
/**
|
||||||
|
* @brief Get an other thread name
|
||||||
|
* @praram[in] _thread Thread handle
|
||||||
|
* @return The external thread name of the thread
|
||||||
|
*/
|
||||||
|
std::string getName(std11::thread& _thread);
|
||||||
|
/**
|
||||||
|
* @brief Set the Current thread priority [-20..0] for RT and ]0..50] for normal priority
|
||||||
|
* @param[in] _priority New priority of the thread
|
||||||
|
* @note If your process have not the right to change thread name, it does not work
|
||||||
|
*/
|
||||||
|
void setPriority(int32_t _priority);
|
||||||
|
/**
|
||||||
|
* @brief Set an other thread priority [-20..0] for RT and ]0..50] for normal priority
|
||||||
|
* @param[in] _thread Thread handle
|
||||||
|
* @param[in] _priority New priority of the thread
|
||||||
|
* @note If your process have not the right to change thread name, it does not work
|
||||||
|
*/
|
||||||
|
void setPriority(std11::thread& _thread, int32_t _priority);
|
||||||
|
/**
|
||||||
|
* @brief get the Current thread priority [-20..0] for RT and ]0..50] for normal priority
|
||||||
|
* @return current priority of the thread
|
||||||
|
*/
|
||||||
|
int32_t getPriority();
|
||||||
|
/**
|
||||||
|
* @brief Get an other thread priority [-20..0] for RT and ]0..50] for normal priority
|
||||||
|
* @param[in] _thread Thread handle
|
||||||
|
* @return current priority of the thread
|
||||||
|
*/
|
||||||
|
int32_t getPriority(std11::thread& _thread);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
@ -19,6 +19,7 @@ def create(target):
|
|||||||
'etk/tool.cpp',
|
'etk/tool.cpp',
|
||||||
'etk/Noise.cpp',
|
'etk/Noise.cpp',
|
||||||
'etk/Color.cpp',
|
'etk/Color.cpp',
|
||||||
|
'etk/thread/tools.cpp',
|
||||||
'etk/math/Matrix4.cpp',
|
'etk/math/Matrix4.cpp',
|
||||||
'etk/math/Vector2D.cpp',
|
'etk/math/Vector2D.cpp',
|
||||||
'etk/math/Vector3D.cpp',
|
'etk/math/Vector3D.cpp',
|
||||||
|
Loading…
x
Reference in New Issue
Block a user