nkjnm
Dependencies: MAX44000 nexpaq_mdk
Fork of LED_Demo by
mbd_os/TESTS/mbedmicro-rtos-mbed/threads/main.cpp@7:3a65ef12ba31, 2016-11-04 (annotated)
- Committer:
- nitsshukla
- Date:
- Fri Nov 04 12:06:04 2016 +0000
- Revision:
- 7:3a65ef12ba31
- Parent:
- 1:55a6170b404f
kghj;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nexpaq | 1:55a6170b404f | 1 | #include "mbed.h" |
nexpaq | 1:55a6170b404f | 2 | #include "greentea-client/test_env.h" |
nexpaq | 1:55a6170b404f | 3 | #include "unity.h" |
nexpaq | 1:55a6170b404f | 4 | #include "utest.h" |
nexpaq | 1:55a6170b404f | 5 | #include "rtos.h" |
nexpaq | 1:55a6170b404f | 6 | #include "SynchronizedIntegral.h" |
nexpaq | 1:55a6170b404f | 7 | #include "LockGuard.h" |
nexpaq | 1:55a6170b404f | 8 | |
nexpaq | 1:55a6170b404f | 9 | #if defined(MBED_RTOS_SINGLE_THREAD) |
nexpaq | 1:55a6170b404f | 10 | #error [NOT_SUPPORTED] test not supported |
nexpaq | 1:55a6170b404f | 11 | #endif |
nexpaq | 1:55a6170b404f | 12 | |
nexpaq | 1:55a6170b404f | 13 | /* |
nexpaq | 1:55a6170b404f | 14 | * The stack size is defined in cmsis_os.h mainly dependent on the underlying toolchain and |
nexpaq | 1:55a6170b404f | 15 | * the C standard library. For GCC, ARM_STD and IAR it is defined with a size of 2048 bytes |
nexpaq | 1:55a6170b404f | 16 | * and for ARM_MICRO 512. Because of reduce RAM size some targets need a reduced stacksize. |
nexpaq | 1:55a6170b404f | 17 | */ |
nexpaq | 1:55a6170b404f | 18 | #if defined(TARGET_MCU_NRF51822) || defined(TARGET_MCU_NRF52832) |
nexpaq | 1:55a6170b404f | 19 | #define STACK_SIZE 512 |
nexpaq | 1:55a6170b404f | 20 | #else |
nexpaq | 1:55a6170b404f | 21 | #define STACK_SIZE DEFAULT_STACK_SIZE |
nexpaq | 1:55a6170b404f | 22 | #endif |
nexpaq | 1:55a6170b404f | 23 | |
nexpaq | 1:55a6170b404f | 24 | using namespace utest::v1; |
nexpaq | 1:55a6170b404f | 25 | |
nexpaq | 1:55a6170b404f | 26 | // The counter type used accross all the tests |
nexpaq | 1:55a6170b404f | 27 | // It is internall ysynchronized so read |
nexpaq | 1:55a6170b404f | 28 | typedef SynchronizedIntegral<int> counter_t; |
nexpaq | 1:55a6170b404f | 29 | |
nexpaq | 1:55a6170b404f | 30 | // Tasks with different functions to test on threads |
nexpaq | 1:55a6170b404f | 31 | void increment(counter_t* counter) { |
nexpaq | 1:55a6170b404f | 32 | (*counter)++; |
nexpaq | 1:55a6170b404f | 33 | } |
nexpaq | 1:55a6170b404f | 34 | |
nexpaq | 1:55a6170b404f | 35 | void increment_with_yield(counter_t* counter) { |
nexpaq | 1:55a6170b404f | 36 | Thread::yield(); |
nexpaq | 1:55a6170b404f | 37 | (*counter)++; |
nexpaq | 1:55a6170b404f | 38 | } |
nexpaq | 1:55a6170b404f | 39 | |
nexpaq | 1:55a6170b404f | 40 | void increment_with_wait(counter_t* counter) { |
nexpaq | 1:55a6170b404f | 41 | Thread::wait(100); |
nexpaq | 1:55a6170b404f | 42 | (*counter)++; |
nexpaq | 1:55a6170b404f | 43 | } |
nexpaq | 1:55a6170b404f | 44 | |
nexpaq | 1:55a6170b404f | 45 | void increment_with_child(counter_t* counter) { |
nexpaq | 1:55a6170b404f | 46 | Thread child(counter, increment, osPriorityNormal, STACK_SIZE); |
nexpaq | 1:55a6170b404f | 47 | child.join(); |
nexpaq | 1:55a6170b404f | 48 | } |
nexpaq | 1:55a6170b404f | 49 | |
nexpaq | 1:55a6170b404f | 50 | void increment_with_murder(counter_t* counter) { |
nexpaq | 1:55a6170b404f | 51 | { |
nexpaq | 1:55a6170b404f | 52 | // take ownership of the counter mutex so it prevent the child to |
nexpaq | 1:55a6170b404f | 53 | // modify counter. |
nexpaq | 1:55a6170b404f | 54 | LockGuard lock(counter->internal_mutex()); |
nexpaq | 1:55a6170b404f | 55 | Thread child(counter, increment, osPriorityNormal, STACK_SIZE); |
nexpaq | 1:55a6170b404f | 56 | child.terminate(); |
nexpaq | 1:55a6170b404f | 57 | } |
nexpaq | 1:55a6170b404f | 58 | |
nexpaq | 1:55a6170b404f | 59 | (*counter)++; |
nexpaq | 1:55a6170b404f | 60 | } |
nexpaq | 1:55a6170b404f | 61 | |
nexpaq | 1:55a6170b404f | 62 | void self_terminate(Thread *self) { |
nexpaq | 1:55a6170b404f | 63 | self->terminate(); |
nexpaq | 1:55a6170b404f | 64 | // Code should not get here |
nexpaq | 1:55a6170b404f | 65 | TEST_ASSERT(0); |
nexpaq | 1:55a6170b404f | 66 | } |
nexpaq | 1:55a6170b404f | 67 | |
nexpaq | 1:55a6170b404f | 68 | // Tests that spawn tasks in different configurations |
nexpaq | 1:55a6170b404f | 69 | template <void (*F)(counter_t *)> |
nexpaq | 1:55a6170b404f | 70 | void test_single_thread() { |
nexpaq | 1:55a6170b404f | 71 | counter_t counter(0); |
nexpaq | 1:55a6170b404f | 72 | Thread thread(&counter, F, osPriorityNormal, STACK_SIZE); |
nexpaq | 1:55a6170b404f | 73 | thread.join(); |
nexpaq | 1:55a6170b404f | 74 | TEST_ASSERT_EQUAL(counter, 1); |
nexpaq | 1:55a6170b404f | 75 | } |
nexpaq | 1:55a6170b404f | 76 | |
nexpaq | 1:55a6170b404f | 77 | template <int N, void (*F)(counter_t *)> |
nexpaq | 1:55a6170b404f | 78 | void test_parallel_threads() { |
nexpaq | 1:55a6170b404f | 79 | counter_t counter(0); |
nexpaq | 1:55a6170b404f | 80 | Thread *threads[N]; |
nexpaq | 1:55a6170b404f | 81 | |
nexpaq | 1:55a6170b404f | 82 | for (int i = 0; i < N; i++) { |
nexpaq | 1:55a6170b404f | 83 | threads[i] = new Thread(&counter, F, osPriorityNormal, STACK_SIZE); |
nexpaq | 1:55a6170b404f | 84 | } |
nexpaq | 1:55a6170b404f | 85 | |
nexpaq | 1:55a6170b404f | 86 | for (int i = 0; i < N; i++) { |
nexpaq | 1:55a6170b404f | 87 | threads[i]->join(); |
nexpaq | 1:55a6170b404f | 88 | delete threads[i]; |
nexpaq | 1:55a6170b404f | 89 | } |
nexpaq | 1:55a6170b404f | 90 | |
nexpaq | 1:55a6170b404f | 91 | TEST_ASSERT_EQUAL(counter, N); |
nexpaq | 1:55a6170b404f | 92 | } |
nexpaq | 1:55a6170b404f | 93 | |
nexpaq | 1:55a6170b404f | 94 | template <int N, void (*F)(counter_t *)> |
nexpaq | 1:55a6170b404f | 95 | void test_serial_threads() { |
nexpaq | 1:55a6170b404f | 96 | counter_t counter(0); |
nexpaq | 1:55a6170b404f | 97 | |
nexpaq | 1:55a6170b404f | 98 | for (int i = 0; i < N; i++) { |
nexpaq | 1:55a6170b404f | 99 | Thread thread(&counter, F, osPriorityNormal, STACK_SIZE); |
nexpaq | 1:55a6170b404f | 100 | thread.join(); |
nexpaq | 1:55a6170b404f | 101 | } |
nexpaq | 1:55a6170b404f | 102 | |
nexpaq | 1:55a6170b404f | 103 | TEST_ASSERT_EQUAL(counter, N); |
nexpaq | 1:55a6170b404f | 104 | } |
nexpaq | 1:55a6170b404f | 105 | |
nexpaq | 1:55a6170b404f | 106 | void test_self_terminate() { |
nexpaq | 1:55a6170b404f | 107 | Thread *thread = new Thread(osPriorityNormal, STACK_SIZE); |
nexpaq | 1:55a6170b404f | 108 | thread->start(thread, self_terminate); |
nexpaq | 1:55a6170b404f | 109 | thread->join(); |
nexpaq | 1:55a6170b404f | 110 | delete thread; |
nexpaq | 1:55a6170b404f | 111 | } |
nexpaq | 1:55a6170b404f | 112 | |
nexpaq | 1:55a6170b404f | 113 | utest::v1::status_t test_setup(const size_t number_of_cases) { |
nexpaq | 1:55a6170b404f | 114 | GREENTEA_SETUP(40, "default_auto"); |
nexpaq | 1:55a6170b404f | 115 | return verbose_test_setup_handler(number_of_cases); |
nexpaq | 1:55a6170b404f | 116 | } |
nexpaq | 1:55a6170b404f | 117 | |
nexpaq | 1:55a6170b404f | 118 | // Test cases |
nexpaq | 1:55a6170b404f | 119 | Case cases[] = { |
nexpaq | 1:55a6170b404f | 120 | Case("Testing single thread", test_single_thread<increment>), |
nexpaq | 1:55a6170b404f | 121 | Case("Testing parallel threads", test_parallel_threads<3, increment>), |
nexpaq | 1:55a6170b404f | 122 | Case("Testing serial threads", test_serial_threads<10, increment>), |
nexpaq | 1:55a6170b404f | 123 | |
nexpaq | 1:55a6170b404f | 124 | Case("Testing single thread with yield", test_single_thread<increment_with_yield>), |
nexpaq | 1:55a6170b404f | 125 | Case("Testing parallel threads with yield", test_parallel_threads<3, increment_with_yield>), |
nexpaq | 1:55a6170b404f | 126 | Case("Testing serial threads with yield", test_serial_threads<10, increment_with_yield>), |
nexpaq | 1:55a6170b404f | 127 | |
nexpaq | 1:55a6170b404f | 128 | Case("Testing single thread with wait", test_single_thread<increment_with_wait>), |
nexpaq | 1:55a6170b404f | 129 | Case("Testing parallel threads with wait", test_parallel_threads<3, increment_with_wait>), |
nexpaq | 1:55a6170b404f | 130 | Case("Testing serial threads with wait", test_serial_threads<10, increment_with_wait>), |
nexpaq | 1:55a6170b404f | 131 | |
nexpaq | 1:55a6170b404f | 132 | Case("Testing single thread with child", test_single_thread<increment_with_child>), |
nexpaq | 1:55a6170b404f | 133 | Case("Testing parallel threads with child", test_parallel_threads<2, increment_with_child>), |
nexpaq | 1:55a6170b404f | 134 | Case("Testing serial threads with child", test_serial_threads<10, increment_with_child>), |
nexpaq | 1:55a6170b404f | 135 | |
nexpaq | 1:55a6170b404f | 136 | Case("Testing single thread with murder", test_single_thread<increment_with_murder>), |
nexpaq | 1:55a6170b404f | 137 | Case("Testing parallel threads with murder", test_parallel_threads<3, increment_with_murder>), |
nexpaq | 1:55a6170b404f | 138 | Case("Testing serial threads with murder", test_serial_threads<10, increment_with_murder>), |
nexpaq | 1:55a6170b404f | 139 | |
nexpaq | 1:55a6170b404f | 140 | Case("Testing thread self terminate", test_self_terminate), |
nexpaq | 1:55a6170b404f | 141 | }; |
nexpaq | 1:55a6170b404f | 142 | |
nexpaq | 1:55a6170b404f | 143 | Specification specification(test_setup, cases); |
nexpaq | 1:55a6170b404f | 144 | |
nexpaq | 1:55a6170b404f | 145 | int main() { |
nexpaq | 1:55a6170b404f | 146 | return !Harness::run(specification); |
nexpaq | 1:55a6170b404f | 147 | } |