diff --git a/kernel/base/include/los_futex_pri.h b/kernel/base/include/los_futex_pri.h index 78c30af731c7b9c47ed7e133caccf44f5341c963..6f96a0e20cc2adf68005cc80d86788e64cec0fe6 100644 --- a/kernel/base/include/los_futex_pri.h +++ b/kernel/base/include/los_futex_pri.h @@ -79,7 +79,8 @@ typedef struct { UINT32 index; /* hash bucket index | 哈希桶索引 OsFutexKeyToIndex */ UINT32 pid; /* private:process id shared:OS_INVALID(-1) | 私有锁:进程ID , 共享锁为 -1 */ LOS_DL_LIST pendList; /* point to pendList in TCB struct | 指向 TCB 结构中的 pendList, 通过它找到任务*/ - LOS_DL_LIST queueList; /* thread list blocked by this lock | 挂等待这把锁的任务,上面挂的是 FutexNode.queueList*/ + LOS_DL_LIST queueList; /* thread list blocked by this lock | 挂等待这把锁的任务,其实这里挂到是FutexNode.queueList , + 通过 queueList 可以找到 pendList ,通过 pendList又可以找到真正的任务*/ LOS_DL_LIST futexList; /* point to the next FutexNode | 下一把Futex锁*/ } FutexNode; diff --git a/kernel/base/include/los_sched_pri.h b/kernel/base/include/los_sched_pri.h index b981b977e231e6a5a05598ce03143250c4832759..3725399b8bc4b7cf03b015946dcdd4cb406fe294 100644 --- a/kernel/base/include/los_sched_pri.h +++ b/kernel/base/include/los_sched_pri.h @@ -405,7 +405,7 @@ typedef struct { UINTPTR userArea; ///< 用户空间的堆区开始位置 UINTPTR userMapBase; ///< 用户空间的栈顶位置,内存来自用户空间,和topOfStack有本质的区别. UINT32 userMapSize; /**< user thread stack size ,real size : userMapSize + USER_STACK_MIN_SIZE | 用户栈大小 */ - FutexNode futex; ///< 实现快锁功能 + FutexNode futex; ///< 快锁节点功能 , 一个任务只能等一把快锁 #endif UINT32 processID; /**< Which belong process */ LOS_DL_LIST joinList; /**< join list | 联结链表,允许任务之间相互释放彼此 */ diff --git a/kernel/base/ipc/los_futex.c b/kernel/base/ipc/los_futex.c index 6fb6c0dcbbdaf98bef9093ae477a1b311046a4f1..3462418377509b4856b3bb230229b43885f33932 100644 --- a/kernel/base/ipc/los_futex.c +++ b/kernel/base/ipc/los_futex.c @@ -326,7 +326,7 @@ EXIT: return; } - +///< 这块代码谁写的? 这种命名 ... STATIC FutexNode *OsFutexDeleteAlreadyWakeTaskAndGetNext(const FutexNode *node, FutexNode **headNode, BOOL isDeleteHead) { FutexNode *tempNode = (FutexNode *)node; @@ -388,7 +388,7 @@ STATIC VOID OsFutexInsertNewFutexKeyToHash(FutexNode *node) EXIT: return; } -///< +///< 从后往前插入快锁 Form写错了 @note_thinking STATIC INT32 OsFutexInsertFindFormBackToFront(LOS_DL_LIST *queueList, const LosTaskCB *runTask, FutexNode *node) { LOS_DL_LIST *listHead = queueList; @@ -469,33 +469,33 @@ STATIC INT32 OsFutexRecycleAndFindHeadNode(FutexNode *headNode, FutexNode *node, ///< 将快锁挂到任务的阻塞链表上 STATIC INT32 OsFutexInsertTasktoPendList(FutexNode **firstNode, FutexNode *node, const LosTaskCB *run) { - LosTaskCB *taskHead = OS_TCB_FROM_PENDLIST(LOS_DL_LIST_FIRST(&((*firstNode)->pendList))); + LosTaskCB *taskHead = OS_TCB_FROM_PENDLIST(LOS_DL_LIST_FIRST(&((*firstNode)->pendList)));//获取阻塞链表首个任务 LOS_DL_LIST *queueList = &((*firstNode)->queueList); FutexNode *tailNode = NULL; LosTaskCB *taskTail = NULL; - if (run->priority < taskHead->priority) { + if (run->priority < taskHead->priority) {//任务的优先级比较 /* The one with the highest priority is inserted at the top of the queue */ - LOS_ListTailInsert(queueList, &(node->queueList)); - OsFutexReplaceQueueListHeadNode(*firstNode, node); + LOS_ListTailInsert(queueList, &(node->queueList));//查到queueList的尾部 + OsFutexReplaceQueueListHeadNode(*firstNode, node);//同时交换futexList链表上的位置 *firstNode = node; return LOS_OK; } - + //如果等锁链表上没有任务或者当前任务大于链表首个任务 if (LOS_ListEmpty(queueList) && (run->priority >= taskHead->priority)) { /* Insert the next position in the queue with equal priority */ - LOS_ListHeadInsert(queueList, &(node->queueList)); + LOS_ListHeadInsert(queueList, &(node->queueList));//从头部插入当前任务,当前任务是要被挂起的 return LOS_OK; } - - tailNode = OS_FUTEX_FROM_QUEUELIST(LOS_DL_LIST_LAST(queueList)); - taskTail = OS_TCB_FROM_PENDLIST(LOS_DL_LIST_FIRST(&(tailNode->pendList))); - if ((run->priority >= taskTail->priority) || - ((run->priority - taskHead->priority) > (taskTail->priority - run->priority))) { - return OsFutexInsertFindFormBackToFront(queueList, run, node); + + tailNode = OS_FUTEX_FROM_QUEUELIST(LOS_DL_LIST_LAST(queueList));//获取尾部节点 + taskTail = OS_TCB_FROM_PENDLIST(LOS_DL_LIST_FIRST(&(tailNode->pendList)));//获取阻塞任务的最后一个 + if ((run->priority >= taskTail->priority) ||//当前任务优先级比最后一个更高,或者 ... 没看懂, 为啥要这样 ? @notethinking + ((run->priority - taskHead->priority) > (taskTail->priority - run->priority))) {//跟最后一个比较优先级 + return OsFutexInsertFindFormBackToFront(queueList, run, node);//从后往前插入 } - return OsFutexInsertFindFromFrontToBack(queueList, run, node); + return OsFutexInsertFindFromFrontToBack(queueList, run, node);//否则从前往后插入 } /// 由指定快锁找到对应哈希桶 STATIC FutexNode *OsFindFutexNode(const FutexNode *node) diff --git a/zzz/git/push.sh b/zzz/git/push.sh index 91662ce5051b5f76ddb48d70819b8c68c0f3a213..f9ad0397c062ee974b292f1f3e88e24a94a5ff19 100644 --- a/zzz/git/push.sh +++ b/zzz/git/push.sh @@ -1,5 +1,5 @@ git add -A -git commit -m ' 注解 Futex 内核态实现 +git commit -m ' 围绕FutexNode注解相关逻辑 百图画鸿蒙 + 百文说内核 + 百万注源码 => 挖透鸿蒙内核源码 鸿蒙研究站 | http://weharmonyos.com (国内) | https://weharmony.github.io (国外)