• 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_Semaphore.c
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_sem"
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <pthread.h>
33 #include <semaphore.h>
34 
35 #include "Rockchip_OSAL_Memory.h"
36 #include "Rockchip_OSAL_Log.h"
37 #include "Rockchip_OSAL_Semaphore.h"
38 
Rockchip_OSAL_SemaphoreCreate(OMX_HANDLETYPE * semaphoreHandle)39 OMX_ERRORTYPE Rockchip_OSAL_SemaphoreCreate(OMX_HANDLETYPE *semaphoreHandle)
40 {
41     sem_t *sema;
42 
43     sema = (sem_t *)Rockchip_OSAL_Malloc(sizeof(sem_t));
44     if (!sema)
45         return OMX_ErrorInsufficientResources;
46 
47     if (sem_init(sema, 0, 0) != 0) {
48         Rockchip_OSAL_Free(sema);
49         return OMX_ErrorUndefined;
50     }
51 
52     *semaphoreHandle = (OMX_HANDLETYPE)sema;
53 
54     omx_trace("Rockchip_OSAL_SemaphorePost %p", sema);
55     return OMX_ErrorNone;
56 }
57 
Rockchip_OSAL_SemaphoreTerminate(OMX_HANDLETYPE semaphoreHandle)58 OMX_ERRORTYPE Rockchip_OSAL_SemaphoreTerminate(OMX_HANDLETYPE semaphoreHandle)
59 {
60     sem_t *sema = (sem_t *)semaphoreHandle;
61 
62     if (sema == NULL)
63         return OMX_ErrorBadParameter;
64 
65     if (sem_destroy(sema) != 0)
66         return OMX_ErrorUndefined;
67 
68     Rockchip_OSAL_Free(sema);
69     return OMX_ErrorNone;
70 }
71 
Rockchip_OSAL_SemaphoreWait(OMX_HANDLETYPE semaphoreHandle)72 OMX_ERRORTYPE Rockchip_OSAL_SemaphoreWait(OMX_HANDLETYPE semaphoreHandle)
73 {
74     omx_trace("Rockchip_OSAL_SemaphoreWait %p", semaphoreHandle);
75     sem_t *sema = (sem_t *)semaphoreHandle;
76 
77     FunctionIn();
78 
79     if (sema == NULL)
80         return OMX_ErrorBadParameter;
81 
82     if (sem_wait(sema) != 0)
83         return OMX_ErrorUndefined;
84 
85     FunctionOut();
86 
87     return OMX_ErrorNone;
88 }
89 
Rockchip_OSAL_SemaphorePost(OMX_HANDLETYPE semaphoreHandle)90 OMX_ERRORTYPE Rockchip_OSAL_SemaphorePost(OMX_HANDLETYPE semaphoreHandle)
91 {
92     sem_t *sema = (sem_t *)semaphoreHandle;
93 
94     FunctionIn();
95 
96     if (sema == NULL)
97         return OMX_ErrorBadParameter;
98 
99     if (sem_post(sema) != 0)
100         return OMX_ErrorUndefined;
101 
102     FunctionOut();
103 
104     return OMX_ErrorNone;
105 }
106 
Rockchip_OSAL_Set_SemaphoreCount(OMX_HANDLETYPE semaphoreHandle,OMX_S32 val)107 OMX_ERRORTYPE Rockchip_OSAL_Set_SemaphoreCount(OMX_HANDLETYPE semaphoreHandle, OMX_S32 val)
108 {
109     sem_t *sema = (sem_t *)semaphoreHandle;
110 
111     if (sema == NULL)
112         return OMX_ErrorBadParameter;
113 
114     if (sem_init(sema, 0, val) != 0)
115         return OMX_ErrorUndefined;
116 
117     return OMX_ErrorNone;
118 }
119 
Rockchip_OSAL_Get_SemaphoreCount(OMX_HANDLETYPE semaphoreHandle,OMX_S32 * val)120 OMX_ERRORTYPE Rockchip_OSAL_Get_SemaphoreCount(OMX_HANDLETYPE semaphoreHandle, OMX_S32 *val)
121 {
122     sem_t *sema = (sem_t *)semaphoreHandle;
123     int semaVal = 0;
124 
125     if (sema == NULL)
126         return OMX_ErrorBadParameter;
127 
128     if (sem_getvalue(sema, &semaVal) != 0)
129         return OMX_ErrorUndefined;
130 
131     *val = (OMX_S32)semaVal;
132 
133     return OMX_ErrorNone;
134 }