From b4b5240356b6fa324a33e893c07829cf6ecdb65e Mon Sep 17 00:00:00 2001 From: Tor Egge Date: Mon, 30 Oct 2017 14:45:07 +0000 Subject: Stop using FastOS_Mutex and FastOS_Cond in fastos unit tests. --- fastos/src/tests/job.h | 8 +-- fastos/src/tests/thread_bounce_test.cpp | 28 ++++++---- fastos/src/tests/thread_joinwait_test.cpp | 6 +- fastos/src/tests/thread_mutex_test.cpp | 21 +++---- fastos/src/tests/thread_test_base.hpp | 74 +++++++++++++------------ fastos/src/tests/threadtest.cpp | 91 +++++++++++++++---------------- 6 files changed, 115 insertions(+), 113 deletions(-) (limited to 'fastos/src/tests') diff --git a/fastos/src/tests/job.h b/fastos/src/tests/job.h index 37df1f78ef5..1d35ec95270 100644 --- a/fastos/src/tests/job.h +++ b/fastos/src/tests/job.h @@ -2,8 +2,8 @@ #pragma once -#include -#include +#include +#include enum JobCode { @@ -31,8 +31,8 @@ private: public: JobCode code; char *message; - FastOS_Mutex *mutex; - FastOS_Cond *condition; + std::mutex *mutex; + std::condition_variable *condition; FastOS_ThreadInterface *otherThread, *ownThread; double *timebuf; double average; diff --git a/fastos/src/tests/thread_bounce_test.cpp b/fastos/src/tests/thread_bounce_test.cpp index bf94f3e1aab..423221d55cb 100644 --- a/fastos/src/tests/thread_bounce_test.cpp +++ b/fastos/src/tests/thread_bounce_test.cpp @@ -14,8 +14,10 @@ class Thread_Bounce_Test : public ThreadTestBase TestHeader("Bounce Test"); FastOS_ThreadPool pool(128 * 1024); - FastOS_Cond cond1; - FastOS_Cond cond2; + std::mutex mutex1; + std::condition_variable cond1; + std::mutex mutex2; + std::condition_variable cond2; Job job1; Job job2; FastOS_Time checkTime; @@ -28,7 +30,9 @@ class Thread_Bounce_Test : public ThreadTestBase job2.code = BOUNCE_CONDITIONS; job1.otherjob = &job2; job2.otherjob = &job1; + job1.mutex = &mutex1; job1.condition = &cond1; + job2.mutex = &mutex2; job2.condition = &cond2; job1.ownThread = pool.NewThread(this, static_cast(&job1)); @@ -44,28 +48,28 @@ class Thread_Bounce_Test : public ThreadTestBase left = static_cast(checkTime.MilliSecsToNow()); } - cond1.Lock(); + mutex1.lock(); cnt1 = job1.bouncewakeupcnt; - cond1.Unlock(); - cond2.Lock(); + mutex1.unlock(); + mutex2.lock(); cnt2 = job2.bouncewakeupcnt; - cond2.Unlock(); + mutex2.unlock(); cntsum = cnt1 + cnt2; Progress(lastcntsum != cntsum, "%d bounces", cntsum); lastcntsum = cntsum; } job1.ownThread->SetBreakFlag(); - cond1.Lock(); + mutex1.lock(); job1.bouncewakeup = true; - cond1.Signal(); - cond1.Unlock(); + cond1.notify_one(); + mutex1.unlock(); job2.ownThread->SetBreakFlag(); - cond2.Lock(); + mutex2.lock(); job2.bouncewakeup = true; - cond2.Signal(); - cond2.Unlock(); + cond2.notify_one(); + mutex2.unlock(); pool.Close(); Progress(true, "Pool closed."); diff --git a/fastos/src/tests/thread_joinwait_test.cpp b/fastos/src/tests/thread_joinwait_test.cpp index 05ab1627334..7153a05f836 100644 --- a/fastos/src/tests/thread_joinwait_test.cpp +++ b/fastos/src/tests/thread_joinwait_test.cpp @@ -25,11 +25,11 @@ class Thread_JoinWait_Test : public ThreadTestBase Job jobs[testThreads]; - FastOS_Mutex jobMutex; + std::mutex jobMutex; // The mutex is used to pause the first threads until we have created // the last one. - jobMutex.Lock(); + jobMutex.lock(); for(i=0; i + static volatile int64_t number; #define INCREASE_NUMBER_AMOUNT 10000 +using namespace std::chrono_literals; + class ThreadTestBase : public BaseTest, public FastOS_Runnable { private: - FastOS_Mutex printMutex; + std::mutex printMutex; public: ThreadTestBase(void) @@ -19,9 +23,8 @@ public: void PrintProgress (char *string) override { - printMutex.Lock(); + std::lock_guard guard(printMutex); BaseTest::PrintProgress(string); - printMutex.Unlock(); } void Run (FastOS_ThreadInterface *thread, void *arg) override; @@ -93,8 +96,10 @@ void ThreadTestBase::Run (FastOS_ThreadInterface *thread, void *arg) { int result; - if(job->mutex != nullptr) - job->mutex->Lock(); + std::unique_lock guard; + if(job->mutex != nullptr) { + guard = std::unique_lock(*job->mutex); + } result = static_cast(number); @@ -107,8 +112,7 @@ void ThreadTestBase::Run (FastOS_ThreadInterface *thread, void *arg) FastOS_Thread::Sleep(1000); } - if(job->mutex != nullptr) - job->mutex->Unlock(); + guard = std::unique_lock(); job->result = result; // This marks the end of the thread @@ -132,26 +136,23 @@ void ThreadTestBase::Run (FastOS_ThreadInterface *thread, void *arg) case WAIT_FOR_THREAD_TO_FINISH: { - if(job->mutex) - job->mutex->Lock(); + std::unique_lock guard; + if (job->mutex != nullptr) { + guard = std::unique_lock(*job->mutex); + } if(job->otherThread != nullptr) job->otherThread->Join(); - if(job->mutex) - job->mutex->Unlock(); break; } case WAIT_FOR_CONDITION: { - job->condition->Lock(); - + std::unique_lock guard(*job->mutex); job->result = 1; - - job->condition->Wait(); - job->condition->Unlock(); - + job->condition->wait(guard); + guard.unlock(); job->result = 0; break; @@ -160,25 +161,25 @@ void ThreadTestBase::Run (FastOS_ThreadInterface *thread, void *arg) case BOUNCE_CONDITIONS: { while (!thread->GetBreakFlag()) { - job->otherjob->condition->Lock(); - job->otherjob->bouncewakeupcnt++; - job->otherjob->bouncewakeup = true; - job->otherjob->condition->Signal(); - job->otherjob->condition->Unlock(); - - job->condition->Lock(); - while (!job->bouncewakeup) - job->condition->TimedWait(1); - job->bouncewakeup = false; - job->condition->Unlock(); + { + std::lock_guard guard(*job->otherjob->mutex); + job->otherjob->bouncewakeupcnt++; + job->otherjob->bouncewakeup = true; + job->otherjob->condition->notify_one(); + } + std::unique_lock guard(*job->mutex); + while (!job->bouncewakeup) { + job->condition->wait_for(guard, 1ms); + } + job->bouncewakeup = false; } break; } case TEST_ID: { - job->mutex->Lock(); // Initially the parent threads owns the lock - job->mutex->Unlock(); // It is unlocked when we should start + job->mutex->lock(); // Initially the parent threads owns the lock + job->mutex->unlock(); // It is unlocked when we should start FastOS_ThreadId currentId = FastOS_Thread::GetCurrentThreadId(); @@ -192,18 +193,19 @@ void ThreadTestBase::Run (FastOS_ThreadInterface *thread, void *arg) case WAIT2SEC_AND_SIGNALCOND: { FastOS_Thread::Sleep(2000); - job->condition->Signal(); + job->condition->notify_one(); job->result = 1; break; } case HOLD_MUTEX_FOR2SEC: { - job->mutex->Lock(); - FastOS_Thread::Sleep(2000); - job->mutex->Unlock(); - job->result = 1; - break; + { + std::lock_guard guard(*job->mutex); + FastOS_Thread::Sleep(2000); + } + job->result = 1; + break; } case WAIT_2_SEC: diff --git a/fastos/src/tests/threadtest.cpp b/fastos/src/tests/threadtest.cpp index 81ea234fb97..b0b64697129 100644 --- a/fastos/src/tests/threadtest.cpp +++ b/fastos/src/tests/threadtest.cpp @@ -5,18 +5,18 @@ #include "thread_test_base.hpp" #include #include +#include #define MUTEX_TEST_THREADS 6 #define MAX_THREADS 7 - class ThreadTest : public ThreadTestBase { int Main () override; void WaitForXThreadsToHaveWait (Job *jobs, int jobCount, - FastOS_Cond *condition, + std::mutex &mutex, int numWait) { Progress(true, "Waiting for %d threads to be in wait state", numWait); @@ -26,16 +26,15 @@ class ThreadTest : public ThreadTestBase { int waitingThreads=0; - condition->Lock(); - - for(int i=0; i guard(mutex); + for(int i=0; iUnlock(); - if(waitingThreads != oldNumber) Progress(true, "%d threads are waiting", waitingThreads); @@ -323,12 +322,14 @@ class ThreadTest : public ThreadTestBase } void SharedSignalAndBroadcastTest (Job *jobs, int numThreads, - FastOS_Cond *condition, + std::mutex *mutex, + std::condition_variable *condition, FastOS_ThreadPool *pool) { for(int i=0; iNewThread(this, static_cast(&jobs[i])); @@ -338,7 +339,7 @@ class ThreadTest : public ThreadTestBase } WaitForXThreadsToHaveWait (jobs, numThreads, - condition, numThreads); + *mutex, numThreads); // Threads are not guaranteed to have entered sleep yet, // as this test only tests for result code @@ -354,15 +355,16 @@ class ThreadTest : public ThreadTestBase FastOS_ThreadPool pool(128*1024); Job jobs[numThreads]; - FastOS_Cond condition; + std::mutex mutex; + std::condition_variable condition; - SharedSignalAndBroadcastTest(jobs, numThreads, &condition, &pool); + SharedSignalAndBroadcastTest(jobs, numThreads, &mutex, &condition, &pool); for(int i=0; i(&job)); @@ -461,18 +466,17 @@ class ThreadTest : public ThreadTestBase if(job.ownThread != nullptr) { - condition.Lock(); - bool gotCond = condition.TimedWait(500); + std::unique_lock guard(mutex); + bool gotCond = condition.wait_for(guard, 500ms) == std::cv_status::no_timeout; Progress(!gotCond, "We should not get the condition just yet (%s)", gotCond ? "got it" : "didn't get it"); - gotCond = condition.TimedWait(500); + gotCond = condition.wait_for(guard, 500ms) == std::cv_status::no_timeout; Progress(!gotCond, "We should not get the condition just yet (%s)", gotCond ? "got it" : "didn't get it"); - gotCond = condition.TimedWait(5000); + gotCond = condition.wait_for(guard, 5000ms) == std::cv_status::no_timeout; Progress(gotCond, "We should have got the condition now (%s)", gotCond ? "got it" : "didn't get it"); - condition.Unlock(); - } + } Progress(true, "Waiting for threads to finish using pool.Close()..."); pool.Close(); @@ -491,31 +495,22 @@ class ThreadTest : public ThreadTestBase for(i=0; iLock(); - mtx->Unlock(); + std::mutex *mtx = new std::mutex; + mtx->lock(); + mtx->unlock(); delete mtx; if((i % progressIndex) == (progressIndex - 1)) - Progress(true, "Tested %d FastOS_Mutex instances", i + 1); - } - - for(i=0; iLock(); + mutexes[i]->lock(); for(i=0; iUnlock(); + mutexes[i]->unlock(); nowTime.SetNow(); Progress(true, "Tested %d mutexes at time: %d ms", allocCount, -- cgit v1.2.3