Home | History | Annotate | Download | only in sec_osal
      1 /*
      2  *
      3  * Copyright 2010 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        SEC_OSAL_Semaphore.c
     20  * @brief
     21  * @author      SeungBeom Kim (sbcrux.kim (at) samsung.com)
     22  * @version     1.0
     23  * @history
     24  *   2010.7.15 : 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 "SEC_OSAL_Memory.h"
     34 #include "SEC_OSAL_Semaphore.h"
     35 
     36 #undef SEC_LOG_TAG
     37 #define SEC_LOG_TAG    "SEC_LOG_SEMA"
     38 #define SEC_LOG_OFF
     39 #include "SEC_OSAL_Log.h"
     40 
     41 
     42 OMX_ERRORTYPE SEC_OSAL_SemaphoreCreate(OMX_HANDLETYPE *semaphoreHandle)
     43 {
     44     sem_t *sema;
     45 
     46     sema = (sem_t *)SEC_OSAL_Malloc(sizeof(sem_t));
     47     if (!sema)
     48         return OMX_ErrorInsufficientResources;
     49 
     50     if (sem_init(sema, 0, 0) != 0)
     51         return OMX_ErrorUndefined;
     52 
     53     *semaphoreHandle = (OMX_HANDLETYPE)sema;
     54     return OMX_ErrorNone;
     55 }
     56 
     57 OMX_ERRORTYPE SEC_OSAL_SemaphoreTerminate(OMX_HANDLETYPE semaphoreHandle)
     58 {
     59     sem_t *sema = (sem_t *)semaphoreHandle;
     60 
     61     if (sema == NULL)
     62         return OMX_ErrorBadParameter;
     63 
     64     if (sem_destroy(sema) != 0)
     65         return OMX_ErrorUndefined;
     66 
     67     SEC_OSAL_Free(sema);
     68     return OMX_ErrorNone;
     69 }
     70 
     71 OMX_ERRORTYPE SEC_OSAL_SemaphoreWait(OMX_HANDLETYPE semaphoreHandle)
     72 {
     73     sem_t *sema = (sem_t *)semaphoreHandle;
     74 
     75     FunctionIn();
     76 
     77     if (sema == NULL)
     78         return OMX_ErrorBadParameter;
     79 
     80     if (sem_wait(sema) != 0)
     81         return OMX_ErrorUndefined;
     82 
     83     FunctionOut();
     84 
     85     return OMX_ErrorNone;
     86 }
     87 
     88 OMX_ERRORTYPE SEC_OSAL_SemaphorePost(OMX_HANDLETYPE semaphoreHandle)
     89 {
     90     sem_t *sema = (sem_t *)semaphoreHandle;
     91 
     92     FunctionIn();
     93 
     94     if (sema == NULL)
     95         return OMX_ErrorBadParameter;
     96 
     97     if (sem_post(sema) != 0)
     98         return OMX_ErrorUndefined;
     99 
    100     FunctionOut();
    101 
    102     return OMX_ErrorNone;
    103 }
    104 
    105 OMX_ERRORTYPE SEC_OSAL_Set_SemaphoreCount(OMX_HANDLETYPE semaphoreHandle, OMX_S32 val)
    106 {
    107     sem_t *sema = (sem_t *)semaphoreHandle;
    108 
    109     if (sema == NULL)
    110         return OMX_ErrorBadParameter;
    111 
    112     if (sem_init(sema, 0, val) != 0)
    113         return OMX_ErrorUndefined;
    114 
    115     return OMX_ErrorNone;
    116 }
    117 
    118 OMX_ERRORTYPE SEC_OSAL_Get_SemaphoreCount(OMX_HANDLETYPE semaphoreHandle, OMX_S32 *val)
    119 {
    120     sem_t *sema = (sem_t *)semaphoreHandle;
    121     OMX_U32 semaVal = 0;
    122 
    123     if (sema == NULL)
    124         return OMX_ErrorBadParameter;
    125 
    126     if (sem_getvalue(sema, &semaVal) != 0)
    127         return OMX_ErrorUndefined;
    128 
    129     *val = semaVal;
    130 
    131     return OMX_ErrorNone;
    132 }
    133