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

View File

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