1 /*
2 * Copyright (c) 2009-2022 Huawei Technologies Co., Ltd. All rights reserved.
3 *
4 * UniProton is licensed under Mulan PSL v2.
5 * You can use this software according to the terms and conditions of the Mulan PSL v2.
6 * You may obtain a copy of Mulan PSL v2 at:
7 * http://license.coscl.org.cn/MulanPSL2
8 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
9 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
10 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
11 * See the Mulan PSL v2 for more details.
12 * Create: 2009-12-22
13 * Description: 信号量模块
14 */
15 #include "prt_sem_external.h"
16
17 /*
18 * 描述:获取信号量详细信息。
19 */
PRT_SemGetInfo(SemHandle semHandle,struct SemInfo * semInfo)20 OS_SEC_L4_TEXT U32 PRT_SemGetInfo(SemHandle semHandle, struct SemInfo *semInfo)
21 {
22 struct TagSemCb *semGet = NULL;
23 uintptr_t intSave;
24
25 if (semInfo == NULL) {
26 return OS_ERRNO_SEM_INFO_NULL;
27 }
28
29 if (semHandle >= (SemHandle)g_maxSem) {
30 return OS_ERRNO_SEM_INVALID;
31 }
32 semGet = GET_SEM(semHandle);
33
34 intSave = OsIntLock();
35 if (semGet->semStat == OS_SEM_UNUSED) {
36 OsIntRestore(intSave);
37 return OS_ERRNO_SEM_INVALID;
38 }
39
40 semInfo->owner = semGet->semOwner;
41 semInfo->count = semGet->semCount;
42 semInfo->mode = semGet->semMode;
43 semInfo->type = SEM_TYPE_COUNT;
44
45 OsIntRestore(intSave);
46 return OS_OK;
47 }
48
49 /*
50 * 描述:获取指定信号量的计数
51 */
PRT_SemGetCount(SemHandle semHandle,U32 * semCnt)52 OS_SEC_L4_TEXT U32 PRT_SemGetCount(SemHandle semHandle, U32 *semCnt)
53 {
54 U32 ret;
55 struct SemInfo semInfo = {0};
56
57 if (semCnt == NULL) {
58 return OS_ERRNO_SEM_COUNT_GET_PTR_NULL;
59 }
60
61 ret = PRT_SemGetInfo(semHandle, &semInfo);
62 if (ret == OS_OK) {
63 *semCnt = semInfo.count;
64 }
65
66 return ret;
67 }
68
69 /*
70 * 描述:获取阻塞在指定核内信号量的任务PID清单
71 */
PRT_SemGetPendList(SemHandle semHandle,U32 * tskCnt,U32 * pidBuf,U32 bufLen)72 OS_SEC_L4_TEXT U32 PRT_SemGetPendList(SemHandle semHandle, U32 *tskCnt, U32 *pidBuf, U32 bufLen)
73 {
74 uintptr_t intSave;
75 U32 taskCount = 0;
76 U32 len = (bufLen / sizeof(U32));
77 struct TagTskCb *tskCb = NULL;
78 struct TagSemCb *semCb = NULL;
79
80 if (tskCnt == NULL) {
81 return OS_ERRNO_SEM_INPUT_ERROR;
82 }
83
84 *tskCnt = 0;
85 if ((pidBuf == NULL) || (len == 0)) {
86 return OS_ERRNO_SEM_INPUT_ERROR;
87 }
88
89 if (semHandle >= (SemHandle)g_maxSem) {
90 return OS_ERRNO_SEM_INVALID;
91 }
92
93 semCb = GET_SEM(semHandle);
94 intSave = OsIntLock();
95 if (semCb->semStat == OS_SEM_UNUSED) {
96 OsIntRestore(intSave);
97 return OS_ERRNO_SEM_INVALID;
98 }
99
100 LIST_FOR_EACH(tskCb, &semCb->semList, struct TagTskCb, pendList) {
101 if (taskCount < len) {
102 pidBuf[taskCount] = tskCb->taskPid;
103 }
104 taskCount++;
105 }
106
107 *tskCnt = taskCount;
108
109 OsIntRestore(intSave);
110
111 if (taskCount > len) {
112 return OS_ERRNO_SEM_INPUT_BUF_NOT_ENOUGH;
113 }
114
115 return OS_OK;
116 }
117