• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "securec.h"
16 #include "prt_queue_external.h"
17 #include "prt_mem_external.h"
18 #include "prt_lib_external.h"
19 
20 /* 队列最大个数 */
21 OS_SEC_BSS U16 g_maxQueue;
22 OS_SEC_BSS struct TagQueCb *g_allQueue;
23 
24 /*
25  * 描述:队列注册
26  */
OsQueueRegister(U16 maxQueue)27 OS_SEC_L4_TEXT U32 OsQueueRegister(U16 maxQueue)
28 {
29     if (maxQueue == 0) {
30         return OS_ERRNO_QUEUE_MAXNUM_ZERO;
31     }
32 
33     g_maxQueue = maxQueue;
34     return OS_OK;
35 }
36 
OsQueueConfigInit(void)37 OS_SEC_L4_TEXT U32 OsQueueConfigInit(void)
38 {
39     void *addr = NULL;
40 
41     addr = OsMemAlloc(OS_MID_QUEUE, OS_MEM_DEFAULT_FSC_PT, g_maxQueue * sizeof(struct TagQueCb));
42     if (addr == NULL) {
43         return OS_ERRNO_QUEUE_NO_MEMORY;
44     }
45 
46     if (memset_s(addr, g_maxQueue * sizeof(struct TagQueCb), 0, g_maxQueue * sizeof(struct TagQueCb)) != EOK) {
47         OS_GOTO_SYS_ERROR1();
48     }
49 
50     g_allQueue = (struct TagQueCb *)addr;
51 
52     return OS_OK;
53 }
54 
OsQueueGetNodeSize(U16 nodeSize)55 OS_SEC_ALW_INLINE INLINE U32 OsQueueGetNodeSize(U16 nodeSize)
56 {
57     return ALIGN(nodeSize, OS_QUEUE_NODE_SIZE_ALIGN) + OS_QUEUE_NODE_HEAD_LEN;
58 }
59 
OsQueueCreatParaCheck(U16 nodeNum,U16 nodeSize,const U32 * queueId)60 OS_SEC_L4_TEXT U32 OsQueueCreatParaCheck(U16 nodeNum, U16 nodeSize, const U32 *queueId)
61 {
62     if (queueId == NULL) {
63         return OS_ERRNO_QUEUE_CREAT_PTR_NULL;
64     }
65 
66     if ((nodeNum == 0) || (nodeSize == 0)) {
67         return OS_ERRNO_QUEUE_PARA_ZERO;
68     }
69 
70     if (OsQueueGetNodeSize(nodeSize) > OS_MAX_U16) {
71         return OS_ERRNO_QUEUE_NSIZE_INVALID;
72     }
73     return OS_OK;
74 }
75 
OsQueueCreate(U16 nodeNum,U16 maxNodeSize,U32 * queueId)76 OS_SEC_L4_TEXT U32 OsQueueCreate(U16 nodeNum, U16 maxNodeSize, U32 *queueId)
77 {
78     U32 index;
79     U32 qId = 0;
80     struct QueNode *queueNode = NULL;
81     U16 nodeSize = maxNodeSize;
82     struct TagQueCb *queueCb = NULL;
83 
84     /* 获取一个空闲的队列资源 */
85     queueCb = g_allQueue;
86     for (index = 0; index < g_maxQueue; index++, queueCb++) {
87         if (queueCb->queueState == OS_QUEUE_UNUSED) {
88             qId = OS_QUEUE_ID(index);
89             break;
90         }
91     }
92 
93     if (index == g_maxQueue) {
94         return OS_ERRNO_QUEUE_CB_UNAVAILABLE;
95     }
96 
97     nodeSize = (U16)OsQueueGetNodeSize(nodeSize);
98     queueCb->queue = (U8 *)OsMemAlloc(OS_MID_QUEUE, OS_MEM_DEFAULT_FSC_PT, (U32)nodeNum * (U32)nodeSize);
99     if (queueCb->queue == NULL) {
100         return OS_ERRNO_QUEUE_CREATE_NO_MEMORY;
101     }
102 
103     for (index = 0; index < nodeNum; index++) {
104         queueNode = (struct QueNode *)(uintptr_t)&queueCb->queue[index * nodeSize];
105         queueNode->srcPid = OS_QUEUE_PID_INVALID;
106     }
107 
108     queueCb->nodeNum = nodeNum;
109     queueCb->nodeSize = nodeSize;
110     queueCb->queueState = OS_QUEUE_USED;
111     INIT_LIST_OBJECT(&queueCb->writeList);
112     INIT_LIST_OBJECT(&queueCb->readList);
113     queueCb->writableCnt = nodeNum;
114     queueCb->queueHead = 0;
115     queueCb->queueTail = 0;
116     queueCb->nodePeak = 0;
117     queueCb->readableCnt = 0;
118 
119     *queueId = qId;
120 
121     return OS_OK;
122 }
123 
124 /*
125  * 描述:创建队列接口
126  */
PRT_QueueCreate(U16 nodeNum,U16 maxNodeSize,U32 * queueId)127 OS_SEC_L4_TEXT U32 PRT_QueueCreate(U16 nodeNum, U16 maxNodeSize, U32 *queueId)
128 {
129     uintptr_t intSave;
130     U32 ret;
131     U32 qId = 0;
132 
133     ret = OsQueueCreatParaCheck(nodeNum, maxNodeSize, queueId);
134     if (ret != OS_OK) {
135         return ret;
136     }
137 
138     intSave = OsIntLock();
139     ret = OsQueueCreate(nodeNum, maxNodeSize, &qId);
140     if (ret != OS_OK) {
141         OsIntRestore(intSave);
142         return ret;
143     }
144 
145     *queueId = qId;
146     OsIntRestore(intSave);
147     return OS_OK;
148 }
149