Marco Zecchini
/
Example_RTOS
Rtos API example
mbed-os/TESTS/mbedmicro-rtos-mbed/mutex/main.cpp@0:9fca2b23d0ba, 2019-02-23 (annotated)
- Committer:
- marcozecchini
- Date:
- Sat Feb 23 12:13:36 2019 +0000
- Revision:
- 0:9fca2b23d0ba
final commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
marcozecchini | 0:9fca2b23d0ba | 1 | /* mbed Microcontroller Library |
marcozecchini | 0:9fca2b23d0ba | 2 | * Copyright (c) 2017 ARM Limited |
marcozecchini | 0:9fca2b23d0ba | 3 | * |
marcozecchini | 0:9fca2b23d0ba | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
marcozecchini | 0:9fca2b23d0ba | 5 | * you may not use this file except in compliance with the License. |
marcozecchini | 0:9fca2b23d0ba | 6 | * You may obtain a copy of the License at |
marcozecchini | 0:9fca2b23d0ba | 7 | * |
marcozecchini | 0:9fca2b23d0ba | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
marcozecchini | 0:9fca2b23d0ba | 9 | * |
marcozecchini | 0:9fca2b23d0ba | 10 | * Unless required by applicable law or agreed to in writing, software |
marcozecchini | 0:9fca2b23d0ba | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
marcozecchini | 0:9fca2b23d0ba | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
marcozecchini | 0:9fca2b23d0ba | 13 | * See the License for the specific language governing permissions and |
marcozecchini | 0:9fca2b23d0ba | 14 | * limitations under the License. |
marcozecchini | 0:9fca2b23d0ba | 15 | */ |
marcozecchini | 0:9fca2b23d0ba | 16 | #include "mbed.h" |
marcozecchini | 0:9fca2b23d0ba | 17 | #include "greentea-client/test_env.h" |
marcozecchini | 0:9fca2b23d0ba | 18 | #include "unity.h" |
marcozecchini | 0:9fca2b23d0ba | 19 | #include "utest.h" |
marcozecchini | 0:9fca2b23d0ba | 20 | #include "rtos.h" |
marcozecchini | 0:9fca2b23d0ba | 21 | |
marcozecchini | 0:9fca2b23d0ba | 22 | #if defined(MBED_RTOS_SINGLE_THREAD) |
marcozecchini | 0:9fca2b23d0ba | 23 | #error [NOT_SUPPORTED] test not supported |
marcozecchini | 0:9fca2b23d0ba | 24 | #endif |
marcozecchini | 0:9fca2b23d0ba | 25 | |
marcozecchini | 0:9fca2b23d0ba | 26 | using namespace utest::v1; |
marcozecchini | 0:9fca2b23d0ba | 27 | |
marcozecchini | 0:9fca2b23d0ba | 28 | #define TEST_STACK_SIZE 512 |
marcozecchini | 0:9fca2b23d0ba | 29 | |
marcozecchini | 0:9fca2b23d0ba | 30 | #define TEST_LONG_DELAY 20 |
marcozecchini | 0:9fca2b23d0ba | 31 | #define TEST_DELAY 10 |
marcozecchini | 0:9fca2b23d0ba | 32 | #define SIGNALS_TO_EMIT 100 |
marcozecchini | 0:9fca2b23d0ba | 33 | |
marcozecchini | 0:9fca2b23d0ba | 34 | Mutex stdio_mutex; |
marcozecchini | 0:9fca2b23d0ba | 35 | |
marcozecchini | 0:9fca2b23d0ba | 36 | volatile int change_counter = 0; |
marcozecchini | 0:9fca2b23d0ba | 37 | volatile bool changing_counter = false; |
marcozecchini | 0:9fca2b23d0ba | 38 | volatile bool mutex_defect = false; |
marcozecchini | 0:9fca2b23d0ba | 39 | |
marcozecchini | 0:9fca2b23d0ba | 40 | bool manipulate_protected_zone(const int thread_delay) |
marcozecchini | 0:9fca2b23d0ba | 41 | { |
marcozecchini | 0:9fca2b23d0ba | 42 | bool result = true; |
marcozecchini | 0:9fca2b23d0ba | 43 | |
marcozecchini | 0:9fca2b23d0ba | 44 | osStatus stat = stdio_mutex.lock(); |
marcozecchini | 0:9fca2b23d0ba | 45 | TEST_ASSERT_EQUAL(osOK, stat); |
marcozecchini | 0:9fca2b23d0ba | 46 | |
marcozecchini | 0:9fca2b23d0ba | 47 | core_util_critical_section_enter(); |
marcozecchini | 0:9fca2b23d0ba | 48 | if (changing_counter == true) { |
marcozecchini | 0:9fca2b23d0ba | 49 | result = false; |
marcozecchini | 0:9fca2b23d0ba | 50 | mutex_defect = true; |
marcozecchini | 0:9fca2b23d0ba | 51 | } |
marcozecchini | 0:9fca2b23d0ba | 52 | changing_counter = true; |
marcozecchini | 0:9fca2b23d0ba | 53 | |
marcozecchini | 0:9fca2b23d0ba | 54 | change_counter++; |
marcozecchini | 0:9fca2b23d0ba | 55 | core_util_critical_section_exit(); |
marcozecchini | 0:9fca2b23d0ba | 56 | |
marcozecchini | 0:9fca2b23d0ba | 57 | Thread::wait(thread_delay); |
marcozecchini | 0:9fca2b23d0ba | 58 | |
marcozecchini | 0:9fca2b23d0ba | 59 | core_util_critical_section_enter(); |
marcozecchini | 0:9fca2b23d0ba | 60 | changing_counter = false; |
marcozecchini | 0:9fca2b23d0ba | 61 | core_util_critical_section_exit(); |
marcozecchini | 0:9fca2b23d0ba | 62 | |
marcozecchini | 0:9fca2b23d0ba | 63 | stat = stdio_mutex.unlock(); |
marcozecchini | 0:9fca2b23d0ba | 64 | TEST_ASSERT_EQUAL(osOK, stat); |
marcozecchini | 0:9fca2b23d0ba | 65 | return result; |
marcozecchini | 0:9fca2b23d0ba | 66 | } |
marcozecchini | 0:9fca2b23d0ba | 67 | |
marcozecchini | 0:9fca2b23d0ba | 68 | void test_thread(int const *thread_delay) |
marcozecchini | 0:9fca2b23d0ba | 69 | { |
marcozecchini | 0:9fca2b23d0ba | 70 | while (true) { |
marcozecchini | 0:9fca2b23d0ba | 71 | manipulate_protected_zone(*thread_delay); |
marcozecchini | 0:9fca2b23d0ba | 72 | } |
marcozecchini | 0:9fca2b23d0ba | 73 | } |
marcozecchini | 0:9fca2b23d0ba | 74 | |
marcozecchini | 0:9fca2b23d0ba | 75 | /** Test multiple thread |
marcozecchini | 0:9fca2b23d0ba | 76 | |
marcozecchini | 0:9fca2b23d0ba | 77 | Given 3 threads started with different delays and a section protected with a mutex |
marcozecchini | 0:9fca2b23d0ba | 78 | when each thread runs it tries to lock the mutex |
marcozecchini | 0:9fca2b23d0ba | 79 | then no more than one thread should be able to access protected region |
marcozecchini | 0:9fca2b23d0ba | 80 | */ |
marcozecchini | 0:9fca2b23d0ba | 81 | void test_multiple_threads(void) |
marcozecchini | 0:9fca2b23d0ba | 82 | { |
marcozecchini | 0:9fca2b23d0ba | 83 | const int t1_delay = TEST_DELAY * 1; |
marcozecchini | 0:9fca2b23d0ba | 84 | const int t2_delay = TEST_DELAY * 2; |
marcozecchini | 0:9fca2b23d0ba | 85 | const int t3_delay = TEST_DELAY * 3; |
marcozecchini | 0:9fca2b23d0ba | 86 | |
marcozecchini | 0:9fca2b23d0ba | 87 | Thread t2(osPriorityNormal, TEST_STACK_SIZE); |
marcozecchini | 0:9fca2b23d0ba | 88 | Thread t3(osPriorityNormal, TEST_STACK_SIZE); |
marcozecchini | 0:9fca2b23d0ba | 89 | |
marcozecchini | 0:9fca2b23d0ba | 90 | t2.start(callback(test_thread, &t2_delay)); |
marcozecchini | 0:9fca2b23d0ba | 91 | t3.start(callback(test_thread, &t3_delay)); |
marcozecchini | 0:9fca2b23d0ba | 92 | |
marcozecchini | 0:9fca2b23d0ba | 93 | while (true) { |
marcozecchini | 0:9fca2b23d0ba | 94 | // Thread 1 action |
marcozecchini | 0:9fca2b23d0ba | 95 | Thread::wait(t1_delay); |
marcozecchini | 0:9fca2b23d0ba | 96 | manipulate_protected_zone(t1_delay); |
marcozecchini | 0:9fca2b23d0ba | 97 | |
marcozecchini | 0:9fca2b23d0ba | 98 | core_util_critical_section_enter(); |
marcozecchini | 0:9fca2b23d0ba | 99 | if (change_counter >= SIGNALS_TO_EMIT or mutex_defect == true) { |
marcozecchini | 0:9fca2b23d0ba | 100 | core_util_critical_section_exit(); |
marcozecchini | 0:9fca2b23d0ba | 101 | t2.terminate(); |
marcozecchini | 0:9fca2b23d0ba | 102 | t3.terminate(); |
marcozecchini | 0:9fca2b23d0ba | 103 | break; |
marcozecchini | 0:9fca2b23d0ba | 104 | } |
marcozecchini | 0:9fca2b23d0ba | 105 | core_util_critical_section_exit(); |
marcozecchini | 0:9fca2b23d0ba | 106 | } |
marcozecchini | 0:9fca2b23d0ba | 107 | |
marcozecchini | 0:9fca2b23d0ba | 108 | TEST_ASSERT_EQUAL(false, mutex_defect); |
marcozecchini | 0:9fca2b23d0ba | 109 | } |
marcozecchini | 0:9fca2b23d0ba | 110 | |
marcozecchini | 0:9fca2b23d0ba | 111 | void test_dual_thread_nolock_lock_thread(Mutex *mutex) |
marcozecchini | 0:9fca2b23d0ba | 112 | { |
marcozecchini | 0:9fca2b23d0ba | 113 | osStatus stat = mutex->lock(osWaitForever); |
marcozecchini | 0:9fca2b23d0ba | 114 | TEST_ASSERT_EQUAL(osOK, stat); |
marcozecchini | 0:9fca2b23d0ba | 115 | |
marcozecchini | 0:9fca2b23d0ba | 116 | stat = mutex->unlock(); |
marcozecchini | 0:9fca2b23d0ba | 117 | TEST_ASSERT_EQUAL(osOK, stat); |
marcozecchini | 0:9fca2b23d0ba | 118 | } |
marcozecchini | 0:9fca2b23d0ba | 119 | |
marcozecchini | 0:9fca2b23d0ba | 120 | void test_dual_thread_nolock_trylock_thread(Mutex *mutex) |
marcozecchini | 0:9fca2b23d0ba | 121 | { |
marcozecchini | 0:9fca2b23d0ba | 122 | bool stat_b = mutex->trylock(); |
marcozecchini | 0:9fca2b23d0ba | 123 | TEST_ASSERT_EQUAL(true, stat_b); |
marcozecchini | 0:9fca2b23d0ba | 124 | |
marcozecchini | 0:9fca2b23d0ba | 125 | osStatus stat = mutex->unlock(); |
marcozecchini | 0:9fca2b23d0ba | 126 | TEST_ASSERT_EQUAL(osOK, stat); |
marcozecchini | 0:9fca2b23d0ba | 127 | } |
marcozecchini | 0:9fca2b23d0ba | 128 | |
marcozecchini | 0:9fca2b23d0ba | 129 | /** Test dual thread no-lock |
marcozecchini | 0:9fca2b23d0ba | 130 | |
marcozecchini | 0:9fca2b23d0ba | 131 | Test dual thread second thread lock |
marcozecchini | 0:9fca2b23d0ba | 132 | Given two threads A & B and a mutex |
marcozecchini | 0:9fca2b23d0ba | 133 | When thread A creates a mutex and starts thread B |
marcozecchini | 0:9fca2b23d0ba | 134 | and thread B calls @a lock and @a unlock |
marcozecchini | 0:9fca2b23d0ba | 135 | Then returned statuses are osOK |
marcozecchini | 0:9fca2b23d0ba | 136 | |
marcozecchini | 0:9fca2b23d0ba | 137 | Test dual thread second thread trylock |
marcozecchini | 0:9fca2b23d0ba | 138 | Given two threads A & B and a mutex |
marcozecchini | 0:9fca2b23d0ba | 139 | When thread A creates a mutex and starts thread B |
marcozecchini | 0:9fca2b23d0ba | 140 | and thread B calls @a trylock and @a unlock |
marcozecchini | 0:9fca2b23d0ba | 141 | Then returned statuses are true and osOK |
marcozecchini | 0:9fca2b23d0ba | 142 | */ |
marcozecchini | 0:9fca2b23d0ba | 143 | template <void (*F)(Mutex *)> |
marcozecchini | 0:9fca2b23d0ba | 144 | void test_dual_thread_nolock(void) |
marcozecchini | 0:9fca2b23d0ba | 145 | { |
marcozecchini | 0:9fca2b23d0ba | 146 | Mutex mutex; |
marcozecchini | 0:9fca2b23d0ba | 147 | Thread thread(osPriorityNormal, TEST_STACK_SIZE); |
marcozecchini | 0:9fca2b23d0ba | 148 | |
marcozecchini | 0:9fca2b23d0ba | 149 | thread.start(callback(F, &mutex)); |
marcozecchini | 0:9fca2b23d0ba | 150 | |
marcozecchini | 0:9fca2b23d0ba | 151 | wait_ms(TEST_DELAY); |
marcozecchini | 0:9fca2b23d0ba | 152 | } |
marcozecchini | 0:9fca2b23d0ba | 153 | |
marcozecchini | 0:9fca2b23d0ba | 154 | void test_dual_thread_lock_unlock_thread(Mutex *mutex) |
marcozecchini | 0:9fca2b23d0ba | 155 | { |
marcozecchini | 0:9fca2b23d0ba | 156 | osStatus stat = mutex->lock(osWaitForever); |
marcozecchini | 0:9fca2b23d0ba | 157 | TEST_ASSERT_EQUAL(osOK, stat); |
marcozecchini | 0:9fca2b23d0ba | 158 | } |
marcozecchini | 0:9fca2b23d0ba | 159 | |
marcozecchini | 0:9fca2b23d0ba | 160 | /** Test dual thread lock unlock |
marcozecchini | 0:9fca2b23d0ba | 161 | |
marcozecchini | 0:9fca2b23d0ba | 162 | Given two threads and a lock |
marcozecchini | 0:9fca2b23d0ba | 163 | When thread A locks the lock and starts thread B |
marcozecchini | 0:9fca2b23d0ba | 164 | and thread B calls @a lock on the mutex |
marcozecchini | 0:9fca2b23d0ba | 165 | Then thread B waits for thread A to unlock the lock |
marcozecchini | 0:9fca2b23d0ba | 166 | When thread A calls @a unlock on the mutex |
marcozecchini | 0:9fca2b23d0ba | 167 | Then thread B acquires the lock |
marcozecchini | 0:9fca2b23d0ba | 168 | */ |
marcozecchini | 0:9fca2b23d0ba | 169 | void test_dual_thread_lock_unlock(void) |
marcozecchini | 0:9fca2b23d0ba | 170 | { |
marcozecchini | 0:9fca2b23d0ba | 171 | Mutex mutex; |
marcozecchini | 0:9fca2b23d0ba | 172 | osStatus stat; |
marcozecchini | 0:9fca2b23d0ba | 173 | Thread thread(osPriorityNormal, TEST_STACK_SIZE); |
marcozecchini | 0:9fca2b23d0ba | 174 | |
marcozecchini | 0:9fca2b23d0ba | 175 | stat = mutex.lock(); |
marcozecchini | 0:9fca2b23d0ba | 176 | TEST_ASSERT_EQUAL(osOK, stat); |
marcozecchini | 0:9fca2b23d0ba | 177 | |
marcozecchini | 0:9fca2b23d0ba | 178 | thread.start(callback(test_dual_thread_lock_unlock_thread, &mutex)); |
marcozecchini | 0:9fca2b23d0ba | 179 | |
marcozecchini | 0:9fca2b23d0ba | 180 | stat = mutex.unlock(); |
marcozecchini | 0:9fca2b23d0ba | 181 | TEST_ASSERT_EQUAL(osOK, stat); |
marcozecchini | 0:9fca2b23d0ba | 182 | |
marcozecchini | 0:9fca2b23d0ba | 183 | wait_ms(TEST_DELAY); |
marcozecchini | 0:9fca2b23d0ba | 184 | } |
marcozecchini | 0:9fca2b23d0ba | 185 | |
marcozecchini | 0:9fca2b23d0ba | 186 | void test_dual_thread_lock_trylock_thread(Mutex *mutex) |
marcozecchini | 0:9fca2b23d0ba | 187 | { |
marcozecchini | 0:9fca2b23d0ba | 188 | bool stat = mutex->trylock(); |
marcozecchini | 0:9fca2b23d0ba | 189 | TEST_ASSERT_EQUAL(false, stat); |
marcozecchini | 0:9fca2b23d0ba | 190 | } |
marcozecchini | 0:9fca2b23d0ba | 191 | |
marcozecchini | 0:9fca2b23d0ba | 192 | void test_dual_thread_lock_lock_thread(Mutex *mutex) |
marcozecchini | 0:9fca2b23d0ba | 193 | { |
marcozecchini | 0:9fca2b23d0ba | 194 | Timer timer; |
marcozecchini | 0:9fca2b23d0ba | 195 | timer.start(); |
marcozecchini | 0:9fca2b23d0ba | 196 | |
marcozecchini | 0:9fca2b23d0ba | 197 | osStatus stat = mutex->lock(TEST_DELAY); |
marcozecchini | 0:9fca2b23d0ba | 198 | TEST_ASSERT_EQUAL(osErrorTimeout, stat); |
marcozecchini | 0:9fca2b23d0ba | 199 | TEST_ASSERT_UINT32_WITHIN(5000, TEST_DELAY*1000, timer.read_us()); |
marcozecchini | 0:9fca2b23d0ba | 200 | } |
marcozecchini | 0:9fca2b23d0ba | 201 | |
marcozecchini | 0:9fca2b23d0ba | 202 | /** Test dual thread lock |
marcozecchini | 0:9fca2b23d0ba | 203 | |
marcozecchini | 0:9fca2b23d0ba | 204 | Test dual thread lock locked |
marcozecchini | 0:9fca2b23d0ba | 205 | Given a mutex and two threads A & B |
marcozecchini | 0:9fca2b23d0ba | 206 | When thread A calls @a lock and starts thread B |
marcozecchini | 0:9fca2b23d0ba | 207 | and thread B calls @a lock with 500ms timeout |
marcozecchini | 0:9fca2b23d0ba | 208 | Then thread B waits 500ms and timeouts |
marcozecchini | 0:9fca2b23d0ba | 209 | |
marcozecchini | 0:9fca2b23d0ba | 210 | Test dual thread trylock locked |
marcozecchini | 0:9fca2b23d0ba | 211 | Given a mutex and two threads A & B |
marcozecchini | 0:9fca2b23d0ba | 212 | When thread A calls @a lock and starts thread B |
marcozecchini | 0:9fca2b23d0ba | 213 | Then thread B calls @a trylock |
marcozecchini | 0:9fca2b23d0ba | 214 | and thread B fails to acquire the lock |
marcozecchini | 0:9fca2b23d0ba | 215 | */ |
marcozecchini | 0:9fca2b23d0ba | 216 | template <void (*F)(Mutex *)> |
marcozecchini | 0:9fca2b23d0ba | 217 | void test_dual_thread_lock(void) |
marcozecchini | 0:9fca2b23d0ba | 218 | { |
marcozecchini | 0:9fca2b23d0ba | 219 | Mutex mutex; |
marcozecchini | 0:9fca2b23d0ba | 220 | osStatus stat; |
marcozecchini | 0:9fca2b23d0ba | 221 | Thread thread(osPriorityNormal, TEST_STACK_SIZE); |
marcozecchini | 0:9fca2b23d0ba | 222 | |
marcozecchini | 0:9fca2b23d0ba | 223 | stat = mutex.lock(); |
marcozecchini | 0:9fca2b23d0ba | 224 | TEST_ASSERT_EQUAL(osOK, stat); |
marcozecchini | 0:9fca2b23d0ba | 225 | |
marcozecchini | 0:9fca2b23d0ba | 226 | thread.start(callback(F, &mutex)); |
marcozecchini | 0:9fca2b23d0ba | 227 | |
marcozecchini | 0:9fca2b23d0ba | 228 | wait_ms(TEST_LONG_DELAY); |
marcozecchini | 0:9fca2b23d0ba | 229 | |
marcozecchini | 0:9fca2b23d0ba | 230 | stat = mutex.unlock(); |
marcozecchini | 0:9fca2b23d0ba | 231 | TEST_ASSERT_EQUAL(osOK, stat); |
marcozecchini | 0:9fca2b23d0ba | 232 | } |
marcozecchini | 0:9fca2b23d0ba | 233 | |
marcozecchini | 0:9fca2b23d0ba | 234 | /** Test single thread lock recursive |
marcozecchini | 0:9fca2b23d0ba | 235 | |
marcozecchini | 0:9fca2b23d0ba | 236 | Given a mutex and a single running thread |
marcozecchini | 0:9fca2b23d0ba | 237 | When thread calls @a lock twice and @a unlock twice on the mutex |
marcozecchini | 0:9fca2b23d0ba | 238 | Then the returned statuses are osOK |
marcozecchini | 0:9fca2b23d0ba | 239 | */ |
marcozecchini | 0:9fca2b23d0ba | 240 | void test_single_thread_lock_recursive(void) |
marcozecchini | 0:9fca2b23d0ba | 241 | { |
marcozecchini | 0:9fca2b23d0ba | 242 | Mutex mutex; |
marcozecchini | 0:9fca2b23d0ba | 243 | osStatus stat; |
marcozecchini | 0:9fca2b23d0ba | 244 | |
marcozecchini | 0:9fca2b23d0ba | 245 | stat = mutex.lock(); |
marcozecchini | 0:9fca2b23d0ba | 246 | TEST_ASSERT_EQUAL(osOK, stat); |
marcozecchini | 0:9fca2b23d0ba | 247 | |
marcozecchini | 0:9fca2b23d0ba | 248 | stat = mutex.lock(); |
marcozecchini | 0:9fca2b23d0ba | 249 | TEST_ASSERT_EQUAL(osOK, stat); |
marcozecchini | 0:9fca2b23d0ba | 250 | |
marcozecchini | 0:9fca2b23d0ba | 251 | stat = mutex.unlock(); |
marcozecchini | 0:9fca2b23d0ba | 252 | TEST_ASSERT_EQUAL(osOK, stat); |
marcozecchini | 0:9fca2b23d0ba | 253 | |
marcozecchini | 0:9fca2b23d0ba | 254 | stat = mutex.unlock(); |
marcozecchini | 0:9fca2b23d0ba | 255 | TEST_ASSERT_EQUAL(osOK, stat); |
marcozecchini | 0:9fca2b23d0ba | 256 | } |
marcozecchini | 0:9fca2b23d0ba | 257 | |
marcozecchini | 0:9fca2b23d0ba | 258 | /** Test single thread trylock |
marcozecchini | 0:9fca2b23d0ba | 259 | |
marcozecchini | 0:9fca2b23d0ba | 260 | Given a mutex and a single running thread |
marcozecchini | 0:9fca2b23d0ba | 261 | When thread calls @a trylock and @a unlock on the mutex |
marcozecchini | 0:9fca2b23d0ba | 262 | Then the returned statuses are osOK |
marcozecchini | 0:9fca2b23d0ba | 263 | */ |
marcozecchini | 0:9fca2b23d0ba | 264 | void test_single_thread_trylock(void) |
marcozecchini | 0:9fca2b23d0ba | 265 | { |
marcozecchini | 0:9fca2b23d0ba | 266 | Mutex mutex; |
marcozecchini | 0:9fca2b23d0ba | 267 | |
marcozecchini | 0:9fca2b23d0ba | 268 | bool stat_b = mutex.trylock(); |
marcozecchini | 0:9fca2b23d0ba | 269 | TEST_ASSERT_EQUAL(true, stat_b); |
marcozecchini | 0:9fca2b23d0ba | 270 | |
marcozecchini | 0:9fca2b23d0ba | 271 | osStatus stat = mutex.unlock(); |
marcozecchini | 0:9fca2b23d0ba | 272 | TEST_ASSERT_EQUAL(osOK, stat); |
marcozecchini | 0:9fca2b23d0ba | 273 | } |
marcozecchini | 0:9fca2b23d0ba | 274 | |
marcozecchini | 0:9fca2b23d0ba | 275 | /** Test single thread lock |
marcozecchini | 0:9fca2b23d0ba | 276 | |
marcozecchini | 0:9fca2b23d0ba | 277 | Given a mutex and a single running thread |
marcozecchini | 0:9fca2b23d0ba | 278 | When thread calls @a lock and @a unlock on the mutex |
marcozecchini | 0:9fca2b23d0ba | 279 | Then the returned statuses are osOK |
marcozecchini | 0:9fca2b23d0ba | 280 | */ |
marcozecchini | 0:9fca2b23d0ba | 281 | void test_single_thread_lock(void) |
marcozecchini | 0:9fca2b23d0ba | 282 | { |
marcozecchini | 0:9fca2b23d0ba | 283 | Mutex mutex; |
marcozecchini | 0:9fca2b23d0ba | 284 | osStatus stat; |
marcozecchini | 0:9fca2b23d0ba | 285 | |
marcozecchini | 0:9fca2b23d0ba | 286 | stat = mutex.lock(); |
marcozecchini | 0:9fca2b23d0ba | 287 | TEST_ASSERT_EQUAL(osOK, stat); |
marcozecchini | 0:9fca2b23d0ba | 288 | |
marcozecchini | 0:9fca2b23d0ba | 289 | stat = mutex.unlock(); |
marcozecchini | 0:9fca2b23d0ba | 290 | TEST_ASSERT_EQUAL(osOK, stat); |
marcozecchini | 0:9fca2b23d0ba | 291 | } |
marcozecchini | 0:9fca2b23d0ba | 292 | |
marcozecchini | 0:9fca2b23d0ba | 293 | utest::v1::status_t test_setup(const size_t number_of_cases) |
marcozecchini | 0:9fca2b23d0ba | 294 | { |
marcozecchini | 0:9fca2b23d0ba | 295 | GREENTEA_SETUP(10, "default_auto"); |
marcozecchini | 0:9fca2b23d0ba | 296 | return verbose_test_setup_handler(number_of_cases); |
marcozecchini | 0:9fca2b23d0ba | 297 | } |
marcozecchini | 0:9fca2b23d0ba | 298 | |
marcozecchini | 0:9fca2b23d0ba | 299 | Case cases[] = { |
marcozecchini | 0:9fca2b23d0ba | 300 | Case("Test single thread lock", test_single_thread_lock), |
marcozecchini | 0:9fca2b23d0ba | 301 | Case("Test single thread trylock", test_single_thread_trylock), |
marcozecchini | 0:9fca2b23d0ba | 302 | Case("Test single thread lock recursive", test_single_thread_lock_recursive), |
marcozecchini | 0:9fca2b23d0ba | 303 | Case("Test dual thread lock locked", test_dual_thread_lock<test_dual_thread_lock_lock_thread>), |
marcozecchini | 0:9fca2b23d0ba | 304 | Case("Test dual thread trylock locked", test_dual_thread_lock<test_dual_thread_lock_trylock_thread>), |
marcozecchini | 0:9fca2b23d0ba | 305 | Case("Test dual thread lock unlock", test_dual_thread_lock_unlock), |
marcozecchini | 0:9fca2b23d0ba | 306 | Case("Test dual thread second thread lock", test_dual_thread_nolock<test_dual_thread_nolock_lock_thread>), |
marcozecchini | 0:9fca2b23d0ba | 307 | Case("Test dual thread second thread trylock", test_dual_thread_nolock<test_dual_thread_nolock_trylock_thread>), |
marcozecchini | 0:9fca2b23d0ba | 308 | Case("Test multiple thread", test_multiple_threads), |
marcozecchini | 0:9fca2b23d0ba | 309 | }; |
marcozecchini | 0:9fca2b23d0ba | 310 | |
marcozecchini | 0:9fca2b23d0ba | 311 | Specification specification(test_setup, cases); |
marcozecchini | 0:9fca2b23d0ba | 312 | |
marcozecchini | 0:9fca2b23d0ba | 313 | int main() |
marcozecchini | 0:9fca2b23d0ba | 314 | { |
marcozecchini | 0:9fca2b23d0ba | 315 | return !Harness::run(specification); |
marcozecchini | 0:9fca2b23d0ba | 316 | } |