@@ -66,8 +66,18 @@ void increment_with_wait(counter_t* counter) {
66
66
(*counter)++;
67
67
}
68
68
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
+ }
71
81
child->start (callback (increment, counter));
72
82
child->join ();
73
83
delete child;
@@ -78,12 +88,21 @@ void increment_with_murder(counter_t* counter) {
78
88
// take ownership of the counter mutex so it prevent the child to
79
89
// modify counter.
80
90
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
+ }
82
100
child->start (callback (increment, counter));
83
101
child->terminate ();
84
102
delete child;
85
103
}
86
104
105
+ end:
87
106
(*counter)++;
88
107
}
89
108
@@ -126,7 +145,12 @@ void self_terminate(Thread *self) {
126
145
then the final value of the counter is equal to 1
127
146
*/
128
147
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
+
130
154
counter_t counter (0 );
131
155
Thread thread (osPriorityNormal, THREAD_STACK_SIZE);
132
156
thread.start (callback (F, &counter));
@@ -165,7 +189,12 @@ void test_single_thread() {
165
189
then the final value of the counter is equal to number of parallel threads
166
190
*/
167
191
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
+
169
198
counter_t counter (0 );
170
199
ParallelThread<osPriorityNormal, PARALLEL_THREAD_STACK_SIZE> threads[N];
171
200
@@ -213,6 +242,9 @@ void test_parallel_threads() {
213
242
template <int N, void (*F)(counter_t *)>
214
243
void test_serial_threads () {
215
244
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" );
216
248
217
249
for (int i = 0 ; i < N; i++) {
218
250
Thread thread (osPriorityNormal, THREAD_STACK_SIZE);
@@ -229,10 +261,20 @@ void test_serial_threads() {
229
261
when the thread calls @a terminate on its self
230
262
then the thread terminates execution cleanly
231
263
*/
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
+ }
234
274
thread->start (callback (self_terminate, thread));
235
275
thread->join ();
276
+
277
+ end:
236
278
delete thread;
237
279
}
238
280
@@ -290,6 +332,10 @@ void signal_wait_multibit_tout()
290
332
template <int S, void (*F)()>
291
333
void test_thread_signal ()
292
334
{
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
+
293
339
Thread t_wait (osPriorityNormal, THREAD_STACK_SIZE);
294
340
295
341
t_wait.start (callback (F));
@@ -326,6 +372,10 @@ void signal_clr()
326
372
*/
327
373
void test_thread_signal_clr ()
328
374
{
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
+
329
379
Thread t_wait (osPriorityNormal, THREAD_STACK_SIZE);
330
380
331
381
t_wait.start (callback (signal_clr));
@@ -360,7 +410,12 @@ void stack_info() {
360
410
and the reported stack size is as requested in the constructor
361
411
and the sum of free and used stack sizes is equal to the total stack size
362
412
*/
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
+
364
419
Thread t (osPriorityNormal, THREAD_STACK_SIZE);
365
420
t.start (callback (stack_info));
366
421
@@ -409,7 +464,12 @@ void test_thread_wait() {
409
464
when the name is queried using @a get_name
410
465
then the returned name is as set
411
466
*/
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
+
413
473
const char tname[] = " Amazing thread" ;
414
474
Thread t (osPriorityNormal, THREAD_STACK_SIZE, NULL , tname);
415
475
t.start (callback (thread_wait_signal));
@@ -431,6 +491,10 @@ void test_deleted_thread()
431
491
*/
432
492
void test_deleted ()
433
493
{
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
+
434
498
Thread t (osPriorityNormal, THREAD_STACK_SIZE);
435
499
436
500
TEST_ASSERT_EQUAL (Thread::Deleted, t.get_state ());
@@ -454,6 +518,10 @@ void test_delay_thread()
454
518
*/
455
519
void test_delay ()
456
520
{
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
+
457
525
Thread t (osPriorityNormal, THREAD_STACK_SIZE);
458
526
459
527
t.start (callback (test_delay_thread));
@@ -479,6 +547,10 @@ void test_signal_thread()
479
547
*/
480
548
void test_signal ()
481
549
{
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
+
482
554
Thread t (osPriorityNormal, THREAD_STACK_SIZE);
483
555
484
556
t.start (callback (test_signal_thread));
@@ -503,6 +575,10 @@ void test_evt_flag_thread(osEventFlagsId_t evtflg)
503
575
*/
504
576
void test_evt_flag ()
505
577
{
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
+
506
582
Thread t (osPriorityNormal, THREAD_STACK_SIZE);
507
583
mbed_rtos_storage_event_flags_t evtflg_mem;
508
584
osEventFlagsAttr_t evtflg_attr;
@@ -535,6 +611,10 @@ void test_mutex_thread(Mutex *mutex)
535
611
*/
536
612
void test_mutex ()
537
613
{
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
+
538
618
Thread t (osPriorityNormal, THREAD_STACK_SIZE);
539
619
Mutex mutex;
540
620
@@ -562,6 +642,10 @@ void test_semaphore_thread(Semaphore *sem)
562
642
*/
563
643
void test_semaphore ()
564
644
{
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
+
565
649
Thread t (osPriorityNormal, THREAD_STACK_SIZE);
566
650
Semaphore sem;
567
651
@@ -587,6 +671,10 @@ void test_msg_get_thread(Queue<int32_t, 1> *queue)
587
671
*/
588
672
void test_msg_get ()
589
673
{
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
+
590
678
Thread t (osPriorityNormal, THREAD_STACK_SIZE);
591
679
Queue<int32_t , 1 > queue;
592
680
@@ -613,6 +701,10 @@ void test_msg_put_thread(Queue<int32_t, 1> *queue)
613
701
*/
614
702
void test_msg_put ()
615
703
{
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
+
616
708
Thread t (osPriorityNormal, THREAD_STACK_SIZE);
617
709
Queue<int32_t , 1 > queue;
618
710
@@ -660,7 +752,12 @@ void test_thread_ext_stack() {
660
752
when new priority is set using @a set_priority
661
753
then priority is changed and can be retrieved using @a get_priority
662
754
*/
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
+
664
761
Thread t (osPriorityNormal, THREAD_STACK_SIZE);
665
762
t.start (callback (thread_wait_signal));
666
763
0 commit comments