diff options
author | Arne H Juul <arnej@yahoo-inc.com> | 2016-06-30 12:32:06 +0200 |
---|---|---|
committer | Arne H Juul <arnej@yahoo-inc.com> | 2016-06-30 12:32:06 +0200 |
commit | 84231cd1e4321779202f5dd2188d505f0b77501e (patch) | |
tree | 49a343131efce1b28d5fc2ef5279d1f767b044b6 /fastos | |
parent | 141b92cbe0db97b28430a150c218d66455ba6398 (diff) |
move common code to new base class
Diffstat (limited to 'fastos')
-rw-r--r-- | fastos/src/tests/base_thread.hpp | 220 | ||||
-rw-r--r-- | fastos/src/tests/thread_bounce_test.cpp | 223 | ||||
-rw-r--r-- | fastos/src/tests/thread_joinwait_test.cpp | 222 | ||||
-rw-r--r-- | fastos/src/tests/thread_mutex_test.cpp | 219 | ||||
-rw-r--r-- | fastos/src/tests/thread_sleep_test.cpp | 223 | ||||
-rw-r--r-- | fastos/src/tests/thread_stats_test.cpp | 223 | ||||
-rw-r--r-- | fastos/src/tests/threadtest.cpp | 262 |
7 files changed, 254 insertions, 1338 deletions
diff --git a/fastos/src/tests/base_thread.hpp b/fastos/src/tests/base_thread.hpp new file mode 100644 index 00000000000..d0303056fc2 --- /dev/null +++ b/fastos/src/tests/base_thread.hpp @@ -0,0 +1,220 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#pragma once + +static volatile int64_t number; +#define INCREASE_NUMBER_AMOUNT 10000 + +class BaseForThreadTest : public BaseTest, public FastOS_Runnable +{ +private: + FastOS_Mutex printMutex; + +public: + BaseForThreadTest(void) + : printMutex() + { + } + virtual ~BaseForThreadTest() {}; + + void PrintProgress (char *string) + { + printMutex.Lock(); + BaseTest::PrintProgress(string); + printMutex.Unlock(); + } + + void Run (FastOS_ThreadInterface *thread, void *arg); + + void WaitForThreadsToFinish (Job *jobs, int count) + { + int i; + + Progress(true, "Waiting for threads to finish..."); + for(;;) + { + bool threadsFinished=true; + + for(i=0; i<count; i++) + { + if(jobs[i].result == -1) + { + threadsFinished = false; + break; + } + } + + FastOS_Thread::Sleep(500); + + if(threadsFinished) + break; + } + + Progress(true, "Threads finished"); + } +}; + + +void BaseForThreadTest::Run (FastOS_ThreadInterface *thread, void *arg) +{ + if(arg == NULL) + return; + + Job *job = static_cast<Job *>(arg); + char someStack[15*1024]; + + memset(someStack, 0, 15*1024); + + switch(job->code) + { + case SILENTNOP: + { + job->result = 1; + break; + } + + case NOP: + { + Progress(true, "Doing NOP"); + job->result = 1; + break; + } + + case PRINT_MESSAGE_AND_WAIT3SEC: + { + Progress(true, "Thread printing message: [%s]", job->message); + job->result = strlen(job->message); + + FastOS_Thread::Sleep(3000); + break; + } + + case INCREASE_NUMBER: + { + int result; + + if(job->mutex != NULL) + job->mutex->Lock(); + + result = static_cast<int>(number); + + int sleepOn = (INCREASE_NUMBER_AMOUNT/2) * 321/10000; + for(int i=0; i<(INCREASE_NUMBER_AMOUNT/2); i++) + { + number = number + 2; + + if(i == sleepOn) + FastOS_Thread::Sleep(1000); + } + + if(job->mutex != NULL) + job->mutex->Unlock(); + + job->result = result; // This marks the end of the thread + + break; + } + + case WAIT_FOR_BREAK_FLAG: + { + for(;;) + { + FastOS_Thread::Sleep(1000); + + if(thread->GetBreakFlag()) + { + Progress(true, "Thread %p got breakflag", thread); + break; + } + } + break; + } + + case WAIT_FOR_THREAD_TO_FINISH: + { + if(job->mutex) + job->mutex->Lock(); + + if(job->otherThread != NULL) + job->otherThread->Join(); + + if(job->mutex) + job->mutex->Unlock(); + break; + } + + case WAIT_FOR_CONDITION: + { + job->condition->Lock(); + + job->result = 1; + + job->condition->Wait(); + job->condition->Unlock(); + + job->result = 0; + + break; + } + + 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(); + } + break; + } + + case TEST_ID: + { + 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(); + + if(currentId == job->_threadId) + job->result = 1; + else + job->result = -1; + break; + } + + case WAIT2SEC_AND_SIGNALCOND: + { + FastOS_Thread::Sleep(2000); + job->condition->Signal(); + job->result = 1; + break; + } + + case HOLD_MUTEX_FOR2SEC: + { + job->mutex->Lock(); + FastOS_Thread::Sleep(2000); + job->mutex->Unlock(); + job->result = 1; + break; + } + + case WAIT_2_SEC: + { + FastOS_Thread::Sleep(2000); + job->result = 1; + break; + } + + default: + Progress(false, "Unknown jobcode"); + break; + } +} diff --git a/fastos/src/tests/thread_bounce_test.cpp b/fastos/src/tests/thread_bounce_test.cpp index 51c986b0502..f9f77b9ff33 100644 --- a/fastos/src/tests/thread_bounce_test.cpp +++ b/fastos/src/tests/thread_bounce_test.cpp @@ -4,62 +4,10 @@ #include <vespa/fastos/fastos.h> #include "tests.h" #include "jobs.h" +#include "base_thread.hpp" -static volatile int64_t number; -#define INCREASE_NUMBER_AMOUNT 10000 - -#define MUTEX_TEST_THREADS 6 -#define MAX_THREADS 7 - - -class ThreadTest : public BaseTest, public FastOS_Runnable +class ThreadTest : public BaseForThreadTest { -private: - FastOS_Mutex printMutex; - -public: - ThreadTest(void) - : printMutex() - { - } - virtual ~ThreadTest() {}; - - void PrintProgress (char *string) - { - printMutex.Lock(); - BaseTest::PrintProgress(string); - printMutex.Unlock(); - } - - void Run (FastOS_ThreadInterface *thread, void *arg); - - void WaitForThreadsToFinish (Job *jobs, int count) - { - int i; - - Progress(true, "Waiting for threads to finish..."); - for(;;) - { - bool threadsFinished=true; - - for(i=0; i<count; i++) - { - if(jobs[i].result == -1) - { - threadsFinished = false; - break; - } - } - - FastOS_Thread::Sleep(500); - - if(threadsFinished) - break; - } - - Progress(true, "Threads finished"); - } - int Main (); void BounceTest(void) @@ -123,7 +71,6 @@ public: pool.Close(); Progress(true, "Pool closed."); PrintSeparator(); - } }; @@ -141,172 +88,6 @@ int ThreadTest::Main () return 0; } -volatile int busyCnt; - -void ThreadTest::Run (FastOS_ThreadInterface *thread, void *arg) -{ - if(arg == NULL) - return; - - Job *job = static_cast<Job *>(arg); - char someStack[15*1024]; - - memset(someStack, 0, 15*1024); - - switch(job->code) - { - case SILENTNOP: - { - job->result = 1; - break; - } - - case NOP: - { - Progress(true, "Doing NOP"); - job->result = 1; - break; - } - - case PRINT_MESSAGE_AND_WAIT3SEC: - { - Progress(true, "Thread printing message: [%s]", job->message); - job->result = strlen(job->message); - - FastOS_Thread::Sleep(3000); - break; - } - - case INCREASE_NUMBER: - { - int result; - - if(job->mutex != NULL) - job->mutex->Lock(); - - result = static_cast<int>(number); - - int sleepOn = (INCREASE_NUMBER_AMOUNT/2) * 321/10000; - for(int i=0; i<(INCREASE_NUMBER_AMOUNT/2); i++) - { - number = number + 2; - - if(i == sleepOn) - FastOS_Thread::Sleep(1000); - } - - if(job->mutex != NULL) - job->mutex->Unlock(); - - job->result = result; // This marks the end of the thread - - break; - } - - case WAIT_FOR_BREAK_FLAG: - { - for(;;) - { - FastOS_Thread::Sleep(1000); - - if(thread->GetBreakFlag()) - { - Progress(true, "Thread %p got breakflag", thread); - break; - } - } - break; - } - - case WAIT_FOR_THREAD_TO_FINISH: - { - if(job->mutex) - job->mutex->Lock(); - - if(job->otherThread != NULL) - job->otherThread->Join(); - - if(job->mutex) - job->mutex->Unlock(); - break; - } - - case WAIT_FOR_CONDITION: - { - job->condition->Lock(); - - job->result = 1; - - job->condition->Wait(); - job->condition->Unlock(); - - job->result = 0; - - break; - } - - 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(); - } - break; - } - - case TEST_ID: - { - 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(); - - if(currentId == job->_threadId) - job->result = 1; - else - job->result = -1; - break; - } - - case WAIT2SEC_AND_SIGNALCOND: - { - FastOS_Thread::Sleep(2000); - job->condition->Signal(); - job->result = 1; - break; - } - - case HOLD_MUTEX_FOR2SEC: - { - job->mutex->Lock(); - FastOS_Thread::Sleep(2000); - job->mutex->Unlock(); - job->result = 1; - break; - } - - case WAIT_2_SEC: - { - FastOS_Thread::Sleep(2000); - job->result = 1; - break; - } - - default: - Progress(false, "Unknown jobcode"); - break; - } -} - int main (int argc, char **argv) { ThreadTest app; diff --git a/fastos/src/tests/thread_joinwait_test.cpp b/fastos/src/tests/thread_joinwait_test.cpp index 003c515ef4a..433adf7b38e 100644 --- a/fastos/src/tests/thread_joinwait_test.cpp +++ b/fastos/src/tests/thread_joinwait_test.cpp @@ -4,62 +4,10 @@ #include <vespa/fastos/fastos.h> #include "tests.h" #include "jobs.h" +#include "base_thread.hpp" -static volatile int64_t number; -#define INCREASE_NUMBER_AMOUNT 10000 - -#define MUTEX_TEST_THREADS 6 -#define MAX_THREADS 7 - - -class ThreadTest : public BaseTest, public FastOS_Runnable +class ThreadTest : public BaseForThreadTest { -private: - FastOS_Mutex printMutex; - -public: - ThreadTest(void) - : printMutex() - { - } - virtual ~ThreadTest() {}; - - void PrintProgress (char *string) - { - printMutex.Lock(); - BaseTest::PrintProgress(string); - printMutex.Unlock(); - } - - void Run (FastOS_ThreadInterface *thread, void *arg); - - void WaitForThreadsToFinish (Job *jobs, int count) - { - int i; - - Progress(true, "Waiting for threads to finish..."); - for(;;) - { - bool threadsFinished=true; - - for(i=0; i<count; i++) - { - if(jobs[i].result == -1) - { - threadsFinished = false; - break; - } - } - - FastOS_Thread::Sleep(500); - - if(threadsFinished) - break; - } - - Progress(true, "Threads finished"); - } - int Main (); void SingleThreadJoinWaitMultipleTest(int variant) @@ -167,172 +115,6 @@ int ThreadTest::Main () return 0; } -volatile int busyCnt; - -void ThreadTest::Run (FastOS_ThreadInterface *thread, void *arg) -{ - if(arg == NULL) - return; - - Job *job = static_cast<Job *>(arg); - char someStack[15*1024]; - - memset(someStack, 0, 15*1024); - - switch(job->code) - { - case SILENTNOP: - { - job->result = 1; - break; - } - - case NOP: - { - Progress(true, "Doing NOP"); - job->result = 1; - break; - } - - case PRINT_MESSAGE_AND_WAIT3SEC: - { - Progress(true, "Thread printing message: [%s]", job->message); - job->result = strlen(job->message); - - FastOS_Thread::Sleep(3000); - break; - } - - case INCREASE_NUMBER: - { - int result; - - if(job->mutex != NULL) - job->mutex->Lock(); - - result = static_cast<int>(number); - - int sleepOn = (INCREASE_NUMBER_AMOUNT/2) * 321/10000; - for(int i=0; i<(INCREASE_NUMBER_AMOUNT/2); i++) - { - number = number + 2; - - if(i == sleepOn) - FastOS_Thread::Sleep(1000); - } - - if(job->mutex != NULL) - job->mutex->Unlock(); - - job->result = result; // This marks the end of the thread - - break; - } - - case WAIT_FOR_BREAK_FLAG: - { - for(;;) - { - FastOS_Thread::Sleep(1000); - - if(thread->GetBreakFlag()) - { - Progress(true, "Thread %p got breakflag", thread); - break; - } - } - break; - } - - case WAIT_FOR_THREAD_TO_FINISH: - { - if(job->mutex) - job->mutex->Lock(); - - if(job->otherThread != NULL) - job->otherThread->Join(); - - if(job->mutex) - job->mutex->Unlock(); - break; - } - - case WAIT_FOR_CONDITION: - { - job->condition->Lock(); - - job->result = 1; - - job->condition->Wait(); - job->condition->Unlock(); - - job->result = 0; - - break; - } - - 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(); - } - break; - } - - case TEST_ID: - { - 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(); - - if(currentId == job->_threadId) - job->result = 1; - else - job->result = -1; - break; - } - - case WAIT2SEC_AND_SIGNALCOND: - { - FastOS_Thread::Sleep(2000); - job->condition->Signal(); - job->result = 1; - break; - } - - case HOLD_MUTEX_FOR2SEC: - { - job->mutex->Lock(); - FastOS_Thread::Sleep(2000); - job->mutex->Unlock(); - job->result = 1; - break; - } - - case WAIT_2_SEC: - { - FastOS_Thread::Sleep(2000); - job->result = 1; - break; - } - - default: - Progress(false, "Unknown jobcode"); - break; - } -} - int main (int argc, char **argv) { ThreadTest app; diff --git a/fastos/src/tests/thread_mutex_test.cpp b/fastos/src/tests/thread_mutex_test.cpp index 90bdc40dbf5..aa888fbdd4b 100644 --- a/fastos/src/tests/thread_mutex_test.cpp +++ b/fastos/src/tests/thread_mutex_test.cpp @@ -4,62 +4,13 @@ #include <vespa/fastos/fastos.h> #include "tests.h" #include "jobs.h" - -static volatile int64_t number; -#define INCREASE_NUMBER_AMOUNT 10000 +#include "base_thread.hpp" #define MUTEX_TEST_THREADS 6 #define MAX_THREADS 7 - -class ThreadTest : public BaseTest, public FastOS_Runnable +class ThreadTest : public BaseForThreadTest { -private: - FastOS_Mutex printMutex; - -public: - ThreadTest(void) - : printMutex() - { - } - virtual ~ThreadTest() {}; - - void PrintProgress (char *string) - { - printMutex.Lock(); - BaseTest::PrintProgress(string); - printMutex.Unlock(); - } - - void Run (FastOS_ThreadInterface *thread, void *arg); - - void WaitForThreadsToFinish (Job *jobs, int count) - { - int i; - - Progress(true, "Waiting for threads to finish..."); - for(;;) - { - bool threadsFinished=true; - - for(i=0; i<count; i++) - { - if(jobs[i].result == -1) - { - threadsFinished = false; - break; - } - } - - FastOS_Thread::Sleep(500); - - if(threadsFinished) - break; - } - - Progress(true, "Threads finished"); - } - int Main (); void MutexTest (bool usingMutex) @@ -237,172 +188,6 @@ int ThreadTest::Main () return 0; } -volatile int busyCnt; - -void ThreadTest::Run (FastOS_ThreadInterface *thread, void *arg) -{ - if(arg == NULL) - return; - - Job *job = static_cast<Job *>(arg); - char someStack[15*1024]; - - memset(someStack, 0, 15*1024); - - switch(job->code) - { - case SILENTNOP: - { - job->result = 1; - break; - } - - case NOP: - { - Progress(true, "Doing NOP"); - job->result = 1; - break; - } - - case PRINT_MESSAGE_AND_WAIT3SEC: - { - Progress(true, "Thread printing message: [%s]", job->message); - job->result = strlen(job->message); - - FastOS_Thread::Sleep(3000); - break; - } - - case INCREASE_NUMBER: - { - int result; - - if(job->mutex != NULL) - job->mutex->Lock(); - - result = static_cast<int>(number); - - int sleepOn = (INCREASE_NUMBER_AMOUNT/2) * 321/10000; - for(int i=0; i<(INCREASE_NUMBER_AMOUNT/2); i++) - { - number = number + 2; - - if(i == sleepOn) - FastOS_Thread::Sleep(1000); - } - - if(job->mutex != NULL) - job->mutex->Unlock(); - - job->result = result; // This marks the end of the thread - - break; - } - - case WAIT_FOR_BREAK_FLAG: - { - for(;;) - { - FastOS_Thread::Sleep(1000); - - if(thread->GetBreakFlag()) - { - Progress(true, "Thread %p got breakflag", thread); - break; - } - } - break; - } - - case WAIT_FOR_THREAD_TO_FINISH: - { - if(job->mutex) - job->mutex->Lock(); - - if(job->otherThread != NULL) - job->otherThread->Join(); - - if(job->mutex) - job->mutex->Unlock(); - break; - } - - case WAIT_FOR_CONDITION: - { - job->condition->Lock(); - - job->result = 1; - - job->condition->Wait(); - job->condition->Unlock(); - - job->result = 0; - - break; - } - - 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(); - } - break; - } - - case TEST_ID: - { - 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(); - - if(currentId == job->_threadId) - job->result = 1; - else - job->result = -1; - break; - } - - case WAIT2SEC_AND_SIGNALCOND: - { - FastOS_Thread::Sleep(2000); - job->condition->Signal(); - job->result = 1; - break; - } - - case HOLD_MUTEX_FOR2SEC: - { - job->mutex->Lock(); - FastOS_Thread::Sleep(2000); - job->mutex->Unlock(); - job->result = 1; - break; - } - - case WAIT_2_SEC: - { - FastOS_Thread::Sleep(2000); - job->result = 1; - break; - } - - default: - Progress(false, "Unknown jobcode"); - break; - } -} - int main (int argc, char **argv) { ThreadTest app; diff --git a/fastos/src/tests/thread_sleep_test.cpp b/fastos/src/tests/thread_sleep_test.cpp index 25cd6f6095b..0fe94a81467 100644 --- a/fastos/src/tests/thread_sleep_test.cpp +++ b/fastos/src/tests/thread_sleep_test.cpp @@ -4,62 +4,10 @@ #include <vespa/fastos/fastos.h> #include "tests.h" #include "jobs.h" +#include "base_thread.hpp" -static volatile int64_t number; -#define INCREASE_NUMBER_AMOUNT 10000 - -#define MUTEX_TEST_THREADS 6 -#define MAX_THREADS 7 - - -class ThreadTest : public BaseTest, public FastOS_Runnable +class ThreadTest : public BaseForThreadTest { -private: - FastOS_Mutex printMutex; - -public: - ThreadTest(void) - : printMutex() - { - } - virtual ~ThreadTest() {}; - - void PrintProgress (char *string) - { - printMutex.Lock(); - BaseTest::PrintProgress(string); - printMutex.Unlock(); - } - - void Run (FastOS_ThreadInterface *thread, void *arg); - - void WaitForThreadsToFinish (Job *jobs, int count) - { - int i; - - Progress(true, "Waiting for threads to finish..."); - for(;;) - { - bool threadsFinished=true; - - for(i=0; i<count; i++) - { - if(jobs[i].result == -1) - { - threadsFinished = false; - break; - } - } - - FastOS_Thread::Sleep(500); - - if(threadsFinished) - break; - } - - Progress(true, "Threads finished"); - } - int Main (); void CreateSingleThread () @@ -84,7 +32,6 @@ public: delete(pool); PrintSeparator(); } - }; int ThreadTest::Main () @@ -100,172 +47,6 @@ int ThreadTest::Main () return 0; } -volatile int busyCnt; - -void ThreadTest::Run (FastOS_ThreadInterface *thread, void *arg) -{ - if(arg == NULL) - return; - - Job *job = static_cast<Job *>(arg); - char someStack[15*1024]; - - memset(someStack, 0, 15*1024); - - switch(job->code) - { - case SILENTNOP: - { - job->result = 1; - break; - } - - case NOP: - { - Progress(true, "Doing NOP"); - job->result = 1; - break; - } - - case PRINT_MESSAGE_AND_WAIT3SEC: - { - Progress(true, "Thread printing message: [%s]", job->message); - job->result = strlen(job->message); - - FastOS_Thread::Sleep(3000); - break; - } - - case INCREASE_NUMBER: - { - int result; - - if(job->mutex != NULL) - job->mutex->Lock(); - - result = static_cast<int>(number); - - int sleepOn = (INCREASE_NUMBER_AMOUNT/2) * 321/10000; - for(int i=0; i<(INCREASE_NUMBER_AMOUNT/2); i++) - { - number = number + 2; - - if(i == sleepOn) - FastOS_Thread::Sleep(1000); - } - - if(job->mutex != NULL) - job->mutex->Unlock(); - - job->result = result; // This marks the end of the thread - - break; - } - - case WAIT_FOR_BREAK_FLAG: - { - for(;;) - { - FastOS_Thread::Sleep(1000); - - if(thread->GetBreakFlag()) - { - Progress(true, "Thread %p got breakflag", thread); - break; - } - } - break; - } - - case WAIT_FOR_THREAD_TO_FINISH: - { - if(job->mutex) - job->mutex->Lock(); - - if(job->otherThread != NULL) - job->otherThread->Join(); - - if(job->mutex) - job->mutex->Unlock(); - break; - } - - case WAIT_FOR_CONDITION: - { - job->condition->Lock(); - - job->result = 1; - - job->condition->Wait(); - job->condition->Unlock(); - - job->result = 0; - - break; - } - - 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(); - } - break; - } - - case TEST_ID: - { - 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(); - - if(currentId == job->_threadId) - job->result = 1; - else - job->result = -1; - break; - } - - case WAIT2SEC_AND_SIGNALCOND: - { - FastOS_Thread::Sleep(2000); - job->condition->Signal(); - job->result = 1; - break; - } - - case HOLD_MUTEX_FOR2SEC: - { - job->mutex->Lock(); - FastOS_Thread::Sleep(2000); - job->mutex->Unlock(); - job->result = 1; - break; - } - - case WAIT_2_SEC: - { - FastOS_Thread::Sleep(2000); - job->result = 1; - break; - } - - default: - Progress(false, "Unknown jobcode"); - break; - } -} - int main (int argc, char **argv) { ThreadTest app; diff --git a/fastos/src/tests/thread_stats_test.cpp b/fastos/src/tests/thread_stats_test.cpp index a570212b657..53859a88c78 100644 --- a/fastos/src/tests/thread_stats_test.cpp +++ b/fastos/src/tests/thread_stats_test.cpp @@ -4,63 +4,10 @@ #include <vespa/fastos/fastos.h> #include "tests.h" #include "jobs.h" +#include "base_thread.hpp" -static volatile int64_t number; -#define INCREASE_NUMBER_AMOUNT 10000 - -#define MUTEX_TEST_THREADS 6 -#define MAX_THREADS 7 - - -class ThreadTest : public BaseTest, public FastOS_Runnable +class ThreadTest : public BaseForThreadTest { -private: - FastOS_Mutex printMutex; - -public: - ThreadTest(void) - : printMutex() - { - } - virtual ~ThreadTest() {}; - - void PrintProgress (char *string) - { - printMutex.Lock(); - BaseTest::PrintProgress(string); - printMutex.Unlock(); - } - - void Run (FastOS_ThreadInterface *thread, void *arg); - - void WaitForThreadsToFinish (Job *jobs, int count) - { - int i; - - Progress(true, "Waiting for threads to finish..."); - for(;;) - { - bool threadsFinished=true; - - for(i=0; i<count; i++) - { - if(jobs[i].result == -1) - { - threadsFinished = false; - break; - } - } - - FastOS_Thread::Sleep(500); - - if(threadsFinished) - break; - } - - Progress(true, "Threads finished"); - } - - void ThreadStatsTest () { int inactiveThreads; @@ -184,172 +131,6 @@ int ThreadTest::Main () return 0; } -volatile int busyCnt; - -void ThreadTest::Run (FastOS_ThreadInterface *thread, void *arg) -{ - if(arg == NULL) - return; - - Job *job = static_cast<Job *>(arg); - char someStack[15*1024]; - - memset(someStack, 0, 15*1024); - - switch(job->code) - { - case SILENTNOP: - { - job->result = 1; - break; - } - - case NOP: - { - Progress(true, "Doing NOP"); - job->result = 1; - break; - } - - case PRINT_MESSAGE_AND_WAIT3SEC: - { - Progress(true, "Thread printing message: [%s]", job->message); - job->result = strlen(job->message); - - FastOS_Thread::Sleep(3000); - break; - } - - case INCREASE_NUMBER: - { - int result; - - if(job->mutex != NULL) - job->mutex->Lock(); - - result = static_cast<int>(number); - - int sleepOn = (INCREASE_NUMBER_AMOUNT/2) * 321/10000; - for(int i=0; i<(INCREASE_NUMBER_AMOUNT/2); i++) - { - number = number + 2; - - if(i == sleepOn) - FastOS_Thread::Sleep(1000); - } - - if(job->mutex != NULL) - job->mutex->Unlock(); - - job->result = result; // This marks the end of the thread - - break; - } - - case WAIT_FOR_BREAK_FLAG: - { - for(;;) - { - FastOS_Thread::Sleep(1000); - - if(thread->GetBreakFlag()) - { - Progress(true, "Thread %p got breakflag", thread); - break; - } - } - break; - } - - case WAIT_FOR_THREAD_TO_FINISH: - { - if(job->mutex) - job->mutex->Lock(); - - if(job->otherThread != NULL) - job->otherThread->Join(); - - if(job->mutex) - job->mutex->Unlock(); - break; - } - - case WAIT_FOR_CONDITION: - { - job->condition->Lock(); - - job->result = 1; - - job->condition->Wait(); - job->condition->Unlock(); - - job->result = 0; - - break; - } - - 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(); - } - break; - } - - case TEST_ID: - { - 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(); - - if(currentId == job->_threadId) - job->result = 1; - else - job->result = -1; - break; - } - - case WAIT2SEC_AND_SIGNALCOND: - { - FastOS_Thread::Sleep(2000); - job->condition->Signal(); - job->result = 1; - break; - } - - case HOLD_MUTEX_FOR2SEC: - { - job->mutex->Lock(); - FastOS_Thread::Sleep(2000); - job->mutex->Unlock(); - job->result = 1; - break; - } - - case WAIT_2_SEC: - { - FastOS_Thread::Sleep(2000); - job->result = 1; - break; - } - - default: - Progress(false, "Unknown jobcode"); - break; - } -} - int main (int argc, char **argv) { ThreadTest app; diff --git a/fastos/src/tests/threadtest.cpp b/fastos/src/tests/threadtest.cpp index cf2ab70bec9..6d4a6b03b8f 100644 --- a/fastos/src/tests/threadtest.cpp +++ b/fastos/src/tests/threadtest.cpp @@ -4,65 +4,51 @@ #include <vespa/fastos/fastos.h> #include "tests.h" #include "jobs.h" +#include "base_thread.hpp" -static volatile int64_t number; -#define INCREASE_NUMBER_AMOUNT 10000 - #define MUTEX_TEST_THREADS 6 #define MAX_THREADS 7 -class ThreadTest : public BaseTest, public FastOS_Runnable +class ThreadTest : public BaseForThreadTest { -private: - FastOS_Mutex printMutex; - -public: - ThreadTest(void) - : printMutex() - { - } - virtual ~ThreadTest() {}; - - void PrintProgress (char *string) - { - printMutex.Lock(); - BaseTest::PrintProgress(string); - printMutex.Unlock(); - } - - void Run (FastOS_ThreadInterface *thread, void *arg); + int Main (); - void WaitForThreadsToFinish (Job *jobs, int count) + void WaitForXThreadsToHaveWait (Job *jobs, + int jobCount, + FastOS_Cond *condition, + int numWait) { - int i; + Progress(true, "Waiting for %d threads to be in wait state", numWait); - Progress(true, "Waiting for threads to finish..."); + int oldNumber=-10000; for(;;) { - bool threadsFinished=true; + int waitingThreads=0; - for(i=0; i<count; i++) + condition->Lock(); + + for(int i=0; i<jobCount; i++) { - if(jobs[i].result == -1) - { - threadsFinished = false; - break; - } + if(jobs[i].result == 1) + waitingThreads++; } - FastOS_Thread::Sleep(500); + condition->Unlock(); - if(threadsFinished) + if(waitingThreads != oldNumber) + Progress(true, "%d threads are waiting", waitingThreads); + + oldNumber = waitingThreads; + + if(waitingThreads == numWait) break; - } - Progress(true, "Threads finished"); + FastOS_Thread::Sleep(100); + } } - int Main (); - void TooManyThreadsTest () { TestHeader("Too Many Threads Test"); @@ -337,40 +323,6 @@ public: PrintSeparator(); } - void WaitForXThreadsToHaveWait (Job *jobs, - int jobCount, - FastOS_Cond *condition, - int numWait) - { - Progress(true, "Waiting for %d threads to be in wait state", numWait); - - int oldNumber=-10000; - for(;;) - { - int waitingThreads=0; - - condition->Lock(); - - 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); - - oldNumber = waitingThreads; - - if(waitingThreads == numWait) - break; - - FastOS_Thread::Sleep(100); - } - } - void SharedSignalAndBroadcastTest (Job *jobs, int numThreads, FastOS_Cond *condition, FastOS_ThreadPool *pool) @@ -644,172 +596,6 @@ int ThreadTest::Main () return 0; } -volatile int busyCnt; - -void ThreadTest::Run (FastOS_ThreadInterface *thread, void *arg) -{ - if(arg == NULL) - return; - - Job *job = static_cast<Job *>(arg); - char someStack[15*1024]; - - memset(someStack, 0, 15*1024); - - switch(job->code) - { - case SILENTNOP: - { - job->result = 1; - break; - } - - case NOP: - { - Progress(true, "Doing NOP"); - job->result = 1; - break; - } - - case PRINT_MESSAGE_AND_WAIT3SEC: - { - Progress(true, "Thread printing message: [%s]", job->message); - job->result = strlen(job->message); - - FastOS_Thread::Sleep(3000); - break; - } - - case INCREASE_NUMBER: - { - int result; - - if(job->mutex != NULL) - job->mutex->Lock(); - - result = static_cast<int>(number); - - int sleepOn = (INCREASE_NUMBER_AMOUNT/2) * 321/10000; - for(int i=0; i<(INCREASE_NUMBER_AMOUNT/2); i++) - { - number = number + 2; - - if(i == sleepOn) - FastOS_Thread::Sleep(1000); - } - - if(job->mutex != NULL) - job->mutex->Unlock(); - - job->result = result; // This marks the end of the thread - - break; - } - - case WAIT_FOR_BREAK_FLAG: - { - for(;;) - { - FastOS_Thread::Sleep(1000); - - if(thread->GetBreakFlag()) - { - Progress(true, "Thread %p got breakflag", thread); - break; - } - } - break; - } - - case WAIT_FOR_THREAD_TO_FINISH: - { - if(job->mutex) - job->mutex->Lock(); - - if(job->otherThread != NULL) - job->otherThread->Join(); - - if(job->mutex) - job->mutex->Unlock(); - break; - } - - case WAIT_FOR_CONDITION: - { - job->condition->Lock(); - - job->result = 1; - - job->condition->Wait(); - job->condition->Unlock(); - - job->result = 0; - - break; - } - - 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(); - } - break; - } - - case TEST_ID: - { - 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(); - - if(currentId == job->_threadId) - job->result = 1; - else - job->result = -1; - break; - } - - case WAIT2SEC_AND_SIGNALCOND: - { - FastOS_Thread::Sleep(2000); - job->condition->Signal(); - job->result = 1; - break; - } - - case HOLD_MUTEX_FOR2SEC: - { - job->mutex->Lock(); - FastOS_Thread::Sleep(2000); - job->mutex->Unlock(); - job->result = 1; - break; - } - - case WAIT_2_SEC: - { - FastOS_Thread::Sleep(2000); - job->result = 1; - break; - } - - default: - Progress(false, "Unknown jobcode"); - break; - } -} - int main (int argc, char **argv) { ThreadTest app; |