PIThread now setup itself in thread func

This commit is contained in:
2024-12-29 10:50:38 +03:00
parent 0973379f53
commit 49713ddc57
3 changed files with 43 additions and 33 deletions

View File

@@ -533,7 +533,6 @@ PRIVATE_DEFINITION_START(PIThread)
pthread_t thread = 0; pthread_t thread = 0;
sched_param sparam; sched_param sparam;
#endif #endif
std::atomic_bool starting = {false};
PRIVATE_DEFINITION_END(PIThread) PRIVATE_DEFINITION_END(PIThread)
@@ -732,16 +731,15 @@ int PIThread::priority2System(PIThread::Priority p) {
bool PIThread::_startThread(void * func) { bool PIThread::_startThread(void * func) {
terminating = false; terminating = false;
running_ = true; running_ = true;
PRIVATE->starting = true;
PIScopeExitCall ec([this] { PRIVATE->starting = false; });
#ifdef FREERTOS #ifdef FREERTOS
auto name_ba = createThreadName();
if (xTaskCreate((__THREAD_FUNC_RET__(*)(void *))func, if (xTaskCreate((__THREAD_FUNC_RET__(*)(void *))func,
((PIString &)name().elided(15, 0.4f).resize(15, PIChar('\0'))).dataAscii(), // A name just for humans (const char *)name_ba.data(), // A name just for humans
128, // This stack size can be checked & adjusted by reading the Stack Highwater 128, // This stack size can be checked & adjusted by reading the Stack Highwater
this, this,
priority_, priority_,
&PRIVATE->thread) == pdPASS) { &PRIVATE->thread) == pdPASS) {
@@ -753,13 +751,12 @@ bool PIThread::_startThread(void * func) {
if (PRIVATE->thread) CloseHandle(PRIVATE->thread); if (PRIVATE->thread) CloseHandle(PRIVATE->thread);
# ifdef CC_GCC # ifdef CC_GCC
PRIVATE->thread = (void *)_beginthreadex(0, 0, (__THREAD_FUNC_RET__(*)(void *))func, this, 0, 0); PRIVATE->thread = (void *)_beginthreadex(0, 0, (__THREAD_FUNC_RET__(*)(void *))func, this, CREATE_SUSPENDED, 0);
# else # else
PRIVATE->thread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)func, this, 0, 0); PRIVATE->thread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)func, this, CREATE_SUSPENDED, 0);
# endif # endif
// piCout << "started" << PRIVATE->thread;
if (PRIVATE->thread != 0) { if (PRIVATE->thread != 0) {
setPriority(priority_); ResumeThread(PRIVATE->thread);
return true; return true;
} }
@@ -769,23 +766,10 @@ bool PIThread::_startThread(void * func) {
pthread_attr_init(&attr); pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
int ret = pthread_create(&PRIVATE->thread, &attr, (__THREAD_FUNC_RET__(*)(void *))func, this); int ret = pthread_create(&PRIVATE->thread, &attr, (__THREAD_FUNC_RET__(*)(void *))func, this);
// PICout(PICoutManipulators::DefaultControls) << "pthread_create" << PRIVATE->thread;
pthread_attr_destroy(&attr); pthread_attr_destroy(&attr);
// PICout(PICoutManipulators::DefaultControls) << "pthread_create" << PRIVATE->thread;
// piCout << "started" << PRIVATE->thread;
if (ret == 0) { if (ret == 0) {
// if (name().isNotEmpty()) {
PIString tname = name().simplified();
tname.elide(15, 0.4f).resize(15, PIChar('\0'));
PIByteArray tn_data = tname.toAscii();
tn_data.resize(16);
tn_data.back() = 0;
# ifdef MAC_OS
pthread_setname_np((const char *)tn_data.data());
pthread_threadid_np(PRIVATE->thread, (__uint64_t *)&tid_);
# else
pthread_setname_np(PRIVATE->thread, (const char *)tn_data.data());
# endif
setPriority(priority_);
// }
return true; return true;
} }
@@ -818,7 +802,6 @@ void PIThread::setPriority(PIThread::Priority prior) {
= priority2System(priority_); = priority2System(priority_);
pthread_setschedparam(PRIVATE->thread, policy_, &(PRIVATE->sparam)); pthread_setschedparam(PRIVATE->thread, policy_, &(PRIVATE->sparam));
# else # else
if (!running_ || (PRIVATE->thread == 0)) return;
SetThreadPriority(PRIVATE->thread, priority2System(priority_)); SetThreadPriority(PRIVATE->thread, priority2System(priority_));
# endif # endif
#endif // FREERTOS #endif // FREERTOS
@@ -896,6 +879,8 @@ void PIThread::_beginThread() {
#ifdef LINUX #ifdef LINUX
tid_ = gettid(); tid_ = gettid();
#endif #endif
setPriority(priority_);
setThreadName();
PIINTROSPECTION_THREAD_START(this); PIINTROSPECTION_THREAD_START(this);
REGISTER_THREAD(this); REGISTER_THREAD(this);
running_ = true; running_ = true;
@@ -934,11 +919,10 @@ void PIThread::_endThread() {
PIScopeExitCall ec([this] { PIScopeExitCall ec([this] {
terminating = running_ = false; terminating = running_ = false;
tid_ = -1; tid_ = -1;
PRIVATE->thread = 0;
}); });
while (PRIVATE->starting) // while (PRIVATE->starting)
piMinSleep(); // piMinSleep();
// PICout(PICoutManipulators::DefaultControls) << "thread" << this << "stop" << "..."; // PICout(PICoutManipulators::DefaultControls) << "thread" << this << "stop" << "...";
stopped(); stopped();
// PICout(PICoutManipulators::DefaultControls) << "thread" << this << "stop" << "ok"; // PICout(PICoutManipulators::DefaultControls) << "thread" << this << "stop" << "ok";
if (lockRun) thread_mutex.lock(); if (lockRun) thread_mutex.lock();
@@ -1077,3 +1061,26 @@ void PIThread::runOnce(std::function<void()> func, const PIString & name) {
#endif #endif
t->startOnce(); t->startOnce();
} }
PIByteArray PIThread::createThreadName(int size) const {
PIString tname = name().simplified();
tname.elide(size - 1, 0.4f).resize(size - 1, PIChar('\0'));
PIByteArray ret = tname.toAscii();
ret.resize(size);
ret.back() = 0;
return ret;
}
void PIThread::setThreadName() {
#ifndef WINDOWS
auto name_ba = createThreadName();
# ifdef MAC_OS
pthread_setname_np((const char *)name_ba.data());
pthread_threadid_np(PRIVATE->thread, (__uint64_t *)&tid_);
# else
pthread_setname_np(PRIVATE->thread, (const char *)name_ba.data());
# endif
#endif
}

View File

@@ -299,6 +299,9 @@ private:
void _beginThread(); void _beginThread();
void _runThread(); void _runThread();
void _endThread(); void _endThread();
PIByteArray createThreadName(int size = 16) const;
void setThreadName();
}; };

View File

@@ -45,7 +45,7 @@ void usage() {
} }
PIString confDir() { PIString pisdConfDir() {
return return
#ifdef WINDOWS #ifdef WINDOWS
PIDir::home().path() + "/AppData/Local" PIDir::home().path() + "/AppData/Local"
@@ -74,7 +74,7 @@ int main(int argc, char * argv[]) {
} }
PINetworkAddress addr = PINetworkAddress("0.0.0.0", 10101); PINetworkAddress addr = PINetworkAddress("0.0.0.0", 10101);
PIString conf_path = confDir(); PIString conf_path = pisdConfDir();
PIDir::make(conf_path); PIDir::make(conf_path);
conf_path += "/picloud.conf"; conf_path += "/picloud.conf";
uint max_connections = 1000; uint max_connections = 1000;