Skip to content

Commit 988d165

Browse files
authored
Merge pull request #15059 from LDong-Arm/bd_greentea_fixes
Fix Thread::start() and general_block_device test's thread allocation/deallocation
2 parents 3031898 + 0b868d5 commit 988d165

File tree

3 files changed

+18
-19
lines changed

3 files changed

+18
-19
lines changed

rtos/include/rtos/Thread.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ class Thread : private mbed::NonCopyable<Thread> {
131131

132132
/** Starts a thread executing the specified function.
133133
@param task function to be executed by this thread.
134-
@return status code that indicates the execution status of the function.
134+
@return status code that indicates the execution status of the function,
135+
or osErrorNoMemory if stack allocation failed.
135136
@note a thread can only be started once
136137
137138
@note You cannot call this function ISR context.

rtos/source/Thread.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,11 @@ osStatus Thread::start(mbed::Callback<void()> task)
8181
}
8282

8383
if (_attr.stack_mem == nullptr) {
84-
_attr.stack_mem = new uint32_t[_attr.stack_size / sizeof(uint32_t)];
85-
MBED_ASSERT(_attr.stack_mem != nullptr);
84+
_attr.stack_mem = new (std::nothrow) uint32_t[_attr.stack_size / sizeof(uint32_t)];
85+
if (_attr.stack_mem == nullptr) {
86+
_mutex.unlock();
87+
return osErrorNoMemory;
88+
}
8689
}
8790

8891
//Fill the stack with a magic word for maximum usage checking

storage/blockdevice/tests/TESTS/blockdevice/general_block_device/main.cpp

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -421,40 +421,35 @@ void test_multi_threads()
421421

422422
osStatus threadStatus;
423423
int i_ind, j_ind;
424-
char *dummy;
425424

426-
rtos::Thread **bd_thread = new (std::nothrow) rtos::Thread*[TEST_NUM_OF_THREADS];
427-
TEST_SKIP_UNLESS_MESSAGE((*bd_thread) != NULL, "not enough heap to run test.");
428-
memset(bd_thread, 0, TEST_NUM_OF_THREADS * sizeof(rtos::Thread *));
425+
rtos::Thread *bd_thread[TEST_NUM_OF_THREADS] {};
429426

430427
for (i_ind = 0; i_ind < TEST_NUM_OF_THREADS; i_ind++) {
431428

432429
bd_thread[i_ind] = new (std::nothrow) rtos::Thread((osPriority_t)((int)osPriorityNormal), TEST_THREAD_STACK_SIZE);
433-
dummy = new (std::nothrow) char[TEST_THREAD_STACK_SIZE];
434430

435-
if (!bd_thread[i_ind] || !dummy) {
436-
utest_printf("Not enough heap to run Thread %d !\n", i_ind + 1);
431+
if (!bd_thread[i_ind]) {
432+
utest_printf("Not enough heap to create Thread %d\n", i_ind + 1);
437433
break;
438434
}
439-
delete[] dummy;
440435

441436
threadStatus = bd_thread[i_ind]->start(callback(test_thread_job));
442-
if (threadStatus != 0) {
443-
utest_printf("Thread %d Start Failed!\n", i_ind + 1);
437+
if (threadStatus == osErrorNoMemory) {
438+
utest_printf("Not enough heap to start Thread %d\n", i_ind + 1);
439+
} else if (threadStatus != osOK) {
440+
utest_printf("Thread %d failed to start: %d\n", i_ind + 1, threadStatus);
444441
break;
445442
}
446443
}
447444

445+
// Join threads that successfully started
448446
for (j_ind = 0; j_ind < i_ind; j_ind++) {
449447
bd_thread[j_ind]->join();
450448
}
451449

452-
if (bd_thread) {
453-
for (j_ind = 0; j_ind < i_ind; j_ind++) {
454-
delete bd_thread[j_ind];
455-
}
456-
457-
delete[] bd_thread;
450+
// Delete all threads, even those that failed to start
451+
for (j_ind = 0; j_ind < TEST_NUM_OF_THREADS; j_ind++) {
452+
delete bd_thread[j_ind];
458453
}
459454
}
460455
#endif

0 commit comments

Comments
 (0)