1/*
2 *
3 * Copyright 2012 Samsung Electronics S.LSI 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        Exynos_OSAL_Semaphore.c
20 * @brief
21 * @author      SeungBeom Kim (sbcrux.kim@samsung.com)
22 * @version     2.0.0
23 * @history
24 *   2012.02.20 : Create
25 */
26
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <pthread.h>
31#include <semaphore.h>
32
33#include "Exynos_OSAL_Memory.h"
34#include "Exynos_OSAL_Semaphore.h"
35
36#undef EXYNOS_LOG_TAG
37#define EXYNOS_LOG_TAG    "EXYNOS_LOG_SEMA"
38#define EXYNOS_LOG_OFF
39#include "Exynos_OSAL_Log.h"
40
41
42OMX_ERRORTYPE Exynos_OSAL_SemaphoreCreate(OMX_HANDLETYPE *semaphoreHandle)
43{
44    sem_t *sema;
45
46    sema = (sem_t *)Exynos_OSAL_Malloc(sizeof(sem_t));
47    if (!sema)
48        return OMX_ErrorInsufficientResources;
49
50    if (sem_init(sema, 0, 0) != 0) {
51        Exynos_OSAL_Free(sema);
52        return OMX_ErrorUndefined;
53    }
54
55    *semaphoreHandle = (OMX_HANDLETYPE)sema;
56    return OMX_ErrorNone;
57}
58
59OMX_ERRORTYPE Exynos_OSAL_SemaphoreTerminate(OMX_HANDLETYPE semaphoreHandle)
60{
61    sem_t *sema = (sem_t *)semaphoreHandle;
62
63    if (sema == NULL)
64        return OMX_ErrorBadParameter;
65
66    if (sem_destroy(sema) != 0)
67        return OMX_ErrorUndefined;
68
69    Exynos_OSAL_Free(sema);
70    return OMX_ErrorNone;
71}
72
73OMX_ERRORTYPE Exynos_OSAL_SemaphoreWait(OMX_HANDLETYPE semaphoreHandle)
74{
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
90OMX_ERRORTYPE Exynos_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
107OMX_ERRORTYPE Exynos_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
120OMX_ERRORTYPE Exynos_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}
135