Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: blinky_max32630fthr
TESTS/mbedmicro-rtos-mbed/mutex/main.cpp@0:5c4d7b2438d3, 2016-11-11 (annotated)
- Committer:
- switches
- Date:
- Fri Nov 11 20:59:50 2016 +0000
- Revision:
- 0:5c4d7b2438d3
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
switches | 0:5c4d7b2438d3 | 1 | #include "mbed.h" |
switches | 0:5c4d7b2438d3 | 2 | #include "greentea-client/test_env.h" |
switches | 0:5c4d7b2438d3 | 3 | #include "rtos.h" |
switches | 0:5c4d7b2438d3 | 4 | |
switches | 0:5c4d7b2438d3 | 5 | #if defined(MBED_RTOS_SINGLE_THREAD) |
switches | 0:5c4d7b2438d3 | 6 | #error [NOT_SUPPORTED] test not supported |
switches | 0:5c4d7b2438d3 | 7 | #endif |
switches | 0:5c4d7b2438d3 | 8 | |
switches | 0:5c4d7b2438d3 | 9 | #define THREAD_DELAY 50 |
switches | 0:5c4d7b2438d3 | 10 | #define SIGNALS_TO_EMIT 100 |
switches | 0:5c4d7b2438d3 | 11 | |
switches | 0:5c4d7b2438d3 | 12 | /* |
switches | 0:5c4d7b2438d3 | 13 | * The stack size is defined in cmsis_os.h mainly dependent on the underlying toolchain and |
switches | 0:5c4d7b2438d3 | 14 | * the C standard library. For GCC, ARM_STD and IAR it is defined with a size of 2048 bytes |
switches | 0:5c4d7b2438d3 | 15 | * and for ARM_MICRO 512. Because of reduce RAM size some targets need a reduced stacksize. |
switches | 0:5c4d7b2438d3 | 16 | */ |
switches | 0:5c4d7b2438d3 | 17 | #if defined(TARGET_STM32F334R8) && defined(TOOLCHAIN_IAR) |
switches | 0:5c4d7b2438d3 | 18 | #define STACK_SIZE DEFAULT_STACK_SIZE/4 |
switches | 0:5c4d7b2438d3 | 19 | #elif defined(TARGET_STM32F070RB) |
switches | 0:5c4d7b2438d3 | 20 | #define STACK_SIZE DEFAULT_STACK_SIZE/2 |
switches | 0:5c4d7b2438d3 | 21 | #elif defined(TARGET_STM32F072RB) |
switches | 0:5c4d7b2438d3 | 22 | #define STACK_SIZE DEFAULT_STACK_SIZE/2 |
switches | 0:5c4d7b2438d3 | 23 | #elif defined(TARGET_STM32F302R8) && defined(TOOLCHAIN_IAR) |
switches | 0:5c4d7b2438d3 | 24 | #define STACK_SIZE DEFAULT_STACK_SIZE/2 |
switches | 0:5c4d7b2438d3 | 25 | #elif defined(TARGET_STM32F303K8) && defined(TOOLCHAIN_IAR) |
switches | 0:5c4d7b2438d3 | 26 | #define STACK_SIZE DEFAULT_STACK_SIZE/2 |
switches | 0:5c4d7b2438d3 | 27 | #elif defined(TARGET_STM32L073RZ) |
switches | 0:5c4d7b2438d3 | 28 | #define STACK_SIZE DEFAULT_STACK_SIZE/2 |
switches | 0:5c4d7b2438d3 | 29 | #elif (defined(TARGET_EFM32HG_STK3400)) && !defined(TOOLCHAIN_ARM_MICRO) |
switches | 0:5c4d7b2438d3 | 30 | #define STACK_SIZE 512 |
switches | 0:5c4d7b2438d3 | 31 | #elif (defined(TARGET_EFM32LG_STK3600) || defined(TARGET_EFM32WG_STK3800) || defined(TARGET_EFM32PG_STK3401)) && !defined(TOOLCHAIN_ARM_MICRO) |
switches | 0:5c4d7b2438d3 | 32 | #define STACK_SIZE 768 |
switches | 0:5c4d7b2438d3 | 33 | #elif (defined(TARGET_EFM32GG_STK3700)) && !defined(TOOLCHAIN_ARM_MICRO) |
switches | 0:5c4d7b2438d3 | 34 | #define STACK_SIZE 1536 |
switches | 0:5c4d7b2438d3 | 35 | #elif defined(TARGET_MCU_NRF51822) || defined(TARGET_MCU_NRF52832) |
switches | 0:5c4d7b2438d3 | 36 | #define STACK_SIZE 1024 |
switches | 0:5c4d7b2438d3 | 37 | #elif defined(TARGET_XDOT_L151CC) |
switches | 0:5c4d7b2438d3 | 38 | #define STACK_SIZE 1024 |
switches | 0:5c4d7b2438d3 | 39 | #else |
switches | 0:5c4d7b2438d3 | 40 | #define STACK_SIZE DEFAULT_STACK_SIZE |
switches | 0:5c4d7b2438d3 | 41 | #endif |
switches | 0:5c4d7b2438d3 | 42 | |
switches | 0:5c4d7b2438d3 | 43 | void print_char(char c = '*') { |
switches | 0:5c4d7b2438d3 | 44 | printf("%c", c); |
switches | 0:5c4d7b2438d3 | 45 | fflush(stdout); |
switches | 0:5c4d7b2438d3 | 46 | } |
switches | 0:5c4d7b2438d3 | 47 | |
switches | 0:5c4d7b2438d3 | 48 | Mutex stdio_mutex; |
switches | 0:5c4d7b2438d3 | 49 | DigitalOut led(LED1); |
switches | 0:5c4d7b2438d3 | 50 | |
switches | 0:5c4d7b2438d3 | 51 | volatile int change_counter = 0; |
switches | 0:5c4d7b2438d3 | 52 | volatile bool changing_counter = false; |
switches | 0:5c4d7b2438d3 | 53 | volatile bool mutex_defect = false; |
switches | 0:5c4d7b2438d3 | 54 | |
switches | 0:5c4d7b2438d3 | 55 | bool manipulate_protected_zone(const int thread_delay) { |
switches | 0:5c4d7b2438d3 | 56 | bool result = true; |
switches | 0:5c4d7b2438d3 | 57 | |
switches | 0:5c4d7b2438d3 | 58 | stdio_mutex.lock(); // LOCK |
switches | 0:5c4d7b2438d3 | 59 | if (changing_counter == true) { |
switches | 0:5c4d7b2438d3 | 60 | // 'e' stands for error. If changing_counter is true access is not exclusively |
switches | 0:5c4d7b2438d3 | 61 | print_char('e'); |
switches | 0:5c4d7b2438d3 | 62 | result = false; |
switches | 0:5c4d7b2438d3 | 63 | mutex_defect = true; |
switches | 0:5c4d7b2438d3 | 64 | } |
switches | 0:5c4d7b2438d3 | 65 | changing_counter = true; |
switches | 0:5c4d7b2438d3 | 66 | |
switches | 0:5c4d7b2438d3 | 67 | // Some action on protected |
switches | 0:5c4d7b2438d3 | 68 | led = !led; |
switches | 0:5c4d7b2438d3 | 69 | change_counter++; |
switches | 0:5c4d7b2438d3 | 70 | print_char('.'); |
switches | 0:5c4d7b2438d3 | 71 | Thread::wait(thread_delay); |
switches | 0:5c4d7b2438d3 | 72 | |
switches | 0:5c4d7b2438d3 | 73 | changing_counter = false; |
switches | 0:5c4d7b2438d3 | 74 | stdio_mutex.unlock(); // UNLOCK |
switches | 0:5c4d7b2438d3 | 75 | return result; |
switches | 0:5c4d7b2438d3 | 76 | } |
switches | 0:5c4d7b2438d3 | 77 | |
switches | 0:5c4d7b2438d3 | 78 | void test_thread(void const *args) { |
switches | 0:5c4d7b2438d3 | 79 | const int thread_delay = int(args); |
switches | 0:5c4d7b2438d3 | 80 | while (true) { |
switches | 0:5c4d7b2438d3 | 81 | manipulate_protected_zone(thread_delay); |
switches | 0:5c4d7b2438d3 | 82 | } |
switches | 0:5c4d7b2438d3 | 83 | } |
switches | 0:5c4d7b2438d3 | 84 | |
switches | 0:5c4d7b2438d3 | 85 | int main() { |
switches | 0:5c4d7b2438d3 | 86 | GREENTEA_SETUP(20, "default_auto"); |
switches | 0:5c4d7b2438d3 | 87 | |
switches | 0:5c4d7b2438d3 | 88 | const int t1_delay = THREAD_DELAY * 1; |
switches | 0:5c4d7b2438d3 | 89 | const int t2_delay = THREAD_DELAY * 2; |
switches | 0:5c4d7b2438d3 | 90 | const int t3_delay = THREAD_DELAY * 3; |
switches | 0:5c4d7b2438d3 | 91 | Thread t2(test_thread, (void *)t2_delay, osPriorityNormal, STACK_SIZE); |
switches | 0:5c4d7b2438d3 | 92 | Thread t3(test_thread, (void *)t3_delay, osPriorityNormal, STACK_SIZE); |
switches | 0:5c4d7b2438d3 | 93 | |
switches | 0:5c4d7b2438d3 | 94 | while (true) { |
switches | 0:5c4d7b2438d3 | 95 | // Thread 1 action |
switches | 0:5c4d7b2438d3 | 96 | Thread::wait(t1_delay); |
switches | 0:5c4d7b2438d3 | 97 | manipulate_protected_zone(t1_delay); |
switches | 0:5c4d7b2438d3 | 98 | if (change_counter >= SIGNALS_TO_EMIT or mutex_defect == true) { |
switches | 0:5c4d7b2438d3 | 99 | t2.terminate(); |
switches | 0:5c4d7b2438d3 | 100 | t3.terminate(); |
switches | 0:5c4d7b2438d3 | 101 | break; |
switches | 0:5c4d7b2438d3 | 102 | } |
switches | 0:5c4d7b2438d3 | 103 | } |
switches | 0:5c4d7b2438d3 | 104 | |
switches | 0:5c4d7b2438d3 | 105 | fflush(stdout); |
switches | 0:5c4d7b2438d3 | 106 | GREENTEA_TESTSUITE_RESULT(!mutex_defect); |
switches | 0:5c4d7b2438d3 | 107 | return 0; |
switches | 0:5c4d7b2438d3 | 108 | } |