summaryrefslogtreecommitdiffstats
path: root/fastos/src/tests/thread_test_base.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'fastos/src/tests/thread_test_base.hpp')
-rw-r--r--fastos/src/tests/thread_test_base.hpp74
1 files changed, 38 insertions, 36 deletions
diff --git a/fastos/src/tests/thread_test_base.hpp b/fastos/src/tests/thread_test_base.hpp
index 5305b132d3c..7966e95b369 100644
--- a/fastos/src/tests/thread_test_base.hpp
+++ b/fastos/src/tests/thread_test_base.hpp
@@ -2,13 +2,17 @@
#pragma once
+#include <chrono>
+
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<std::mutex> 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<std::mutex> guard;
+ if(job->mutex != nullptr) {
+ guard = std::unique_lock<std::mutex>(*job->mutex);
+ }
result = static_cast<int>(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<std::mutex>();
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<std::mutex> guard;
+ if (job->mutex != nullptr) {
+ guard = std::unique_lock<std::mutex>(*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<std::mutex> 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<std::mutex> guard(*job->otherjob->mutex);
+ job->otherjob->bouncewakeupcnt++;
+ job->otherjob->bouncewakeup = true;
+ job->otherjob->condition->notify_one();
+ }
+ std::unique_lock<std::mutex> 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<std::mutex> guard(*job->mutex);
+ FastOS_Thread::Sleep(2000);
+ }
+ job->result = 1;
+ break;
}
case WAIT_2_SEC: