From be06a8d3685b946150e2fdf15847878d2b283500 Mon Sep 17 00:00:00 2001 From: Alex Fabijanic Date: Thu, 21 Jul 2022 22:42:53 +0200 Subject: [PATCH] fix(Thread): call std::this_thread::sleep_for() to sleep #3703 --- Foundation/include/Poco/Thread.h | 10 +++-- Foundation/include/Poco/Thread_POSIX.h | 1 - Foundation/include/Poco/Thread_VX.h | 1 - Foundation/include/Poco/Thread_WIN32.h | 7 ---- Foundation/include/Poco/Thread_WINCE.h | 7 ---- Foundation/src/Thread_POSIX.cpp | 58 -------------------------- Foundation/src/Thread_VX.cpp | 25 ----------- 7 files changed, 6 insertions(+), 103 deletions(-) diff --git a/Foundation/include/Poco/Thread.h b/Foundation/include/Poco/Thread.h index 891773f85e..828c3365bb 100644 --- a/Foundation/include/Poco/Thread.h +++ b/Foundation/include/Poco/Thread.h @@ -21,6 +21,8 @@ #include "Poco/Foundation.h" #include "Poco/Event.h" #include "Poco/Mutex.h" +#include +#include #if defined(POCO_OS_FAMILY_WINDOWS) @@ -317,15 +319,15 @@ inline bool Thread::isRunning() const } -inline void Thread::sleep(long milliseconds) +inline void Thread::yield() { - sleepImpl(milliseconds); + yieldImpl(); } -inline void Thread::yield() +inline void Thread::sleep(long milliseconds) { - yieldImpl(); + std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds)); } diff --git a/Foundation/include/Poco/Thread_POSIX.h b/Foundation/include/Poco/Thread_POSIX.h index cd3b1e62d1..6c6ab82b23 100644 --- a/Foundation/include/Poco/Thread_POSIX.h +++ b/Foundation/include/Poco/Thread_POSIX.h @@ -80,7 +80,6 @@ class Foundation_API ThreadImpl void joinImpl(); bool joinImpl(long milliseconds); bool isRunningImpl() const; - static void sleepImpl(long milliseconds); static void yieldImpl(); static ThreadImpl* currentImpl(); static TIDImpl currentTidImpl(); diff --git a/Foundation/include/Poco/Thread_VX.h b/Foundation/include/Poco/Thread_VX.h index d44c869bdb..ace541097b 100644 --- a/Foundation/include/Poco/Thread_VX.h +++ b/Foundation/include/Poco/Thread_VX.h @@ -90,7 +90,6 @@ class Foundation_API ThreadImpl void joinImpl(); bool joinImpl(long milliseconds); bool isRunningImpl() const; - static void sleepImpl(long milliseconds); static void yieldImpl(); static ThreadImpl* currentImpl(); static TIDImpl currentTidImpl(); diff --git a/Foundation/include/Poco/Thread_WIN32.h b/Foundation/include/Poco/Thread_WIN32.h index 4967d60a66..822b6e1258 100644 --- a/Foundation/include/Poco/Thread_WIN32.h +++ b/Foundation/include/Poco/Thread_WIN32.h @@ -75,7 +75,6 @@ class Foundation_API ThreadImpl void joinImpl(); bool joinImpl(long milliseconds); bool isRunningImpl() const; - static void sleepImpl(long milliseconds); static void yieldImpl(); static ThreadImpl* currentImpl(); static TIDImpl currentTidImpl(); @@ -155,12 +154,6 @@ inline int ThreadImpl::getMaxOSPriorityImpl(int /* policy */) } -inline void ThreadImpl::sleepImpl(long milliseconds) -{ - Sleep(DWORD(milliseconds)); -} - - inline void ThreadImpl::yieldImpl() { Sleep(0); diff --git a/Foundation/include/Poco/Thread_WINCE.h b/Foundation/include/Poco/Thread_WINCE.h index e4996c92d5..b99258aba5 100644 --- a/Foundation/include/Poco/Thread_WINCE.h +++ b/Foundation/include/Poco/Thread_WINCE.h @@ -75,7 +75,6 @@ class Foundation_API ThreadImpl void joinImpl(); bool joinImpl(long milliseconds); bool isRunningImpl() const; - static void sleepImpl(long milliseconds); static void yieldImpl(); static ThreadImpl* currentImpl(); static TIDImpl currentTidImpl(); @@ -151,12 +150,6 @@ inline int ThreadImpl::getMaxOSPriorityImpl(int /* policy */) } -inline void ThreadImpl::sleepImpl(long milliseconds) -{ - Sleep(DWORD(milliseconds)); -} - - inline void ThreadImpl::yieldImpl() { Sleep(0); diff --git a/Foundation/src/Thread_POSIX.cpp b/Foundation/src/Thread_POSIX.cpp index fe6c66a859..3f66b12d4d 100644 --- a/Foundation/src/Thread_POSIX.cpp +++ b/Foundation/src/Thread_POSIX.cpp @@ -327,64 +327,6 @@ long ThreadImpl::currentOsTidImpl() #endif } -void ThreadImpl::sleepImpl(long milliseconds) -{ -#if defined(__digital__) - // This is specific to DECThreads - struct timespec interval; - interval.tv_sec = milliseconds / 1000; - interval.tv_nsec = (milliseconds % 1000)*1000000; - pthread_delay_np(&interval); -#elif POCO_OS == POCO_OS_LINUX || POCO_OS == POCO_OS_ANDROID || POCO_OS == POCO_OS_MAC_OS_X || POCO_OS == POCO_OS_QNX || POCO_OS == POCO_OS_VXWORKS - struct timespec expiration; - int rc = clock_gettime(CLOCK_MONOTONIC, &expiration); - if (rc == 0) - { - Poco::UInt64 add = expiration.tv_nsec + (milliseconds * 1000000); - expiration.tv_nsec = add % 1000000000; - expiration.tv_sec += add / 1000000000; - - do - { - rc = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &expiration, NULL); - } while (rc != 0 && errno == EINTR); - - if (rc != 0) - { - throw Poco::SystemException(Poco::format("Thread::sleep(): clock_nanosleep() '%s' (%d) ms=%ld, tv_sec=%ld, tv_nsec=%ld", - Poco::Error::getMessage(rc), rc, milliseconds, expiration.tv_sec, expiration.tv_nsec)); - } - } - else - { - throw Poco::SystemException(Poco::format("Thread::sleep(): clock_gettime(): '%s' (%d) ms=%ld, tv_sec=%ld, tv_nsec=%ld", - Poco::Error::getMessage(rc), rc, milliseconds, expiration.tv_sec, expiration.tv_nsec)); - } -#else - Poco::Timespan remainingTime(1000*Poco::Timespan::TimeDiff(milliseconds)); - int rc; - do - { - struct timeval tv; - tv.tv_sec = (long) remainingTime.totalSeconds(); - tv.tv_usec = (long) remainingTime.useconds(); - Poco::Timestamp start; - rc = ::select(0, NULL, NULL, NULL, &tv); - if (rc < 0 && errno == EINTR) - { - Poco::Timestamp end; - Poco::Timespan waited = start.elapsed(); - if (waited < remainingTime) - remainingTime -= waited; - else - remainingTime = 0; - } - } - while (remainingTime > 0 && rc < 0 && errno == EINTR); - if (rc < 0 && remainingTime > 0) throw Poco::SystemException("Thread::sleep(): select() failed"); -#endif -} - void* ThreadImpl::runnableEntry(void* pThread) { diff --git a/Foundation/src/Thread_VX.cpp b/Foundation/src/Thread_VX.cpp index acb64a63c4..4a4d13e95a 100644 --- a/Foundation/src/Thread_VX.cpp +++ b/Foundation/src/Thread_VX.cpp @@ -181,31 +181,6 @@ long ThreadImpl::currentOsTidImpl() return taskIdSelf(); } -void ThreadImpl::sleepImpl(long milliseconds) -{ - Poco::Timespan remainingTime(1000*Poco::Timespan::TimeDiff(milliseconds)); - int rc; - do - { - struct timespec ts; - ts.tv_sec = (long) remainingTime.totalSeconds(); - ts.tv_nsec = (long) remainingTime.useconds()*1000; - Poco::Timestamp start; - rc = ::nanosleep(&ts, 0); - if (rc < 0 && errno == EINTR) - { - Poco::Timestamp end; - Poco::Timespan waited = start.elapsed(); - if (waited < remainingTime) - remainingTime -= waited; - else - remainingTime = 0; - } - } - while (remainingTime > 0 && rc < 0 && errno == EINTR); - if (rc < 0 && remainingTime > 0) throw Poco::SystemException("Thread::sleep(): nanosleep() failed"); -} - void ThreadImpl::runnableEntry(void* pThread, int, int, int, int, int, int, int, int, int) {