/********************************************************************************************************* ** 函数名称: API_TShellGetOption ** 功能描述: 获取新的 shell 选项. ** 输 入 : hTShellHandle shell 线程 ** pulOpt shell 选项 ** 输 出 : 错误代码. ** 全局变量: ** 调用模块: API 函数 *********************************************************************************************************/ LW_API INT API_TShellGetOption (LW_OBJECT_HANDLE hTShellHandle, ULONG *pulOpt) { PLW_CLASS_TCB ptcbShell; UINT16 usIndex; if (!pulOpt) { _ErrorHandle(EINVAL); return (PX_ERROR); } usIndex = _ObjectGetIndex(hTShellHandle); #if LW_CFG_ARG_CHK_EN > 0 if (!_ObjectClassOK(hTShellHandle, _OBJECT_THREAD)) { /* 检查 ID 类型有效性 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "thread handle invalidate.\r\n"); _ErrorHandle(ERROR_KERNEL_HANDLE_NULL); return (PX_ERROR); } #endif __KERNEL_ENTER(); /* 进入内核 */ if (_Thread_Invalid(usIndex)) { __KERNEL_EXIT(); /* 退出内核 */ _ErrorHandle(ERROR_KERNEL_HANDLE_NULL); return (PX_ERROR); } ptcbShell = __GET_TCB_FROM_INDEX(usIndex); *pulOpt = __TTINY_SHELL_GET_OPT(ptcbShell); __KERNEL_EXIT(); /* 退出内核 */ return (ERROR_NONE); }
/********************************************************************************************************* ** 函数名称: API_SetLastErrorEx ** 功能描述: 设置指定任务的 errno ** 输 入 : ulId 任务 ID ** ulError 错误号 ** 输 出 : ERROR CODE ** 全局变量: ** 调用模块: API 函数 *********************************************************************************************************/ LW_API ULONG API_SetLastErrorEx (LW_OBJECT_HANDLE ulId, ULONG ulError) { REGISTER UINT16 usIndex; REGISTER PLW_CLASS_TCB ptcb; usIndex = _ObjectGetIndex(ulId); if (!_ObjectClassOK(ulId, _OBJECT_THREAD)) { _ErrorHandle(ERROR_KERNEL_HANDLE_NULL); return (ERROR_KERNEL_HANDLE_NULL); } if (_Thread_Index_Invalid(usIndex)) { /* 检查线程有效性 */ _ErrorHandle(ERROR_THREAD_NULL); return (ERROR_THREAD_NULL); } __KERNEL_ENTER(); /* 进入内核 */ if (_Thread_Invalid(usIndex)) { __KERNEL_EXIT(); /* 退出内核 */ _ErrorHandle(ERROR_THREAD_NULL); return (ERROR_THREAD_NULL); } ptcb = _K_ptcbTCBIdTable[usIndex]; ptcb->TCB_ulLastError = ulError; __KERNEL_EXIT(); /* 退出内核 */ return (ERROR_NONE); }
/********************************************************************************************************* ** 函数名称: API_ThreadJoin ** 功能描述: 线程合并 ** 输 入 : ** ulId 要合并的目的线程句柄 ** ppvRetValAddr 存放线程返回值得地址 ** 输 出 : ID ** 全局变量: ** 调用模块: API 函数 (不得在中断中调用) *********************************************************************************************************/ LW_API ULONG API_ThreadJoin (LW_OBJECT_HANDLE ulId, PVOID *ppvRetValAddr) { REGISTER UINT16 usIndex; REGISTER PLW_CLASS_TCB ptcbCur; REGISTER PLW_CLASS_TCB ptcb; usIndex = _ObjectGetIndex(ulId); 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); #if LW_CFG_ARG_CHK_EN > 0 if (!_ObjectClassOK(ulId, _OBJECT_THREAD)) { /* 检查 ID 类型有效性 */ _ErrorHandle(ERROR_KERNEL_HANDLE_NULL); return (ERROR_KERNEL_HANDLE_NULL); } if (_Thread_Index_Invalid(usIndex)) { /* 检查线程有效性 */ _ErrorHandle(ERROR_THREAD_NULL); return (ERROR_THREAD_NULL); } #endif __THREAD_CANCEL_POINT(); /* 测试取消点 */ __KERNEL_ENTER(); /* 进入内核 */ if (_Thread_Invalid(usIndex)) { __KERNEL_EXIT(); /* 退出内核 */ _ErrorHandle(ERROR_THREAD_NULL); return (ERROR_THREAD_NULL); } ptcb = _K_ptcbTCBIdTable[usIndex]; if (ptcb == ptcbCur) { /* 不能阻塞自己 */ __KERNEL_EXIT(); /* 退出内核 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "thread join self.\r\n"); _ErrorHandle(ERROR_THREAD_JOIN_SELF); return (ERROR_THREAD_JOIN_SELF); } if (ptcb->TCB_bDetachFlag) { __KERNEL_EXIT(); /* 退出内核 */ _ErrorHandle(ERROR_THREAD_DETACHED); return (ERROR_THREAD_DETACHED); } _ThreadJoin(ptcb, ppvRetValAddr); /* 合并 */ __KERNEL_EXIT(); /* 退出内核 */ return (ERROR_NONE); }
/********************************************************************************************************* ** 函数名称: API_ThreadCancel ** 功能描述: 取消一个指定的线程 ** 输 入 : ulId 线程句柄 ** 输 出 : ERROR_NONE or ESRCH ** 全局变量: ** 调用模块: API 函数 *********************************************************************************************************/ LW_API ULONG API_ThreadCancel (LW_OBJECT_HANDLE *pulId) { INTREG iregInterLevel; REGISTER LW_OBJECT_HANDLE ulId; REGISTER UINT16 usIndex; REGISTER PLW_CLASS_TCB ptcbDel; ulId = *pulId; usIndex = _ObjectGetIndex(ulId); #if LW_CFG_ARG_CHK_EN > 0 if (!_ObjectClassOK(ulId, _OBJECT_THREAD)) { /* 检查 ID 类型有效性 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "thread handle invalidate.\r\n"); _ErrorHandle(ERROR_KERNEL_HANDLE_NULL); return (ERROR_KERNEL_HANDLE_NULL); } if (_Thread_Index_Invalid(usIndex)) { /* 检查线程有效性 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "thread handle invalidate.\r\n"); _ErrorHandle(ERROR_THREAD_NULL); return (ERROR_THREAD_NULL); } #endif __KERNEL_ENTER(); /* 进入内核 */ if (_Thread_Invalid(usIndex)) { __KERNEL_EXIT(); /* 退出内核 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "thread handle invalidate.\r\n"); _ErrorHandle(ERROR_THREAD_NULL); return (ERROR_THREAD_NULL); } iregInterLevel = KN_INT_DISABLE(); ptcbDel = _K_ptcbTCBIdTable[usIndex]; if (ptcbDel->TCB_iCancelState == LW_THREAD_CANCEL_ENABLE) { /* 允许 CANCEL */ if (ptcbDel->TCB_iCancelType == LW_THREAD_CANCEL_DEFERRED) { /* 延迟 CANCEL */ ptcbDel->TCB_bCancelRequest = LW_TRUE; /* 目标线程进入下一个 TEST 点 */ __KERNEL_EXIT_IRQ(iregInterLevel); /* 退出内核并打开中断 */ } else { /* 异步取消 */ __KERNEL_EXIT_IRQ(iregInterLevel); /* 退出内核并打开中断 */ #if LW_CFG_SIGNAL_EN > 0 kill(ulId, SIGCANCEL); /* 立即发送取消信号 */ #else return (API_ThreadDelete(ulId, LW_NULL)); #endif /* LW_CFG_SIGNAL_EN > 0 */ } } else { __KERNEL_EXIT_IRQ(iregInterLevel); /* 退出内核并打开中断 */ _ErrorHandle(ERROR_THREAD_DISCANCEL); /* 不允许 CACNCEL */ return (ERROR_THREAD_DISCANCEL); } return (ERROR_NONE); }
LW_API PVOID API_RegionGetAlign (LW_OBJECT_HANDLE ulId, size_t stByteSize, size_t stAlign) { REGISTER PLW_CLASS_HEAP pheap; REGISTER UINT16 usIndex; REGISTER PVOID pvAllocate; usIndex = _ObjectGetIndex(ulId); if (LW_CPU_GET_CUR_NESTING()) { /* 不能在中断中调用 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "called from ISR.\r\n"); _ErrorHandle(ERROR_KERNEL_IN_ISR); return (LW_NULL); } #if LW_CFG_ARG_CHK_EN > 0 if (!stByteSize) { _DebugHandle(__ERRORMESSAGE_LEVEL, "ulByteSize invalidate\r\n"); _ErrorHandle(ERROR_REGION_SIZE); return (LW_NULL); } if (!_ObjectClassOK(ulId, _OBJECT_REGION)) { /* 对象类型检查 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "region handle invalidate.\r\n"); _ErrorHandle(ERROR_KERNEL_HANDLE_NULL); return (LW_NULL); } if ((stAlign < sizeof(PVOID)) || (stAlign & (stAlign - 1))) { /* 检查对齐关系 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "iAlign invalidate.\r\n"); _ErrorHandle(ERROR_REGION_ALIGN); return (LW_NULL); } __KERNEL_ENTER(); /* 进入内核 */ if (_Heap_Index_Invalid(usIndex)) { /* 缓冲区索引检查 */ __KERNEL_EXIT(); /* 退出内核 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "region handle invalidate.\r\n"); _ErrorHandle(ERROR_KERNEL_HANDLE_NULL); return (LW_NULL); } #else __KERNEL_ENTER(); /* 进入内核 */ #endif pheap = &_K_heapBuffer[usIndex]; __KERNEL_EXIT(); /* 退出内核 */ pvAllocate = _HeapAllocateAlign(pheap, stByteSize, stAlign, __func__); if (!pvAllocate) { /* 是否分配成功 */ _ErrorHandle(ERROR_REGION_NOMEM); } return (pvAllocate); }
LW_API ULONG API_RegionAddMem (LW_OBJECT_HANDLE ulId, PVOID pvMem, size_t stByteSize) { REGISTER PLW_CLASS_HEAP pheap; REGISTER UINT16 usIndex; usIndex = _ObjectGetIndex(ulId); if (LW_CPU_GET_CUR_NESTING()) { /* 不能在中断中调用 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "called from ISR.\r\n"); _ErrorHandle(ERROR_KERNEL_IN_ISR); return (ERROR_KERNEL_IN_ISR); } #if LW_CFG_ARG_CHK_EN > 0 if (!pvMem || !_Addresses_Is_Aligned(pvMem)) { /* 检查地址是否对齐 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "pvLowAddr is not aligned.\r\n"); _ErrorHandle(ERROR_KERNEL_MEMORY); return (ERROR_KERNEL_MEMORY); } if (_Heap_ByteSize_Invalid(stByteSize)) { /* 分段太小 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "ulRegionByteSize is too low.\r\n"); _ErrorHandle(ERROR_REGION_SIZE); return (ERROR_REGION_SIZE); } if (!_ObjectClassOK(ulId, _OBJECT_REGION)) { /* 对象类型检查 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "region handle invalidate.\r\n"); _ErrorHandle(ERROR_KERNEL_HANDLE_NULL); return (ERROR_KERNEL_HANDLE_NULL); } __KERNEL_ENTER(); /* 进入内核 */ if (_Heap_Index_Invalid(usIndex)) { /* 缓冲区索引检查 */ __KERNEL_EXIT(); /* 退出内核 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "region handle invalidate.\r\n"); _ErrorHandle(ERROR_KERNEL_HANDLE_NULL); return (ERROR_KERNEL_HANDLE_NULL); } #else __KERNEL_ENTER(); /* 进入内核 */ #endif pheap = &_K_heapBuffer[usIndex]; __KERNEL_EXIT(); /* 退出内核 */ _HeapAddMemory(pheap, pvMem, stByteSize); return (ERROR_NONE); }
LW_API ULONG API_RmsGetName (LW_OBJECT_HANDLE ulId, PCHAR pcName) { REGISTER PLW_CLASS_RMS prms; REGISTER UINT16 usIndex; usIndex = _ObjectGetIndex(ulId); if (LW_CPU_GET_CUR_NESTING()) { /* 不能在中断中调用 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "called from ISR.\r\n"); _ErrorHandle(ERROR_KERNEL_IN_ISR); return (ERROR_KERNEL_IN_ISR); } #if LW_CFG_ARG_CHK_EN > 0 if (!pcName) { _DebugHandle(__ERRORMESSAGE_LEVEL, "name invalidate.\r\n"); _ErrorHandle(ERROR_KERNEL_PNAME_NULL); return (ERROR_KERNEL_PNAME_NULL); } if (!_ObjectClassOK(ulId, _OBJECT_RMS)) { /* 对象类型检查 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "RMS handle invalidate.\r\n"); _ErrorHandle(ERROR_KERNEL_HANDLE_NULL); return (ERROR_KERNEL_HANDLE_NULL); } if (_Rms_Index_Invalid(usIndex)) { /* 缓冲区索引检查 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "RMS handle invalidate.\r\n"); _ErrorHandle(ERROR_KERNEL_HANDLE_NULL); return (ERROR_KERNEL_HANDLE_NULL); } __KERNEL_ENTER(); /* 进入内核 */ if (_Rms_Type_Invalid(usIndex)) { __KERNEL_EXIT(); /* 退出内核 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "RMS handle invalidate.\r\n"); _ErrorHandle(ERROR_RMS_NULL); return (ERROR_RMS_NULL); } #else __KERNEL_ENTER(); /* 进入内核 */ #endif prms = &_K_rmsBuffer[usIndex]; lib_strcpy(pcName, prms->RMS_cRmsName); __KERNEL_EXIT(); /* 退出内核 */ return (ERROR_NONE); }
LW_API ULONG API_SemaphoreCStatus (LW_OBJECT_HANDLE ulId, ULONG *pulCounter, ULONG *pulOption, ULONG *pulThreadBlockNum) { INTREG iregInterLevel; REGISTER UINT16 usIndex; REGISTER PLW_CLASS_EVENT pevent; usIndex = _ObjectGetIndex(ulId); #if LW_CFG_ARG_CHK_EN > 0 if (!_ObjectClassOK(ulId, _OBJECT_SEM_C)) { /* 类型是否正确 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "semaphore handle invalidate.\r\n"); _ErrorHandle(ERROR_KERNEL_HANDLE_NULL); return (ERROR_KERNEL_HANDLE_NULL); } if (_Event_Index_Invalid(usIndex)) { /* 下标是否正正确 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "semaphore handle invalidate.\r\n"); _ErrorHandle(ERROR_KERNEL_HANDLE_NULL); return (ERROR_KERNEL_HANDLE_NULL); } #endif pevent = &_K_eventBuffer[usIndex]; iregInterLevel = __KERNEL_ENTER_IRQ(); /* 进入内核 */ if (_Event_Type_Invalid(usIndex, LW_TYPE_EVENT_SEMC)) { __KERNEL_EXIT_IRQ(iregInterLevel); /* 退出内核 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "semaphore handle invalidate.\r\n"); _ErrorHandle(ERROR_EVENT_TYPE); return (ERROR_EVENT_TYPE); } if (pulCounter) { *pulCounter = pevent->EVENT_ulCounter; } if (pulOption) { *pulOption = pevent->EVENT_ulOption; } if (pulThreadBlockNum) { *pulThreadBlockNum = _EventWaitNum(pevent); } __KERNEL_EXIT_IRQ(iregInterLevel); /* 退出内核 */ return (ERROR_NONE); }
LW_API ULONG API_MsgQueueClear (LW_OBJECT_HANDLE ulId) { INTREG iregInterLevel; REGISTER UINT16 usIndex; REGISTER PLW_CLASS_EVENT pevent; REGISTER PLW_CLASS_MSGQUEUE pmsgqueue; usIndex = _ObjectGetIndex(ulId); #if LW_CFG_ARG_CHK_EN > 0 if (!_ObjectClassOK(ulId, _OBJECT_MSGQUEUE)) { /* 类型是否正确 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "msgqueue handle invalidate.\r\n"); _ErrorHandle(ERROR_KERNEL_HANDLE_NULL); return (ERROR_KERNEL_HANDLE_NULL); } if (_Event_Index_Invalid(usIndex)) { /* 下标是否正正确 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "msgqueue handle invalidate.\r\n"); _ErrorHandle(ERROR_KERNEL_HANDLE_NULL); return (ERROR_KERNEL_HANDLE_NULL); } #endif pevent = &_K_eventBuffer[usIndex]; iregInterLevel = __KERNEL_ENTER_IRQ(); /* 进入内核 */ if (_Event_Type_Invalid(usIndex, LW_TYPE_EVENT_MSGQUEUE)) { __KERNEL_EXIT_IRQ(iregInterLevel); /* 退出内核 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "msgqueue handle invalidate.\r\n"); _ErrorHandle(ERROR_MSGQUEUE_TYPE); return (ERROR_MSGQUEUE_TYPE); } pmsgqueue = (PLW_CLASS_MSGQUEUE)pevent->EVENT_pvPtr; pevent->EVENT_ulCounter = 0ul; pmsgqueue->MSGQUEUE_pucInputPtr = pmsgqueue->MSGQUEUE_pucBufferLowAddr; pmsgqueue->MSGQUEUE_pucOutputPtr = pmsgqueue->MSGQUEUE_pucBufferLowAddr; __KERNEL_EXIT_IRQ(iregInterLevel); /* 退出内核 */ MONITOR_EVT_LONG1(MONITOR_EVENT_ID_MSGQ, MONITOR_EVENT_MSGQ_CLEAR, ulId, LW_NULL); return (ERROR_NONE); }
LW_API ULONG API_EventSetStatus (LW_OBJECT_HANDLE ulId, ULONG *pulEvent, ULONG *pulOption) { INTREG iregInterLevel; REGISTER UINT16 usIndex; REGISTER PLW_CLASS_EVENTSET pes; usIndex = _ObjectGetIndex(ulId); #if LW_CFG_ARG_CHK_EN > 0 if (!_ObjectClassOK(ulId, _OBJECT_EVENT_SET)) { _DebugHandle(__ERRORMESSAGE_LEVEL, "eventset handle invalidate.\r\n"); _ErrorHandle(ERROR_KERNEL_HANDLE_NULL); return (ERROR_KERNEL_HANDLE_NULL); } if (_EventSet_Index_Invalid(usIndex)) { _DebugHandle(__ERRORMESSAGE_LEVEL, "eventset handle invalidate.\r\n"); _ErrorHandle(ERROR_KERNEL_HANDLE_NULL); return (ERROR_KERNEL_HANDLE_NULL); } #endif pes = &_K_esBuffer[usIndex]; iregInterLevel = __KERNEL_ENTER_IRQ(); /* 进入内核 */ if (_EventSet_Type_Invalid(usIndex, LW_TYPE_EVENT_EVENTSET)) { __KERNEL_EXIT_IRQ(iregInterLevel); /* 退出内核 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "eventset handle invalidate.\r\n"); _ErrorHandle(ERROR_EVENTSET_TYPE); return (ERROR_EVENTSET_TYPE); } if (pulEvent) { *pulEvent = pes->EVENTSET_ulEventSets; } if (pulOption) { *pulOption = 0ul; } __KERNEL_EXIT_IRQ(iregInterLevel); /* 退出内核 */ return (ERROR_NONE); }
/********************************************************************************************************* ** 函数名称: API_ThreadIsSafe ** 功能描述: 检测目标线程是否处于安全模式. ** 输 入 : ** ulId 线程句柄 ** 输 出 : ** 全局变量: ** 调用模块: API 函数 *********************************************************************************************************/ LW_API BOOL API_ThreadIsSafe (LW_OBJECT_HANDLE ulId) { REGISTER UINT16 usIndex; REGISTER PLW_CLASS_TCB ptcb; REGISTER BOOL bIsInSafeMode; usIndex = _ObjectGetIndex(ulId); #if LW_CFG_ARG_CHK_EN > 0 if (!_ObjectClassOK(ulId, _OBJECT_THREAD)) { /* 检查 ID 类型有效性 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "thread handle invalidate.\r\n"); _ErrorHandle(ERROR_KERNEL_HANDLE_NULL); return (LW_FALSE); } if (_Thread_Index_Invalid(usIndex)) { /* 检查线程有效性 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "thread handle invalidate.\r\n"); _ErrorHandle(ERROR_THREAD_NULL); return (LW_FALSE); } #endif __KERNEL_ENTER(); /* 进入内核 */ if (_Thread_Invalid(usIndex)) { __KERNEL_EXIT(); /* 退出内核 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "thread handle invalidate.\r\n"); _ErrorHandle(ERROR_THREAD_NULL); return (LW_FALSE); } ptcb = _K_ptcbTCBIdTable[usIndex]; /* 获得 TCB 指针 */ if (ptcb->TCB_ulThreadSafeCounter) { bIsInSafeMode = LW_TRUE; } else { bIsInSafeMode = LW_FALSE; } __KERNEL_EXIT(); /* 退出内核 */ return (bIsInSafeMode); }
/********************************************************************************************************* ** 函数名称: API_ThreadGetSliceEx ** 功能描述: 获得指定线程时间片(扩展接口) ** 输 入 : ** ulId 线程ID ** pusSliceTemp 时间片 ** pusCounter 剩余时间片 ** 输 出 : ** 全局变量: ** 调用模块: API 函数 *********************************************************************************************************/ LW_API ULONG API_ThreadGetSliceEx (LW_OBJECT_HANDLE ulId, UINT16 *pusSliceTemp, UINT16 *pusCounter) { REGISTER UINT16 usIndex; REGISTER PLW_CLASS_TCB ptcb; usIndex = _ObjectGetIndex(ulId); #if LW_CFG_ARG_CHK_EN > 0 if (!_ObjectClassOK(ulId, _OBJECT_THREAD)) { /* 检查 ID 类型有效性 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "thread handle invalidate.\r\n"); _ErrorHandle(ERROR_KERNEL_HANDLE_NULL); return (ERROR_KERNEL_HANDLE_NULL); } if (_Thread_Index_Invalid(usIndex)) { /* 检查线程有效性 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "thread handle invalidate.\r\n"); _ErrorHandle(ERROR_THREAD_NULL); return (ERROR_THREAD_NULL); } #endif __KERNEL_ENTER(); /* 进入内核 */ if (_Thread_Invalid(usIndex)) { __KERNEL_EXIT(); /* 退出内核 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "thread handle invalidate.\r\n"); _ErrorHandle(ERROR_THREAD_NULL); return (ERROR_THREAD_NULL); } ptcb = _K_ptcbTCBIdTable[usIndex]; if (pusSliceTemp) { *pusSliceTemp = ptcb->TCB_usSchedSlice; } if (pusCounter) { *pusCounter = ptcb->TCB_usSchedCounter; } __KERNEL_EXIT(); /* 退出内核 */ return (ERROR_NONE); }
/********************************************************************************************************* ** 函数名称: __resAddHandleHook ** 功能描述: 资源管理器加入一个内核资源 ** 输 入 : ulHandle 资源句柄 ** ulOption 创建选项 ** 输 出 : NONE ** 全局变量: ** 调用模块: *********************************************************************************************************/ static VOID __resAddHandleHook (LW_OBJECT_HANDLE ulHandle, ULONG ulOption) { PLW_RESOURCE_H presh; pid_t pid = getpid(); if (!_ObjectClassOK(ulHandle, _OBJECT_THREAD)) { /* 不是线程 */ if ((ulOption & LW_OPTION_OBJECT_GLOBAL) || (pid == 0)) { /* 全局对象或内核任务建立 */ return; /* 不记录 */ } } __LW_RESH_LOCK(); presh = __resGetHandleBuffer(ulHandle); if (presh) { presh->RESH_ulHandle = ulHandle; presh->RESH_pid = pid; if (ulOption & LW_OPTION_OBJECT_GLOBAL) { presh->RESH_bIsGlobal = LW_TRUE; } else { presh->RESH_bIsGlobal = LW_FALSE; } } __LW_RESH_UNLOCK(); }
/********************************************************************************************************* ** 函数名称: sigqueue ** 功能描述: 发送队列类型信号, 如果是进程, 将发送给其主线程. ** 输 入 : ulId 线程 id 或者 进程号 ** iSigNo 信号 ** sigvalue 信号 value ** 输 出 : ERROR or OK ** 全局变量: ** 调用模块: API 函数 *********************************************************************************************************/ LW_API INT sigqueue (LW_OBJECT_HANDLE ulId, INT iSigNo, const union sigval sigvalue) { REGISTER UINT16 usIndex; REGISTER PLW_CLASS_TCB ptcb; LW_SEND_VAL sendval; #if LW_CFG_MODULELOADER_EN > 0 if (ulId < LW_CFG_MAX_THREADS) { /* 进程号 */ ulId = vprocMainThread((pid_t)ulId); } #endif /* LW_CFG_MODULELOADER_EN > 0 */ usIndex = _ObjectGetIndex(ulId); if (!_ObjectClassOK(ulId, _OBJECT_THREAD)) { /* 检查 ID 类型有效性 */ _ErrorHandle(ESRCH); return (PX_ERROR); } if (_Thread_Index_Invalid(usIndex)) { /* 检查线程有效性 */ _ErrorHandle(ESRCH); return (PX_ERROR); } if (!__issig(iSigNo)) { _ErrorHandle(EINVAL); return (PX_ERROR); } if (LW_CPU_GET_CUR_NESTING() || (ulId == API_ThreadIdSelf())) { _excJobAdd((VOIDFUNCPTR)sigqueue, (PVOID)ulId, (PVOID)iSigNo, (PVOID)sigvalue.sival_ptr, 0, 0, 0); return (ERROR_NONE); } #if LW_CFG_SMP_EN > 0 if (LW_NCPUS > 1) { /* 正工作在 SMP 多核模式 */ if (API_ThreadStop(ulId)) { return (PX_ERROR); } } #endif /* LW_CFG_SMP_EN */ __KERNEL_ENTER(); /* 进入内核 */ if (_Thread_Invalid(usIndex)) { __KERNEL_EXIT(); /* 退出内核 */ _ErrorHandle(ESRCH); return (PX_ERROR); } ptcb = __GET_TCB_FROM_INDEX(usIndex); if (ptcb->TCB_iDeleteProcStatus) { __KERNEL_EXIT(); /* 退出内核 */ _ErrorHandle(ERROR_THREAD_OTHER_DELETE); return (PX_ERROR); } sendval = _doSigQueue(ptcb, iSigNo, sigvalue); #if LW_CFG_SMP_EN > 0 if (LW_NCPUS > 1) { /* 正工作在 SMP 多核模式 */ _ThreadContinue(ptcb, LW_FALSE); /* 在内核状态下唤醒被停止线程 */ } #endif /* LW_CFG_SMP_EN */ __KERNEL_EXIT(); /* 退出内核 */ #if LW_CFG_SIGNALFD_EN > 0 if (sendval == SEND_BLOCK) { _sigfdReadUnblock(ulId, iSigNo); } #endif /* LW_CFG_SIGNALFD_EN > 0 */ return (ERROR_NONE); }
LW_API ULONG API_SemaphoreMPost (LW_OBJECT_HANDLE ulId) { INTREG iregInterLevel; PLW_CLASS_TCB ptcbCur; REGISTER UINT16 usIndex; REGISTER PLW_CLASS_EVENT pevent; REGISTER PLW_CLASS_TCB ptcb; REGISTER PLW_LIST_RING *ppringList; /* 等待队列地址 */ usIndex = _ObjectGetIndex(ulId); 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); /* 当前任务控制块 */ #if LW_CFG_ARG_CHK_EN > 0 if (!_ObjectClassOK(ulId, _OBJECT_SEM_M)) { /* 类型是否正确 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "semaphore handle invalidate.\r\n"); _ErrorHandle(ERROR_KERNEL_HANDLE_NULL); return (ERROR_KERNEL_HANDLE_NULL); } if (_Event_Index_Invalid(usIndex)) { /* 下标是否正正确 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "semaphore handle invalidate.\r\n"); _ErrorHandle(ERROR_KERNEL_HANDLE_NULL); return (ERROR_KERNEL_HANDLE_NULL); } #endif pevent = &_K_eventBuffer[usIndex]; if (!LW_SYS_STATUS_IS_RUNNING()) { /* 系统还没有启动 */ return (ERROR_NONE); } __KERNEL_ENTER(); /* 进入内核 */ if (_Event_Type_Invalid(usIndex, LW_TYPE_EVENT_MUTEX)) { __KERNEL_EXIT(); /* 退出内核 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "semaphore handle invalidate.\r\n"); _ErrorHandle(ERROR_EVENT_TYPE); return (ERROR_EVENT_TYPE); } if (ptcbCur != (PLW_CLASS_TCB)pevent->EVENT_pvTcbOwn) { /* 是否是拥有者 */ __KERNEL_EXIT(); /* 退出内核 */ _ErrorHandle(ERROR_EVENT_NOT_OWN); /* 没有事件所有权 */ return (ERROR_EVENT_NOT_OWN); } if (pevent->EVENT_pvPtr) { /* 检测是否进行了连续调用 */ pevent->EVENT_pvPtr = (PVOID)((ULONG)pevent->EVENT_pvPtr - 1); /* 临时计数器-- */ __KERNEL_EXIT(); /* 退出内核 */ return (ERROR_NONE); } iregInterLevel = KN_INT_DISABLE(); if (_EventWaitNum(pevent)) { if (pevent->EVENT_ulOption & LW_OPTION_WAIT_PRIORITY) { /* 优先级等待队列 */ _EVENT_DEL_Q_PRIORITY(ppringList); /* 检查需要激活的队列 */ /* 激活优先级等待线程 */ ptcb = _EventReadyPriorityLowLevel(pevent, LW_NULL, ppringList); } else { _EVENT_DEL_Q_FIFO(ppringList); /* 检查需要激活的FIFO队列 */ /* 激活FIFO等待线程 */ ptcb = _EventReadyFifoLowLevel(pevent, LW_NULL, ppringList); } KN_INT_ENABLE(iregInterLevel); _EventPrioTryResume(pevent, ptcbCur); /* 尝试返回之前的优先级 */ pevent->EVENT_ulMaxCounter = (ULONG)ptcb->TCB_ucPriority; pevent->EVENT_pvTcbOwn = (PVOID)ptcb; /* 保存线程信息 */ _EventReadyHighLevel(ptcb, LW_THREAD_STATUS_SEM); /* 处理 TCB */ MONITOR_EVT_LONG2(MONITOR_EVENT_ID_SEMM, MONITOR_EVENT_SEM_POST, ulId, ptcb->TCB_ulId, LW_NULL); __KERNEL_EXIT(); /* 退出内核 */ if (pevent->EVENT_ulOption & LW_OPTION_DELETE_SAFE) { /* 退出安全模式 */ _ThreadUnsafeInternal(); } return (ERROR_NONE); } else { /* 没有线程等待 */ KN_INT_ENABLE(iregInterLevel); if (pevent->EVENT_ulCounter == LW_FALSE) { /* 检查是否还有空间加 */ pevent->EVENT_ulCounter = (ULONG)LW_TRUE; pevent->EVENT_ulMaxCounter = LW_PRIO_LOWEST; /* 清空保存信息 */ pevent->EVENT_pvTcbOwn = LW_NULL; __KERNEL_EXIT(); /* 退出内核 */ if (pevent->EVENT_ulOption & LW_OPTION_DELETE_SAFE) { /* 退出安全模式 */ _ThreadUnsafeInternal(); } return (ERROR_NONE); } else { /* 已经满了 */ __KERNEL_EXIT(); /* 退出内核 */ _ErrorHandle(ERROR_EVENT_FULL); return (ERROR_EVENT_FULL); } } }
LW_API ULONG API_MsgQueueReceive (LW_OBJECT_HANDLE ulId, PVOID pvMsgBuffer, size_t stMaxByteSize, size_t *pstMsgLen, ULONG ulTimeout) { INTREG iregInterLevel; PLW_CLASS_TCB ptcbCur; REGISTER UINT16 usIndex; REGISTER PLW_CLASS_EVENT pevent; REGISTER PLW_CLASS_MSGQUEUE pmsgqueue; REGISTER UINT8 ucPriorityIndex; REGISTER PLW_LIST_RING *ppringList; ULONG ulTimeSave; /* 系统事件记录 */ INT iSchedRet; ULONG ulEventOption; /* 事件创建选项 */ size_t stMsgLenTemp; /* 临时记录变量 */ usIndex = _ObjectGetIndex(ulId); 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); /* 当前任务控制块 */ if (pstMsgLen == LW_NULL) { pstMsgLen = &stMsgLenTemp; /* 临时变量记录消息长短 */ } __wait_again: #if LW_CFG_ARG_CHK_EN > 0 if (!pvMsgBuffer || !stMaxByteSize) { _DebugHandle(__ERRORMESSAGE_LEVEL, "pvMsgBuffer invalidate.\r\n"); _ErrorHandle(ERROR_MSGQUEUE_MSG_NULL); return (ERROR_MSGQUEUE_MSG_NULL); } if (!_ObjectClassOK(ulId, _OBJECT_MSGQUEUE)) { _DebugHandle(__ERRORMESSAGE_LEVEL, "msgqueue handle invalidate.\r\n"); _ErrorHandle(ERROR_KERNEL_HANDLE_NULL); return (ERROR_KERNEL_HANDLE_NULL); } if (_Event_Index_Invalid(usIndex)) { _DebugHandle(__ERRORMESSAGE_LEVEL, "msgqueue handle invalidate.\r\n"); _ErrorHandle(ERROR_KERNEL_HANDLE_NULL); return (ERROR_KERNEL_HANDLE_NULL); } #endif pevent = &_K_eventBuffer[usIndex]; iregInterLevel = __KERNEL_ENTER_IRQ(); /* 进入内核 */ if (_Event_Type_Invalid(usIndex, LW_TYPE_EVENT_MSGQUEUE)) { __KERNEL_EXIT_IRQ(iregInterLevel); /* 退出内核 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "msgqueue handle invalidate.\r\n"); _ErrorHandle(ERROR_MSGQUEUE_TYPE); return (ERROR_MSGQUEUE_TYPE); } pmsgqueue = (PLW_CLASS_MSGQUEUE)pevent->EVENT_pvPtr; ptcbCur->TCB_ulRecvOption = LW_OPTION_NOERROR; /* 接收大消息自动截断 */ if (pevent->EVENT_ulCounter) { /* 事件有效 */ pevent->EVENT_ulCounter--; _MsgQueueGetMsg(pmsgqueue, pvMsgBuffer, stMaxByteSize, pstMsgLen); /* 获得消息 */ __KERNEL_EXIT_IRQ(iregInterLevel); /* 退出内核 */ return (ERROR_NONE); } if (ulTimeout == LW_OPTION_NOT_WAIT) { /* 不等待 */ __KERNEL_EXIT_IRQ(iregInterLevel); /* 退出内核 */ _ErrorHandle(ERROR_THREAD_WAIT_TIMEOUT); /* 超时 */ return (ERROR_THREAD_WAIT_TIMEOUT); } ptcbCur->TCB_pstMsgByteSize = pstMsgLen; ptcbCur->TCB_stMaxByteSize = stMaxByteSize; ptcbCur->TCB_pvMsgQueueMessage = pvMsgBuffer; /* 记录信息 */ ptcbCur->TCB_usStatus |= LW_THREAD_STATUS_MSGQUEUE; /* 写状态位,开始等待 */ ptcbCur->TCB_ucWaitTimeout = LW_WAIT_TIME_CLEAR; /* 清空等待时间 */ if (ulTimeout == LW_OPTION_WAIT_INFINITE) { /* 是否是无穷等待 */ ptcbCur->TCB_ulDelay = 0ul; } else { ptcbCur->TCB_ulDelay = ulTimeout; /* 设置超时时间 */ } __KERNEL_TIME_GET_NO_SPINLOCK(ulTimeSave, ULONG); /* 记录系统时间 */ if (pevent->EVENT_ulOption & LW_OPTION_WAIT_PRIORITY) { _EVENT_INDEX_Q_PRIORITY(ptcbCur->TCB_ucPriority, ucPriorityIndex); _EVENT_PRIORITY_Q_PTR(ppringList, ucPriorityIndex); ptcbCur->TCB_ppringPriorityQueue = ppringList; /* 记录等待队列位置 */ _EventWaitPriority(pevent, ppringList); /* 加入优先级等待表 */ } else { /* 按 FIFO 等待 */ _EVENT_FIFO_Q_PTR(ppringList); /* 确定 FIFO 队列的位置 */ _EventWaitFifo(pevent, ppringList); /* 加入 FIFO 等待表 */ } KN_INT_ENABLE(iregInterLevel); /* 使能中断 */ ulEventOption = pevent->EVENT_ulOption; MONITOR_EVT_LONG2(MONITOR_EVENT_ID_MSGQ, MONITOR_EVENT_MSGQ_PEND, ulId, ulTimeout, LW_NULL); iSchedRet = __KERNEL_EXIT(); /* 调度器解锁 */ if (iSchedRet == LW_SIGNAL_EINTR) { if (ulEventOption & LW_OPTION_SIGNAL_INTER) { _ErrorHandle(EINTR); return (EINTR); } ulTimeout = _sigTimeoutRecalc(ulTimeSave, ulTimeout); /* 重新计算超时时间 */ goto __wait_again; } else if (iSchedRet == LW_SIGNAL_RESTART) { ulTimeout = _sigTimeoutRecalc(ulTimeSave, ulTimeout); /* 重新计算超时时间 */ goto __wait_again; } if (ptcbCur->TCB_ucWaitTimeout == LW_WAIT_TIME_OUT) { iregInterLevel = __KERNEL_ENTER_IRQ(); /* 进入内核 */ if (ptcbCur->TCB_ucWaitTimeout == LW_WAIT_TIME_CLEAR) { /* 是否在上面瞬间被激活 */ __KERNEL_EXIT_IRQ(iregInterLevel); /* 退出内核 */ return (ERROR_NONE); } if (pevent->EVENT_ulOption & LW_OPTION_WAIT_PRIORITY) { _EventTimeoutPriority(pevent, ppringList); /* 等待超时恢复 */ } else { _EventTimeoutFifo(pevent, ppringList); /* 等待超时恢复 */ } __KERNEL_EXIT_IRQ(iregInterLevel); /* 退出内核 */ _ErrorHandle(ERROR_THREAD_WAIT_TIMEOUT); /* 超时 */ return (ERROR_THREAD_WAIT_TIMEOUT); } else { if (ptcbCur->TCB_ucIsEventDelete == LW_EVENT_EXIST) { /* 事件是否存在 */ return (ERROR_NONE); } else { _ErrorHandle(ERROR_MSGQUEUE_WAS_DELETED); /* 已经被删除 */ return (ERROR_MSGQUEUE_WAS_DELETED); } } }
LW_API ULONG API_RmsStatus (LW_OBJECT_HANDLE ulId, UINT8 *pucStatus, ULONG *pulTimeLeft, LW_OBJECT_HANDLE *pulOwnerId) { INTREG iregInterLevel; REGISTER PLW_CLASS_RMS prms; REGISTER UINT16 usIndex; usIndex = _ObjectGetIndex(ulId); if (LW_CPU_GET_CUR_NESTING()) { /* 不能在中断中调用 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "called from ISR.\r\n"); _ErrorHandle(ERROR_KERNEL_IN_ISR); return (ERROR_KERNEL_IN_ISR); } #if LW_CFG_ARG_CHK_EN > 0 if (!_ObjectClassOK(ulId, _OBJECT_RMS)) { /* 对象类型检查 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "RMS handle invalidate.\r\n"); _ErrorHandle(ERROR_KERNEL_HANDLE_NULL); return (ERROR_KERNEL_HANDLE_NULL); } if (_Rms_Index_Invalid(usIndex)) { /* 缓冲区索引检查 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "RMS handle invalidate.\r\n"); _ErrorHandle(ERROR_KERNEL_HANDLE_NULL); return (ERROR_KERNEL_HANDLE_NULL); } iregInterLevel = __KERNEL_ENTER_IRQ(); /* 进入内核 */ if (_Rms_Type_Invalid(usIndex)) { __KERNEL_EXIT_IRQ(iregInterLevel); /* 退出内核 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "RMS handle invalidate.\r\n"); _ErrorHandle(ERROR_RMS_NULL); return (ERROR_RMS_NULL); } #else iregInterLevel = __KERNEL_ENTER_IRQ(); /* 进入内核 */ #endif prms = &_K_rmsBuffer[usIndex]; if (pucStatus) { *pucStatus = prms->RMS_ucStatus; } if (prms->RMS_ucStatus == LW_RMS_EXPIRED) { /* 有线程进行周期等待 */ if (pulOwnerId) { *pulOwnerId = prms->RMS_ptcbOwner->TCB_ulId; } if (pulTimeLeft) { _WakeupStatus(&_K_wuDelay, &prms->RMS_ptcbOwner->TCB_wunDelay, pulTimeLeft); } } else { /* 无效状态 */ if (pulOwnerId) { *pulOwnerId = LW_OBJECT_HANDLE_INVALID; } if (pulTimeLeft) { *pulTimeLeft = 0ul; } } __KERNEL_EXIT_IRQ(iregInterLevel); /* 退出内核 */ return (ERROR_NONE); }
LW_API ULONG API_SemaphoreBDelete (LW_OBJECT_HANDLE *pulId) { INTREG iregInterLevel; REGISTER UINT16 usIndex; REGISTER PLW_CLASS_EVENT pevent; REGISTER PLW_CLASS_TCB ptcb; REGISTER PLW_LIST_RING *ppringList; /* 等待队列地址 */ REGISTER LW_OBJECT_HANDLE ulId; ulId = *pulId; usIndex = _ObjectGetIndex(ulId); if (LW_CPU_GET_CUR_NESTING()) { /* 不能在中断中调用 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "called from ISR.\r\n"); _ErrorHandle(ERROR_KERNEL_IN_ISR); return (ERROR_KERNEL_IN_ISR); } #if LW_CFG_ARG_CHK_EN > 0 if (!_ObjectClassOK(ulId, _OBJECT_SEM_B)) { /* 类型是否正确 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "semaphore handle invalidate.\r\n"); _ErrorHandle(ERROR_KERNEL_HANDLE_NULL); return (ERROR_KERNEL_HANDLE_NULL); } if (_Event_Index_Invalid(usIndex)) { /* 下标是否正正确 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "semaphore handle invalidate.\r\n"); _ErrorHandle(ERROR_KERNEL_HANDLE_NULL); return (ERROR_KERNEL_HANDLE_NULL); } #endif pevent = &_K_eventBuffer[usIndex]; iregInterLevel = __KERNEL_ENTER_IRQ(); /* 进入内核 */ if (_Event_Type_Invalid(usIndex, LW_TYPE_EVENT_SEMB)) { __KERNEL_EXIT_IRQ(iregInterLevel); /* 退出内核 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "semaphore handle invalidate.\r\n"); _ErrorHandle(ERROR_EVENT_TYPE); return (ERROR_EVENT_TYPE); } _ObjectCloseId(pulId); /* 清除句柄 */ pevent->EVENT_ucType = LW_TYPE_EVENT_UNUSED; /* 事件类型为空 */ while (_EventWaitNum(pevent)) { /* 是否存在正在等待的任务 */ if (pevent->EVENT_ulOption & LW_OPTION_WAIT_PRIORITY) { /* 优先级等待队列 */ _EVENT_DEL_Q_PRIORITY(ppringList); /* 检查需要激活的队列 */ /* 激活优先级等待线程 */ ptcb = _EventReadyPriorityLowLevel(pevent, LW_NULL, ppringList); } else { _EVENT_DEL_Q_FIFO(ppringList); /* 查找 FIFO 队列指针地址 */ /* 激活FIFO等待线程 */ ptcb = _EventReadyFifoLowLevel(pevent, LW_NULL, ppringList); } KN_INT_ENABLE(iregInterLevel); /* 打开中断 */ ptcb->TCB_ucIsEventDelete = LW_EVENT_DELETE; /* 事件已经被删除 */ _EventReadyHighLevel(ptcb, LW_THREAD_STATUS_SEM); /* 处理 TCB */ iregInterLevel = KN_INT_DISABLE(); /* 关闭中断 */ } pevent->EVENT_pvTcbOwn = LW_NULL; _Free_Event_Object(pevent); /* 交还控制块 */ __KERNEL_EXIT_IRQ(iregInterLevel); /* 退出内核 */ __LW_OBJECT_DELETE_HOOK(ulId); MONITOR_EVT_LONG1(MONITOR_EVENT_ID_SEMB, MONITOR_EVENT_SEM_DELETE, ulId, LW_NULL); _DebugFormat(__LOGMESSAGE_LEVEL, "semaphore \"%s\" has been delete.\r\n", pevent->EVENT_cEventName); return (ERROR_NONE); }
LW_API ULONG API_ThreadVarDelete (LW_OBJECT_HANDLE ulId, ULONG *pulAddr) { REGISTER UINT16 usIndex; REGISTER PLW_CLASS_TCB ptcbCur; REGISTER PLW_CLASS_TCB ptcb; REGISTER PLW_LIST_LINE plineVar; REGISTER PLW_CLASS_THREADVAR pthreadvar; usIndex = _ObjectGetIndex(ulId); 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); #if LW_CFG_ARG_CHK_EN > 0 if (!_ObjectClassOK(ulId, _OBJECT_THREAD)) { _DebugHandle(__ERRORMESSAGE_LEVEL, "thread handle invalidate.\r\n"); _ErrorHandle(ERROR_KERNEL_HANDLE_NULL); return (ERROR_KERNEL_HANDLE_NULL); } if (_Thread_Index_Invalid(usIndex)) { /* 检查线程有效性 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "thread handle invalidate.\r\n"); _ErrorHandle(ERROR_THREAD_NULL); return (ERROR_THREAD_NULL); } #endif __KERNEL_ENTER(); /* 进入内核 */ if (_Thread_Invalid(usIndex)) { __KERNEL_EXIT(); /* 退出内核 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "thread handle invalidate.\r\n"); _ErrorHandle(ERROR_THREAD_NULL); return (ERROR_THREAD_NULL); } ptcb = _K_ptcbTCBIdTable[usIndex]; for (plineVar = ptcb->TCB_plinePrivateVars; /* 查找 */ plineVar != LW_NULL; plineVar = _list_line_get_next(plineVar)) { pthreadvar = _LIST_ENTRY(plineVar, LW_CLASS_THREADVAR, PRIVATEVAR_lineVarList); if (pthreadvar->PRIVATEVAR_pulAddress == pulAddr) { if (ptcb == ptcbCur) { *pulAddr = pthreadvar->PRIVATEVAR_ulValueSave; } _List_Line_Del(plineVar, &ptcb->TCB_plinePrivateVars); /* 从 TCB 中解链 */ _Free_ThreadVar_Object(pthreadvar); /* 释放控制块 */ __KERNEL_EXIT(); /* 退出内核 */ return (ERROR_NONE); } } __KERNEL_EXIT(); /* 退出内核 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "var is not in thread context.\r\n"); _ErrorHandle(ERROR_THREAD_VAR_NOT_EXIST); return (ERROR_THREAD_VAR_NOT_EXIST); }
LW_API ULONG API_SemaphoreCPost (LW_OBJECT_HANDLE ulId) { INTREG iregInterLevel; REGISTER UINT16 usIndex; REGISTER PLW_CLASS_EVENT pevent; REGISTER PLW_CLASS_TCB ptcb; REGISTER PLW_LIST_RING *ppringList; /* 等待队列地址 */ usIndex = _ObjectGetIndex(ulId); #if LW_CFG_ARG_CHK_EN > 0 if (!_ObjectClassOK(ulId, _OBJECT_SEM_C)) { /* 类型是否正确 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "semaphore handle invalidate.\r\n"); _ErrorHandle(ERROR_KERNEL_HANDLE_NULL); return (ERROR_KERNEL_HANDLE_NULL); } if (_Event_Index_Invalid(usIndex)) { /* 下标是否正正确 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "semaphore handle invalidate.\r\n"); _ErrorHandle(ERROR_KERNEL_HANDLE_NULL); return (ERROR_KERNEL_HANDLE_NULL); } #endif pevent = &_K_eventBuffer[usIndex]; iregInterLevel = __KERNEL_ENTER_IRQ(); /* 进入内核 */ if (_Event_Type_Invalid(usIndex, LW_TYPE_EVENT_SEMC)) { __KERNEL_EXIT_IRQ(iregInterLevel); /* 退出内核 */ _DebugHandle(__ERRORMESSAGE_LEVEL, "semaphore handle invalidate.\r\n"); _ErrorHandle(ERROR_EVENT_TYPE); return (ERROR_EVENT_TYPE); } if (_EventWaitNum(pevent)) { if (pevent->EVENT_ulOption & LW_OPTION_WAIT_PRIORITY) { /* 优先级等待队列 */ _EVENT_DEL_Q_PRIORITY(ppringList); /* 检查需要激活的队列 */ /* 激活优先级等待线程 */ ptcb = _EventReadyPriorityLowLevel(pevent, LW_NULL, ppringList); } else { _EVENT_DEL_Q_FIFO(ppringList); /* 检查需要激活的FIFO队列 */ /* 激活FIFO等待线程 */ ptcb = _EventReadyFifoLowLevel(pevent, LW_NULL, ppringList); } KN_INT_ENABLE(iregInterLevel); /* 使能中断 */ _EventReadyHighLevel(ptcb, LW_THREAD_STATUS_SEM); /* 处理 TCB */ MONITOR_EVT_LONG2(MONITOR_EVENT_ID_SEMC, MONITOR_EVENT_SEM_POST, ulId, ptcb->TCB_ulId, LW_NULL); __KERNEL_EXIT(); /* 退出内核 */ return (ERROR_NONE); } else { /* 没有线程等待 */ if (pevent->EVENT_ulCounter < pevent->EVENT_ulMaxCounter) { /* 检查是否还有空间加 */ pevent->EVENT_ulCounter++; __KERNEL_EXIT_IRQ(iregInterLevel); /* 退出内核 */ return (ERROR_NONE); } else { /* 已经满了 */ __KERNEL_EXIT_IRQ(iregInterLevel); /* 退出内核 */ _ErrorHandle(ERROR_EVENT_FULL); return (ERROR_EVENT_FULL); } } }