Example #1
0
/*********************************************************************************************************
** 函数名称: archSpinLock
** 功能描述: spinlock 上锁
** 输 入  : psl        spinlock 指针
** 输 出  : 0: 没有获取
**           1: 正常加锁
** 全局变量: 
** 调用模块: 
*********************************************************************************************************/
INT  archSpinLock (spinlock_t  *psl)
{
    if (psl->SL_pcpuOwner == LW_CPU_GET_CUR()) {
        psl->SL_ulCounter++;
        _BugFormat((psl->SL_ulCounter > 10), LW_TRUE, 
                   "spinlock RECURSIVE %lu!\r\n", psl->SL_ulCounter);
        return  (1);                                                    /*  重复调用                    */
    }
    
    armSpinLock(&psl->SL_sltData);
    
    psl->SL_pcpuOwner = LW_CPU_GET_CUR();                               /*  保存当前 CPU                */
    
    return  (1);                                                        /*  加锁成功                    */
}
Example #2
0
/*********************************************************************************************************
** 函数名称: _SmpSpinTryLock
** 功能描述: 自旋锁尝试加锁操作
** 输 入  : psl           自旋锁
** 输 出  : LW_TRUE 加锁正常 LW_FALSE 加锁失败
** 全局变量:
** 调用模块:
*********************************************************************************************************/
BOOL  _SmpSpinTryLock (spinlock_t *psl)
{
    INTREG          iregInterLevel;
    PLW_CLASS_CPU   pcpuCur;
    INT             iRet;

    iregInterLevel = KN_INT_DISABLE();

    pcpuCur = LW_CPU_GET_CUR();
    if (!pcpuCur->CPU_ulInterNesting) {
        __THREAD_LOCK_INC(pcpuCur->CPU_ptcbTCBCur);                     /*  锁定任务在当前 CPU          */
    }

    LW_CPU_SPIN_NESTING_INC(pcpuCur);

    iRet = __ARCH_SPIN_TRYLOCK(psl);
    KN_SMP_MB();

    if (iRet != LW_SPIN_OK) {
        if (!pcpuCur->CPU_ulInterNesting) {
            __THREAD_LOCK_DEC(pcpuCur->CPU_ptcbTCBCur);                 /*  解锁失败, 解除任务锁定      */
        }

        LW_CPU_SPIN_NESTING_DEC(pcpuCur);
    }

    KN_INT_ENABLE(iregInterLevel);

    return  ((iRet == LW_SPIN_OK) ? (LW_TRUE) : (LW_FALSE));
}
Example #3
0
/*********************************************************************************************************
** 函数名称: API_GetLastError
** 功能描述: 获得系统最近一个错误
** 输 入  : 
** 输 出  : ERROR CODE
** 全局变量: 
** 调用模块: 
                                           API 函数
*********************************************************************************************************/
LW_API  
ULONG  API_GetLastError (VOID)
{
    INTREG          iregInterLevel;
    PLW_CLASS_CPU   pcpuCur;
    PLW_CLASS_TCB   ptcbCur;
    ULONG           ulLastError;
    
    iregInterLevel = KN_INT_DISABLE();                                  /*  关闭中断, 防止调度到其他 CPU*/
    
    pcpuCur = LW_CPU_GET_CUR();
    if (pcpuCur->CPU_ulInterNesting) {
        ulLastError = pcpuCur->CPU_ulInterError[pcpuCur->CPU_ulInterNesting];
    
    } else {
        ptcbCur = pcpuCur->CPU_ptcbTCBCur;
        if (ptcbCur) {
            ulLastError = ptcbCur->TCB_ulLastError;
        } else {
            ulLastError = _K_ulNotRunError;
        }
    }
    
    KN_INT_ENABLE(iregInterLevel);

    return  (ulLastError);
}
Example #4
0
/*********************************************************************************************************
** 函数名称: _SmpSpinUnlockTask
** 功能描述: 自旋锁原始解锁操作. (不锁定中断, 同时允许加锁后调用可能产生阻塞的操作)
** 输 入  : psl               自旋锁
** 输 出  : 调度器返回值
** 全局变量:
** 调用模块:
*********************************************************************************************************/
INT  _SmpSpinUnlockTask (spinlock_t *psl)
{
    INTREG          iregInterLevel;
    PLW_CLASS_CPU   pcpuCur;
    PLW_CLASS_TCB   ptcbCur;
    BOOL            bTrySched = LW_FALSE;

    iregInterLevel = KN_INT_DISABLE();

    KN_SMP_MB();
    __ARCH_SPIN_UNLOCK_RAW(psl);

    pcpuCur = LW_CPU_GET_CUR();
    _BugHandle(pcpuCur->CPU_ulInterNesting, LW_TRUE, "called from ISR.\r\n");

    ptcbCur = pcpuCur->CPU_ptcbTCBCur;
    __THREAD_LOCK_DEC(ptcbCur);                                         /*  解除任务锁定                */
    if (__ISNEED_SCHED(pcpuCur, 0)) {
        bTrySched = LW_TRUE;                                            /*  需要尝试调度                */
    }

    KN_INT_ENABLE(iregInterLevel);

    if (bTrySched) {
        return  (_ThreadSched(ptcbCur));

    } else {
        return  (ERROR_NONE);
    }
}
Example #5
0
/*********************************************************************************************************
** 函数名称: _SmpSpinUnlockIrq
** 功能描述: 自旋锁解锁操作, 连同解锁中断
** 输 入  : psl               自旋锁
**           iregInterLevel    中断锁定信息
** 输 出  : 调度器返回值
** 全局变量:
** 调用模块:
*********************************************************************************************************/
INT  _SmpSpinUnlockIrq (spinlock_t *psl, INTREG  iregInterLevel)
{
    PLW_CLASS_CPU   pcpuCur;
    PLW_CLASS_TCB   ptcbCur;
    BOOL            bTrySched = LW_FALSE;
    INT             iRet;

    KN_SMP_MB();
    iRet = __ARCH_SPIN_UNLOCK(psl);
    _BugFormat((iRet != LW_SPIN_OK), LW_TRUE, "unlock error 0x%p!\r\n", psl);

    pcpuCur = LW_CPU_GET_CUR();
    if (!pcpuCur->CPU_ulInterNesting) {
        ptcbCur = pcpuCur->CPU_ptcbTCBCur;
        __THREAD_LOCK_DEC(ptcbCur);                                     /*  解除任务锁定                */
        if (__ISNEED_SCHED(pcpuCur, 0)) {
            bTrySched = LW_TRUE;                                        /*  需要尝试调度                */
        }
    }

    LW_CPU_SPIN_NESTING_DEC(pcpuCur);

    KN_INT_ENABLE(iregInterLevel);

    if (bTrySched) {
        return  (_ThreadSched(ptcbCur));

    } else {
        return  (ERROR_NONE);
    }
}
Example #6
0
/*********************************************************************************************************
** 函数名称: __errno
** 功能描述: posix 获得当前 errno
** 输 入  : NONE
** 输 出  : errno
** 全局变量: 
** 调用模块: 
** 注  意  : 由于 longwing 由于历史原因采用 ulong 保存错误编号, 而 posix 使用 errno_t 类型, 而绝大多数系统
             将 errno_t 定义为 int 型, 假设使用 GCC 3.x 以上版本带有 -fstrict-aliasing 参数.这里的代码可能
             会产生一个警告: strict aliasing, 目前将此警告忽略处理.
*********************************************************************************************************/
errno_t *__errno (VOID)
{
    INTREG          iregInterLevel;
    PLW_CLASS_CPU   pcpuCur;
    PLW_CLASS_TCB   ptcbCur;
    errno_t        *perrno;
    
    iregInterLevel = KN_INT_DISABLE();                                  /*  关闭中断, 防止调度到其他 CPU*/
    
    pcpuCur = LW_CPU_GET_CUR();
    if (pcpuCur->CPU_ulInterNesting) {
        perrno = (errno_t *)(&pcpuCur->CPU_ulInterError[pcpuCur->CPU_ulInterNesting]);
    
    } else {
        ptcbCur = pcpuCur->CPU_ptcbTCBCur;
        if (ptcbCur) {
            perrno = (errno_t *)(&ptcbCur->TCB_ulLastError);
        } else {
            perrno = (errno_t *)(&_K_ulNotRunError);
        }
    }
    
    KN_INT_ENABLE(iregInterLevel);
    
    return  (perrno);
}
Example #7
0
/*********************************************************************************************************
** 函数名称: _ErrorHandle
** 功能描述: 记录当前错误号
** 输 入  : ulErrorCode       当前错误号
** 输 出  : NONE
** 全局变量: 
** 调用模块: 
** 注  意  : 
*********************************************************************************************************/
VOID  _ErrorHandle (ULONG  ulErrorCode)
{
    INTREG          iregInterLevel;
    PLW_CLASS_CPU   pcpuCur;
    PLW_CLASS_TCB   ptcbCur;
    
#if LW_CFG_ERRORNO_AUTO_CLEAR == 0
    if (ulErrorCode == 0) {
        return;
    }
#endif

    iregInterLevel = KN_INT_DISABLE();                                  /*  关闭中断, 防止调度到其他 CPU*/
    
    pcpuCur = LW_CPU_GET_CUR();
    if (pcpuCur->CPU_ulInterNesting) {
        pcpuCur->CPU_ulInterError[pcpuCur->CPU_ulInterNesting] = ulErrorCode;
    
    } else {
        ptcbCur = pcpuCur->CPU_ptcbTCBCur;
        if (ptcbCur) {
            ptcbCur->TCB_ulLastError = ulErrorCode;
        } else {
            _K_ulNotRunError = ulErrorCode;
        }
    }
    
    KN_INT_ENABLE(iregInterLevel);
}
Example #8
0
/*********************************************************************************************************
** 函数名称: _UpSpinUnlock
** 功能描述: 自旋锁解锁操作
** 输 入  : psl           自旋锁
** 输 出  : 调度器返回值
** 全局变量: 
** 调用模块: 
*********************************************************************************************************/
INT  _UpSpinUnlock (spinlock_t *psl)
{
    INTREG          iregInterLevel;
    PLW_CLASS_CPU   pcpuCur   = LW_CPU_GET_CUR();
    BOOL            bTrySched = LW_FALSE;
    
    iregInterLevel = KN_INT_DISABLE();
    
    if (!pcpuCur->CPU_ulInterNesting) {
        __THREAD_LOCK_DEC(pcpuCur->CPU_ptcbTCBCur);                     /*  解除任务锁定                */
        if (__COULD_SCHED(pcpuCur, 0)) {
            bTrySched = LW_TRUE;                                        /*  需要尝试调度                */
        }
    }
    
    KN_INT_ENABLE(iregInterLevel);
    
    if (bTrySched) {
        return  (_ThreadSched(pcpuCur->CPU_ptcbTCBCur));
    
    } else {
        return  (ERROR_NONE);
    }
    
    return  (ERROR_NONE);
}
/*********************************************************************************************************
** 函数名称: _UpKernelUnlockIgnIrq
** 功能描述: 内核自旋锁解锁操作, 忽略中断锁定 (必须在中断关闭的状态下被调用)
** 输 入  : NONE
** 输 出  : NONE
** 全局变量: 
** 调用模块: 
*********************************************************************************************************/
VOID  _UpKernelUnlockIgnIrq (VOID)
{
    PLW_CLASS_CPU   pcpuCur = LW_CPU_GET_CUR();
    
    if (!pcpuCur->CPU_ulInterNesting) {
        __THREAD_LOCK_DEC(pcpuCur->CPU_ptcbTCBCur);                     /*  解锁任务在当前 CPU          */
    }
}
Example #10
0
/*********************************************************************************************************
** 函数名称: _UpSpinLock
** 功能描述: 自旋锁加锁操作
** 输 入  : psl           自旋锁
** 输 出  : NONE
** 全局变量: 
** 调用模块: 
*********************************************************************************************************/
VOID  _UpSpinLock (spinlock_t *psl)
{
    PLW_CLASS_CPU   pcpuCur = LW_CPU_GET_CUR();
    
    if (!pcpuCur->CPU_ulInterNesting) {
        __THREAD_LOCK_INC(pcpuCur->CPU_ptcbTCBCur);                     /*  锁定任务在当前 CPU          */
    }
}
/*********************************************************************************************************
** 函数名称: _UpKernelUnlockSched
** 功能描述: 内核 SMP 调度器切换完成后专用释放函数 (关中断状态下被调用)
** 输 入  : ptcbOwner     锁的持有者
** 输 出  : NONE
** 全局变量: 
** 调用模块: 
*********************************************************************************************************/
VOID  _UpKernelUnlockSched (PLW_CLASS_TCB  ptcbOwner)
{
    PLW_CLASS_CPU   pcpuCur = LW_CPU_GET_CUR();
    
    if (!pcpuCur->CPU_ulInterNesting) {
        __THREAD_LOCK_DEC(ptcbOwner);                                   /*  解锁任务在当前 CPU          */
    }
}
Example #12
0
/*********************************************************************************************************
** 函数名称: _UpSpinLockIrq
** 功能描述: 自旋锁加锁操作, 连同锁定中断
** 输 入  : psl               自旋锁
**           piregInterLevel   中断锁定信息
** 输 出  : NONE
** 全局变量: 
** 调用模块: 
*********************************************************************************************************/
VOID  _UpSpinLockIrq (spinlock_t *psl, INTREG  *piregInterLevel)
{
    PLW_CLASS_CPU   pcpuCur = LW_CPU_GET_CUR();
    
    if (!pcpuCur->CPU_ulInterNesting) {
        __THREAD_LOCK_INC(pcpuCur->CPU_ptcbTCBCur);                     /*  锁定任务在当前 CPU          */
    }
    
    *piregInterLevel = KN_INT_DISABLE();
}
/*********************************************************************************************************
** 函数名称: _UpKernelUnlockQuick
** 功能描述: 内核自旋锁解锁操作, 连同解锁中断, 不进行尝试调度
** 输 入  : iregInterLevel    中断锁定信息
** 输 出  : NONE
** 全局变量: 
** 调用模块: 
*********************************************************************************************************/
VOID  _UpKernelUnlockQuick (INTREG  iregInterLevel)
{
    PLW_CLASS_CPU   pcpuCur = LW_CPU_GET_CUR();
    
    if (!pcpuCur->CPU_ulInterNesting) {
        __THREAD_LOCK_DEC(pcpuCur->CPU_ptcbTCBCur);                     /*  解锁任务在当前 CPU          */
    }
    
    KN_INT_ENABLE(iregInterLevel);
}
Example #14
0
/*********************************************************************************************************
** 函数名称: _UpSpinTryLock
** 功能描述: 自旋锁尝试加锁操作
** 输 入  : psl           自旋锁
** 输 出  : LW_TRUE 加锁正常 LW_FALSE 加锁失败
** 全局变量: 
** 调用模块: 
*********************************************************************************************************/
BOOL  _UpSpinTryLock (spinlock_t *psl)
{
    PLW_CLASS_CPU   pcpuCur = LW_CPU_GET_CUR();
    
    if (!pcpuCur->CPU_ulInterNesting) {
        __THREAD_LOCK_INC(pcpuCur->CPU_ptcbTCBCur);                     /*  锁定任务在当前 CPU          */
    }
    
    return  (LW_TRUE);
}
/*********************************************************************************************************
** 函数名称: _UpKernelLockQuick
** 功能描述: 内核自旋锁加锁操作, 连同锁定中断
** 输 入  : piregInterLevel   中断锁定信息
** 输 出  : NONE
** 全局变量: 
** 调用模块: 
*********************************************************************************************************/
VOID  _UpKernelLockQuick (INTREG  *piregInterLevel)
{
    PLW_CLASS_CPU   pcpuCur = LW_CPU_GET_CUR();
    
    if (!pcpuCur->CPU_ulInterNesting) {
        __THREAD_LOCK_INC(pcpuCur->CPU_ptcbTCBCur);                     /*  锁定任务在当前 CPU          */
    }
    
    *piregInterLevel = KN_INT_DISABLE();
}
Example #16
0
LW_API
VOID  API_KernelPrimaryStart (PKERNEL_START_ROUTINE  pfuncStartHook)
#endif                                                                  /*  LW_CFG_MEMORY_HEAP_...      */
{
    INT     iError;
    
    _BugHandle(LW_SYS_STATUS_IS_RUNNING(), LW_TRUE, "kernel is already start.\r\n");
    _BugHandle(LW_NCPUS > LW_CFG_MAX_PROCESSORS, LW_TRUE, "LW_NCPUS > LW_CFG_MAX_PROCESSORS.\r\n");
    _DebugHandle(__LOGMESSAGE_LEVEL, "longwing(TM) kernel initialize...\r\n");
    
#if LW_CFG_MEMORY_HEAP_CONFIG_TYPE > 0
    _BugHandle(!pvKernelHeapMem, LW_TRUE, "heap memory invalidate.\r\n");
    _BugHandle(!_Addresses_Is_Aligned(pvKernelHeapMem), LW_TRUE, "heap memory is not align.\r\n");
    _BugHandle(stKernelHeapSize < LW_CFG_KB_SIZE, LW_TRUE, "heap memory is too little.\r\n");
    
    if (pvSystemHeapMem && stSystemHeapSize) {
        _BugHandle(!pvSystemHeapMem, LW_TRUE, "heap memory invalidate.\r\n");
        _BugHandle(!_Addresses_Is_Aligned(pvSystemHeapMem), LW_TRUE, "heap memory is not align.\r\n");
        _BugHandle(stSystemHeapSize < LW_CFG_KB_SIZE, LW_TRUE, "heap memory is too little.\r\n");
    }
    _KernelPrimaryLowLevelInit(pvKernelHeapMem, stKernelHeapSize,
                               pvSystemHeapMem, stSystemHeapSize);      /*  内核底层初始化              */
#else
    _KernelPrimaryLowLevelInit();                                       /*  内核底层初始化              */
#endif                                                                  /*  LW_CFG_MEMORY_HEAP_...      */
    
    _DebugHandle(__LOGMESSAGE_LEVEL, "kernel interrupt vector initialize...\r\n");
    bspIntInit();                                                       /*  初始化中断系统              */
    
    _KernelHighLevelInit();                                             /*  内核高级初始化              */
    
    iError = _cppRtInit();                                              /*  CPP 运行时库初始化          */
    if (iError != ERROR_NONE) {
        _DebugHandle(__ERRORMESSAGE_LEVEL, "c++ run time lib initialized error!\r\n");
    
    } else {
        _DebugHandle(__LOGMESSAGE_LEVEL, "c++ run time lib initialized.\r\n");
    }
    
    _DebugHandle(__LOGMESSAGE_LEVEL, "kernel primary cpu usrStartup...\r\n");
    if (pfuncStartHook) {                                               /*  用户是否要求需要初始化      */
        pfuncStartHook();                                               /*  用户系统初始化              */
    }
    
#if LW_CFG_MODULELOADER_EN > 0
    _resInit();                                                         /*  从现在开始记录资源情况      */
#endif                                                                  /*  LW_CFG_MODULELOADER_EN > 0  */
    
#if LW_CFG_SMP_EN > 0
    _KernelBootSecondary();                                             /*  从核可以进行初始化操作      */
#endif                                                                  /*  LW_CFG_SMP_EN               */

    _KernelPrimaryEntry(LW_CPU_GET_CUR());                              /*  启动内核                    */
                                                                        /*  多核将在第一次调度被启用    */
}
Example #17
0
/*********************************************************************************************************
** 函数名称: _SmpSpinLockIgnIrq
** 功能描述: 自旋锁加锁操作, 忽略中断锁定 (必须在中断关闭的状态下被调用)
** 输 入  : psl           自旋锁
** 输 出  : NONE
** 全局变量:
** 调用模块:
*********************************************************************************************************/
VOID  _SmpSpinLockIgnIrq (spinlock_t *psl)
{
    PLW_CLASS_CPU   pcpuCur;

    pcpuCur = LW_CPU_GET_CUR();
    if (!pcpuCur->CPU_ulInterNesting) {
        __THREAD_LOCK_INC(pcpuCur->CPU_ptcbTCBCur);                     /*  锁定任务在当前 CPU          */
    }

    LW_CPU_SPIN_NESTING_INC(pcpuCur);

    __ARCH_SPIN_LOCK(psl);                                              /*  驱动保证锁定必须成功        */
    KN_SMP_MB();
}
Example #18
0
VOID  _SchedYield (PLW_CLASS_TCB  ptcb, PLW_CLASS_PCB  ppcb)
{
    UINT8          ucPriority;
    REGISTER PLW_CLASS_CPU  pcpu;

    if (__LW_THREAD_IS_RUNNING(ptcb)) {                                 /*  必须正在执行                */
        pcpu = LW_CPU_GET_CUR();
        if (_SchedSeekPriority(pcpu, &ucPriority) &&                    /*  就绪未运行任务的最高优先级  */
                LW_PRIO_IS_HIGH_OR_EQU(ucPriority,
                                       ptcb->TCB_ucPriority)) {
            ptcb->TCB_usSchedCounter = 0;                               /*  没收剩余时间片              */
            LW_CAND_ROT(LW_CPU_GET(ptcb->TCB_ulCPUId)) = LW_TRUE;       /*  下次调度时检查轮转          */
        }
    }
}
Example #19
0
/*********************************************************************************************************
** 函数名称: _SmpSpinUnlockIgnIrq
** 功能描述: 自旋锁解锁操作, 忽略中断锁定 (必须在中断关闭的状态下被调用)
** 输 入  : psl           自旋锁
** 输 出  : NONE
** 全局变量:
** 调用模块:
*********************************************************************************************************/
VOID  _SmpSpinUnlockIgnIrq (spinlock_t *psl)
{
    PLW_CLASS_CPU   pcpuCur;
    INT             iRet;

    KN_SMP_MB();
    iRet = __ARCH_SPIN_UNLOCK(psl);
    _BugFormat((iRet != LW_SPIN_OK), LW_TRUE, "unlock error 0x%p!\r\n", psl);

    pcpuCur = LW_CPU_GET_CUR();
    if (!pcpuCur->CPU_ulInterNesting) {
        __THREAD_LOCK_DEC(pcpuCur->CPU_ptcbTCBCur);                     /*  解除任务锁定                */
    }

    LW_CPU_SPIN_NESTING_DEC(pcpuCur);
}
Example #20
0
VOID  API_KernelSecondaryStart (PKERNEL_START_ROUTINE  pfuncStartHook)
{
    KN_SMP_MB();
    while (KN_SECONDARY_WAIT()) {
        LW_SPINLOCK_DELAY();                                            /*  短延迟并释放总线            */
    }
    
    _KernelSecondaryLowLevelInit();                                     /*  从核底层初始化              */
    
    _DebugHandle(__LOGMESSAGE_LEVEL, "kernel secondary cpu usrStartup...\r\n");
    if (pfuncStartHook) {                                               /*  用户是否要求需要初始化      */
        pfuncStartHook();                                               /*  用户系统初始化              */
    }

    _KernelSecondaryCoreStartup(LW_CPU_GET_CUR());                      /*  主核初始化完毕直接启动多任务*/
}
Example #21
0
/*********************************************************************************************************
** 函数名称: _SmpSpinLockTask
** 功能描述: 自旋锁原始加锁操作. (不锁定中断, 同时允许加锁后调用可能产生阻塞的操作)
** 输 入  : psl               自旋锁
** 输 出  : NONE
** 全局变量:
** 调用模块:
*********************************************************************************************************/
VOID  _SmpSpinLockTask (spinlock_t *psl)
{
    INTREG          iregInterLevel;
    PLW_CLASS_CPU   pcpuCur;

    iregInterLevel = KN_INT_DISABLE();

    pcpuCur = LW_CPU_GET_CUR();
    _BugHandle(pcpuCur->CPU_ulInterNesting, LW_TRUE, "called from ISR.\r\n");

    __THREAD_LOCK_INC(pcpuCur->CPU_ptcbTCBCur);                         /*  锁定任务在当前 CPU          */

    __ARCH_SPIN_LOCK_RAW(psl);                                          /*  驱动保证锁定必须成功        */
    KN_SMP_MB();

    KN_INT_ENABLE(iregInterLevel);
}
Example #22
0
/*********************************************************************************************************
** 函数名称: archSpinUnlock
** 功能描述: spinlock 解锁
** 输 入  : psl        spinlock 指针
** 输 出  : 0: 没有获取
**           1: 正常解锁
** 全局变量: 
** 调用模块: 
*********************************************************************************************************/
INT  archSpinUnlock (spinlock_t  *psl)
{
    if (psl->SL_pcpuOwner != LW_CPU_GET_CUR()) {
        return  (0);                                                    /*  没有权利释放                */
    }
    
    if (psl->SL_ulCounter) {
        psl->SL_ulCounter--;                                            /*  减少重复调用次数            */
        return  (1);
    }

    psl->SL_pcpuOwner = LW_NULL;                                        /*  没有 CPU 获取               */
    KN_SMP_WMB();
    
    armSpinUnlock(&psl->SL_sltData);                                    /*  解锁                        */
    
    return  (1);
}
Example #23
0
/*********************************************************************************************************
** 函数名称: _SmpSpinLock
** 功能描述: 自旋锁加锁操作
** 输 入  : psl           自旋锁
** 输 出  : NONE
** 全局变量:
** 调用模块:
*********************************************************************************************************/
VOID  _SmpSpinLock (spinlock_t *psl)
{
    INTREG          iregInterLevel;
    PLW_CLASS_CPU   pcpuCur;

    iregInterLevel = KN_INT_DISABLE();

    pcpuCur = LW_CPU_GET_CUR();
    if (!pcpuCur->CPU_ulInterNesting) {
        __THREAD_LOCK_INC(pcpuCur->CPU_ptcbTCBCur);                     /*  锁定任务在当前 CPU          */
    }

    LW_CPU_SPIN_NESTING_INC(pcpuCur);

    __ARCH_SPIN_LOCK(psl);                                              /*  驱动保证锁定必须成功        */
    KN_SMP_MB();

    KN_INT_ENABLE(iregInterLevel);
}
Example #24
0
/*********************************************************************************************************
** 函数名称: API_InterEnter
** 功能描述: 内核中断入口函数 (在关中断的情况下被调用)
** 输 入  : 
** 输 出  : 中断层数
** 全局变量: 
** 调用模块: 
                                           API 函数
*********************************************************************************************************/
LW_API
ULONG    API_InterEnter (VOID)
{
    PLW_CLASS_CPU  pcpu;
    
    pcpu = LW_CPU_GET_CUR();
    if (pcpu->CPU_ulInterNesting != LW_CFG_MAX_INTER_SRC) {
        pcpu->CPU_ulInterNesting++;
    }
    
    KN_SMP_WMB();                                                       /*  等待以上操作完成            */

#if LW_CFG_CPU_FPU_EN > 0
    if (LW_KERN_FPU_EN_GET()) {                                         /*  中断状态允许使用浮点运算    */
        __fpuInterEnter(pcpu);
    }
#endif                                                                  /*  LW_CFG_CPU_FPU_EN > 0       */

    return  (pcpu->CPU_ulInterNesting);
}
Example #25
0
/*********************************************************************************************************
** 函数名称: _SmpSpinTryLockTask
** 功能描述: 自旋锁尝试原始加锁操作. (不锁定中断, 同时允许加锁后调用可能产生阻塞的操作)
** 输 入  : psl               自旋锁
** 输 出  : LW_TRUE 加锁正常 LW_FALSE 加锁失败
** 全局变量:
** 调用模块:
*********************************************************************************************************/
BOOL  _SmpSpinTryLockTask (spinlock_t *psl)
{
    INTREG          iregInterLevel;
    PLW_CLASS_CPU   pcpuCur;
    INT             iRet;

    iregInterLevel = KN_INT_DISABLE();

    pcpuCur = LW_CPU_GET_CUR();
    _BugHandle(pcpuCur->CPU_ulInterNesting, LW_TRUE, "called from ISR.\r\n");

    __THREAD_LOCK_INC(pcpuCur->CPU_ptcbTCBCur);                         /*  锁定任务在当前 CPU          */

    iRet = __ARCH_SPIN_TRYLOCK_RAW(psl);
    KN_SMP_MB();

    if (iRet != LW_SPIN_OK) {
        __THREAD_LOCK_DEC(pcpuCur->CPU_ptcbTCBCur);                     /*  解锁失败, 解除任务锁定      */
    }

    KN_INT_ENABLE(iregInterLevel);

    return  ((iRet == LW_SPIN_OK) ? (LW_TRUE) : (LW_FALSE));
}
Example #26
0
/*********************************************************************************************************
** 函数名称: API_InterExit
** 功能描述: 内核中断出口函数 (在关中断的情况下被调用)
** 输 入  : NONE
** 输 出  : NONE
** 全局变量: 
** 调用模块: 
                                           API 函数
*********************************************************************************************************/
LW_API
VOID    API_InterExit (VOID)
{
    PLW_CLASS_CPU  pcpu;
    
    pcpu = LW_CPU_GET_CUR();
    
#if LW_CFG_INTER_INFO > 0
    if (pcpu->CPU_ulInterNestingMax < pcpu->CPU_ulInterNesting) {
        pcpu->CPU_ulInterNestingMax = pcpu->CPU_ulInterNesting;
    }
#endif                                                                  /*  LW_CFG_INTER_INFO > 0       */

    if (pcpu->CPU_ulInterNesting) {                                     /*  系统中断嵌套层数--          */
        pcpu->CPU_ulInterNesting--;
    }
    
    KN_SMP_WMB();                                                       /*  等待以上操作完成            */
    
    if (pcpu->CPU_ulInterNesting) {                                     /*  查看系统是否在中断嵌套中    */
#if LW_CFG_CPU_FPU_EN > 0                                               /*  恢复上一等级中断 FPU CTX    */
        if (LW_KERN_FPU_EN_GET()) {
            __fpuInterExit(pcpu);
        }
#endif                                                                  /*  LW_CFG_CPU_FPU_EN > 0       */
        return;
    }
    
    __KERNEL_SCHED_INT(pcpu);                                           /*  中断中的调度                */
    
#if LW_CFG_CPU_FPU_EN > 0
    if (LW_KERN_FPU_EN_GET()) {
        __fpuInterExit(pcpu);
    }
#endif                                                                  /*  LW_CFG_CPU_FPU_EN > 0       */
}
Example #27
0
/*********************************************************************************************************
** 函数名称: API_CoroutineExit
** 功能描述: 在当前线程正在执行的协程删除
** 输 入  : NONE
** 输 出  : ERROR
** 全局变量: 
** 调用模块: 
                                           API 函数
*********************************************************************************************************/
LW_API  
ULONG   API_CoroutineExit (VOID)
{
             INTREG                 iregInterLevel;
             
             PLW_CLASS_CPU          pcpuCur;
             PLW_CLASS_TCB          ptcbCur;
    REGISTER PLW_CLASS_COROUTINE    pcrcbExit;
    REGISTER PLW_CLASS_COROUTINE    pcrcbNext;
    REGISTER PLW_LIST_RING          pringNext;
    
    if (!LW_SYS_STATUS_IS_RUNNING()) {                                  /*  系统必须已经启动            */
        _ErrorHandle(ERROR_KERNEL_NOT_RUNNING);
        return  (ERROR_KERNEL_NOT_RUNNING);
    }
    
    if (LW_CPU_GET_CUR_NESTING()) {                                     /*  不能在中断中调用            */
        _DebugHandle(__ERRORMESSAGE_LEVEL, "called from ISR.\r\n");
        _ErrorHandle(ERROR_KERNEL_IN_ISR);
        return  (ERROR_KERNEL_IN_ISR);
    }
    
    LW_TCB_GET_CUR_SAFE(ptcbCur);
    
    pcrcbExit = _LIST_ENTRY(ptcbCur->TCB_pringCoroutineHeader, 
                            LW_CLASS_COROUTINE, 
                            COROUTINE_ringRoutine);                     /*  获得当前协程                */
    
    if (&pcrcbExit->COROUTINE_ringRoutine == 
        _list_ring_get_next(&pcrcbExit->COROUTINE_ringRoutine)) {       /*  仅有这一个协程              */

#if LW_CFG_THREAD_DEL_EN > 0
        API_ThreadExit(LW_NULL);
#endif                                                                  /*  LW_CFG_THREAD_DEL_EN > 0    */
        return  (ERROR_NONE);
    }
    
    pringNext = _list_ring_get_next(&pcrcbExit->COROUTINE_ringRoutine);
    pcrcbNext = _LIST_ENTRY(pringNext, LW_CLASS_COROUTINE, 
                            COROUTINE_ringRoutine);                     /*  获得下一个协程              */

    LW_SPIN_LOCK_QUICK(&ptcbCur->TCB_slLock, &iregInterLevel);
    _List_Ring_Del(&pcrcbExit->COROUTINE_ringRoutine,
                   &ptcbCur->TCB_pringCoroutineHeader);                 /*  从协程表中删除              */
    LW_SPIN_UNLOCK_QUICK(&ptcbCur->TCB_slLock, iregInterLevel);
    
    ptcbCur->TCB_pringCoroutineHeader = pringNext;                      /*  转动到下一个协程            */
    
    MONITOR_EVT_LONG2(MONITOR_EVENT_ID_COROUTINE, MONITOR_EVENT_COROUTINE_DELETE, 
                      ptcbCur->TCB_ulId, pcrcbExit, LW_NULL);
    
    if (pcrcbExit->COROUTINE_bIsNeedFree) {
        _StackFree(ptcbCur,
                   pcrcbExit->COROUTINE_pstkStackLowAddr, LW_TRUE);     /*  释放内存                    */
    }
    
    iregInterLevel = KN_INT_DISABLE();                                  /*  关闭中断                    */
    
    INIT_DUMMY_STACK();
    pcpuCur                = LW_CPU_GET_CUR();
    pcpuCur->CPU_pcrcbCur  = &_K_pcrcbDummy;
    pcpuCur->CPU_pcrcbNext = pcrcbNext;
    archCrtCtxSwitch(LW_CPU_GET_CUR());                                 /*  协程切换                    */
    
    KN_INT_ENABLE(iregInterLevel);                                      /*  打开中断                    */

    return  (ERROR_NONE);                                               /*  理论上是无法运行到这里的    */
}