Skip to content

Trap earlier when a Thread instance is re-used #3862

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions rtos/Thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -117,6 +118,7 @@ osStatus Thread::terminate() {
osThreadId local_id = _tid;
_join_sem.release();
_tid = (osThreadId)NULL;
_finished = true;

ret = osThreadTerminate(local_id);

Expand Down Expand Up @@ -177,11 +179,15 @@ int32_t Thread::signal_clr(int32_t signals) {
Thread::State Thread::get_state() {
#if !defined(__MBED_CMSIS_RTOS_CA9) && !defined(__MBED_CMSIS_RTOS_CM)
#ifdef CMSIS_OS_RTX
State status = Deleted;
State status;
_mutex.lock();

if (_tid != NULL) {
status = (State)_thread_def.tcb.state;
} else if (_finished) {
status = Deleted;
} else {
status = Inactive;
}

_mutex.unlock();
Expand Down Expand Up @@ -367,6 +373,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
}
Expand Down
10 changes: 8 additions & 2 deletions rtos/Thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ class Thread {
/** 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);

Expand Down Expand Up @@ -251,7 +252,7 @@ class Thread {

/** State of the Thread */
enum State {
Inactive, /**< Not created or terminated */
Inactive, /**< Not created */
Ready, /**< Ready to run */
Running, /**< Running */
WaitingDelay, /**< Waiting for a delay to occur */
Expand Down Expand Up @@ -330,6 +331,10 @@ class Thread {
virtual ~Thread();

private:
/* disallow copy constructor and assignment operators */
Thread(const Thread&);
Thread& operator=(const Thread&);

// Required to share definitions without
// delegated constructors
void constructor(osPriority priority=osPriorityNormal,
Expand All @@ -344,9 +349,10 @@ class Thread {
mbed::Callback<void()> _task;
osThreadId _tid;
osThreadDef_t _thread_def;
bool _dynamic_stack;
Semaphore _join_sem;
Mutex _mutex;
bool _dynamic_stack;
bool _finished;
};

}
Expand Down