Skip to content

Commit 70522bb

Browse files
Merge pull request #5360 from maciejbocianski/thread_fix
threads test adjust to run on devices with small RAM
2 parents 4e22295 + bc99556 commit 70522bb

File tree

1 file changed

+34
-25
lines changed
  • TESTS/mbedmicro-rtos-mbed/threads

1 file changed

+34
-25
lines changed

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

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,22 @@
2525
#error [NOT_SUPPORTED] test not supported
2626
#endif
2727

28-
#define THREAD_STACK_SIZE 768
28+
#define THREAD_STACK_SIZE 512
29+
#define PARALLEL_THREAD_STACK_SIZE 384
30+
#define CHILD_THREAD_STACK_SIZE 384
2931

3032
using namespace utest::v1;
3133

3234
// The counter type used accross all the tests
3335
// It is internall ysynchronized so read
3436
typedef SynchronizedIntegral<int> counter_t;
3537

38+
template<osPriority P, uint32_t S>
39+
class ParallelThread : public Thread {
40+
public:
41+
ParallelThread() : Thread(P, S) { }
42+
};
43+
3644
// Tasks with different functions to test on threads
3745
void increment(counter_t* counter) {
3846
(*counter)++;
@@ -49,19 +57,21 @@ void increment_with_wait(counter_t* counter) {
4957
}
5058

5159
void increment_with_child(counter_t* counter) {
52-
Thread child(osPriorityNormal, THREAD_STACK_SIZE/2);
53-
child.start(callback(increment, counter));
54-
child.join();
60+
Thread *child = new Thread(osPriorityNormal, CHILD_THREAD_STACK_SIZE);
61+
child->start(callback(increment, counter));
62+
child->join();
63+
delete child;
5564
}
5665

5766
void increment_with_murder(counter_t* counter) {
5867
{
5968
// take ownership of the counter mutex so it prevent the child to
6069
// modify counter.
6170
LockGuard lock(counter->internal_mutex());
62-
Thread child(osPriorityNormal, THREAD_STACK_SIZE);
63-
child.start(callback(increment, counter));
64-
child.terminate();
71+
Thread *child = new Thread(osPriorityNormal, CHILD_THREAD_STACK_SIZE);
72+
child->start(callback(increment, counter));
73+
child->terminate();
74+
delete child;
6575
}
6676

6777
(*counter)++;
@@ -147,16 +157,14 @@ void test_single_thread() {
147157
template <int N, void (*F)(counter_t *)>
148158
void test_parallel_threads() {
149159
counter_t counter(0);
150-
Thread *threads[N];
160+
ParallelThread<osPriorityNormal, PARALLEL_THREAD_STACK_SIZE> threads[N];
151161

152162
for (int i = 0; i < N; i++) {
153-
threads[i] = new Thread(osPriorityNormal, THREAD_STACK_SIZE);
154-
threads[i]->start(callback(F, &counter));
163+
threads[i].start(callback(F, &counter));
155164
}
156165

157166
for (int i = 0; i < N; i++) {
158-
threads[i]->join();
159-
delete threads[i];
167+
threads[i].join();
160168
}
161169

162170
TEST_ASSERT_EQUAL(counter, N);
@@ -272,7 +280,7 @@ void signal_wait_multibit_tout()
272280
template <int S, void (*F)()>
273281
void test_thread_signal()
274282
{
275-
Thread t_wait;
283+
Thread t_wait(osPriorityNormal, THREAD_STACK_SIZE);
276284

277285
t_wait.start(callback(F));
278286

@@ -308,7 +316,7 @@ void signal_clr()
308316
*/
309317
void test_thread_signal_clr()
310318
{
311-
Thread t_wait;
319+
Thread t_wait(osPriorityNormal, THREAD_STACK_SIZE);
312320

313321
t_wait.start(callback(signal_clr));
314322

@@ -413,7 +421,7 @@ void test_deleted_thread()
413421
*/
414422
void test_deleted()
415423
{
416-
Thread t;
424+
Thread t(osPriorityNormal, THREAD_STACK_SIZE);
417425

418426
TEST_ASSERT_EQUAL(Thread::Deleted, t.get_state());
419427

@@ -436,7 +444,7 @@ void test_delay_thread()
436444
*/
437445
void test_delay()
438446
{
439-
Thread t;
447+
Thread t(osPriorityNormal, THREAD_STACK_SIZE);
440448

441449
t.start(callback(test_delay_thread));
442450

@@ -461,7 +469,7 @@ void test_signal_thread()
461469
*/
462470
void test_signal()
463471
{
464-
Thread t;
472+
Thread t(osPriorityNormal, THREAD_STACK_SIZE);
465473

466474
t.start(callback(test_signal_thread));
467475

@@ -485,7 +493,7 @@ void test_evt_flag_thread(osEventFlagsId_t evtflg)
485493
*/
486494
void test_evt_flag()
487495
{
488-
Thread t;
496+
Thread t(osPriorityNormal, THREAD_STACK_SIZE);
489497
mbed_rtos_storage_event_flags_t evtflg_mem;
490498
osEventFlagsAttr_t evtflg_attr;
491499
osEventFlagsId_t evtflg;
@@ -517,7 +525,7 @@ void test_mutex_thread(Mutex *mutex)
517525
*/
518526
void test_mutex()
519527
{
520-
Thread t;
528+
Thread t(osPriorityNormal, THREAD_STACK_SIZE);
521529
Mutex mutex;
522530

523531
mutex.lock();
@@ -544,7 +552,7 @@ void test_semaphore_thread(Semaphore *sem)
544552
*/
545553
void test_semaphore()
546554
{
547-
Thread t;
555+
Thread t(osPriorityNormal, THREAD_STACK_SIZE);
548556
Semaphore sem;
549557

550558
t.start(callback(test_semaphore_thread, &sem));
@@ -569,7 +577,7 @@ void test_msg_get_thread(Queue<int32_t, 1> *queue)
569577
*/
570578
void test_msg_get()
571579
{
572-
Thread t;
580+
Thread t(osPriorityNormal, THREAD_STACK_SIZE);
573581
Queue<int32_t, 1> queue;
574582

575583
t.start(callback(test_msg_get_thread, &queue));
@@ -595,7 +603,7 @@ void test_msg_put_thread(Queue<int32_t, 1> *queue)
595603
*/
596604
void test_msg_put()
597605
{
598-
Thread t;
606+
Thread t(osPriorityNormal, THREAD_STACK_SIZE);
599607
Queue<int32_t, 1> queue;
600608

601609
queue.put((int32_t *)0xE1EE7);
@@ -643,7 +651,7 @@ void test_thread_ext_stack() {
643651
then priority is changed and can be retrieved using @a get_priority
644652
*/
645653
void test_thread_prio() {
646-
Thread t(osPriorityNormal);
654+
Thread t(osPriorityNormal, THREAD_STACK_SIZE);
647655
t.start(callback(thread_wait_signal));
648656

649657
TEST_ASSERT_EQUAL(osPriorityNormal, t.get_priority());
@@ -679,11 +687,11 @@ static const case_t cases[] = {
679687
{"Testing serial threads with wait", test_serial_threads<10, increment_with_wait>, DEFAULT_HANDLERS},
680688

681689
{"Testing single thread with child", test_single_thread<increment_with_child>, DEFAULT_HANDLERS},
682-
{"Testing parallel threads with child", test_parallel_threads<3, increment_with_child>, DEFAULT_HANDLERS},
690+
{"Testing parallel threads with child", test_parallel_threads<2, increment_with_child>, DEFAULT_HANDLERS},
683691
{"Testing serial threads with child", test_serial_threads<10, increment_with_child>, DEFAULT_HANDLERS},
684692

685693
{"Testing single thread with murder", test_single_thread<increment_with_murder>, DEFAULT_HANDLERS},
686-
{"Testing parallel threads with murder", test_parallel_threads<3, increment_with_murder>, DEFAULT_HANDLERS},
694+
{"Testing parallel threads with murder", test_parallel_threads<2, increment_with_murder>, DEFAULT_HANDLERS},
687695
{"Testing serial threads with murder", test_serial_threads<10, increment_with_murder>, DEFAULT_HANDLERS},
688696

689697
{"Testing thread self terminate", test_self_terminate, DEFAULT_HANDLERS},
@@ -710,6 +718,7 @@ static const case_t cases[] = {
710718

711719
{"Testing thread with external stack memory", test_thread_ext_stack, DEFAULT_HANDLERS},
712720
{"Testing thread priority ops", test_thread_prio, DEFAULT_HANDLERS}
721+
713722
};
714723

715724
Specification specification(test_setup, cases);

0 commit comments

Comments
 (0)