summaryrefslogtreecommitdiffstats
path: root/fastos/src/tests
diff options
context:
space:
mode:
Diffstat (limited to 'fastos/src/tests')
-rw-r--r--fastos/src/tests/job.h9
-rw-r--r--fastos/src/tests/processtest.cpp23
-rw-r--r--fastos/src/tests/thread_bounce_test.cpp28
-rw-r--r--fastos/src/tests/thread_joinwait_test.cpp6
-rw-r--r--fastos/src/tests/thread_mutex_test.cpp21
-rw-r--r--fastos/src/tests/thread_test_base.hpp74
-rw-r--r--fastos/src/tests/threadtest.cpp91
-rw-r--r--fastos/src/tests/typetest.cpp3
8 files changed, 126 insertions, 129 deletions
diff --git a/fastos/src/tests/job.h b/fastos/src/tests/job.h
index a5b84fa0f9c..1d35ec95270 100644
--- a/fastos/src/tests/job.h
+++ b/fastos/src/tests/job.h
@@ -2,6 +2,9 @@
#pragma once
+#include <mutex>
+#include <condition_variable>
+
enum JobCode
{
PRINT_MESSAGE_AND_WAIT3SEC,
@@ -28,9 +31,8 @@ private:
public:
JobCode code;
char *message;
- FastOS_Mutex *mutex;
- FastOS_Cond *condition;
- FastOS_BoolCond *boolcondition;
+ std::mutex *mutex;
+ std::condition_variable *condition;
FastOS_ThreadInterface *otherThread, *ownThread;
double *timebuf;
double average;
@@ -45,7 +47,6 @@ public:
message(nullptr),
mutex(nullptr),
condition(nullptr),
- boolcondition(nullptr),
otherThread(nullptr),
ownThread(nullptr),
timebuf(nullptr),
diff --git a/fastos/src/tests/processtest.cpp b/fastos/src/tests/processtest.cpp
index 11e1307027d..cd6839fd0aa 100644
--- a/fastos/src/tests/processtest.cpp
+++ b/fastos/src/tests/processtest.cpp
@@ -17,15 +17,14 @@ public:
static int _allocCount;
static int _successCount;
static int _failCount;
- static FastOS_Mutex *_counterLock;
+ static std::mutex *_counterLock;
MyListener (const char *title)
: _title(title),
_receivedBytes(0)
{
- _counterLock->Lock();
- _allocCount++;
- _counterLock->Unlock();
+ std::lock_guard<std::mutex> guard(*_counterLock);
+ _allocCount++;
}
virtual ~MyListener ()
@@ -34,14 +33,13 @@ public:
const int correctByteCount = 16;
- _counterLock->Lock();
+ std::lock_guard<std::mutex> guard(*_counterLock);
if(_receivedBytes == (isStdout ? correctByteCount : 0))
_successCount++;
else
_failCount++;
_allocCount--;
- _counterLock->Unlock();
}
void OnReceiveData (const void *data, size_t length) override
@@ -62,7 +60,7 @@ public:
int MyListener::_allocCount = 0;
int MyListener::_successCount = 0;
int MyListener::_failCount = 0;
-FastOS_Mutex *MyListener::_counterLock = nullptr;
+std::mutex *MyListener::_counterLock = nullptr;
class ThreadRunJob : public FastOS_Runnable
@@ -122,7 +120,7 @@ private:
// or not.
bool _gotMessage;
int _receivedMessages;
- FastOS_Mutex *_counterLock;
+ std::mutex *_counterLock;
bool _isChild;
public:
ProcessTest ()
@@ -156,9 +154,8 @@ public:
// We only have the counter lock if we are the parent process.
if(_counterLock != nullptr)
{
- _counterLock->Lock();
- _receivedMessages++;
- _counterLock->Unlock();
+ std::lock_guard<std::mutex> guard(*_counterLock);
+ _receivedMessages++;
}
}
@@ -219,7 +216,7 @@ public:
const int numLoops = 100;
const int numEachTime = 40;
- MyListener::_counterLock = new FastOS_Mutex();
+ MyListener::_counterLock = new std::mutex;
char testHeader[200];
strcpy(testHeader, "Process Test");
@@ -381,7 +378,7 @@ public:
TestHeader ("IPC Test");
const char *childProgram = _argv[1];
- _counterLock = new FastOS_Mutex();
+ _counterLock = new std::mutex;
int i;
for(i=0; i<30; i++)
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<void *>(&job1));
@@ -44,28 +48,28 @@ class Thread_Bounce_Test : public ThreadTestBase
left = static_cast<int>(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<lastThreadNum; i++)
{
@@ -68,7 +68,7 @@ class Thread_JoinWait_Test : public ThreadTestBase
}
}
- jobMutex.Unlock();
+ jobMutex.unlock();
if((variant & 1) != 0)
{
diff --git a/fastos/src/tests/thread_mutex_test.cpp b/fastos/src/tests/thread_mutex_test.cpp
index b8ac575038b..d49cf37163d 100644
--- a/fastos/src/tests/thread_mutex_test.cpp
+++ b/fastos/src/tests/thread_mutex_test.cpp
@@ -25,10 +25,11 @@ class Thread_Mutex_Test : public ThreadTestBase
{
int i;
Job jobs[MUTEX_TEST_THREADS];
- FastOS_Mutex *myMutex=nullptr;
+ std::mutex *myMutex=nullptr;
- if(usingMutex)
- myMutex = new FastOS_Mutex();
+ if(usingMutex) {
+ myMutex = new std::mutex;
+ }
for(i=0; i<MUTEX_TEST_THREADS; i++)
{
@@ -117,7 +118,7 @@ class Thread_Mutex_Test : public ThreadTestBase
FastOS_ThreadPool pool(128*1024);
Job job;
- FastOS_Mutex mtx;
+ std::mutex mtx;
job.code = HOLD_MUTEX_FOR2SEC;
job.result = -1;
@@ -135,28 +136,28 @@ class Thread_Mutex_Test : public ThreadTestBase
for(int i=0; i<5; i++)
{
- lockrc = mtx.TryLock();
+ lockrc = mtx.try_lock();
Progress(!lockrc, "We should not get the mutex lock just yet (%s)",
lockrc ? "got it" : "didn't get it");
if(lockrc) {
- mtx.Unlock();
+ mtx.unlock();
break;
}
}
FastOS_Thread::Sleep(2000);
- lockrc = mtx.TryLock();
+ lockrc = mtx.try_lock();
Progress(lockrc, "We should get the mutex lock now (%s)",
lockrc ? "got it" : "didn't get it");
if(lockrc)
- mtx.Unlock();
+ mtx.unlock();
Progress(true, "Attempting to do normal lock...");
- mtx.Lock();
+ mtx.lock();
Progress(true, "Got lock. Attempt to do normal unlock...");
- mtx.Unlock();
+ mtx.unlock();
Progress(true, "Unlock OK.");
}
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:
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 <vespa/fastos/time.h>
#include <cstdlib>
+#include <chrono>
#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<jobCount; i++)
{
- if(jobs[i].result == 1)
- waitingThreads++;
+ std::lock_guard<std::mutex> guard(mutex);
+ for(int i=0; i<jobCount; i++)
+ {
+ if(jobs[i].result == 1)
+ waitingThreads++;
+ }
}
- condition->Unlock();
-
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; i<numThreads; i++)
{
jobs[i].code = WAIT_FOR_CONDITION;
+ jobs[i].mutex = mutex;
jobs[i].condition = condition;
jobs[i].ownThread = pool->NewThread(this,
static_cast<void *>(&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<numThreads; i++)
{
- condition.Signal();
+ condition.notify_one();
WaitForXThreadsToHaveWait(jobs, numThreads,
- &condition, numThreads-1-i);
+ mutex, numThreads-1-i);
}
Progress(true, "Waiting for threads to finish using pool.Close()...");
@@ -379,12 +381,13 @@ 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);
- condition.Broadcast();
- WaitForXThreadsToHaveWait(jobs, numThreads, &condition, 0);
+ condition.notify_all();
+ WaitForXThreadsToHaveWait(jobs, numThreads, mutex, 0);
Progress(true, "Waiting for threads to finish using pool.Close()...");
pool.Close();
@@ -401,9 +404,9 @@ class ThreadTest : public ThreadTestBase
FastOS_ThreadPool pool(128*1024);
Job jobs[numThreads];
- FastOS_Mutex slowStartMutex;
+ std::mutex slowStartMutex;
- slowStartMutex.Lock(); // Halt all threads until we want them to run
+ slowStartMutex.lock(); // Halt all threads until we want them to run
for(i=0; i<numThreads; i++) {
jobs[i].code = TEST_ID;
@@ -428,7 +431,7 @@ class ThreadTest : public ThreadTestBase
}
}
- slowStartMutex.Unlock(); // Allow threads to run
+ slowStartMutex.unlock(); // Allow threads to run
Progress(true, "Waiting for threads to finish using pool.Close()...");
pool.Close();
@@ -449,10 +452,12 @@ class ThreadTest : public ThreadTestBase
FastOS_ThreadPool pool(128*1024);
Job job;
- FastOS_Cond condition;
+ std::mutex mutex;
+ std::condition_variable condition;
job.code = WAIT2SEC_AND_SIGNALCOND;
job.result = -1;
+ job.mutex = &mutex;
job.condition = &condition;
job.ownThread = pool.NewThread(this,
static_cast<void *>(&job));
@@ -461,18 +466,17 @@ class ThreadTest : public ThreadTestBase
if(job.ownThread != nullptr)
{
- condition.Lock();
- bool gotCond = condition.TimedWait(500);
+ std::unique_lock<std::mutex> 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; i<allocCount; i++)
{
- FastOS_Mutex *mtx = new FastOS_Mutex();
- mtx->Lock();
- 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; i<allocCount; i++)
- {
- FastOS_Cond *cond = new FastOS_Cond();
- delete cond;
-
- if((i % progressIndex) == (progressIndex - 1))
- Progress(true, "Tested %d FastOS_Cond instances", i+1);
+ Progress(true, "Tested %d std::mutex instances", i + 1);
}
for(i=0; i<allocCount; i++)
{
- FastOS_BoolCond *cond = new FastOS_BoolCond();
+ std::condition_variable *cond = new std::condition_variable;
delete cond;
if((i % progressIndex) == (progressIndex - 1))
- Progress(true, "Tested %d FastOS_BoolCond instances", i+1);
+ Progress(true, "Tested %d std::condition_variable instances", i+1);
}
PrintSeparator();
@@ -528,13 +523,13 @@ class ThreadTest : public ThreadTestBase
const int allocCount = 150000;
int i;
- FastOS_Mutex **mutexes = new FastOS_Mutex*[allocCount];
+ std::mutex **mutexes = new std::mutex*[allocCount];
FastOS_Time startTime, nowTime;
startTime.SetNow();
for(i=0; i<allocCount; i++)
- mutexes[i] = new FastOS_Mutex();
+ mutexes[i] = new std::mutex;
nowTime.SetNow();
Progress(true, "Allocated %d mutexes at time: %d ms", allocCount,
@@ -543,10 +538,10 @@ class ThreadTest : public ThreadTestBase
for(int e=0; e<4; e++)
{
for(i=0; i<allocCount; i++)
- mutexes[i]->Lock();
+ mutexes[i]->lock();
for(i=0; i<allocCount; i++)
- mutexes[i]->Unlock();
+ mutexes[i]->unlock();
nowTime.SetNow();
Progress(true, "Tested %d mutexes at time: %d ms", allocCount,
diff --git a/fastos/src/tests/typetest.cpp b/fastos/src/tests/typetest.cpp
index 209af305501..503c9a30d24 100644
--- a/fastos/src/tests/typetest.cpp
+++ b/fastos/src/tests/typetest.cpp
@@ -16,11 +16,8 @@ private:
TestHeader("Object Sizes (bytes)");
Progress(true, "FastOS_Application: %d", sizeof(FastOS_Application));
- Progress(true, "FastOS_BoolCond %d", sizeof(FastOS_BoolCond));
- Progress(true, "FastOS_Cond %d", sizeof(FastOS_Cond));
Progress(true, "FastOS_DirectoryScan %d", sizeof(FastOS_DirectoryScan));
Progress(true, "FastOS_File: %d", sizeof(FastOS_File));
- Progress(true, "FastOS_Mutex: %d", sizeof(FastOS_Mutex));
Progress(true, "FastOS_Runnable %d", sizeof(FastOS_Runnable));
Progress(true, "FastOS_ServerSocket %d", sizeof(FastOS_ServerSocket));
Progress(true, "FastOS_Socket: %d", sizeof(FastOS_Socket));