diff options
author | Harald Musum <musum@yahoo-inc.com> | 2017-10-31 21:18:48 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-10-31 21:18:48 +0100 |
commit | 9c182da4cc47f43d4d7fa3d9df18ddd39a34d694 (patch) | |
tree | 3617462ce921e1f94e361142b6f22b38e4bfdbf1 /fastos/src/tests | |
parent | 405bd2337aa06c52262659a3e9ed8e41cdb4a93b (diff) |
Revert "Toregge/use standard locks in fastos"
Diffstat (limited to 'fastos/src/tests')
-rw-r--r-- | fastos/src/tests/job.h | 9 | ||||
-rw-r--r-- | fastos/src/tests/processtest.cpp | 23 | ||||
-rw-r--r-- | fastos/src/tests/thread_bounce_test.cpp | 28 | ||||
-rw-r--r-- | fastos/src/tests/thread_joinwait_test.cpp | 6 | ||||
-rw-r--r-- | fastos/src/tests/thread_mutex_test.cpp | 21 | ||||
-rw-r--r-- | fastos/src/tests/thread_test_base.hpp | 74 | ||||
-rw-r--r-- | fastos/src/tests/threadtest.cpp | 91 | ||||
-rw-r--r-- | fastos/src/tests/typetest.cpp | 3 |
8 files changed, 129 insertions, 126 deletions
diff --git a/fastos/src/tests/job.h b/fastos/src/tests/job.h index 1d35ec95270..a5b84fa0f9c 100644 --- a/fastos/src/tests/job.h +++ b/fastos/src/tests/job.h @@ -2,9 +2,6 @@ #pragma once -#include <mutex> -#include <condition_variable> - enum JobCode { PRINT_MESSAGE_AND_WAIT3SEC, @@ -31,8 +28,9 @@ private: public: JobCode code; char *message; - std::mutex *mutex; - std::condition_variable *condition; + FastOS_Mutex *mutex; + FastOS_Cond *condition; + FastOS_BoolCond *boolcondition; FastOS_ThreadInterface *otherThread, *ownThread; double *timebuf; double average; @@ -47,6 +45,7 @@ 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 cd6839fd0aa..11e1307027d 100644 --- a/fastos/src/tests/processtest.cpp +++ b/fastos/src/tests/processtest.cpp @@ -17,14 +17,15 @@ public: static int _allocCount; static int _successCount; static int _failCount; - static std::mutex *_counterLock; + static FastOS_Mutex *_counterLock; MyListener (const char *title) : _title(title), _receivedBytes(0) { - std::lock_guard<std::mutex> guard(*_counterLock); - _allocCount++; + _counterLock->Lock(); + _allocCount++; + _counterLock->Unlock(); } virtual ~MyListener () @@ -33,13 +34,14 @@ public: const int correctByteCount = 16; - std::lock_guard<std::mutex> guard(*_counterLock); + _counterLock->Lock(); if(_receivedBytes == (isStdout ? correctByteCount : 0)) _successCount++; else _failCount++; _allocCount--; + _counterLock->Unlock(); } void OnReceiveData (const void *data, size_t length) override @@ -60,7 +62,7 @@ public: int MyListener::_allocCount = 0; int MyListener::_successCount = 0; int MyListener::_failCount = 0; -std::mutex *MyListener::_counterLock = nullptr; +FastOS_Mutex *MyListener::_counterLock = nullptr; class ThreadRunJob : public FastOS_Runnable @@ -120,7 +122,7 @@ private: // or not. bool _gotMessage; int _receivedMessages; - std::mutex *_counterLock; + FastOS_Mutex *_counterLock; bool _isChild; public: ProcessTest () @@ -154,8 +156,9 @@ public: // We only have the counter lock if we are the parent process. if(_counterLock != nullptr) { - std::lock_guard<std::mutex> guard(*_counterLock); - _receivedMessages++; + _counterLock->Lock(); + _receivedMessages++; + _counterLock->Unlock(); } } @@ -216,7 +219,7 @@ public: const int numLoops = 100; const int numEachTime = 40; - MyListener::_counterLock = new std::mutex; + MyListener::_counterLock = new FastOS_Mutex(); char testHeader[200]; strcpy(testHeader, "Process Test"); @@ -378,7 +381,7 @@ public: TestHeader ("IPC Test"); const char *childProgram = _argv[1]; - _counterLock = new std::mutex; + _counterLock = new FastOS_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 423221d55cb..bf94f3e1aab 100644 --- a/fastos/src/tests/thread_bounce_test.cpp +++ b/fastos/src/tests/thread_bounce_test.cpp @@ -14,10 +14,8 @@ class Thread_Bounce_Test : public ThreadTestBase TestHeader("Bounce Test"); FastOS_ThreadPool pool(128 * 1024); - std::mutex mutex1; - std::condition_variable cond1; - std::mutex mutex2; - std::condition_variable cond2; + FastOS_Cond cond1; + FastOS_Cond cond2; Job job1; Job job2; FastOS_Time checkTime; @@ -30,9 +28,7 @@ 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)); @@ -48,28 +44,28 @@ class Thread_Bounce_Test : public ThreadTestBase left = static_cast<int>(checkTime.MilliSecsToNow()); } - mutex1.lock(); + cond1.Lock(); cnt1 = job1.bouncewakeupcnt; - mutex1.unlock(); - mutex2.lock(); + cond1.Unlock(); + cond2.Lock(); cnt2 = job2.bouncewakeupcnt; - mutex2.unlock(); + cond2.Unlock(); cntsum = cnt1 + cnt2; Progress(lastcntsum != cntsum, "%d bounces", cntsum); lastcntsum = cntsum; } job1.ownThread->SetBreakFlag(); - mutex1.lock(); + cond1.Lock(); job1.bouncewakeup = true; - cond1.notify_one(); - mutex1.unlock(); + cond1.Signal(); + cond1.Unlock(); job2.ownThread->SetBreakFlag(); - mutex2.lock(); + cond2.Lock(); job2.bouncewakeup = true; - cond2.notify_one(); - mutex2.unlock(); + cond2.Signal(); + cond2.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 7153a05f836..05ab1627334 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]; - std::mutex jobMutex; + FastOS_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 d49cf37163d..b8ac575038b 100644 --- a/fastos/src/tests/thread_mutex_test.cpp +++ b/fastos/src/tests/thread_mutex_test.cpp @@ -25,11 +25,10 @@ class Thread_Mutex_Test : public ThreadTestBase { int i; Job jobs[MUTEX_TEST_THREADS]; - std::mutex *myMutex=nullptr; + FastOS_Mutex *myMutex=nullptr; - if(usingMutex) { - myMutex = new std::mutex; - } + if(usingMutex) + myMutex = new FastOS_Mutex(); for(i=0; i<MUTEX_TEST_THREADS; i++) { @@ -118,7 +117,7 @@ class Thread_Mutex_Test : public ThreadTestBase FastOS_ThreadPool pool(128*1024); Job job; - std::mutex mtx; + FastOS_Mutex mtx; job.code = HOLD_MUTEX_FOR2SEC; job.result = -1; @@ -136,28 +135,28 @@ class Thread_Mutex_Test : public ThreadTestBase for(int i=0; i<5; i++) { - lockrc = mtx.try_lock(); + lockrc = mtx.TryLock(); 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.try_lock(); + lockrc = mtx.TryLock(); 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 7966e95b369..5305b132d3c 100644 --- a/fastos/src/tests/thread_test_base.hpp +++ b/fastos/src/tests/thread_test_base.hpp @@ -2,17 +2,13 @@ #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: - std::mutex printMutex; + FastOS_Mutex printMutex; public: ThreadTestBase(void) @@ -23,8 +19,9 @@ public: void PrintProgress (char *string) override { - std::lock_guard<std::mutex> guard(printMutex); + printMutex.Lock(); BaseTest::PrintProgress(string); + printMutex.Unlock(); } void Run (FastOS_ThreadInterface *thread, void *arg) override; @@ -96,10 +93,8 @@ void ThreadTestBase::Run (FastOS_ThreadInterface *thread, void *arg) { int result; - std::unique_lock<std::mutex> guard; - if(job->mutex != nullptr) { - guard = std::unique_lock<std::mutex>(*job->mutex); - } + if(job->mutex != nullptr) + job->mutex->Lock(); result = static_cast<int>(number); @@ -112,7 +107,8 @@ void ThreadTestBase::Run (FastOS_ThreadInterface *thread, void *arg) FastOS_Thread::Sleep(1000); } - guard = std::unique_lock<std::mutex>(); + if(job->mutex != nullptr) + job->mutex->Unlock(); job->result = result; // This marks the end of the thread @@ -136,23 +132,26 @@ void ThreadTestBase::Run (FastOS_ThreadInterface *thread, void *arg) case WAIT_FOR_THREAD_TO_FINISH: { - std::unique_lock<std::mutex> guard; - if (job->mutex != nullptr) { - guard = std::unique_lock<std::mutex>(*job->mutex); - } + if(job->mutex) + job->mutex->Lock(); if(job->otherThread != nullptr) job->otherThread->Join(); + if(job->mutex) + job->mutex->Unlock(); break; } case WAIT_FOR_CONDITION: { - std::unique_lock<std::mutex> guard(*job->mutex); + job->condition->Lock(); + job->result = 1; - job->condition->wait(guard); - guard.unlock(); + + job->condition->Wait(); + job->condition->Unlock(); + job->result = 0; break; @@ -161,25 +160,25 @@ void ThreadTestBase::Run (FastOS_ThreadInterface *thread, void *arg) case BOUNCE_CONDITIONS: { while (!thread->GetBreakFlag()) { - { - 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; + 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(); } 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(); @@ -193,19 +192,18 @@ void ThreadTestBase::Run (FastOS_ThreadInterface *thread, void *arg) case WAIT2SEC_AND_SIGNALCOND: { FastOS_Thread::Sleep(2000); - job->condition->notify_one(); + job->condition->Signal(); job->result = 1; break; } case HOLD_MUTEX_FOR2SEC: { - { - std::lock_guard<std::mutex> guard(*job->mutex); - FastOS_Thread::Sleep(2000); - } - job->result = 1; - break; + job->mutex->Lock(); + FastOS_Thread::Sleep(2000); + job->mutex->Unlock(); + job->result = 1; + break; } case WAIT_2_SEC: diff --git a/fastos/src/tests/threadtest.cpp b/fastos/src/tests/threadtest.cpp index b0b64697129..81ea234fb97 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, - std::mutex &mutex, + FastOS_Cond *condition, int numWait) { Progress(true, "Waiting for %d threads to be in wait state", numWait); @@ -26,15 +26,16 @@ class ThreadTest : public ThreadTestBase { int waitingThreads=0; + condition->Lock(); + + for(int i=0; i<jobCount; i++) { - std::lock_guard<std::mutex> guard(mutex); - for(int i=0; i<jobCount; i++) - { - if(jobs[i].result == 1) - waitingThreads++; - } + if(jobs[i].result == 1) + waitingThreads++; } + condition->Unlock(); + if(waitingThreads != oldNumber) Progress(true, "%d threads are waiting", waitingThreads); @@ -322,14 +323,12 @@ class ThreadTest : public ThreadTestBase } void SharedSignalAndBroadcastTest (Job *jobs, int numThreads, - std::mutex *mutex, - std::condition_variable *condition, + FastOS_Cond *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])); @@ -339,7 +338,7 @@ class ThreadTest : public ThreadTestBase } WaitForXThreadsToHaveWait (jobs, numThreads, - *mutex, numThreads); + condition, numThreads); // Threads are not guaranteed to have entered sleep yet, // as this test only tests for result code @@ -355,16 +354,15 @@ class ThreadTest : public ThreadTestBase FastOS_ThreadPool pool(128*1024); Job jobs[numThreads]; - std::mutex mutex; - std::condition_variable condition; + FastOS_Cond condition; - SharedSignalAndBroadcastTest(jobs, numThreads, &mutex, &condition, &pool); + SharedSignalAndBroadcastTest(jobs, numThreads, &condition, &pool); for(int i=0; i<numThreads; i++) { - condition.notify_one(); + condition.Signal(); WaitForXThreadsToHaveWait(jobs, numThreads, - mutex, numThreads-1-i); + &condition, numThreads-1-i); } Progress(true, "Waiting for threads to finish using pool.Close()..."); @@ -381,13 +379,12 @@ class ThreadTest : public ThreadTestBase FastOS_ThreadPool pool(128*1024); Job jobs[numThreads]; - std::mutex mutex; - std::condition_variable condition; + FastOS_Cond condition; - SharedSignalAndBroadcastTest(jobs, numThreads, &mutex, &condition, &pool); + SharedSignalAndBroadcastTest(jobs, numThreads, &condition, &pool); - condition.notify_all(); - WaitForXThreadsToHaveWait(jobs, numThreads, mutex, 0); + condition.Broadcast(); + WaitForXThreadsToHaveWait(jobs, numThreads, &condition, 0); Progress(true, "Waiting for threads to finish using pool.Close()..."); pool.Close(); @@ -404,9 +401,9 @@ class ThreadTest : public ThreadTestBase FastOS_ThreadPool pool(128*1024); Job jobs[numThreads]; - std::mutex slowStartMutex; + FastOS_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; @@ -431,7 +428,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(); @@ -452,12 +449,10 @@ class ThreadTest : public ThreadTestBase FastOS_ThreadPool pool(128*1024); Job job; - std::mutex mutex; - std::condition_variable condition; + FastOS_Cond condition; job.code = WAIT2SEC_AND_SIGNALCOND; job.result = -1; - job.mutex = &mutex; job.condition = &condition; job.ownThread = pool.NewThread(this, static_cast<void *>(&job)); @@ -466,17 +461,18 @@ class ThreadTest : public ThreadTestBase if(job.ownThread != nullptr) { - std::unique_lock<std::mutex> guard(mutex); - bool gotCond = condition.wait_for(guard, 500ms) == std::cv_status::no_timeout; + condition.Lock(); + bool gotCond = condition.TimedWait(500); Progress(!gotCond, "We should not get the condition just yet (%s)", gotCond ? "got it" : "didn't get it"); - gotCond = condition.wait_for(guard, 500ms) == std::cv_status::no_timeout; + gotCond = condition.TimedWait(500); Progress(!gotCond, "We should not get the condition just yet (%s)", gotCond ? "got it" : "didn't get it"); - gotCond = condition.wait_for(guard, 5000ms) == std::cv_status::no_timeout; + gotCond = condition.TimedWait(5000); 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(); @@ -495,22 +491,31 @@ class ThreadTest : public ThreadTestBase for(i=0; i<allocCount; i++) { - std::mutex *mtx = new std::mutex; - mtx->lock(); - mtx->unlock(); + FastOS_Mutex *mtx = new FastOS_Mutex(); + mtx->Lock(); + mtx->Unlock(); delete mtx; if((i % progressIndex) == (progressIndex - 1)) - Progress(true, "Tested %d std::mutex instances", i + 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); } for(i=0; i<allocCount; i++) { - std::condition_variable *cond = new std::condition_variable; + FastOS_BoolCond *cond = new FastOS_BoolCond(); delete cond; if((i % progressIndex) == (progressIndex - 1)) - Progress(true, "Tested %d std::condition_variable instances", i+1); + Progress(true, "Tested %d FastOS_BoolCond instances", i+1); } PrintSeparator(); @@ -523,13 +528,13 @@ class ThreadTest : public ThreadTestBase const int allocCount = 150000; int i; - std::mutex **mutexes = new std::mutex*[allocCount]; + FastOS_Mutex **mutexes = new FastOS_Mutex*[allocCount]; FastOS_Time startTime, nowTime; startTime.SetNow(); for(i=0; i<allocCount; i++) - mutexes[i] = new std::mutex; + mutexes[i] = new FastOS_Mutex(); nowTime.SetNow(); Progress(true, "Allocated %d mutexes at time: %d ms", allocCount, @@ -538,10 +543,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 503c9a30d24..209af305501 100644 --- a/fastos/src/tests/typetest.cpp +++ b/fastos/src/tests/typetest.cpp @@ -16,8 +16,11 @@ 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)); |