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