nkjnm
Dependencies: MAX44000 nexpaq_mdk
Fork of LED_Demo by
mbd_os/TESTS/mbedmicro-rtos-mbed/malloc/main.cpp@1:55a6170b404f, 2016-09-17 (annotated)
- Committer:
- nexpaq
- Date:
- Sat Sep 17 16:32:05 2016 +0000
- Revision:
- 1:55a6170b404f
checking in for sharing
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 "test_env.h" |
nexpaq | 1:55a6170b404f | 3 | #include "rtos.h" |
nexpaq | 1:55a6170b404f | 4 | |
nexpaq | 1:55a6170b404f | 5 | #if defined(MBED_RTOS_SINGLE_THREAD) |
nexpaq | 1:55a6170b404f | 6 | #error [NOT_SUPPORTED] test not supported |
nexpaq | 1:55a6170b404f | 7 | #endif |
nexpaq | 1:55a6170b404f | 8 | |
nexpaq | 1:55a6170b404f | 9 | #define NUM_THREADS 5 |
nexpaq | 1:55a6170b404f | 10 | #define THREAD_STACK_SIZE 256 |
nexpaq | 1:55a6170b404f | 11 | |
nexpaq | 1:55a6170b404f | 12 | DigitalOut led1(LED1); |
nexpaq | 1:55a6170b404f | 13 | volatile bool should_exit = false; |
nexpaq | 1:55a6170b404f | 14 | volatile bool allocation_failure = false; |
nexpaq | 1:55a6170b404f | 15 | |
nexpaq | 1:55a6170b404f | 16 | void task_using_malloc(void) |
nexpaq | 1:55a6170b404f | 17 | { |
nexpaq | 1:55a6170b404f | 18 | void* data; |
nexpaq | 1:55a6170b404f | 19 | while (1) { |
nexpaq | 1:55a6170b404f | 20 | // Repeatedly allocate and free memory |
nexpaq | 1:55a6170b404f | 21 | data = malloc(100); |
nexpaq | 1:55a6170b404f | 22 | if (data != NULL) { |
nexpaq | 1:55a6170b404f | 23 | memset(data, 0, 100); |
nexpaq | 1:55a6170b404f | 24 | } else { |
nexpaq | 1:55a6170b404f | 25 | allocation_failure = true; |
nexpaq | 1:55a6170b404f | 26 | } |
nexpaq | 1:55a6170b404f | 27 | free(data); |
nexpaq | 1:55a6170b404f | 28 | |
nexpaq | 1:55a6170b404f | 29 | if (should_exit) { |
nexpaq | 1:55a6170b404f | 30 | return; |
nexpaq | 1:55a6170b404f | 31 | } |
nexpaq | 1:55a6170b404f | 32 | } |
nexpaq | 1:55a6170b404f | 33 | } |
nexpaq | 1:55a6170b404f | 34 | |
nexpaq | 1:55a6170b404f | 35 | int main() |
nexpaq | 1:55a6170b404f | 36 | { |
nexpaq | 1:55a6170b404f | 37 | Thread *thread_list[NUM_THREADS]; |
nexpaq | 1:55a6170b404f | 38 | int test_time = 15; |
nexpaq | 1:55a6170b404f | 39 | GREENTEA_SETUP(20, "default_auto"); |
nexpaq | 1:55a6170b404f | 40 | |
nexpaq | 1:55a6170b404f | 41 | // Allocate threads for the test |
nexpaq | 1:55a6170b404f | 42 | for (int i = 0; i < NUM_THREADS; i++) { |
nexpaq | 1:55a6170b404f | 43 | thread_list[i] = new Thread(osPriorityNormal, THREAD_STACK_SIZE); |
nexpaq | 1:55a6170b404f | 44 | if (NULL == thread_list[i]) { |
nexpaq | 1:55a6170b404f | 45 | allocation_failure = true; |
nexpaq | 1:55a6170b404f | 46 | } |
nexpaq | 1:55a6170b404f | 47 | thread_list[i]->start(task_using_malloc); |
nexpaq | 1:55a6170b404f | 48 | } |
nexpaq | 1:55a6170b404f | 49 | |
nexpaq | 1:55a6170b404f | 50 | // Give the test time to run |
nexpaq | 1:55a6170b404f | 51 | while (test_time) { |
nexpaq | 1:55a6170b404f | 52 | led1 = !led1; |
nexpaq | 1:55a6170b404f | 53 | Thread::wait(1000); |
nexpaq | 1:55a6170b404f | 54 | test_time--; |
nexpaq | 1:55a6170b404f | 55 | } |
nexpaq | 1:55a6170b404f | 56 | |
nexpaq | 1:55a6170b404f | 57 | // Join and delete all threads |
nexpaq | 1:55a6170b404f | 58 | should_exit = 1; |
nexpaq | 1:55a6170b404f | 59 | for (int i = 0; i < NUM_THREADS; i++) { |
nexpaq | 1:55a6170b404f | 60 | if (NULL == thread_list[i]) { |
nexpaq | 1:55a6170b404f | 61 | continue; |
nexpaq | 1:55a6170b404f | 62 | } |
nexpaq | 1:55a6170b404f | 63 | thread_list[i]->join(); |
nexpaq | 1:55a6170b404f | 64 | delete thread_list[i]; |
nexpaq | 1:55a6170b404f | 65 | } |
nexpaq | 1:55a6170b404f | 66 | |
nexpaq | 1:55a6170b404f | 67 | GREENTEA_TESTSUITE_RESULT(!allocation_failure); |
nexpaq | 1:55a6170b404f | 68 | } |