Lee Kai Xuan / mbed-os

Fork of mbed-os by erkin yucel

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 #define THREAD_STACK_SIZE   256
00011 
00012 DigitalOut led1(LED1);
00013 volatile bool should_exit = false;
00014 volatile bool allocation_failure = false;
00015 
00016 void task_using_malloc(void)
00017 {
00018     void* data;
00019     while (1) {
00020         // Repeatedly allocate and free memory
00021         data = malloc(100);
00022         if (data != NULL) {
00023             memset(data, 0, 100);
00024         } else {
00025             allocation_failure = true;
00026         }
00027         free(data);
00028 
00029         if (should_exit) {
00030             return;
00031         }
00032     }
00033 }
00034 
00035 int main()
00036 {
00037     Thread *thread_list[NUM_THREADS];
00038     int test_time = 15;
00039     GREENTEA_SETUP(20, "default_auto");
00040 
00041     // Allocate threads for the test
00042     for (int i = 0; i < NUM_THREADS; i++) {
00043         thread_list[i] = new Thread(osPriorityNormal, THREAD_STACK_SIZE);
00044         if (NULL == thread_list[i]) {
00045             allocation_failure = true;
00046         }
00047         thread_list[i]->start(task_using_malloc);
00048     }
00049 
00050     // Give the test time to run
00051     while (test_time) {
00052         led1 = !led1;
00053         Thread::wait(1000);
00054         test_time--;
00055     }
00056 
00057     // Join and delete all threads
00058     should_exit = 1;
00059     for (int i = 0; i < NUM_THREADS; i++) {
00060         if (NULL == thread_list[i]) {
00061             continue;
00062         }
00063         thread_list[i]->join();
00064         delete thread_list[i];
00065     }
00066 
00067     GREENTEA_TESTSUITE_RESULT(!allocation_failure);
00068 }