Merge pull request #3862 from c1728p9/thread_start_assert

Trap earlier when a Thread instance is re-used
pull/3939/head
Anna Bridge 2017-03-14 14:36:39 +00:00 committed by GitHub
commit 88fb819e25
2 changed files with 17 additions and 4 deletions

View File

@ -44,6 +44,7 @@ namespace rtos {
void Thread::constructor(osPriority priority, void Thread::constructor(osPriority priority,
uint32_t stack_size, unsigned char *stack_pointer) { uint32_t stack_size, unsigned char *stack_pointer) {
_tid = 0; _tid = 0;
_finished = false;
_dynamic_stack = (stack_pointer == NULL); _dynamic_stack = (stack_pointer == NULL);
#if defined(__MBED_CMSIS_RTOS_CA9) || defined(__MBED_CMSIS_RTOS_CM) #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) { osStatus Thread::start(Callback<void()> task) {
_mutex.lock(); _mutex.lock();
if (_tid != 0) { if ((_tid != 0) || _finished) {
_mutex.unlock(); _mutex.unlock();
return osErrorParameter; return osErrorParameter;
} }
@ -117,6 +118,7 @@ osStatus Thread::terminate() {
osThreadId local_id = _tid; osThreadId local_id = _tid;
_join_sem.release(); _join_sem.release();
_tid = (osThreadId)NULL; _tid = (osThreadId)NULL;
_finished = true;
ret = osThreadTerminate(local_id); ret = osThreadTerminate(local_id);
@ -177,11 +179,15 @@ int32_t Thread::signal_clr(int32_t signals) {
Thread::State Thread::get_state() { Thread::State Thread::get_state() {
#if !defined(__MBED_CMSIS_RTOS_CA9) && !defined(__MBED_CMSIS_RTOS_CM) #if !defined(__MBED_CMSIS_RTOS_CA9) && !defined(__MBED_CMSIS_RTOS_CM)
#ifdef CMSIS_OS_RTX #ifdef CMSIS_OS_RTX
State status = Deleted; State status;
_mutex.lock(); _mutex.lock();
if (_tid != NULL) { if (_tid != NULL) {
status = (State)_thread_def.tcb.state; status = (State)_thread_def.tcb.state;
} else if (_finished) {
status = Deleted;
} else {
status = Inactive;
} }
_mutex.unlock(); _mutex.unlock();
@ -367,6 +373,7 @@ void Thread::_thunk(const void * thread_ptr)
t->_task(); t->_task();
t->_mutex.lock(); t->_mutex.lock();
t->_tid = (osThreadId)NULL; t->_tid = (osThreadId)NULL;
t->_finished = true;
t->_join_sem.release(); t->_join_sem.release();
// rtos will release the mutex automatically // rtos will release the mutex automatically
} }

View File

@ -197,6 +197,7 @@ public:
/** Starts a thread executing the specified function. /** Starts a thread executing the specified function.
@param task function to be executed by this thread. @param task function to be executed by this thread.
@return status code that indicates the execution status of the function. @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); osStatus start(mbed::Callback<void()> task);
@ -251,7 +252,7 @@ public:
/** State of the Thread */ /** State of the Thread */
enum State { enum State {
Inactive, /**< Not created or terminated */ Inactive, /**< Not created */
Ready, /**< Ready to run */ Ready, /**< Ready to run */
Running, /**< Running */ Running, /**< Running */
WaitingDelay, /**< Waiting for a delay to occur */ WaitingDelay, /**< Waiting for a delay to occur */
@ -330,6 +331,10 @@ public:
virtual ~Thread(); virtual ~Thread();
private: private:
/* disallow copy constructor and assignment operators */
Thread(const Thread&);
Thread& operator=(const Thread&);
// Required to share definitions without // Required to share definitions without
// delegated constructors // delegated constructors
void constructor(osPriority priority=osPriorityNormal, void constructor(osPriority priority=osPriorityNormal,
@ -344,9 +349,10 @@ private:
mbed::Callback<void()> _task; mbed::Callback<void()> _task;
osThreadId _tid; osThreadId _tid;
osThreadDef_t _thread_def; osThreadDef_t _thread_def;
bool _dynamic_stack;
Semaphore _join_sem; Semaphore _join_sem;
Mutex _mutex; Mutex _mutex;
bool _dynamic_stack;
bool _finished;
}; };
} }