khronosfws/openmax_al/src/common/xaplatform.c
branchRCL_3
changeset 20 0ac9a5310753
parent 19 095bea5f582e
child 21 999b2818a0eb
equal deleted inserted replaced
19:095bea5f582e 20:0ac9a5310753
     1 /*
       
     2  * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  *
       
    12  * Contributors:
       
    13  *
       
    14  * Description: Platform Specific Utility Functions
       
    15  *
       
    16  */
       
    17 
       
    18 #include <pthread.h>
       
    19 #include <semaphore.h>
       
    20 #include <stdio.h>
       
    21 #include <stdlib.h>
       
    22 #include <assert.h>
       
    23 #include <errno.h>
       
    24 
       
    25 #include "xaplatform.h"
       
    26 
       
    27 /** MACROS **/
       
    28 
       
    29 /**********************************************************************
       
    30  * mutex management
       
    31  **********************************************************************/
       
    32 /* define next if platform supports posix error checking mutex type */
       
    33 #undef _MUTEXERRORSUPPORT
       
    34 
       
    35 #ifdef _MUTEXERRORSUPPORT
       
    36 typedef pthread_mutex_t XA_MTX;
       
    37 #else
       
    38 typedef struct
       
    39     {
       
    40     pthread_mutex_t mutex;
       
    41     pthread_t owner;
       
    42     } XA_MTX;
       
    43 #endif
       
    44 
       
    45 XAresult XAImpl_CreateMutex(XAImplMutexHandle *mtx)
       
    46     {
       
    47     XA_MTX *pMtx = (XA_MTX *) malloc(sizeof(XA_MTX));
       
    48     assert(mtx);
       
    49     if (pMtx)
       
    50         {
       
    51         pthread_mutexattr_t *pAttr = NULL;
       
    52 #ifdef _MUTEXERRORSUPPORT
       
    53         pthread_mutexattr_t attr;
       
    54         pAttr = &attr;
       
    55         pthread_mutexattr_init(pAttr);
       
    56         pthread_mutexattr_settype(pAttr, PTHREAD_MUTEX_ERRORCHECK);
       
    57         if(pthread_mutex_init(pMtx, pAttr))
       
    58 #else
       
    59         if (pthread_mutex_init(&(pMtx->mutex), pAttr))
       
    60 #endif
       
    61             {
       
    62             free(pMtx);
       
    63             *mtx = NULL;
       
    64             return XA_RESULT_INTERNAL_ERROR;
       
    65             }
       
    66         *mtx = (XAImplMutexHandle) pMtx;
       
    67         return XA_RESULT_SUCCESS;
       
    68         }
       
    69     else
       
    70         {
       
    71         return XA_RESULT_MEMORY_FAILURE;
       
    72         }
       
    73     }
       
    74 
       
    75 XAresult XAImpl_LockMutex(XAImplMutexHandle mtx)
       
    76     {
       
    77     XA_MTX *pMtx = (XA_MTX*) mtx;
       
    78     assert(pMtx);
       
    79 #ifdef _MUTEXERRORSUPPORT
       
    80     if(pthread_mutex_lock(pMtx))
       
    81         {
       
    82         return XA_RESULT_PRECONDITIONS_VIOLATED;
       
    83         }
       
    84 #else
       
    85     if (pthread_self() == pMtx->owner || pthread_mutex_unlock(&(pMtx->mutex)))
       
    86         {
       
    87         return XA_RESULT_PRECONDITIONS_VIOLATED;
       
    88         }
       
    89     pMtx->owner = pthread_self();
       
    90 #endif
       
    91     return XA_RESULT_SUCCESS;
       
    92     }
       
    93 
       
    94 XAresult XAImpl_TryLockMutex(XAImplMutexHandle mtx)
       
    95     {
       
    96     XA_MTX *pMtx = (XA_MTX*) mtx;
       
    97     XAint32 mutexRet;
       
    98     XAresult ret = XA_RESULT_SUCCESS;
       
    99     assert(pMtx);
       
   100 
       
   101 #ifdef _MUTEXERRORSUPPORT
       
   102     mutexRet = pthread_ mutex_trylock(pMtx);
       
   103     switch (mutexRet)
       
   104         {
       
   105         case EBUSY:
       
   106         /* if mutex is already locked, return permission denied */
       
   107         ret = XA_RESULT_PERMISSION_DENIED;
       
   108         break;
       
   109         case 0:
       
   110         ret = XA_RESULT_SUCCESS;
       
   111         break;
       
   112         default:
       
   113         ret = XA_RESULT_PRECONDITIONS_VIOLATED;
       
   114         break;
       
   115         }
       
   116 #else
       
   117     if (pthread_self() == pMtx->owner)
       
   118         {
       
   119         return XA_RESULT_PRECONDITIONS_VIOLATED;
       
   120         }
       
   121 
       
   122     mutexRet = pthread_mutex_trylock(&(pMtx->mutex));
       
   123     switch (mutexRet)
       
   124         {
       
   125         case EBUSY:
       
   126             /* if mutex is already locked, return permission denied */
       
   127             ret = XA_RESULT_PERMISSION_DENIED;
       
   128             break;
       
   129         case 0:
       
   130             pMtx->owner = pthread_self();
       
   131             ret = XA_RESULT_SUCCESS;
       
   132             break;
       
   133         default:
       
   134             ret = XA_RESULT_PRECONDITIONS_VIOLATED;
       
   135             break;
       
   136         }
       
   137 #endif
       
   138     return ret;
       
   139     }
       
   140 
       
   141 XAresult XAImpl_UnlockMutex(XAImplMutexHandle mtx)
       
   142     {
       
   143     XA_MTX *pMtx = (XA_MTX*) mtx;
       
   144     assert(pMtx);
       
   145 #ifdef _MUTEXERRORSUPPORT
       
   146     if(pthread_mutex_lock(pMtx))
       
   147         {
       
   148         return XA_RESULT_PRECONDITIONS_VIOLATED;
       
   149         }
       
   150 #else
       
   151     if (pthread_self() != pMtx->owner || pthread_mutex_unlock(&(pMtx->mutex)))
       
   152         {
       
   153         return XA_RESULT_PRECONDITIONS_VIOLATED;
       
   154         }
       
   155     // Changing the below value to 0 since owner is an unsigned int.
       
   156     // pMtx->owner = -1;
       
   157     pMtx->owner = 0;
       
   158 #endif
       
   159     return XA_RESULT_SUCCESS;
       
   160     }
       
   161 
       
   162 void XAImpl_DeleteMutex(XAImplMutexHandle mtx)
       
   163     {
       
   164     XA_MTX *pMtx = (XA_MTX*) mtx;
       
   165     if (pMtx)
       
   166         {
       
   167 #ifdef _MUTEXERRORSUPPORT
       
   168         pthread_mutex_destroy(pMtx);
       
   169 #else
       
   170         pthread_mutex_destroy(&(pMtx->mutex));
       
   171 #endif
       
   172         free(pMtx);
       
   173         }
       
   174     }
       
   175 
       
   176 /**********************************************************************
       
   177  * semaphores
       
   178  **********************************************************************/
       
   179 
       
   180 XAresult XAImpl_CreateSemaphore(XAImplSemHandle *sem)
       
   181     {
       
   182     sem_t *pSem = (sem_t*) malloc(sizeof(sem_t));
       
   183     assert(sem);
       
   184     if (pSem)
       
   185         {
       
   186         if (sem_init(pSem, 0, 0))
       
   187             {
       
   188             free(pSem);
       
   189             *sem = NULL;
       
   190             return XA_RESULT_INTERNAL_ERROR;
       
   191             }
       
   192         *sem = (XAImplSemHandle) pSem;
       
   193         return XA_RESULT_SUCCESS;
       
   194         }
       
   195     else
       
   196         {
       
   197         return XA_RESULT_MEMORY_FAILURE;
       
   198         }
       
   199     }
       
   200 
       
   201 XAresult XAImpl_WaitSemaphore(XAImplSemHandle sem)
       
   202     {
       
   203     sem_t* pSem = (sem_t*) sem;
       
   204     assert(pSem);
       
   205     sem_wait(pSem);
       
   206     return XA_RESULT_SUCCESS;
       
   207     }
       
   208 
       
   209 XAresult XAImpl_PostSemaphore(XAImplSemHandle sem)
       
   210     {
       
   211     sem_t *pSem = (sem_t*) sem;
       
   212     assert(pSem);
       
   213     sem_post(pSem);
       
   214     return XA_RESULT_SUCCESS;
       
   215     }
       
   216 
       
   217 void XAImpl_DeleteSemaphore(XAImplSemHandle sem)
       
   218     {
       
   219     sem_t *pSem = (sem_t*) sem;
       
   220     if (pSem)
       
   221         {
       
   222         sem_destroy(pSem);
       
   223         free(pSem);
       
   224         }
       
   225     }
       
   226 
       
   227 /**********************************************************************
       
   228  * THREADS
       
   229  **********************************************************************/
       
   230 
       
   231 XAresult XAImpl_CreateThreadHandle(XAImplThreadHandle *thd)
       
   232     {
       
   233     pthread_t *pThd = (pthread_t*) malloc(sizeof(pthread_t));
       
   234     assert(thd);
       
   235     if (!pThd)
       
   236         {
       
   237         return XA_RESULT_MEMORY_FAILURE;
       
   238         }
       
   239     *thd = (XAImplThreadHandle) pThd;
       
   240     return XA_RESULT_SUCCESS;
       
   241     }
       
   242 
       
   243 XAresult XAImpl_StartThread(XAImplThreadHandle thd, void *thdattrib,
       
   244         XAImplThreadFunction thdfunc, void* thdfuncargs)
       
   245     {
       
   246     pthread_t *pThd = (pthread_t*) thd;
       
   247     assert(thd);
       
   248     if (pthread_create(pThd, thdattrib, thdfunc, thdfuncargs))
       
   249         {
       
   250         return XA_RESULT_INTERNAL_ERROR;
       
   251         }
       
   252     return XA_RESULT_SUCCESS;
       
   253     }
       
   254 
       
   255 void XAImpl_CancelThread(XAImplThreadHandle thd)
       
   256     {
       
   257     // int res;
       
   258     // TL: TODO: There is no pthread_cancel API in S60, need to replace
       
   259     //res = pthread_cancel(*pThd);
       
   260 
       
   261     }
       
   262 
       
   263 void XAImpl_ExitThread(XAImplThreadHandle thd)
       
   264     {
       
   265     pthread_exit(NULL);
       
   266     }
       
   267 
       
   268 void XAImpl_DeleteThreadHandle(XAImplThreadHandle thd)
       
   269     {
       
   270     pthread_t *pThd = (pthread_t*) thd;
       
   271     if (pThd)
       
   272         {
       
   273         free(pThd);
       
   274         }
       
   275     }
       
   276