• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_Event.h
20  * @brief
21  * @author      csy(csy@rock-chips.com)
22  * @version     1.0.0
23  * @history
24  *   2013.11.26 : Create
25  */
26 #undef  ROCKCHIP_LOG_TAG
27 #define ROCKCHIP_LOG_TAG    "omx_osal_event"
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <pthread.h>
33 #include <errno.h>
34 #include <sys/time.h>
35 
36 #include "Rockchip_OSAL_Memory.h"
37 #include "Rockchip_OSAL_Mutex.h"
38 #include "Rockchip_OSAL_Log.h"
39 #include "Rockchip_OSAL_Event.h"
40 
Rockchip_OSAL_SignalCreate(OMX_HANDLETYPE * eventHandle)41 OMX_ERRORTYPE Rockchip_OSAL_SignalCreate(OMX_HANDLETYPE *eventHandle)
42 {
43     ROCKCHIP_OSAL_THREADEVENT *event;
44     OMX_ERRORTYPE ret = OMX_ErrorNone;
45 
46     event = (ROCKCHIP_OSAL_THREADEVENT *)Rockchip_OSAL_Malloc(sizeof(ROCKCHIP_OSAL_THREADEVENT));
47     if (!event) {
48         ret = OMX_ErrorInsufficientResources;
49         goto EXIT;
50     }
51 
52     Rockchip_OSAL_Memset(event, 0, sizeof(ROCKCHIP_OSAL_THREADEVENT));
53     event->signal = OMX_FALSE;
54 
55     ret = Rockchip_OSAL_MutexCreate(&event->mutex);
56     if (ret != OMX_ErrorNone) {
57         Rockchip_OSAL_Free(event);
58         goto EXIT;
59     }
60 
61     if (pthread_cond_init(&event->condition, NULL)) {
62         Rockchip_OSAL_MutexTerminate(event->mutex);
63         Rockchip_OSAL_Free(event);
64         ret = OMX_ErrorUndefined;
65         goto EXIT;
66     }
67 
68     *eventHandle = (OMX_HANDLETYPE)event;
69     ret = OMX_ErrorNone;
70 
71 EXIT:
72     return ret;
73 }
74 
Rockchip_OSAL_SignalTerminate(OMX_HANDLETYPE eventHandle)75 OMX_ERRORTYPE Rockchip_OSAL_SignalTerminate(OMX_HANDLETYPE eventHandle)
76 {
77     ROCKCHIP_OSAL_THREADEVENT *event = (ROCKCHIP_OSAL_THREADEVENT *)eventHandle;
78     OMX_ERRORTYPE ret = OMX_ErrorNone;
79 
80     if (!event) {
81         ret = OMX_ErrorBadParameter;
82         goto EXIT;
83     }
84 
85     ret = Rockchip_OSAL_MutexLock(event->mutex);
86     if (ret != OMX_ErrorNone) {
87         ret = OMX_ErrorBadParameter;
88         goto EXIT;
89     }
90 
91     if (pthread_cond_destroy(&event->condition)) {
92         ret = OMX_ErrorUndefined;
93         goto EXIT;
94     }
95 
96     ret = Rockchip_OSAL_MutexUnlock(event->mutex);
97     if (ret != OMX_ErrorNone) {
98         ret = OMX_ErrorUndefined;
99         goto EXIT;
100     }
101 
102     ret = Rockchip_OSAL_MutexTerminate(event->mutex);
103     if (ret != OMX_ErrorNone) {
104         ret = OMX_ErrorUndefined;
105         goto EXIT;
106     }
107 
108     Rockchip_OSAL_Free(event);
109 
110 EXIT:
111     return ret;
112 }
113 
Rockchip_OSAL_SignalReset(OMX_HANDLETYPE eventHandle)114 OMX_ERRORTYPE Rockchip_OSAL_SignalReset(OMX_HANDLETYPE eventHandle)
115 {
116     ROCKCHIP_OSAL_THREADEVENT *event = (ROCKCHIP_OSAL_THREADEVENT *)eventHandle;
117     OMX_ERRORTYPE ret = OMX_ErrorNone;
118 
119     if (!event) {
120         ret = OMX_ErrorBadParameter;
121         goto EXIT;
122     }
123 
124     ret = Rockchip_OSAL_MutexLock(event->mutex);
125     if (ret != OMX_ErrorNone) {
126         ret = OMX_ErrorBadParameter;
127         goto EXIT;
128     }
129 
130     event->signal = OMX_FALSE;
131 
132     Rockchip_OSAL_MutexUnlock(event->mutex);
133 
134 EXIT:
135     return ret;
136 }
137 
Rockchip_OSAL_SignalSet(OMX_HANDLETYPE eventHandle)138 OMX_ERRORTYPE Rockchip_OSAL_SignalSet(OMX_HANDLETYPE eventHandle)
139 {
140     ROCKCHIP_OSAL_THREADEVENT *event = (ROCKCHIP_OSAL_THREADEVENT *)eventHandle;
141     OMX_ERRORTYPE ret = OMX_ErrorNone;
142 
143     if (!event) {
144         ret = OMX_ErrorBadParameter;
145         goto EXIT;
146     }
147 
148     ret = Rockchip_OSAL_MutexLock(event->mutex);
149     if (ret != OMX_ErrorNone) {
150         ret = OMX_ErrorBadParameter;
151         goto EXIT;
152     }
153 
154     event->signal = OMX_TRUE;
155     pthread_cond_signal(&event->condition);
156 
157     Rockchip_OSAL_MutexUnlock(event->mutex);
158 
159 EXIT:
160     return ret;
161 }
162 
Rockchip_OSAL_SignalWait(OMX_HANDLETYPE eventHandle,OMX_U32 ms)163 OMX_ERRORTYPE Rockchip_OSAL_SignalWait(OMX_HANDLETYPE eventHandle, OMX_U32 ms)
164 {
165     ROCKCHIP_OSAL_THREADEVENT *event = (ROCKCHIP_OSAL_THREADEVENT *)eventHandle;
166     OMX_ERRORTYPE         ret = OMX_ErrorNone;
167     struct timespec       timeout;
168     struct timeval        now;
169     int                   funcret = 0;
170     OMX_U32               tv_us;
171 
172     FunctionIn();
173 
174     if (!event) {
175         ret = OMX_ErrorBadParameter;
176         goto EXIT;
177     }
178 
179     gettimeofday(&now, NULL);
180 
181     tv_us = now.tv_usec + ms * 1000; // 1000:time shift
182     timeout.tv_sec = now.tv_sec + tv_us / 1000000; // 1000000:time shift
183     timeout.tv_nsec = (tv_us % 1000000) * 1000; // 1000000:time shift, 1000:time shift
184 
185     ret = Rockchip_OSAL_MutexLock(event->mutex);
186     if (ret != OMX_ErrorNone) {
187         ret = OMX_ErrorBadParameter;
188         goto EXIT;
189     }
190 
191     if (ms == 0) {
192         if (!event->signal)
193             ret = OMX_ErrorTimeout;
194     } else if (ms == DEF_MAX_WAIT_TIME) {
195         while (!event->signal)
196             pthread_cond_wait(&event->condition, (pthread_mutex_t *)(event->mutex));
197         ret = OMX_ErrorNone;
198     } else {
199         while (!event->signal) {
200             funcret = pthread_cond_timedwait(&event->condition, (pthread_mutex_t *)(event->mutex), &timeout);
201             if ((!event->signal) && (funcret == ETIMEDOUT)) {
202                 ret = OMX_ErrorTimeout;
203                 break;
204             }
205         }
206     }
207 
208     Rockchip_OSAL_MutexUnlock(event->mutex);
209 
210 EXIT:
211     FunctionOut();
212 
213     return ret;
214 }
215