From 8946dbea418f760e4f0a5cbe2eb2bcbcddd25032 Mon Sep 17 00:00:00 2001 From: Henning Baldersheim Date: Tue, 3 Dec 2019 14:24:41 +0000 Subject: FastOS_THread::Sleep -> std::chrono::sleep_for Renamed Timer -> ScheduledExecutor. Do not include thread.h when not needed in header files. --- fastos/src/tests/processtest.cpp | 4 +++- fastos/src/tests/thread_bounce_test.cpp | 2 +- fastos/src/tests/thread_mutex_test.cpp | 4 ++-- fastos/src/tests/thread_sleep_test.cpp | 2 +- fastos/src/tests/thread_stats_test.cpp | 12 ++++++------ fastos/src/tests/thread_test_base.hpp | 15 ++++++++------- fastos/src/tests/threadtest.cpp | 4 ++-- fastos/src/vespa/fastos/thread.h | 10 +--------- fastos/src/vespa/fastos/unix_process.cpp | 4 +++- fastos/src/vespa/fastos/unix_thread.cpp | 12 ------------ fastos/src/vespa/fastos/unix_thread.h | 1 - 11 files changed, 27 insertions(+), 43 deletions(-) (limited to 'fastos') diff --git a/fastos/src/tests/processtest.cpp b/fastos/src/tests/processtest.cpp index a6729dbb783..5a78eff1d36 100644 --- a/fastos/src/tests/processtest.cpp +++ b/fastos/src/tests/processtest.cpp @@ -3,6 +3,8 @@ #include #include +using namespace std::chrono_literals; + class MyListener : public FastOS_ProcessRedirectListener { private: @@ -119,7 +121,7 @@ public: xproc->WriteStdin(nullptr, 0); } - FastOS_Thread::Sleep(1000); + std::this_thread::sleep_for(1s); } if(i == 10) diff --git a/fastos/src/tests/thread_bounce_test.cpp b/fastos/src/tests/thread_bounce_test.cpp index f7bb7ee1260..84506938455 100644 --- a/fastos/src/tests/thread_bounce_test.cpp +++ b/fastos/src/tests/thread_bounce_test.cpp @@ -43,7 +43,7 @@ class Thread_Bounce_Test : public ThreadTestBase int left = static_cast(checkTime.elapsed().ms()); while (left < 1000) { - FastOS_Thread::Sleep(1000 - left); + std::this_thread::sleep_for(std::chrono::milliseconds(1000 - left)); left = static_cast(checkTime.elapsed().ms()); } diff --git a/fastos/src/tests/thread_mutex_test.cpp b/fastos/src/tests/thread_mutex_test.cpp index d49cf37163d..6d3f8c3c5f0 100644 --- a/fastos/src/tests/thread_mutex_test.cpp +++ b/fastos/src/tests/thread_mutex_test.cpp @@ -132,7 +132,7 @@ class Thread_Mutex_Test : public ThreadTestBase { bool lockrc; - FastOS_Thread::Sleep(1000); + std::this_thread::sleep_for(1s); for(int i=0; i<5; i++) { @@ -145,7 +145,7 @@ class Thread_Mutex_Test : public ThreadTestBase } } - FastOS_Thread::Sleep(2000); + std::this_thread::sleep_for(2s); lockrc = mtx.try_lock(); Progress(lockrc, "We should get the mutex lock now (%s)", diff --git a/fastos/src/tests/thread_sleep_test.cpp b/fastos/src/tests/thread_sleep_test.cpp index 7fd3412b7c3..209b7d3f880 100644 --- a/fastos/src/tests/thread_sleep_test.cpp +++ b/fastos/src/tests/thread_sleep_test.cpp @@ -20,7 +20,7 @@ class Thread_Sleep_Test : public ThreadTestBase Progress(rc, "Creating Thread"); Progress(true, "Sleeping 3 seconds"); - FastOS_Thread::Sleep(3000); + std::this_thread::sleep_for(3s); } Progress(true, "Closing threadpool..."); diff --git a/fastos/src/tests/thread_stats_test.cpp b/fastos/src/tests/thread_stats_test.cpp index 3633c12bcaa..a9d304d411f 100644 --- a/fastos/src/tests/thread_stats_test.cpp +++ b/fastos/src/tests/thread_stats_test.cpp @@ -31,7 +31,7 @@ class Thread_Stats_Test : public ThreadTestBase job[0].ownThread = pool.NewThread(this, static_cast(&job[0])); - FastOS_Thread::Sleep(1000); + std::this_thread::sleep_for(1s); inactiveThreads = pool.GetNumInactiveThreads(); Progress(inactiveThreads == 0, "Inactive threads = %d", inactiveThreads); @@ -44,7 +44,7 @@ class Thread_Stats_Test : public ThreadTestBase job[1].ownThread = pool.NewThread(this, static_cast(&job[1])); - FastOS_Thread::Sleep(1000); + std::this_thread::sleep_for(1s); inactiveThreads = pool.GetNumInactiveThreads(); Progress(inactiveThreads == 0, "Inactive threads = %d", inactiveThreads); @@ -57,7 +57,7 @@ class Thread_Stats_Test : public ThreadTestBase job[0].ownThread->SetBreakFlag(); job[1].ownThread->SetBreakFlag(); - FastOS_Thread::Sleep(3000); + std::this_thread::sleep_for(3s); inactiveThreads = pool.GetNumInactiveThreads(); Progress(inactiveThreads == 2, "Inactive threads = %d", inactiveThreads); @@ -72,7 +72,7 @@ class Thread_Stats_Test : public ThreadTestBase job[0].code = WAIT_FOR_BREAK_FLAG; job[0].ownThread = pool.NewThread(this, static_cast(&job[0])); - FastOS_Thread::Sleep(1000); + std::this_thread::sleep_for(1s); inactiveThreads = pool.GetNumInactiveThreads(); Progress(inactiveThreads == 1, "Inactive threads = %d", inactiveThreads); @@ -84,7 +84,7 @@ class Thread_Stats_Test : public ThreadTestBase job[1].code = WAIT_FOR_BREAK_FLAG; job[1].ownThread = pool.NewThread(this, static_cast(&job[1])); - FastOS_Thread::Sleep(1000); + std::this_thread::sleep_for(1s); inactiveThreads = pool.GetNumInactiveThreads(); Progress(inactiveThreads == 0, "Inactive threads = %d", inactiveThreads); @@ -97,7 +97,7 @@ class Thread_Stats_Test : public ThreadTestBase job[0].ownThread->SetBreakFlag(); job[1].ownThread->SetBreakFlag(); - FastOS_Thread::Sleep(3000); + std::this_thread::sleep_for(3s); inactiveThreads = pool.GetNumInactiveThreads(); Progress(inactiveThreads == 2, "Inactive threads = %d", inactiveThreads); diff --git a/fastos/src/tests/thread_test_base.hpp b/fastos/src/tests/thread_test_base.hpp index 7966e95b369..c4f7ed76ea7 100644 --- a/fastos/src/tests/thread_test_base.hpp +++ b/fastos/src/tests/thread_test_base.hpp @@ -3,6 +3,7 @@ #pragma once #include +#include static volatile int64_t number; #define INCREASE_NUMBER_AMOUNT 10000 @@ -47,7 +48,7 @@ public: } } - FastOS_Thread::Sleep(500); + std::this_thread::sleep_for(500ms); if(threadsFinished) break; @@ -88,7 +89,7 @@ void ThreadTestBase::Run (FastOS_ThreadInterface *thread, void *arg) Progress(true, "Thread printing message: [%s]", job->message); job->result = strlen(job->message); - FastOS_Thread::Sleep(3000); + std::this_thread::sleep_for(3s); break; } @@ -109,7 +110,7 @@ void ThreadTestBase::Run (FastOS_ThreadInterface *thread, void *arg) number = number + 2; if(i == sleepOn) - FastOS_Thread::Sleep(1000); + std::this_thread::sleep_for(1s); } guard = std::unique_lock(); @@ -123,7 +124,7 @@ void ThreadTestBase::Run (FastOS_ThreadInterface *thread, void *arg) { for(;;) { - FastOS_Thread::Sleep(1000); + std::this_thread::sleep_for(1s); if(thread->GetBreakFlag()) { @@ -192,7 +193,7 @@ void ThreadTestBase::Run (FastOS_ThreadInterface *thread, void *arg) case WAIT2SEC_AND_SIGNALCOND: { - FastOS_Thread::Sleep(2000); + std::this_thread::sleep_for(2s); job->condition->notify_one(); job->result = 1; break; @@ -202,7 +203,7 @@ void ThreadTestBase::Run (FastOS_ThreadInterface *thread, void *arg) { { std::lock_guard guard(*job->mutex); - FastOS_Thread::Sleep(2000); + std::this_thread::sleep_for(2s); } job->result = 1; break; @@ -210,7 +211,7 @@ void ThreadTestBase::Run (FastOS_ThreadInterface *thread, void *arg) case WAIT_2_SEC: { - FastOS_Thread::Sleep(2000); + std::this_thread::sleep_for(2s); job->result = 1; break; } diff --git a/fastos/src/tests/threadtest.cpp b/fastos/src/tests/threadtest.cpp index 9507bb1e5d7..0a8a0d2bf02 100644 --- a/fastos/src/tests/threadtest.cpp +++ b/fastos/src/tests/threadtest.cpp @@ -43,7 +43,7 @@ class ThreadTest : public ThreadTestBase if(waitingThreads == numWait) break; - FastOS_Thread::Sleep(100); + std::this_thread::sleep_for(100ms); } } @@ -336,7 +336,7 @@ class ThreadTest : public ThreadTestBase // Threads are not guaranteed to have entered sleep yet, // as this test only tests for result code // Wait another second to be sure. - FastOS_Thread::Sleep(1000); + std::this_thread::sleep_for(1s); } void SignalTest () diff --git a/fastos/src/vespa/fastos/thread.h b/fastos/src/vespa/fastos/thread.h index 12866c71b2c..257acbc92d3 100644 --- a/fastos/src/vespa/fastos/thread.h +++ b/fastos/src/vespa/fastos/thread.h @@ -347,15 +347,7 @@ public: /** * Destructor. */ - virtual ~FastOS_ThreadInterface (){} - - /** - * Sleep for x milliseconds. Attempting to sleep for <1 milliseconds - * will result in failure. - * @param ms Number of milliseconds to sleep. - * @return Boolean success/failure - */ - static bool Sleep(int ms); + virtual ~FastOS_ThreadInterface () {} /** * Instruct a thread to exit. This could be used in conjunction with diff --git a/fastos/src/vespa/fastos/unix_process.cpp b/fastos/src/vespa/fastos/unix_process.cpp index 86d285059b8..4d4197f5354 100644 --- a/fastos/src/vespa/fastos/unix_process.cpp +++ b/fastos/src/vespa/fastos/unix_process.cpp @@ -40,6 +40,8 @@ extern char **environ; #endif +using namespace std::chrono_literals; + static pid_t safe_fork () { pid_t pid; @@ -1629,7 +1631,7 @@ FastOS_UNIX_ProcessStarter::Wait(FastOS_UNIX_Process *process, } } - FastOS_Thread::Sleep(100); + std::this_thread::sleep_for(100ms); } return rc; diff --git a/fastos/src/vespa/fastos/unix_thread.cpp b/fastos/src/vespa/fastos/unix_thread.cpp index 5218bde2630..9e48727deb3 100644 --- a/fastos/src/vespa/fastos/unix_thread.cpp +++ b/fastos/src/vespa/fastos/unix_thread.cpp @@ -83,18 +83,6 @@ FastOS_UNIX_Thread::~FastOS_UNIX_Thread() } } -bool FastOS_UNIX_Thread::Sleep (int ms) -{ - bool rc=false; - - if (ms > 0) { - usleep(ms*1000); - rc = true; - } - - return rc; -} - FastOS_ThreadId FastOS_UNIX_Thread::GetThreadId () { return _handle; diff --git a/fastos/src/vespa/fastos/unix_thread.h b/fastos/src/vespa/fastos/unix_thread.h index c6e0b040fc7..35df3f5745f 100644 --- a/fastos/src/vespa/fastos/unix_thread.h +++ b/fastos/src/vespa/fastos/unix_thread.h @@ -36,7 +36,6 @@ public: ~FastOS_UNIX_Thread(); - static bool Sleep (int ms); FastOS_ThreadId GetThreadId () override; static bool CompareThreadIds (FastOS_ThreadId a, FastOS_ThreadId b); static FastOS_ThreadId GetCurrentThreadId (); -- cgit v1.2.3