int32_t osKernelUnlock (void) { int32_t lock; if(OS_INT_ACTIVE) { return (int32_t)osErrorISR; } if(!g_bTaskScheduled) { return (int32_t)osError; } if(g_usLosTaskLock > 0) { LOS_TaskUnlock(); if (g_usLosTaskLock != 0) { return (int32_t)osError; } lock = 1; } else { lock = 0; } return lock; }
int32_t osKernelRestoreLock (int32_t lock) { if(OS_INT_ACTIVE) { return (int32_t)osErrorISR; } if(!g_bTaskScheduled) { return (int32_t)osError; } switch (lock) { case 0: LOS_TaskUnlock(); if (g_usLosTaskLock != 0) { break; } return 0; case 1: LOS_TaskLock(); return 1; default: break; } return (int32_t)osError; }
UINT32 Example_Semphore(VOID) { UINT32 uwRet = LOS_OK; TSK_INIT_PARAM_S stTask1; TSK_INIT_PARAM_S stTask2; /* create sem */ LOS_SemCreate(0, &g_usSemID); /* lock task schedue */ LOS_TaskLock(); /* create task1 */ memset(&stTask1, 0, sizeof(TSK_INIT_PARAM_S)); stTask1.pfnTaskEntry = (TSK_ENTRY_FUNC)Example_SemTask1; stTask1.pcName = "MutexTsk1"; stTask1.uwStackSize = LOSCFG_BASE_CORE_TSK_IDLE_STACK_SIZE; stTask1.usTaskPrio = TASK_PRIO_TEST; uwRet = LOS_TaskCreate(&g_TestTaskID01, &stTask1); if (uwRet != LOS_OK) { dprintf("task1 create failed .\n"); return LOS_NOK; } /* create task2 */ memset(&stTask2, 0, sizeof(TSK_INIT_PARAM_S)); stTask2.pfnTaskEntry = (TSK_ENTRY_FUNC)Example_SemTask2; stTask2.pcName = "MutexTsk2"; stTask2.uwStackSize = LOSCFG_BASE_CORE_TSK_IDLE_STACK_SIZE; stTask2.usTaskPrio = (TASK_PRIO_TEST - 1); uwRet = LOS_TaskCreate(&g_TestTaskID02, &stTask2); if (uwRet != LOS_OK) { dprintf("task2 create failed .\n"); /* delete task 1 */ if (LOS_OK != LOS_TaskDelete(g_TestTaskID01)) { dprintf("task1 delete failed .\n"); } return LOS_NOK; } /* unlock task schedue */ LOS_TaskUnlock(); uwRet = LOS_SemPost(g_usSemID); /* task delay 40 ticks */ LOS_TaskDelay(40); /* delete sem */ LOS_SemDelete(g_usSemID); return uwRet; }
UINT32 Example_MsgQueue(void) { UINT32 uwRet = 0; UINT32 uwTask1, uwTask2; TSK_INIT_PARAM_S stInitParam1; /*创建任务1*/ stInitParam1.pfnTaskEntry = send_Entry; stInitParam1.usTaskPrio = 9; stInitParam1.uwStackSize = 0x200; stInitParam1.pcName = "sendQueue"; stInitParam1.uwResved = LOS_TASK_STATUS_DETACHED; LOS_TaskLock();//锁住任务,防止新创建的任务比本任务高而发生调度 uwRet = LOS_TaskCreate(&uwTask1, &stInitParam1); if(uwRet != LOS_OK) { dprintf("create task1 failed!,error:%x\n",uwRet); return uwRet; } /*创建任务2*/ stInitParam1.pfnTaskEntry = recv_Entry; uwRet = LOS_TaskCreate(&uwTask2, &stInitParam1); if(uwRet != LOS_OK) { dprintf("create task2 failed!,error:%x\n",uwRet); return uwRet; } /*创建队列*/ uwRet = LOS_QueueCreate("queue", 5, &g_uwQueue, 0, 24); if(uwRet != LOS_OK) { dprintf("create queue failure!,error:%x\n",uwRet); } dprintf("create the queue success!\n"); LOS_TaskUnlock();//解锁任务,只有队列创建后才开始任务调度 return LOS_OK; }