Threadx 塊內存釋放tx_block_release

tx_block_release

應用程序調用tx_block_release釋放分配的內存。
如果tx_block_pool_suspension_list 鏈表有線程掛起,把釋放的塊給掛起線程,並恢復線程。
如果沒有掛起線程等待內存釋放,那麼釋放的內存塊,直接插入空閒內存塊頭部,這樣操作簡單,不需要遍歷鏈表,開銷少。修改內存塊控制字段,指向下一個塊首地址

UINT    _tx_block_release(VOID *block_ptr)
{

    TX_INTERRUPT_SAVE_AREA

    REG_1   TX_BLOCK_POOL   *pool_ptr;          /* Pool pointer            */
    REG_2   TX_THREAD       *thread_ptr;        /* Working thread pointer  */
    REG_3   CHAR_PTR        work_ptr;           /* Working block pointer   */



    /* Disable interrupts to put this block back in the pool.  */
    #def 禁止中斷
    TX_DISABLE

    /* Pickup the pool pointer which is just previous to the starting
       address of the block that the caller sees.  */
    #def 釋放內存首地址後退sizeof(CHAR_PTR)是釋放內存塊的控制字段,存儲了線程池管理結構指針
    work_ptr = ((CHAR_PTR) block_ptr) - sizeof(CHAR_PTR);
    pool_ptr =  *((TX_BLOCK_POOL_PTR *) work_ptr);

    /* Determine if there are any threads suspended on the block pool.  */
    #def 檢查掛起鏈表
    thread_ptr =  pool_ptr -> tx_block_pool_suspension_list;
    if (thread_ptr)
    {
		#def 移除掛起鏈表線程,這裏按照fifo,沒有按照線程優先級高低順序。
        /* Remove the suspended thread from the list.  */

        /* See if this is the only suspended thread on the list.  */
     
        if (thread_ptr == thread_ptr -> tx_suspended_next)
        {

            /* Yes, the only suspended thread.  */

            /* Update the head pointer.  */
            pool_ptr -> tx_block_pool_suspension_list =  TX_NULL;
        }
        else
        {

            /* At least one more thread is on the same expiration list.  */

            /* Update the list head pointer.  */
            pool_ptr -> tx_block_pool_suspension_list =  thread_ptr -> tx_suspended_next;

            /* Update the links of the adjacent threads.  */
            (thread_ptr -> tx_suspended_next) -> tx_suspended_previous =
                thread_ptr -> tx_suspended_previous;
            (thread_ptr -> tx_suspended_previous) -> tx_suspended_next =
                thread_ptr -> tx_suspended_next;
        }

        /* Decrement the suspension count.  */
        pool_ptr -> tx_block_pool_suspended_count--;

        /* Prepare for resumption of the first thread.  */

        /* Clear cleanup routine to avoid timeout.  */
        thread_ptr -> tx_suspend_cleanup =  TX_NULL;

        /* Temporarily disable preemption.  */
        _tx_thread_preempt_disable++;

        /* Restore interrupts.  */
        TX_RESTORE

        /* Return this block pointer to the suspended thread waiting for
           a block.  */
        #def 之前在tx_block_allocate函數中掛起線程時,tx_additional_suspend_info保存了應用程序傳入的存儲內存地址的指針
        #def 所以這裏把申請到的內存賦值給 *tx_additional_suspend_info
        *((CHAR_PTR *) thread_ptr -> tx_additional_suspend_info) = (CHAR_PTR) block_ptr;

        /* Deactivate the timeout timer if necessary.  */
        if (thread_ptr -> tx_thread_timer.tx_list_head)
        {

            /* Deactivate the thread's timeout timer.  */
            _tx_timer_deactivate(&(thread_ptr -> tx_thread_timer));
        }
        else
        {

            /* Clear the remaining time to ensure timer doesn't get activated.  */
            thread_ptr -> tx_thread_timer.tx_remaining_ticks =  0;
        }

        /* Put return status into the thread control block.  */
        #def 設置狀態,tx_suspend_status 用於tx_block_allocate函數返回值
        thread_ptr -> tx_suspend_status =  TX_SUCCESS;

        /* Resume thread.  */
        #def 恢復線程
        if (_tx_thread_resume(thread_ptr))

            /* Preemption is required, transfer control back to
               system.  */
            _tx_thread_system_return();

        /* Return success.  */
        return (TX_SUCCESS);
    }
    else
    {

        /* No thread is suspended for a memory block.  */

        /* Put the block back in the available list.  */
        *((CHAR_PTR *) work_ptr) =  pool_ptr -> tx_block_pool_available_list;

        /* Adjust the head pointer.  */
        pool_ptr -> tx_block_pool_available_list =  work_ptr;

        /* Increment the count of available blocks.  */
        pool_ptr -> tx_block_pool_available++;
    }

    /* Restore interrupts.  */
    TX_RESTORE

    /* Return completion status.  */
    return (TX_SUCCESS);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章