mirror of https://github.com/ARMmbed/mbed-os.git
rtos: Return an error when a Thread is re-used
Calling Thread::start multiple times leads to undefined behavior since the Thread class was not designed to handle being restarted. Return an error code if Thread::start is called a second time to prevent this behavior.pull/4025/head
parent
bb6df591f0
commit
edc473a526
|
@ -44,6 +44,7 @@ namespace rtos {
|
|||
void Thread::constructor(osPriority priority,
|
||||
uint32_t stack_size, unsigned char *stack_pointer) {
|
||||
_tid = 0;
|
||||
_finished = false;
|
||||
_dynamic_stack = (stack_pointer == NULL);
|
||||
|
||||
#if defined(__MBED_CMSIS_RTOS_CA9) || defined(__MBED_CMSIS_RTOS_CM)
|
||||
|
@ -74,7 +75,7 @@ void Thread::constructor(Callback<void()> task,
|
|||
osStatus Thread::start(Callback<void()> task) {
|
||||
_mutex.lock();
|
||||
|
||||
if (_tid != 0) {
|
||||
if ((_tid != 0) || _finished) {
|
||||
_mutex.unlock();
|
||||
return osErrorParameter;
|
||||
}
|
||||
|
@ -117,6 +118,7 @@ osStatus Thread::terminate() {
|
|||
osThreadId local_id = _tid;
|
||||
_join_sem.release();
|
||||
_tid = (osThreadId)NULL;
|
||||
_finished = true;
|
||||
|
||||
ret = osThreadTerminate(local_id);
|
||||
|
||||
|
@ -367,6 +369,7 @@ void Thread::_thunk(const void * thread_ptr)
|
|||
t->_task();
|
||||
t->_mutex.lock();
|
||||
t->_tid = (osThreadId)NULL;
|
||||
t->_finished = true;
|
||||
t->_join_sem.release();
|
||||
// rtos will release the mutex automatically
|
||||
}
|
||||
|
|
|
@ -197,6 +197,7 @@ public:
|
|||
/** Starts a thread executing the specified function.
|
||||
@param task function to be executed by this thread.
|
||||
@return status code that indicates the execution status of the function.
|
||||
@note a thread can only be started once
|
||||
*/
|
||||
osStatus start(mbed::Callback<void()> task);
|
||||
|
||||
|
@ -344,9 +345,10 @@ private:
|
|||
mbed::Callback<void()> _task;
|
||||
osThreadId _tid;
|
||||
osThreadDef_t _thread_def;
|
||||
bool _dynamic_stack;
|
||||
Semaphore _join_sem;
|
||||
Mutex _mutex;
|
||||
bool _dynamic_stack;
|
||||
bool _finished;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue