Skip to content

Commit 75bc3d3

Browse files
author
David Saada
committed
RTOS threads test: Handle out of memory cases
1 parent 1ab05c2 commit 75bc3d3

File tree

1 file changed

+93
-3
lines changed
  • TESTS/mbedmicro-rtos-mbed/threads

1 file changed

+93
-3
lines changed

TESTS/mbedmicro-rtos-mbed/threads/main.cpp

Lines changed: 93 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,16 @@ void increment_with_wait(counter_t *counter)
7171

7272
void increment_with_child(counter_t *counter)
7373
{
74-
Thread *child = new Thread(osPriorityNormal, CHILD_THREAD_STACK_SIZE);
74+
Thread *child = new (std::nothrow) Thread(osPriorityNormal, CHILD_THREAD_STACK_SIZE);
75+
char *dummy = new (std::nothrow) char[CHILD_THREAD_STACK_SIZE];
76+
delete[] dummy;
77+
78+
// Don't fail test due to lack of memory. Call function directly instead
79+
if (!child || !dummy) {
80+
increment(counter);
81+
delete child;
82+
return;
83+
}
7584
child->start(callback(increment, counter));
7685
child->join();
7786
delete child;
@@ -83,12 +92,21 @@ void increment_with_murder(counter_t *counter)
8392
// take ownership of the counter mutex so it prevent the child to
8493
// modify counter.
8594
LockGuard lock(counter->internal_mutex());
86-
Thread *child = new Thread(osPriorityNormal, CHILD_THREAD_STACK_SIZE);
95+
Thread *child = new (std::nothrow) Thread(osPriorityNormal, CHILD_THREAD_STACK_SIZE);
96+
char *dummy = new (std::nothrow) char[CHILD_THREAD_STACK_SIZE];
97+
delete[] dummy;
98+
99+
// Don't fail test due to lack of memory.
100+
if (!child || !dummy) {
101+
delete child;
102+
goto end;
103+
}
87104
child->start(callback(increment, counter));
88105
child->terminate();
89106
delete child;
90107
}
91108

109+
end:
92110
(*counter)++;
93111
}
94112

@@ -134,6 +152,10 @@ void self_terminate(Thread *self)
134152
template <void (*F)(counter_t *)>
135153
void test_single_thread()
136154
{
155+
char *dummy = new (std::nothrow) char[THREAD_STACK_SIZE];
156+
delete[] dummy;
157+
TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory to run test");
158+
137159
counter_t counter(0);
138160
Thread thread(osPriorityNormal, THREAD_STACK_SIZE);
139161
thread.start(callback(F, &counter));
@@ -174,6 +196,10 @@ void test_single_thread()
174196
template <int N, void (*F)(counter_t *)>
175197
void test_parallel_threads()
176198
{
199+
char *dummy = new (std::nothrow) char[PARALLEL_THREAD_STACK_SIZE * N];
200+
delete[] dummy;
201+
TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory to run test");
202+
177203
counter_t counter(0);
178204
ParallelThread<osPriorityNormal, PARALLEL_THREAD_STACK_SIZE> threads[N];
179205

@@ -222,6 +248,9 @@ template <int N, void (*F)(counter_t *)>
222248
void test_serial_threads()
223249
{
224250
counter_t counter(0);
251+
char *dummy = new (std::nothrow) char[THREAD_STACK_SIZE];
252+
delete[] dummy;
253+
TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory to run test");
225254

226255
for (int i = 0; i < N; i++) {
227256
Thread thread(osPriorityNormal, THREAD_STACK_SIZE);
@@ -240,9 +269,18 @@ void test_serial_threads()
240269
*/
241270
void test_self_terminate()
242271
{
243-
Thread *thread = new Thread(osPriorityNormal, THREAD_STACK_SIZE);
272+
Thread *thread = new (std::nothrow) Thread(osPriorityNormal, THREAD_STACK_SIZE);
273+
char *dummy = new (std::nothrow) char[THREAD_STACK_SIZE];
274+
delete[] dummy;
275+
276+
// Don't fail test due to lack of memory.
277+
if (!thread || !dummy) {
278+
goto end;
279+
}
244280
thread->start(callback(self_terminate, thread));
245281
thread->join();
282+
283+
end:
246284
delete thread;
247285
}
248286

@@ -300,6 +338,10 @@ void signal_wait_multibit_tout()
300338
template <int S, void (*F)()>
301339
void test_thread_signal()
302340
{
341+
char *dummy = new (std::nothrow) char[THREAD_STACK_SIZE];
342+
delete[] dummy;
343+
TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory to run test");
344+
303345
Thread t_wait(osPriorityNormal, THREAD_STACK_SIZE);
304346

305347
t_wait.start(callback(F));
@@ -336,6 +378,10 @@ void signal_clr()
336378
*/
337379
void test_thread_signal_clr()
338380
{
381+
char *dummy = new (std::nothrow) char[THREAD_STACK_SIZE];
382+
delete[] dummy;
383+
TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory to run test");
384+
339385
Thread t_wait(osPriorityNormal, THREAD_STACK_SIZE);
340386

341387
t_wait.start(callback(signal_clr));
@@ -374,6 +420,10 @@ void stack_info()
374420
*/
375421
void test_thread_stack_info()
376422
{
423+
char *dummy = new (std::nothrow) char[THREAD_STACK_SIZE];
424+
delete[] dummy;
425+
TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory to run test");
426+
377427
Thread t(osPriorityNormal, THREAD_STACK_SIZE);
378428
t.start(callback(stack_info));
379429

@@ -425,6 +475,10 @@ void test_thread_wait()
425475
*/
426476
void test_thread_name()
427477
{
478+
char *dummy = new (std::nothrow) char[THREAD_STACK_SIZE];
479+
delete[] dummy;
480+
TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory to run test");
481+
428482
const char tname[] = "Amazing thread";
429483
Thread t(osPriorityNormal, THREAD_STACK_SIZE, NULL, tname);
430484
t.start(callback(thread_wait_signal));
@@ -446,6 +500,10 @@ void test_deleted_thread()
446500
*/
447501
void test_deleted()
448502
{
503+
char *dummy = new (std::nothrow) char[THREAD_STACK_SIZE];
504+
delete[] dummy;
505+
TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory to run test");
506+
449507
Thread t(osPriorityNormal, THREAD_STACK_SIZE);
450508

451509
TEST_ASSERT_EQUAL(Thread::Deleted, t.get_state());
@@ -469,6 +527,10 @@ void test_delay_thread()
469527
*/
470528
void test_delay()
471529
{
530+
char *dummy = new (std::nothrow) char[THREAD_STACK_SIZE];
531+
delete[] dummy;
532+
TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory to run test");
533+
472534
Thread t(osPriorityNormal, THREAD_STACK_SIZE);
473535

474536
t.start(callback(test_delay_thread));
@@ -494,6 +556,10 @@ void test_signal_thread()
494556
*/
495557
void test_signal()
496558
{
559+
char *dummy = new (std::nothrow) char[THREAD_STACK_SIZE];
560+
delete[] dummy;
561+
TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory to run test");
562+
497563
Thread t(osPriorityNormal, THREAD_STACK_SIZE);
498564

499565
t.start(callback(test_signal_thread));
@@ -518,6 +584,10 @@ void test_evt_flag_thread(osEventFlagsId_t evtflg)
518584
*/
519585
void test_evt_flag()
520586
{
587+
char *dummy = new (std::nothrow) char[THREAD_STACK_SIZE];
588+
delete[] dummy;
589+
TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory to run test");
590+
521591
Thread t(osPriorityNormal, THREAD_STACK_SIZE);
522592
mbed_rtos_storage_event_flags_t evtflg_mem;
523593
osEventFlagsAttr_t evtflg_attr;
@@ -550,6 +620,10 @@ void test_mutex_thread(Mutex *mutex)
550620
*/
551621
void test_mutex()
552622
{
623+
char *dummy = new (std::nothrow) char[THREAD_STACK_SIZE];
624+
delete[] dummy;
625+
TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory to run test");
626+
553627
Thread t(osPriorityNormal, THREAD_STACK_SIZE);
554628
Mutex mutex;
555629

@@ -577,6 +651,10 @@ void test_semaphore_thread(Semaphore *sem)
577651
*/
578652
void test_semaphore()
579653
{
654+
char *dummy = new (std::nothrow) char[THREAD_STACK_SIZE];
655+
delete[] dummy;
656+
TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory to run test");
657+
580658
Thread t(osPriorityNormal, THREAD_STACK_SIZE);
581659
Semaphore sem;
582660

@@ -602,6 +680,10 @@ void test_msg_get_thread(Queue<int32_t, 1> *queue)
602680
*/
603681
void test_msg_get()
604682
{
683+
char *dummy = new (std::nothrow) char[THREAD_STACK_SIZE];
684+
delete[] dummy;
685+
TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory to run test");
686+
605687
Thread t(osPriorityNormal, THREAD_STACK_SIZE);
606688
Queue<int32_t, 1> queue;
607689

@@ -628,6 +710,10 @@ void test_msg_put_thread(Queue<int32_t, 1> *queue)
628710
*/
629711
void test_msg_put()
630712
{
713+
char *dummy = new (std::nothrow) char[THREAD_STACK_SIZE];
714+
delete[] dummy;
715+
TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory to run test");
716+
631717
Thread t(osPriorityNormal, THREAD_STACK_SIZE);
632718
Queue<int32_t, 1> queue;
633719

@@ -680,6 +766,10 @@ void test_thread_ext_stack()
680766
*/
681767
void test_thread_prio()
682768
{
769+
char *dummy = new (std::nothrow) char[THREAD_STACK_SIZE];
770+
delete[] dummy;
771+
TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory to run test");
772+
683773
Thread t(osPriorityNormal, THREAD_STACK_SIZE);
684774
t.start(callback(thread_wait_signal));
685775

0 commit comments

Comments
 (0)