move std function

This commit is contained in:
2026-03-20 16:31:30 +03:00
parent 4537e40832
commit 96c22e1184
28 changed files with 145 additions and 131 deletions

View File

@@ -86,11 +86,9 @@ void PIConditionVariable::wait(PIMutex & lk) {
}
void PIConditionVariable::wait(PIMutex & lk, const std::function<bool()> & condition) {
bool isCondition;
void PIConditionVariable::wait(PIMutex & lk, std::function<bool()> condition) {
while (true) {
isCondition = condition();
if (isCondition) break;
if (condition()) break;
#if defined(WINDOWS)
SleepConditionVariableCS(&PRIVATE->nativeHandle, (PCRITICAL_SECTION)lk.handle(), INFINITE);
#elif defined(FREERTOS)
@@ -122,8 +120,7 @@ bool PIConditionVariable::waitFor(PIMutex & lk, PISystemTime timeout) {
}
bool PIConditionVariable::waitFor(PIMutex & lk, PISystemTime timeout, const std::function<bool()> & condition) {
bool isCondition;
bool PIConditionVariable::waitFor(PIMutex & lk, PISystemTime timeout, std::function<bool()> condition) {
#if defined(WINDOWS) || defined(FREERTOS)
PITimeMeasurer measurer;
#else
@@ -135,8 +132,7 @@ bool PIConditionVariable::waitFor(PIMutex & lk, PISystemTime timeout, const std:
xEventGroupClearBits(PRIVATE->nativeHandle, 1);
#endif
while (true) {
isCondition = condition();
if (isCondition) break;
if (condition()) break;
bool isTimeout;
#if defined(WINDOWS)
isTimeout = SleepConditionVariableCS(&PRIVATE->nativeHandle,

View File

@@ -106,7 +106,7 @@ public:
//! \param condition вызываемый объект или функция, не принимающая аргументов и возвращающая значение, которое может быть оценено как
//! bool. Вызывается повторно, пока не примет значение true
//!
virtual void wait(PIMutex & lk, const std::function<bool()> & condition);
virtual void wait(PIMutex & lk, std::function<bool ()> condition);
//! \~english Waits for at most \a timeout and returns \c true if awakened before it expires.
@@ -167,7 +167,7 @@ public:
//! bool. Вызывается повторно, пока не примет значение true
//! \return false если достигнут таймаут, или true если условие пробуждения истинно
//!
virtual bool waitFor(PIMutex & lk, PISystemTime timeout, const std::function<bool()> & condition);
virtual bool waitFor(PIMutex & lk, PISystemTime timeout, std::function<bool()> condition);
private:
PRIVATE_DECLARATION(PIP_EXPORT)

View File

@@ -540,7 +540,7 @@ PRIVATE_DEFINITION_END(PIThread)
PIThread::PIThread(void * data, ThreadFunc func, bool startNow, PISystemTime loop_delay): PIObject() {
PIINTROSPECTION_THREAD_NEW(this);
data_ = data;
ret_func = func;
ret_func = std::move(func);
terminating = running_ = lockRun = false;
priority_ = piNormal;
delay_ = loop_delay;
@@ -609,13 +609,13 @@ bool PIThread::start(PISystemTime loop_delay) {
bool PIThread::start(ThreadFunc func) {
ret_func = func;
ret_func = std::move(func);
return start();
}
bool PIThread::start(ThreadFunc func, PISystemTime loop_delay) {
ret_func = func;
ret_func = std::move(func);
delay_ = loop_delay;
return start();
}
@@ -641,7 +641,7 @@ bool PIThread::startOnce() {
bool PIThread::startOnce(ThreadFunc func) {
ret_func = func;
ret_func = std::move(func);
return startOnce();
}
@@ -738,12 +738,12 @@ bool PIThread::_startThread(void * func) {
#ifdef FREERTOS
auto name_ba = createThreadName();
if (xTaskCreate((__THREAD_FUNC_RET__ (*)(void *))func,
(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) {
if (xTaskCreate((__THREAD_FUNC_RET__(*)(void *))func,
(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) {
tid_ = (llong)PRIVATE->thread;
return true;
}
@@ -752,7 +752,7 @@ 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, CREATE_SUSPENDED, 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, CREATE_SUSPENDED, 0);
# endif
@@ -766,7 +766,7 @@ bool PIThread::_startThread(void * func) {
pthread_attr_t attr;
pthread_attr_init(&attr);
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);
pthread_attr_destroy(&attr);
// PICout(PICoutManipulators::DefaultControls) << "pthread_create" << PRIVATE->thread;
// piCout << "started" << PRIVATE->thread;
@@ -884,8 +884,8 @@ void PIThread::_runThread() {
PIINTROSPECTION_THREAD_RUN(this);
// PICout(PICoutManipulators::DefaultControls) << "thread" << this << "lock" << "...";
if (lockRun) thread_mutex.lock();
// PICout(PICoutManipulators::DefaultControls) << "thread" << this << "lock" << "ok";
// PICout(PICoutManipulators::DefaultControls) << "thread" << this << "run" << "...";
// PICout(PICoutManipulators::DefaultControls) << "thread" << this << "lock" << "ok";
// PICout(PICoutManipulators::DefaultControls) << "thread" << this << "run" << "...";
#ifdef PIP_INTROSPECTION
PITimeMeasurer _tm;
#endif
@@ -1042,7 +1042,7 @@ void PIThread::runOnce(PIObject * object, const char * handler, const PIString &
void PIThread::runOnce(std::function<void()> func, const PIString & name) {
PIThread * t = new PIThread();
t->setName(name);
t->setSlot(func);
t->setSlot(std::move(func));
#ifndef MICRO_PIP
__PIThreadCollection::instance()->startedAuto(t);
CONNECT0(void, t, stopped, __PIThreadCollection::instance(), stoppedAuto);

View File

@@ -68,8 +68,8 @@ PIThreadPoolExecutor::~PIThreadPoolExecutor() {
}
void PIThreadPoolExecutor::execute(const std::function<void()> & runnable) {
if (!isShutdown_) taskQueue.offer(runnable);
void PIThreadPoolExecutor::execute(std::function<void()> runnable) {
if (!isShutdown_) taskQueue.offer(std::move(runnable));
}

View File

@@ -59,7 +59,7 @@ public:
//! \~russian
//! Это вызов по принципу best-effort без ожидания результата и без сообщения о том, была ли задача принята.
//! \После запроса на завершение новые задачи игнорируются.
void execute(const std::function<void()> & runnable);
void execute(std::function<void()> runnable);
//! \~english Requests immediate shutdown and stops worker threads without waiting for queued tasks to finish.
//! \~russian Запрашивает немедленное завершение и останавливает рабочие потоки без ожидания завершения задач в очереди.

View File

@@ -136,7 +136,7 @@ PIThreadPoolLoop::~PIThreadPoolLoop() {
void PIThreadPoolLoop::setFunction(std::function<void(int)> f) {
func = f;
func = std::move(f);
}
@@ -163,6 +163,6 @@ void PIThreadPoolLoop::exec(int index_start, int index_count) {
void PIThreadPoolLoop::exec(int index_start, int index_count, std::function<void(int)> f) {
setFunction(f);
setFunction(std::move(f));
exec(index_start, index_count);
}