Skip to content

Commit

Permalink
fix(Thread): call std::this_thread::sleep_for() to sleep #3703
Browse files Browse the repository at this point in the history
  • Loading branch information
aleks-f committed Jul 21, 2022
1 parent da2a962 commit be06a8d
Show file tree
Hide file tree
Showing 7 changed files with 6 additions and 103 deletions.
10 changes: 6 additions & 4 deletions Foundation/include/Poco/Thread.h
Expand Up @@ -21,6 +21,8 @@
#include "Poco/Foundation.h"
#include "Poco/Event.h"
#include "Poco/Mutex.h"
#include <thread>
#include <chrono>


#if defined(POCO_OS_FAMILY_WINDOWS)
Expand Down Expand Up @@ -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));
}


Expand Down
1 change: 0 additions & 1 deletion Foundation/include/Poco/Thread_POSIX.h
Expand Up @@ -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();
Expand Down
1 change: 0 additions & 1 deletion Foundation/include/Poco/Thread_VX.h
Expand Up @@ -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();
Expand Down
7 changes: 0 additions & 7 deletions Foundation/include/Poco/Thread_WIN32.h
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 0 additions & 7 deletions Foundation/include/Poco/Thread_WINCE.h
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
58 changes: 0 additions & 58 deletions Foundation/src/Thread_POSIX.cpp
Expand Up @@ -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)
{
Expand Down
25 changes: 0 additions & 25 deletions Foundation/src/Thread_VX.cpp
Expand Up @@ -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)
{
Expand Down

0 comments on commit be06a8d

Please sign in to comment.