ON Semiconductor / mbed-os

Dependents:   mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "test_env.h"
00003 #include "rtos.h"
00004 
00005 #if defined(MBED_RTOS_SINGLE_THREAD)
00006   #error [NOT_SUPPORTED] test not supported
00007 #endif
00008 
00009 #define NUM_THREADS         5
00010 #if defined(__CORTEX_A9)
00011 #define THREAD_STACK_SIZE   DEFAULT_STACK_SIZE
00012 #else
00013 #define THREAD_STACK_SIZE   256
00014 #endif
00015 
00016 DigitalOut led1(LED1);
00017 volatile bool should_exit = false;
00018 volatile bool allocation_failure = false;
00019 
00020 void task_using_malloc(void)
00021 {
00022     void* data;
00023     while (1) {
00024         // Repeatedly allocate and free memory
00025         data = malloc(100);
00026         if (data != NULL) {
00027             memset(data, 0, 100);
00028         } else {
00029             allocation_failure = true;
00030         }
00031         free(data);
00032 
00033         if (should_exit) {
00034             return;
00035         }
00036     }
00037 }
00038 
00039 int main()
00040 {
00041     Thread *thread_list[NUM_THREADS];
00042     int test_time = 15;
00043     GREENTEA_SETUP(20, "default_auto");
00044 
00045     // Allocate threads for the test
00046     for (int i = 0; i < NUM_THREADS; i++) {
00047         thread_list[i] = new Thread(osPriorityNormal, THREAD_STACK_SIZE);
00048         if (NULL == thread_list[i]) {
00049             allocation_failure = true;
00050         }
00051         thread_list[i]->start(task_using_malloc);
00052     }
00053 
00054     // Give the test time to run
00055     while (test_time) {
00056         led1 = !led1;
00057         Thread::wait(1000);
00058         test_time--;
00059     }
00060 
00061     // Join and delete all threads
00062     should_exit = 1;
00063     for (int i = 0; i < NUM_THREADS; i++) {
00064         if (NULL == thread_list[i]) {
00065             continue;
00066         }
00067         thread_list[i]->join();
00068         delete thread_list[i];
00069     }
00070 
00071     GREENTEA_TESTSUITE_RESULT(!allocation_failure);
00072 }