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