Skip to content

Commit 93b3991

Browse files
David SaadaCruz Monrreal II
authored andcommitted
RTOS threads test: Handle out of memory cases
1 parent 5023f48 commit 93b3991

File tree

1 file changed

+107
-10
lines changed
  • TESTS/mbedmicro-rtos-mbed/threads

1 file changed

+107
-10
lines changed

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

Lines changed: 107 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,18 @@ void increment_with_wait(counter_t* counter) {
6666
(*counter)++;
6767
}
6868

69-
void increment_with_child(counter_t* counter) {
70-
Thread *child = new Thread(osPriorityNormal, CHILD_THREAD_STACK_SIZE);
69+
void increment_with_child(counter_t *counter)
70+
{
71+
Thread *child = new (std::nothrow) Thread(osPriorityNormal, CHILD_THREAD_STACK_SIZE);
72+
char *dummy = new (std::nothrow) char[CHILD_THREAD_STACK_SIZE];
73+
delete[] dummy;
74+
75+
// Don't fail test due to lack of memory. Call function directly instead
76+
if (!child || !dummy) {
77+
increment(counter);
78+
delete child;
79+
return;
80+
}
7181
child->start(callback(increment, counter));
7282
child->join();
7383
delete child;
@@ -78,12 +88,21 @@ void increment_with_murder(counter_t* counter) {
7888
// take ownership of the counter mutex so it prevent the child to
7989
// modify counter.
8090
LockGuard lock(counter->internal_mutex());
81-
Thread *child = new Thread(osPriorityNormal, CHILD_THREAD_STACK_SIZE);
91+
Thread *child = new (std::nothrow) Thread(osPriorityNormal, CHILD_THREAD_STACK_SIZE);
92+
char *dummy = new (std::nothrow) char[CHILD_THREAD_STACK_SIZE];
93+
delete[] dummy;
94+
95+
// Don't fail test due to lack of memory.
96+
if (!child || !dummy) {
97+
delete child;
98+
goto end;
99+
}
82100
child->start(callback(increment, counter));
83101
child->terminate();
84102
delete child;
85103
}
86104

105+
end:
87106
(*counter)++;
88107
}
89108

@@ -126,7 +145,12 @@ void self_terminate(Thread *self) {
126145
then the final value of the counter is equal to 1
127146
*/
128147
template <void (*F)(counter_t *)>
129-
void test_single_thread() {
148+
void test_single_thread()
149+
{
150+
char *dummy = new (std::nothrow) char[THREAD_STACK_SIZE];
151+
delete[] dummy;
152+
TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory to run test");
153+
130154
counter_t counter(0);
131155
Thread thread(osPriorityNormal, THREAD_STACK_SIZE);
132156
thread.start(callback(F, &counter));
@@ -165,7 +189,12 @@ void test_single_thread() {
165189
then the final value of the counter is equal to number of parallel threads
166190
*/
167191
template <int N, void (*F)(counter_t *)>
168-
void test_parallel_threads() {
192+
void test_parallel_threads()
193+
{
194+
char *dummy = new (std::nothrow) char[PARALLEL_THREAD_STACK_SIZE * N];
195+
delete[] dummy;
196+
TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory to run test");
197+
169198
counter_t counter(0);
170199
ParallelThread<osPriorityNormal, PARALLEL_THREAD_STACK_SIZE> threads[N];
171200

@@ -213,6 +242,9 @@ void test_parallel_threads() {
213242
template <int N, void (*F)(counter_t *)>
214243
void test_serial_threads() {
215244
counter_t counter(0);
245+
char *dummy = new (std::nothrow) char[THREAD_STACK_SIZE];
246+
delete[] dummy;
247+
TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory to run test");
216248

217249
for (int i = 0; i < N; i++) {
218250
Thread thread(osPriorityNormal, THREAD_STACK_SIZE);
@@ -229,10 +261,20 @@ void test_serial_threads() {
229261
when the thread calls @a terminate on its self
230262
then the thread terminates execution cleanly
231263
*/
232-
void test_self_terminate() {
233-
Thread *thread = new Thread(osPriorityNormal, THREAD_STACK_SIZE);
264+
void test_self_terminate()
265+
{
266+
Thread *thread = new (std::nothrow) Thread(osPriorityNormal, THREAD_STACK_SIZE);
267+
char *dummy = new (std::nothrow) char[THREAD_STACK_SIZE];
268+
delete[] dummy;
269+
270+
// Don't fail test due to lack of memory.
271+
if (!thread || !dummy) {
272+
goto end;
273+
}
234274
thread->start(callback(self_terminate, thread));
235275
thread->join();
276+
277+
end:
236278
delete thread;
237279
}
238280

@@ -290,6 +332,10 @@ void signal_wait_multibit_tout()
290332
template <int S, void (*F)()>
291333
void test_thread_signal()
292334
{
335+
char *dummy = new (std::nothrow) char[THREAD_STACK_SIZE];
336+
delete[] dummy;
337+
TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory to run test");
338+
293339
Thread t_wait(osPriorityNormal, THREAD_STACK_SIZE);
294340

295341
t_wait.start(callback(F));
@@ -326,6 +372,10 @@ void signal_clr()
326372
*/
327373
void test_thread_signal_clr()
328374
{
375+
char *dummy = new (std::nothrow) char[THREAD_STACK_SIZE];
376+
delete[] dummy;
377+
TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory to run test");
378+
329379
Thread t_wait(osPriorityNormal, THREAD_STACK_SIZE);
330380

331381
t_wait.start(callback(signal_clr));
@@ -360,7 +410,12 @@ void stack_info() {
360410
and the reported stack size is as requested in the constructor
361411
and the sum of free and used stack sizes is equal to the total stack size
362412
*/
363-
void test_thread_stack_info() {
413+
void test_thread_stack_info()
414+
{
415+
char *dummy = new (std::nothrow) char[THREAD_STACK_SIZE];
416+
delete[] dummy;
417+
TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory to run test");
418+
364419
Thread t(osPriorityNormal, THREAD_STACK_SIZE);
365420
t.start(callback(stack_info));
366421

@@ -409,7 +464,12 @@ void test_thread_wait() {
409464
when the name is queried using @a get_name
410465
then the returned name is as set
411466
*/
412-
void test_thread_name() {
467+
void test_thread_name()
468+
{
469+
char *dummy = new (std::nothrow) char[THREAD_STACK_SIZE];
470+
delete[] dummy;
471+
TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory to run test");
472+
413473
const char tname[] = "Amazing thread";
414474
Thread t(osPriorityNormal, THREAD_STACK_SIZE, NULL, tname);
415475
t.start(callback(thread_wait_signal));
@@ -431,6 +491,10 @@ void test_deleted_thread()
431491
*/
432492
void test_deleted()
433493
{
494+
char *dummy = new (std::nothrow) char[THREAD_STACK_SIZE];
495+
delete[] dummy;
496+
TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory to run test");
497+
434498
Thread t(osPriorityNormal, THREAD_STACK_SIZE);
435499

436500
TEST_ASSERT_EQUAL(Thread::Deleted, t.get_state());
@@ -454,6 +518,10 @@ void test_delay_thread()
454518
*/
455519
void test_delay()
456520
{
521+
char *dummy = new (std::nothrow) char[THREAD_STACK_SIZE];
522+
delete[] dummy;
523+
TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory to run test");
524+
457525
Thread t(osPriorityNormal, THREAD_STACK_SIZE);
458526

459527
t.start(callback(test_delay_thread));
@@ -479,6 +547,10 @@ void test_signal_thread()
479547
*/
480548
void test_signal()
481549
{
550+
char *dummy = new (std::nothrow) char[THREAD_STACK_SIZE];
551+
delete[] dummy;
552+
TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory to run test");
553+
482554
Thread t(osPriorityNormal, THREAD_STACK_SIZE);
483555

484556
t.start(callback(test_signal_thread));
@@ -503,6 +575,10 @@ void test_evt_flag_thread(osEventFlagsId_t evtflg)
503575
*/
504576
void test_evt_flag()
505577
{
578+
char *dummy = new (std::nothrow) char[THREAD_STACK_SIZE];
579+
delete[] dummy;
580+
TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory to run test");
581+
506582
Thread t(osPriorityNormal, THREAD_STACK_SIZE);
507583
mbed_rtos_storage_event_flags_t evtflg_mem;
508584
osEventFlagsAttr_t evtflg_attr;
@@ -535,6 +611,10 @@ void test_mutex_thread(Mutex *mutex)
535611
*/
536612
void test_mutex()
537613
{
614+
char *dummy = new (std::nothrow) char[THREAD_STACK_SIZE];
615+
delete[] dummy;
616+
TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory to run test");
617+
538618
Thread t(osPriorityNormal, THREAD_STACK_SIZE);
539619
Mutex mutex;
540620

@@ -562,6 +642,10 @@ void test_semaphore_thread(Semaphore *sem)
562642
*/
563643
void test_semaphore()
564644
{
645+
char *dummy = new (std::nothrow) char[THREAD_STACK_SIZE];
646+
delete[] dummy;
647+
TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory to run test");
648+
565649
Thread t(osPriorityNormal, THREAD_STACK_SIZE);
566650
Semaphore sem;
567651

@@ -587,6 +671,10 @@ void test_msg_get_thread(Queue<int32_t, 1> *queue)
587671
*/
588672
void test_msg_get()
589673
{
674+
char *dummy = new (std::nothrow) char[THREAD_STACK_SIZE];
675+
delete[] dummy;
676+
TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory to run test");
677+
590678
Thread t(osPriorityNormal, THREAD_STACK_SIZE);
591679
Queue<int32_t, 1> queue;
592680

@@ -613,6 +701,10 @@ void test_msg_put_thread(Queue<int32_t, 1> *queue)
613701
*/
614702
void test_msg_put()
615703
{
704+
char *dummy = new (std::nothrow) char[THREAD_STACK_SIZE];
705+
delete[] dummy;
706+
TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory to run test");
707+
616708
Thread t(osPriorityNormal, THREAD_STACK_SIZE);
617709
Queue<int32_t, 1> queue;
618710

@@ -660,7 +752,12 @@ void test_thread_ext_stack() {
660752
when new priority is set using @a set_priority
661753
then priority is changed and can be retrieved using @a get_priority
662754
*/
663-
void test_thread_prio() {
755+
void test_thread_prio()
756+
{
757+
char *dummy = new (std::nothrow) char[THREAD_STACK_SIZE];
758+
delete[] dummy;
759+
TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory to run test");
760+
664761
Thread t(osPriorityNormal, THREAD_STACK_SIZE);
665762
t.start(callback(thread_wait_signal));
666763

0 commit comments

Comments
 (0)