Skip to content

Commit 88fb819

Browse files
authored
Merge pull request #3862 from c1728p9/thread_start_assert
Trap earlier when a Thread instance is re-used
2 parents 693f14e + ab4da40 commit 88fb819

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

rtos/Thread.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ namespace rtos {
4444
void Thread::constructor(osPriority priority,
4545
uint32_t stack_size, unsigned char *stack_pointer) {
4646
_tid = 0;
47+
_finished = false;
4748
_dynamic_stack = (stack_pointer == NULL);
4849

4950
#if defined(__MBED_CMSIS_RTOS_CA9) || defined(__MBED_CMSIS_RTOS_CM)
@@ -74,7 +75,7 @@ void Thread::constructor(Callback<void()> task,
7475
osStatus Thread::start(Callback<void()> task) {
7576
_mutex.lock();
7677

77-
if (_tid != 0) {
78+
if ((_tid != 0) || _finished) {
7879
_mutex.unlock();
7980
return osErrorParameter;
8081
}
@@ -117,6 +118,7 @@ osStatus Thread::terminate() {
117118
osThreadId local_id = _tid;
118119
_join_sem.release();
119120
_tid = (osThreadId)NULL;
121+
_finished = true;
120122

121123
ret = osThreadTerminate(local_id);
122124

@@ -177,11 +179,15 @@ int32_t Thread::signal_clr(int32_t signals) {
177179
Thread::State Thread::get_state() {
178180
#if !defined(__MBED_CMSIS_RTOS_CA9) && !defined(__MBED_CMSIS_RTOS_CM)
179181
#ifdef CMSIS_OS_RTX
180-
State status = Deleted;
182+
State status;
181183
_mutex.lock();
182184

183185
if (_tid != NULL) {
184186
status = (State)_thread_def.tcb.state;
187+
} else if (_finished) {
188+
status = Deleted;
189+
} else {
190+
status = Inactive;
185191
}
186192

187193
_mutex.unlock();
@@ -367,6 +373,7 @@ void Thread::_thunk(const void * thread_ptr)
367373
t->_task();
368374
t->_mutex.lock();
369375
t->_tid = (osThreadId)NULL;
376+
t->_finished = true;
370377
t->_join_sem.release();
371378
// rtos will release the mutex automatically
372379
}

rtos/Thread.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ class Thread {
197197
/** Starts a thread executing the specified function.
198198
@param task function to be executed by this thread.
199199
@return status code that indicates the execution status of the function.
200+
@note a thread can only be started once
200201
*/
201202
osStatus start(mbed::Callback<void()> task);
202203

@@ -251,7 +252,7 @@ class Thread {
251252

252253
/** State of the Thread */
253254
enum State {
254-
Inactive, /**< Not created or terminated */
255+
Inactive, /**< Not created */
255256
Ready, /**< Ready to run */
256257
Running, /**< Running */
257258
WaitingDelay, /**< Waiting for a delay to occur */
@@ -330,6 +331,10 @@ class Thread {
330331
virtual ~Thread();
331332

332333
private:
334+
/* disallow copy constructor and assignment operators */
335+
Thread(const Thread&);
336+
Thread& operator=(const Thread&);
337+
333338
// Required to share definitions without
334339
// delegated constructors
335340
void constructor(osPriority priority=osPriorityNormal,
@@ -344,9 +349,10 @@ class Thread {
344349
mbed::Callback<void()> _task;
345350
osThreadId _tid;
346351
osThreadDef_t _thread_def;
347-
bool _dynamic_stack;
348352
Semaphore _join_sem;
349353
Mutex _mutex;
354+
bool _dynamic_stack;
355+
bool _finished;
350356
};
351357

352358
}

0 commit comments

Comments
 (0)