Skip to content
Open
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
1 change: 1 addition & 0 deletions include/rtdef.h
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,7 @@ struct rt_thread
#endif /* RT_USING_MEM_PROTECTION */

struct rt_spinlock spinlock;
rt_uint8_t defunct; /**< cleanup claimed, protected by spinlock */
rt_ubase_t user_data; /**< private user data beyond this thread */
};
typedef struct rt_thread *rt_thread_t;
Expand Down
43 changes: 37 additions & 6 deletions src/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,26 @@ static void _thread_detach_from_mutex(rt_thread_t thread)
static void _thread_detach_from_mutex(rt_thread_t thread) {}
#endif

static rt_err_t _thread_mark_defunct(rt_thread_t thread)
{
rt_err_t error = -RT_ERROR;
rt_base_t level;

/*
* Scheduler bit-fields use different locking, so this claim has separate
* storage. Only thread initialization clears it for a new lifetime.
*/
level = rt_spin_lock_irqsave(&thread->spinlock);
if (!thread->defunct)
{
thread->defunct = RT_TRUE;
error = RT_EOK;
}
rt_spin_unlock_irqrestore(&thread->spinlock, level);

return error;
}

static void _thread_exit(void)
{
struct rt_thread *thread;
Expand All @@ -126,10 +146,13 @@ static void _thread_exit(void)

rt_thread_close(thread);

_thread_detach_from_mutex(thread);
if (_thread_mark_defunct(thread) == RT_EOK)
{
_thread_detach_from_mutex(thread);

/* insert to defunct thread list */
rt_thread_defunct_enqueue(thread);
/* insert to defunct thread list */
rt_thread_defunct_enqueue(thread);
}

rt_exit_critical_safe(critical_level);

Expand Down Expand Up @@ -228,6 +251,7 @@ static rt_err_t _thread_init(struct rt_thread *thread,

/* error and flags */
thread->error = RT_EOK;
thread->defunct = RT_FALSE;

/* lock init */
#ifdef RT_USING_SMP
Expand Down Expand Up @@ -513,10 +537,17 @@ static rt_err_t _thread_detach(rt_thread_t thread)

error = rt_thread_close(thread);

_thread_detach_from_mutex(thread);
if (error == RT_EOK)
{
error = _thread_mark_defunct(thread);
if (error == RT_EOK)
{
_thread_detach_from_mutex(thread);

/* insert to defunct thread list */
rt_thread_defunct_enqueue(thread);
/* insert to defunct thread list */
rt_thread_defunct_enqueue(thread);
}
}

rt_exit_critical_safe(critical_level);
return error;
Expand Down
Loading
Loading