From 0ded6f16815ca11996a3a1504a271754e4b858ca Mon Sep 17 00:00:00 2001 From: John Buyi Yu Date: Mon, 26 Nov 2018 16:52:19 -0500 Subject: [PATCH] Fixed #8 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. --- source/thread.c | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/source/thread.c b/source/thread.c index 106ed6a..f2c0910 100644 --- a/source/thread.c +++ b/source/thread.c @@ -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(); @@ -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: @@ -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(); }