Compare commits

9 Commits

Author SHA1 Message Date
0973379f53 now works 2024-12-26 16:19:33 +03:00
a3004491d6 no warning on terminate kbd 2024-12-26 16:18:13 +03:00
199a272eb8 now rude 2024-12-26 16:16:01 +03:00
855d1ca1b5 try to fix hang on exit by keyboard 2024-12-26 16:12:02 +03:00
64025e0399 PIThread now wait on _endThread() when starting is really done 2024-12-26 16:01:56 +03:00
6e5556969d fix for last commit 2024-12-25 19:37:55 +03:00
b50eeaef46 fix for last commit 2024-12-25 19:34:49 +03:00
ffc471359a fix thread name for linux 2024-12-25 19:30:15 +03:00
79007e7b4e PIString::arg() now ignore non-suitable %.
PIVector and PIDeque now use std::stable_sort
2024-12-07 12:57:48 +03:00
5 changed files with 41 additions and 21 deletions

View File

@@ -29,10 +29,16 @@
#include "pithread.h"
#include "pitime.h"
#define WAIT_FOR_EXIT \
while (!PIKbdListener::exiting) \
piMSleep(PIP_MIN_MSLEEP * 5); \
if (PIKbdListener::instance()) PIKbdListener::instance()->stopAndWait();
#define WAIT_FOR_EXIT \
while (!PIKbdListener::exiting) \
piMSleep(PIP_MIN_MSLEEP * 5); \
if (PIKbdListener::instance()) { \
PIKbdListener::instance()->stop(); \
if (!PIKbdListener::instance()->waitForFinish(PISystemTime::fromMilliseconds(100))) { \
PIKbdListener::instance()->setDebug(false); \
PIKbdListener::instance()->terminate(); \
} \
}
class PIP_EXPORT PIKbdListener: public PIThread {

View File

@@ -1494,11 +1494,11 @@ public:
//! \~\details
//! \~english The order of equal elements is not guaranteed to be preserved.
//! Elements are compared using operator<.
//! Sorting provided by [std::sort](https://en.cppreference.com/w/cpp/algorithm/sort).
//! Sorting provided by [std::stable_sort](https://en.cppreference.com/w/cpp/algorithm/stable_sort).
//! Complexity `O(N·log(N))`.
//! \~russian Сохранность порядка элементов, имеющих одинаковое значение, не гарантируется.
//! Для сравнения элементов используется оператор `operator<`.
//! Для сортировки используется функция [std::sort](https://ru.cppreference.com/w/cpp/algorithm/sort).
//! Для сортировки используется функция [std::stable_sort](https://ru.cppreference.com/w/cpp/algorithm/stable_sort).
//! Сложность сортировки `O(N·log(N))`.
//! \~\code
//! PIDeque<int> v{5, 7, 4, 2, 8, 6, 1, 9, 0, 3};
@@ -1507,7 +1507,7 @@ public:
//! \endcode
//! \~\sa \a sort(std::function<bool(const T &a, const T &b)> comp)
inline PIDeque<T> & sort() {
std::sort(begin(), end());
std::stable_sort(begin(), end());
return *this;
}
@@ -1524,7 +1524,7 @@ public:
//! While the signature does not need to have const &, the function must not modify the objects passed to it.
//! The function must return `false` for identical elements,
//! otherwise, it will lead to undefined program behavior and memory errors.
//! Sorting provided by [std::sort](https://en.cppreference.com/w/cpp/algorithm/sort).
//! Sorting provided by [std::stable_sort](https://en.cppreference.com/w/cpp/algorithm/stable_sort).
//! Complexity `O(N·log(N))`.
//! \~russian Сохранность порядка элементов, имеющих одинаковое значение, не гарантируется.
//! Для сравнения элементов используется функция сравнения `comp`.
@@ -1532,7 +1532,7 @@ public:
//! второго. Сигнатура функции сравнения должна быть эквивалентна следующей: \code bool comp(const T &a, const T &b); \endcode Сигнатура
//! не обязана содержать const &, однако, функция не может изменять переданные объекты. Функция обязана возвращать `false` для
//! одинаковых элементов, иначе это приведёт к неопределённому поведению программы и ошибкам памяти. Для сортировки используется функция
//! [std::sort](https://ru.cppreference.com/w/cpp/algorithm/sort). Сложность сортировки `O(N·log(N))`.
//! [std::stable_sort](https://ru.cppreference.com/w/cpp/algorithm/stable_sort). Сложность сортировки `O(N·log(N))`.
//! \~\code
//! PIDeque<int> v{5, 7, 4, 2, 8, 6, 1, 9, 0, 3};
//! v.sort([](const int & a, const int & b){return a > b;});
@@ -1540,7 +1540,7 @@ public:
//! \endcode
//! \~\sa \a sort()
inline PIDeque<T> & sort(std::function<bool(const T & a, const T & b)> comp) {
std::sort(begin(), end(), comp);
std::stable_sort(begin(), end(), comp);
return *this;
}

View File

@@ -1420,11 +1420,11 @@ public:
//! \~\details
//! \~english The order of equal elements is not guaranteed to be preserved.
//! Elements are compared using operator<.
//! Sorting provided by [std::sort](https://en.cppreference.com/w/cpp/algorithm/sort).
//! Sorting provided by [std::stable_sort](https://en.cppreference.com/w/cpp/algorithm/stable_sort).
//! Complexity `O(N·log(N))`.
//! \~russian Сохранность порядка элементов, имеющих одинаковое значение, не гарантируется.
//! Для сравнения элементов используется оператор `operator<`.
//! Для сортировки используется функция [std::sort](https://ru.cppreference.com/w/cpp/algorithm/sort).
//! Для сортировки используется функция [std::stable_sort](https://ru.cppreference.com/w/cpp/algorithm/stable_sort).
//! Сложность сортировки `O(N·log(N))`.
//! \~\code
//! PIVector<int> v{5, 7, 4, 2, 8, 6, 1, 9, 0, 3};
@@ -1433,7 +1433,7 @@ public:
//! \endcode
//! \~\sa \a sort(std::function<bool(const T &a, const T &b)> comp)
inline PIVector<T> & sort() {
std::sort(begin(), end());
std::stable_sort(begin(), end());
return *this;
}
@@ -1450,7 +1450,7 @@ public:
//! While the signature does not need to have const &, the function must not modify the objects passed to it.
//! The function must return `false` for identical elements,
//! otherwise, it will lead to undefined program behavior and memory errors.
//! Sorting provided by [std::sort](https://en.cppreference.com/w/cpp/algorithm/sort).
//! Sorting provided by [std::stable_sort](https://en.cppreference.com/w/cpp/algorithm/stable_sort).
//! Complexity `O(N·log(N))`.
//! \~russian Сохранность порядка элементов, имеющих одинаковое значение, не гарантируется.
//! Для сравнения элементов используется функция сравнения `comp`.
@@ -1458,7 +1458,7 @@ public:
//! второго. Сигнатура функции сравнения должна быть эквивалентна следующей: \code bool comp(const T &a, const T &b); \endcode Сигнатура
//! не обязана содержать const &, однако, функция не может изменять переданные объекты. Функция обязана возвращать `false` для
//! одинаковых элементов, иначе это приведёт к неопределённому поведению программы и ошибкам памяти. Для сортировки используется функция
//! [std::sort](https://ru.cppreference.com/w/cpp/algorithm/sort). Сложность сортировки `O(N·log(N))`.
//! [std::stable_sort](https://ru.cppreference.com/w/cpp/algorithm/stable_sort). Сложность сортировки `O(N·log(N))`.
//! \~\code
//! PIVector<int> v{5, 7, 4, 2, 8, 6, 1, 9, 0, 3};
//! v.sort([](const int & a, const int & b){return a > b;});
@@ -1466,7 +1466,7 @@ public:
//! \endcode
//! \~\sa \a sort()
inline PIVector<T> & sort(std::function<bool(const T & a, const T & b)> comp) {
std::sort(begin(), end(), comp);
std::stable_sort(begin(), end(), comp);
return *this;
}

View File

@@ -536,7 +536,7 @@ PIString PIString::minArgPlaceholder() {
bool ok = false;
tmp = mid(i + 1, j - i - 1);
int cur = tmp.toInt(10, &ok);
if (!ok) continue;
if (!ok || tmp.isEmpty()) continue;
if (min < 0 || min > cur) {
min = cur;
ret = tmp;

View File

@@ -533,6 +533,7 @@ PRIVATE_DEFINITION_START(PIThread)
pthread_t thread = 0;
sched_param sparam;
#endif
std::atomic_bool starting = {false};
PRIVATE_DEFINITION_END(PIThread)
@@ -731,8 +732,10 @@ int PIThread::priority2System(PIThread::Priority p) {
bool PIThread::_startThread(void * func) {
terminating = false;
running_ = true;
terminating = false;
running_ = true;
PRIVATE->starting = true;
PIScopeExitCall ec([this] { PRIVATE->starting = false; });
#ifdef FREERTOS
@@ -754,6 +757,7 @@ bool PIThread::_startThread(void * func) {
# else
PRIVATE->thread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)func, this, 0, 0);
# endif
// piCout << "started" << PRIVATE->thread;
if (PRIVATE->thread != 0) {
setPriority(priority_);
return true;
@@ -768,13 +772,20 @@ bool PIThread::_startThread(void * func) {
// PICout(PICoutManipulators::DefaultControls) << "pthread_create" << PRIVATE->thread;
pthread_attr_destroy(&attr);
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(((PIString &)name().elided(15, 0.4f).resize(15, PIChar('\0'))).dataAscii());
pthread_setname_np((const char *)tn_data.data());
pthread_threadid_np(PRIVATE->thread, (__uint64_t *)&tid_);
# else
pthread_setname_np(PRIVATE->thread, ((PIString &)name().elided(15, 0.4f).resize(15, PIChar('\0'))).dataAscii());
pthread_setname_np(PRIVATE->thread, (const char *)tn_data.data());
# endif
setPriority(priority_);
// }
return true;
}
@@ -923,7 +934,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" << "...";
stopped();
// PICout(PICoutManipulators::DefaultControls) << "thread" << this << "stop" << "ok";