From 773e0ce49365f708d24f5c85da5e879b2c5002fa Mon Sep 17 00:00:00 2001 From: Przemek Wirkus Date: Tue, 25 Mar 2014 14:33:22 +0000 Subject: [PATCH] RTOS_2 Semaphore resource locking test automation added to test suite --- libraries/tests/rtos/mbed/mutex/main.cpp | 67 ++++++++++++++++---- libraries/tests/rtos/mbed/semaphore/main.cpp | 51 ++++++++++++--- workspace_tools/tests.py | 10 +-- 3 files changed, 105 insertions(+), 23 deletions(-) diff --git a/libraries/tests/rtos/mbed/mutex/main.cpp b/libraries/tests/rtos/mbed/mutex/main.cpp index fdcfc8235b..6353d8cbb3 100644 --- a/libraries/tests/rtos/mbed/mutex/main.cpp +++ b/libraries/tests/rtos/mbed/mutex/main.cpp @@ -1,24 +1,69 @@ #include "mbed.h" +#include "test_env.h" #include "rtos.h" -Mutex stdio_mutex; +#define THREAD_DELAY 50 +#define SIGNALS_TO_EMIT 100 -void notify(const char* name, int state) { - stdio_mutex.lock(); - printf("%s: %d\n\r", name, state); - stdio_mutex.unlock(); +void print_char(char c = '*') +{ + printf("%c", c); + fflush(stdout); +} + +Mutex stdio_mutex; +DigitalOut led(LED1); + +volatile int change_counter = 0; +volatile bool changing_counter = false; + +bool manipulate_protected_zone(const int thread_delay) { + bool result = true; + + stdio_mutex.lock(); // LOCK + if (changing_counter == true) { + print_char('e'); // if changing_counter is true access is not exclusively + result = false; + notify_completion(false); + exit(1); + } + changing_counter = true; + + // Some action on protected + led = !led; + change_counter++; + print_char('.'); + Thread::wait(thread_delay); + + changing_counter = false; + stdio_mutex.unlock(); // UNLOCK + return result; } void test_thread(void const *args) { + const int thread_delay = int(args); while (true) { - notify((const char*)args, 0); Thread::wait(1000); - notify((const char*)args, 1); Thread::wait(1000); + manipulate_protected_zone(thread_delay); } } int main() { - Thread t2(test_thread, (void *)"Th 2"); - Thread t3(test_thread, (void *)"Th 3"); - - test_thread((void *)"Th 1"); + const int t1_delay = THREAD_DELAY * 1; + const int t2_delay = THREAD_DELAY * 2; + const int t3_delay = THREAD_DELAY * 3; + Thread t2(test_thread, (void *)t2_delay); + Thread t3(test_thread, (void *)t3_delay); + bool result = true; + + while (true) { + // Thread 1 action + Thread::wait(t1_delay); + manipulate_protected_zone(t1_delay); + if (change_counter >= SIGNALS_TO_EMIT) { + break; + } + } + + notify_completion(result); + return 0; } diff --git a/libraries/tests/rtos/mbed/semaphore/main.cpp b/libraries/tests/rtos/mbed/semaphore/main.cpp index 4418f82e60..3cd97ebb43 100644 --- a/libraries/tests/rtos/mbed/semaphore/main.cpp +++ b/libraries/tests/rtos/mbed/semaphore/main.cpp @@ -1,20 +1,55 @@ #include "mbed.h" +#include "test_env.h" #include "rtos.h" -Semaphore two_slots(2); +#define THREAD_DELAY 100 +#define SEMAPHORE_SLOTS 2 +#define SEM_CHANGES 100 -void test_thread(void const *name) { +void print_char(char c = '*') +{ + printf("%c", c); + fflush(stdout); +} + +Semaphore two_slots(SEMAPHORE_SLOTS); + +volatile int change_counter = 0; +volatile int sem_counter = 0; + +void test_thread(void const *delay) { + const int thread_delay = int(delay); while (true) { two_slots.wait(); - printf("%s\n\r", (const char*)name); - Thread::wait(1000); + sem_counter++; + const bool sem_lock_failed = sem_counter > SEMAPHORE_SLOTS; + const char msg = sem_lock_failed ? 'e' : sem_counter + '0'; + print_char(msg); + if (sem_lock_failed) { + notify_completion(false); + exit(1); + } + Thread::wait(thread_delay); + print_char('.'); + sem_counter--; + change_counter++; two_slots.release(); } } int main (void) { - Thread t2(test_thread, (void *)"Th 2"); - Thread t3(test_thread, (void *)"Th 3"); - - test_thread((void *)"Th 1"); + const int t1_delay = THREAD_DELAY * 1; + const int t2_delay = THREAD_DELAY * 2; + const int t3_delay = THREAD_DELAY * 3; + Thread t1(test_thread, (void *)t1_delay); + Thread t2(test_thread, (void *)t2_delay); + Thread t3(test_thread, (void *)t3_delay); + + while (true) { + if (change_counter >= SEM_CHANGES) { + notify_completion(true); + break; + } + } + return 0; } diff --git a/workspace_tools/tests.py b/workspace_tools/tests.py index c1d7e9f898..049c4bd5d5 100644 --- a/workspace_tools/tests.py +++ b/workspace_tools/tests.py @@ -507,15 +507,17 @@ TESTS = [ "host_test": "wait_us_auto" }, { - "id": "RTOS_2", "description": "Mutex", + "id": "RTOS_2", "description": "Mutex resource lock/1", "source_dir": join(TEST_DIR, "rtos", "mbed", "mutex"), - "dependencies": [MBED_LIBRARIES, RTOS_LIBRARIES], - "duration": 20 + "dependencies": [MBED_LIBRARIES, RTOS_LIBRARIES, TEST_MBED_LIB], + "duration": 20, + "automated": True, }, { "id": "RTOS_3", "description": "Semaphore", "source_dir": join(TEST_DIR, "rtos", "mbed", "semaphore"), - "dependencies": [MBED_LIBRARIES, RTOS_LIBRARIES], + "dependencies": [MBED_LIBRARIES, RTOS_LIBRARIES, TEST_MBED_LIB], + "automated": True, }, { "id": "RTOS_4", "description": "Signals",