diff --git a/TESTS/mbedmicro-rtos-mbed/threads/main.cpp b/TESTS/mbedmicro-rtos-mbed/threads/main.cpp index e58981c0e4..90fa96c3ac 100644 --- a/TESTS/mbedmicro-rtos-mbed/threads/main.cpp +++ b/TESTS/mbedmicro-rtos-mbed/threads/main.cpp @@ -66,8 +66,18 @@ void increment_with_wait(counter_t* counter) { (*counter)++; } -void increment_with_child(counter_t* counter) { - Thread *child = new Thread(osPriorityNormal, CHILD_THREAD_STACK_SIZE); +void increment_with_child(counter_t *counter) +{ + Thread *child = new (std::nothrow) Thread(osPriorityNormal, CHILD_THREAD_STACK_SIZE); + char *dummy = new (std::nothrow) char[CHILD_THREAD_STACK_SIZE]; + delete[] dummy; + + // Don't fail test due to lack of memory. Call function directly instead + if (!child || !dummy) { + increment(counter); + delete child; + return; + } child->start(callback(increment, counter)); child->join(); delete child; @@ -78,12 +88,21 @@ void increment_with_murder(counter_t* counter) { // take ownership of the counter mutex so it prevent the child to // modify counter. LockGuard lock(counter->internal_mutex()); - Thread *child = new Thread(osPriorityNormal, CHILD_THREAD_STACK_SIZE); + Thread *child = new (std::nothrow) Thread(osPriorityNormal, CHILD_THREAD_STACK_SIZE); + char *dummy = new (std::nothrow) char[CHILD_THREAD_STACK_SIZE]; + delete[] dummy; + + // Don't fail test due to lack of memory. + if (!child || !dummy) { + delete child; + goto end; + } child->start(callback(increment, counter)); child->terminate(); delete child; } +end: (*counter)++; } @@ -126,7 +145,12 @@ void self_terminate(Thread *self) { then the final value of the counter is equal to 1 */ template -void test_single_thread() { +void test_single_thread() +{ + char *dummy = new (std::nothrow) char[THREAD_STACK_SIZE]; + delete[] dummy; + TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory to run test"); + counter_t counter(0); Thread thread(osPriorityNormal, THREAD_STACK_SIZE); thread.start(callback(F, &counter)); @@ -165,7 +189,12 @@ void test_single_thread() { then the final value of the counter is equal to number of parallel threads */ template -void test_parallel_threads() { +void test_parallel_threads() +{ + char *dummy = new (std::nothrow) char[PARALLEL_THREAD_STACK_SIZE * N]; + delete[] dummy; + TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory to run test"); + counter_t counter(0); ParallelThread threads[N]; @@ -213,6 +242,9 @@ void test_parallel_threads() { template void test_serial_threads() { counter_t counter(0); + char *dummy = new (std::nothrow) char[THREAD_STACK_SIZE]; + delete[] dummy; + TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory to run test"); for (int i = 0; i < N; i++) { Thread thread(osPriorityNormal, THREAD_STACK_SIZE); @@ -229,10 +261,20 @@ void test_serial_threads() { when the thread calls @a terminate on its self then the thread terminates execution cleanly */ -void test_self_terminate() { - Thread *thread = new Thread(osPriorityNormal, THREAD_STACK_SIZE); +void test_self_terminate() +{ + Thread *thread = new (std::nothrow) Thread(osPriorityNormal, THREAD_STACK_SIZE); + char *dummy = new (std::nothrow) char[THREAD_STACK_SIZE]; + delete[] dummy; + + // Don't fail test due to lack of memory. + if (!thread || !dummy) { + goto end; + } thread->start(callback(self_terminate, thread)); thread->join(); + +end: delete thread; } @@ -290,6 +332,10 @@ void signal_wait_multibit_tout() template void test_thread_signal() { + char *dummy = new (std::nothrow) char[THREAD_STACK_SIZE]; + delete[] dummy; + TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory to run test"); + Thread t_wait(osPriorityNormal, THREAD_STACK_SIZE); t_wait.start(callback(F)); @@ -326,6 +372,10 @@ void signal_clr() */ void test_thread_signal_clr() { + char *dummy = new (std::nothrow) char[THREAD_STACK_SIZE]; + delete[] dummy; + TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory to run test"); + Thread t_wait(osPriorityNormal, THREAD_STACK_SIZE); t_wait.start(callback(signal_clr)); @@ -360,7 +410,12 @@ void stack_info() { and the reported stack size is as requested in the constructor and the sum of free and used stack sizes is equal to the total stack size */ -void test_thread_stack_info() { +void test_thread_stack_info() +{ + char *dummy = new (std::nothrow) char[THREAD_STACK_SIZE]; + delete[] dummy; + TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory to run test"); + Thread t(osPriorityNormal, THREAD_STACK_SIZE); t.start(callback(stack_info)); @@ -409,7 +464,12 @@ void test_thread_wait() { when the name is queried using @a get_name then the returned name is as set */ -void test_thread_name() { +void test_thread_name() +{ + char *dummy = new (std::nothrow) char[THREAD_STACK_SIZE]; + delete[] dummy; + TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory to run test"); + const char tname[] = "Amazing thread"; Thread t(osPriorityNormal, THREAD_STACK_SIZE, NULL, tname); t.start(callback(thread_wait_signal)); @@ -431,6 +491,10 @@ void test_deleted_thread() */ void test_deleted() { + char *dummy = new (std::nothrow) char[THREAD_STACK_SIZE]; + delete[] dummy; + TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory to run test"); + Thread t(osPriorityNormal, THREAD_STACK_SIZE); TEST_ASSERT_EQUAL(Thread::Deleted, t.get_state()); @@ -454,6 +518,10 @@ void test_delay_thread() */ void test_delay() { + char *dummy = new (std::nothrow) char[THREAD_STACK_SIZE]; + delete[] dummy; + TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory to run test"); + Thread t(osPriorityNormal, THREAD_STACK_SIZE); t.start(callback(test_delay_thread)); @@ -479,6 +547,10 @@ void test_signal_thread() */ void test_signal() { + char *dummy = new (std::nothrow) char[THREAD_STACK_SIZE]; + delete[] dummy; + TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory to run test"); + Thread t(osPriorityNormal, THREAD_STACK_SIZE); t.start(callback(test_signal_thread)); @@ -503,6 +575,10 @@ void test_evt_flag_thread(osEventFlagsId_t evtflg) */ void test_evt_flag() { + char *dummy = new (std::nothrow) char[THREAD_STACK_SIZE]; + delete[] dummy; + TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory to run test"); + Thread t(osPriorityNormal, THREAD_STACK_SIZE); mbed_rtos_storage_event_flags_t evtflg_mem; osEventFlagsAttr_t evtflg_attr; @@ -535,6 +611,10 @@ void test_mutex_thread(Mutex *mutex) */ void test_mutex() { + char *dummy = new (std::nothrow) char[THREAD_STACK_SIZE]; + delete[] dummy; + TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory to run test"); + Thread t(osPriorityNormal, THREAD_STACK_SIZE); Mutex mutex; @@ -562,6 +642,10 @@ void test_semaphore_thread(Semaphore *sem) */ void test_semaphore() { + char *dummy = new (std::nothrow) char[THREAD_STACK_SIZE]; + delete[] dummy; + TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory to run test"); + Thread t(osPriorityNormal, THREAD_STACK_SIZE); Semaphore sem; @@ -587,6 +671,10 @@ void test_msg_get_thread(Queue *queue) */ void test_msg_get() { + char *dummy = new (std::nothrow) char[THREAD_STACK_SIZE]; + delete[] dummy; + TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory to run test"); + Thread t(osPriorityNormal, THREAD_STACK_SIZE); Queue queue; @@ -613,6 +701,10 @@ void test_msg_put_thread(Queue *queue) */ void test_msg_put() { + char *dummy = new (std::nothrow) char[THREAD_STACK_SIZE]; + delete[] dummy; + TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory to run test"); + Thread t(osPriorityNormal, THREAD_STACK_SIZE); Queue queue; @@ -660,7 +752,12 @@ void test_thread_ext_stack() { when new priority is set using @a set_priority then priority is changed and can be retrieved using @a get_priority */ -void test_thread_prio() { +void test_thread_prio() +{ + char *dummy = new (std::nothrow) char[THREAD_STACK_SIZE]; + delete[] dummy; + TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory to run test"); + Thread t(osPriorityNormal, THREAD_STACK_SIZE); t.start(callback(thread_wait_signal));