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