Skip to content

Commit

Permalink
Fixed #8
Browse files Browse the repository at this point in the history
Moved ``sch_unload_current()`` function call to after the memory deallocation.
Because of rearrangement, ``os_thread_delete(0)`` no longer generates function
call to ``thd_delete_static()``. In ``os_thrad_delete(0)`` current thread will
unload itself after releasing memory.
  • Loading branch information
John Buyi Yu committed Nov 26, 2018
1 parent d18e268 commit 0ded6f1
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions source/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,7 @@ UTIL_SAFE
void os_thread_delete( os_handle_t h_thread )
{
thd_cblk_t *p_thd;
mblk_t *p_mblk;

UTIL_LOCK_EVERYTHING();

Expand All @@ -972,7 +973,25 @@ void os_thread_delete( os_handle_t h_thread )
*/
UTIL_ASSERT(p_thd != NULL);

thd_delete_static( p_thd, &g_sch);
p_thd->state = THD_STATE_DELETED;

/* remove scheduling item */
if( p_thd->item_sch.p_q != NULL )
sch_qitem_remove( &p_thd->item_sch );

/* remove delay item */
if( p_thd->item_delay.p_q != NULL )
sch_qitem_remove( &p_thd->item_delay );

/* release memory */
while( p_thd->mlst.p_head != NULL )
{
p_mblk = p_thd->mlst.p_head;
mlst_remove( p_mblk );
mpool_insert( p_mblk, &g_mpool );
}

p_thd->p_schinfo = NULL;

/*
* If failed:
Expand All @@ -984,9 +1003,11 @@ void os_thread_delete( os_handle_t h_thread )
mpool_free(p_thd->p_stack, &g_mpool);
mpool_free(p_thd, &g_mpool);

/*
* reschedule won't happen until this function returns
*/
if( p_thd == g_sch.p_current )
{
sch_unload_current(&g_sch);
}

UTIL_UNLOCK_EVERYTHING();
}

Expand Down

0 comments on commit 0ded6f1

Please sign in to comment.