1 /*
2 *
3 * Copyright 2013 Rockchip Electronics Co., LTD.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 /*
19 * @file Rockchip_OSAL_Queue.c
20 * @brief
21 * @author csy(csy@rock-chips.com)
22 * @version 1.0.0
23 * @history
24 * 2013.11.26 : Create
25 */
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include "securec.h"
31 #include "Rockchip_OSAL_Memory.h"
32 #include "Rockchip_OSAL_Mutex.h"
33 #include "Rockchip_OSAL_Queue.h"
34
Rockchip_OSAL_QueueCreate(ROCKCHIP_QUEUE * queueHandle,int maxNumElem)35 OMX_ERRORTYPE Rockchip_OSAL_QueueCreate(ROCKCHIP_QUEUE *queueHandle, int maxNumElem)
36 {
37 int i = 0;
38 ROCKCHIP_QElem *newqelem = NULL;
39 ROCKCHIP_QElem *currentqelem = NULL;
40 ROCKCHIP_QUEUE *queue = (ROCKCHIP_QUEUE *)queueHandle;
41
42 OMX_ERRORTYPE ret = OMX_ErrorNone;
43
44 if (!queue)
45 return OMX_ErrorBadParameter;
46
47 ret = Rockchip_OSAL_MutexCreate(&queue->qMutex);
48 if (ret != OMX_ErrorNone)
49 return ret;
50
51 queue->first = (ROCKCHIP_QElem *)Rockchip_OSAL_Malloc(sizeof(ROCKCHIP_QElem));
52 if (queue->first == NULL)
53 return OMX_ErrorInsufficientResources;
54
55 Rockchip_OSAL_Memset(queue->first, 0, sizeof(ROCKCHIP_QElem));
56 currentqelem = queue->last = queue->first;
57 queue->numElem = 0;
58 queue->maxNumElem = maxNumElem;
59 for (i = 0; i < (queue->maxNumElem - 2); i++) { // 2:byte alignment
60 newqelem = (ROCKCHIP_QElem *)Rockchip_OSAL_Malloc(sizeof(ROCKCHIP_QElem));
61 if (newqelem == NULL) {
62 while (queue->first != NULL) {
63 currentqelem = queue->first->qNext;
64 Rockchip_OSAL_Free((OMX_PTR)queue->first);
65 queue->first = currentqelem;
66 }
67 return OMX_ErrorInsufficientResources;
68 } else {
69 Rockchip_OSAL_Memset(newqelem, 0, sizeof(ROCKCHIP_QElem));
70 currentqelem->qNext = newqelem;
71 currentqelem = newqelem;
72 }
73 }
74
75 currentqelem->qNext = queue->first;
76
77 return OMX_ErrorNone;
78 }
79
Rockchip_OSAL_QueueTerminate(ROCKCHIP_QUEUE * queueHandle)80 OMX_ERRORTYPE Rockchip_OSAL_QueueTerminate(ROCKCHIP_QUEUE *queueHandle)
81 {
82 int i = 0;
83 ROCKCHIP_QElem *currentqelem = NULL;
84 ROCKCHIP_QUEUE *queue = (ROCKCHIP_QUEUE *)queueHandle;
85 OMX_ERRORTYPE ret = OMX_ErrorNone;
86
87 if (!queue)
88 return OMX_ErrorBadParameter;
89
90 for (i = 0; i < (queue->maxNumElem - 2); i++) { // 2:byte alignment
91 currentqelem = queue->first->qNext;
92 Rockchip_OSAL_Free(queue->first);
93 queue->first = currentqelem;
94 }
95
96 if (queue->first) {
97 Rockchip_OSAL_Free(queue->first);
98 queue->first = NULL;
99 }
100
101 ret = Rockchip_OSAL_MutexTerminate(queue->qMutex);
102
103 return ret;
104 }
105
Rockchip_OSAL_Queue(ROCKCHIP_QUEUE * queueHandle,void * data)106 int Rockchip_OSAL_Queue(ROCKCHIP_QUEUE *queueHandle, void *data)
107 {
108 ROCKCHIP_QUEUE *queue = (ROCKCHIP_QUEUE *)queueHandle;
109 if (queue == NULL) {
110 return -1;
111 }
112
113 Rockchip_OSAL_MutexLock(queue->qMutex);
114
115 if ((queue->last->data != NULL) || (queue->numElem >= queue->maxNumElem)) {
116 Rockchip_OSAL_MutexUnlock(queue->qMutex);
117 return -1;
118 }
119 queue->last->data = data;
120 queue->last = queue->last->qNext;
121 queue->numElem++;
122
123 Rockchip_OSAL_MutexUnlock(queue->qMutex);
124 return 0;
125 }
126
Rockchip_OSAL_Dequeue(ROCKCHIP_QUEUE * queueHandle)127 void *Rockchip_OSAL_Dequeue(ROCKCHIP_QUEUE *queueHandle)
128 {
129 void *data = NULL;
130 ROCKCHIP_QUEUE *queue = (ROCKCHIP_QUEUE *)queueHandle;
131 if (queue == NULL) {
132 return NULL;
133 }
134
135 Rockchip_OSAL_MutexLock(queue->qMutex);
136
137 if ((queue->first->data == NULL) || (queue->numElem <= 0)) {
138 Rockchip_OSAL_MutexUnlock(queue->qMutex);
139 return NULL;
140 }
141 data = queue->first->data;
142 queue->first->data = NULL;
143 queue->first = queue->first->qNext;
144 queue->numElem--;
145
146 Rockchip_OSAL_MutexUnlock(queue->qMutex);
147 return data;
148 }
149
Rockchip_OSAL_GetElemNum(ROCKCHIP_QUEUE * queueHandle)150 int Rockchip_OSAL_GetElemNum(ROCKCHIP_QUEUE *queueHandle)
151 {
152 int ElemNum = 0;
153 ROCKCHIP_QUEUE *queue = (ROCKCHIP_QUEUE *)queueHandle;
154 if (queue == NULL) {
155 return -1;
156 }
157
158 Rockchip_OSAL_MutexLock(queue->qMutex);
159 ElemNum = queue->numElem;
160 Rockchip_OSAL_MutexUnlock(queue->qMutex);
161 return ElemNum;
162 }
163
Rockchip_OSAL_SetElemNum(ROCKCHIP_QUEUE * queueHandle,int ElemNum)164 int Rockchip_OSAL_SetElemNum(ROCKCHIP_QUEUE *queueHandle, int ElemNum)
165 {
166 ROCKCHIP_QUEUE *queue = (ROCKCHIP_QUEUE *)queueHandle;
167 if (queue == NULL) {
168 return -1;
169 }
170
171 Rockchip_OSAL_MutexLock(queue->qMutex);
172 queue->numElem = ElemNum;
173 Rockchip_OSAL_MutexUnlock(queue->qMutex);
174 return ElemNum;
175 }
176
Rockchip_OSAL_ResetQueue(ROCKCHIP_QUEUE * queueHandle)177 int Rockchip_OSAL_ResetQueue(ROCKCHIP_QUEUE *queueHandle)
178 {
179 ROCKCHIP_QUEUE *queue = (ROCKCHIP_QUEUE *)queueHandle;
180 ROCKCHIP_QElem *currentqelem = NULL;
181
182 if (queue == NULL) {
183 return -1;
184 }
185
186 Rockchip_OSAL_MutexLock(queue->qMutex);
187 queue->first->data = NULL;
188 currentqelem = queue->first->qNext;
189 while (currentqelem != queue->first) {
190 currentqelem->data = NULL;
191 currentqelem = currentqelem->qNext;
192 }
193 queue->last = queue->first;
194 queue->numElem = 0x00;
195 Rockchip_OSAL_MutexUnlock(queue->qMutex);
196
197 return 0;
198 }