Ejemplo n.º 1
0
/*********************************************************************************************************
** 函数名称: _StackFree
** 功能描述: 回收栈内存
** 输 入  : ptcbDel       任务
**           pstk          栈内存
**           bImmed        立即回收
** 输 出  : NONE
** 全局变量:
** 调用模块:
*********************************************************************************************************/
VOID  _StackFree (PLW_CLASS_TCB  ptcb, PLW_STACK  pstk, BOOL  bImmed)
{
#if LW_CFG_MODULELOADER_EN > 0
    vprocStackFree(ptcb, (PVOID)pstk, bImmed);
#else
    __KHEAP_FREE((PVOID)pstk);
#endif                                                                  /*  LW_CFG_MODULELOADER_EN > 0  */
}
Ejemplo n.º 2
0
ULONG  __threadDelete (PLW_CLASS_TCB  ptcbDel, BOOL  bIsInSafe, 
                       PVOID  pvRetVal, BOOL  bIsAlreadyWaitDeath)
{
             INTREG                iregInterLevel;
    REGISTER UINT16                usIndex;
    REGISTER PLW_CLASS_PCB         ppcbDel;
    REGISTER LW_OBJECT_HANDLE      ulId;
    REGISTER PVOID                 pvFreeLowAddr;                       /*  要释放的内存地址            */
             PVOID                 pvVProc;
             
#if (LW_CFG_EVENTSET_EN > 0) && (LW_CFG_MAX_EVENTSETS > 0)
    REGISTER PLW_EVENTSETNODE      pesnPtr;
#endif

    ulId    = ptcbDel->TCB_ulId;
    usIndex = _ObjectGetIndex(ulId);
    
    if (bIsAlreadyWaitDeath == LW_FALSE) {
        _ThreadDeleteWaitDeath(ptcbDel);                                /*  将要删除的线程进入僵死状态  */
    }
    
    __KERNEL_ENTER();                                                   /*  进入内核                    */
    
    ppcbDel = _GetPcb(ptcbDel);
    
    iregInterLevel = KN_INT_DISABLE();                                  /*  关闭中断                    */
    
    if (ptcbDel->TCB_ptcbDeleteWait) {                                  /*  目标线程正在等待其他任务删除*/
        ptcbDel->TCB_ptcbDeleteWait->TCB_ptcbDeleteMe = (PLW_CLASS_TCB)1;
        ptcbDel->TCB_ptcbDeleteWait = LW_NULL;
    }
    
#if (LW_CFG_EVENT_EN > 0) && (LW_CFG_MAX_EVENTS > 0)
    if (ptcbDel->TCB_peventPtr) {                                       /*  等待事件中                  */
        _EventUnlink(ptcbDel);                                          /*  解等待连                    */
    }
#endif

#if (LW_CFG_EVENTSET_EN > 0) && (LW_CFG_MAX_EVENTSETS > 0)
    pesnPtr = ptcbDel->TCB_pesnPtr;
    if (pesnPtr) {
        _EventSetUnlink(pesnPtr);                                       /*  解事件集                    */
    }
#endif

#if LW_CFG_SMP_EN > 0
    if (ptcbDel->TCB_ptcbWaitStatus ||
        ptcbDel->TCB_plineStatusReqHeader) {                            /*  正在请求其他线程改变状态    */
        _ThreadUnwaitStatus(ptcbDel);
    }
#endif                                                                  /*  LW_CFG_SMP_EN               */
    
    if (__LW_THREAD_IS_READY(ptcbDel)) {                                /*  是否就绪                    */
        __DEL_FROM_READY_RING(ptcbDel, ppcbDel);                        /*  从就绪队列中删除            */
        
    } else {
        if (ptcbDel->TCB_usStatus & LW_THREAD_STATUS_DELAY) {
            __DEL_FROM_WAKEUP_LINE(ptcbDel);                            /*  从等待链中删除              */
            ptcbDel->TCB_ulDelay = 0ul;
        }
        ptcbDel->TCB_usStatus = LW_THREAD_STATUS_RDY;                   /*  防止 Tick 中断激活          */
    }
    
#if LW_CFG_SOFTWARE_WATCHDOG_EN > 0
    if (ptcbDel->TCB_bWatchDogInQ) {
        __DEL_FROM_WATCHDOG_LINE(ptcbDel);                              /*  从 watch dog 中删除         */
        ptcbDel->TCB_ulWatchDog = 0ul;
    }
#endif
    KN_INT_ENABLE(iregInterLevel);
    
    pvFreeLowAddr = (PVOID)ptcbDel->TCB_pstkStackLowAddr;               /*  记录地址                    */
    
#if (LW_CFG_THREAD_PRIVATE_VARS_EN > 0) && (LW_CFG_MAX_THREAD_GLB_VARS > 0)
    _ThreadVarDelete(ptcbDel);                                          /*  删除并恢复私有化的全局变量  */
#endif
    
    iregInterLevel = KN_INT_DISABLE();                                  /*  关闭中断                    */ 
    
    _K_usThreadCounter--;
    _K_ptcbTCBIdTable[usIndex] = LW_NULL;                               /*  TCB 表清0                   */
    
    _List_Line_Del(&ptcbDel->TCB_lineManage, &_K_plineTCBHeader);       /*  从管理练表中删除            */
    
    KN_INT_ENABLE(iregInterLevel);                                      /*  打开中断                    */
    
    if (ptcbDel->TCB_ptcbJoin) {
        _ThreadDisjoin(ptcbDel->TCB_ptcbJoin, ptcbDel);                 /*  退出 join 状态, 不操作就绪表*/
    }
    
    _ThreadDisjoinWakeupAll(ptcbDel, pvRetVal);                         /*  DETACH                      */
    
    __KERNEL_EXIT();                                                    /*  退出内核                    */
    
#if LW_CFG_COROUTINE_EN > 0
    _CoroutineFreeAll(ptcbDel);                                         /*  删除协程内存空间            */
#endif                                                                  /*  LW_CFG_COROUTINE_EN > 0     */
    
    if (bIsInSafe) {
        _DebugFormat(__ERRORMESSAGE_LEVEL, 
                     "thread \"%s\" has been delete in SAFE mode.\r\n",
                     ptcbDel->TCB_cThreadName);
    } else {
        _DebugFormat(__LOGMESSAGE_LEVEL, 
                     "thread \"%s\" has been delete.\r\n",
                     ptcbDel->TCB_cThreadName);
    }
    
    pvVProc = ptcbDel->TCB_pvVProcessContext;                           /*  进程信息                    */
    
    if (ptcbDel->TCB_ucStackAutoAllocFlag) {                            /*  是否是内核堆开辟堆栈        */
#if LW_CFG_MODULELOADER_EN > 0
        vprocStackFree(ptcbDel, pvFreeLowAddr, LW_FALSE);
#else
        __KHEAP_FREE(pvFreeLowAddr);                                    /*  释放堆栈空间                */
#endif                                                                  /*  LW_CFG_MODULELOADER_EN > 0  */
    }
    
    _TCBDestroy(ptcbDel);                                               /*  销毁 TCB                    */
    
    __KERNEL_MODE_PROC(
        _Free_Tcb_Object(ptcbDel);                                      /*  释放 ID                     */
    );