khronosfws/openmax_al/src/common/xathreadsafety.c
changeset 12 5a06f39ad45b
child 25 6f7ceef7b1d1
equal deleted inserted replaced
0:71ca22bcf22a 12:5a06f39ad45b
       
     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: 
       
    15 *
       
    16 */
       
    17 
       
    18 #include <stdlib.h>
       
    19 #include "xathreadsafety.h"
       
    20 
       
    21 typedef struct
       
    22 {
       
    23     XAImplMutexHandle mutexTable[XATSCount];
       
    24     XAboolean tsEnabled;
       
    25 }XAThreadSafetyImpl;
       
    26 
       
    27 static XAThreadSafetyImpl* threadSafety;
       
    28 
       
    29 /*
       
    30  * XAresult XAThreadSafety_Init()
       
    31  * Description: Creates mutex table for thread safety support
       
    32  * @return: Success value
       
    33  */
       
    34 XAresult XAThreadSafety_Init( XAboolean tsEnable )
       
    35 {
       
    36     XAresult ret = XA_RESULT_SUCCESS;
       
    37     XAint32 i = 0;
       
    38     DEBUG_API_A1("->XAThreadSafety_Init - tsEnable:%lu",tsEnable);
       
    39 
       
    40     /* Initialize thread safety only once */
       
    41     if ( !threadSafety )
       
    42     {
       
    43         threadSafety = (XAThreadSafetyImpl *)calloc(1,sizeof(XAThreadSafetyImpl));
       
    44         if ( !threadSafety )
       
    45         {
       
    46             DEBUG_ERR("XA_RESULT_MEMORY_FAILURE");
       
    47             DEBUG_API("<-XAThreadSafety_Init");
       
    48             /* memory allocation failed */
       
    49             return XA_RESULT_MEMORY_FAILURE;
       
    50         }
       
    51 
       
    52         threadSafety->tsEnabled = tsEnable;
       
    53 
       
    54         if ( tsEnable )
       
    55         {
       
    56             for( i = 0; i < XATSCount; i++ )
       
    57             {
       
    58                 ret = XAImpl_CreateMutex( &threadSafety->mutexTable[i] );
       
    59                 if ( ret != XA_RESULT_SUCCESS )
       
    60                 {
       
    61                     break;
       
    62                 }
       
    63                 DEBUG_INFO_A2("Created %s:%x",MEDIAOBJECTNAME(i), threadSafety->mutexTable[i] );
       
    64             }
       
    65         }
       
    66         else
       
    67         {
       
    68             DEBUG_INFO("Thread safety: disabled.");
       
    69         }
       
    70     }
       
    71 
       
    72     DEBUG_API("<-XAThreadSafety_Init");
       
    73     return ret;
       
    74 }
       
    75 
       
    76 /*
       
    77  * XAresult XAThreadSafety_Destroy()
       
    78  * Description: Destroys mutex table created for thread safety support
       
    79  */
       
    80 XAresult XAThreadSafety_Destroy()
       
    81 {
       
    82     XAresult ret = XA_RESULT_SUCCESS;
       
    83     XAint32 i = 0;
       
    84     DEBUG_API("->XAThreadSafety_Destroy");
       
    85 
       
    86     if ( threadSafety )
       
    87     {
       
    88         if ( threadSafety->tsEnabled )
       
    89         {
       
    90             for( i = 0; i < XATSCount; i++ )
       
    91             {
       
    92                 DEBUG_INFO_A2("Free %s:%x",MEDIAOBJECTNAME(i), threadSafety->mutexTable[i] );
       
    93                 XAImpl_DeleteMutex( threadSafety->mutexTable[i] );
       
    94             }
       
    95         }
       
    96         free( threadSafety);
       
    97     }
       
    98     else
       
    99     {
       
   100         DEBUG_INFO("Thread safety: disabled.");
       
   101     }
       
   102 
       
   103     DEBUG_API("<-XAThreadSafety_Destroy");
       
   104     return ret;
       
   105 
       
   106 }
       
   107 
       
   108 /*
       
   109  * XAresult XAThreadSafety_Unlock( XAThreadSafetyMediaObjects mediaObject )
       
   110  * Description:
       
   111  * @param XAThreadSafetyMediaObjects mediaObject
       
   112  * @return
       
   113  */
       
   114 XAresult XAThreadSafety_Unlock( XAThreadSafetyMediaObjects mediaObject )
       
   115 {
       
   116     XAresult ret = XA_RESULT_SUCCESS;
       
   117     DEBUG_API("->XAThreadSafety_Unlock");
       
   118 
       
   119     if ( threadSafety )
       
   120     {
       
   121         if ( threadSafety->tsEnabled )
       
   122         {
       
   123             ret = XAImpl_UnlockMutex( threadSafety->mutexTable[mediaObject] );
       
   124             if ( ret == XA_RESULT_SUCCESS)
       
   125             {
       
   126                 DEBUG_INFO_A2("Released lock for %s:%x",MEDIAOBJECTNAME(mediaObject), threadSafety->mutexTable[mediaObject] );
       
   127             }
       
   128         }
       
   129     }
       
   130     else
       
   131     {
       
   132         DEBUG_INFO("Thread safety: disabled.");
       
   133     }
       
   134     DEBUG_API("<-XAThreadSafety_Unlock");
       
   135     return ret;
       
   136 }
       
   137 /*
       
   138  * XAresult XAThreadSafety_TryLock( XAThreadSafetyMediaObjects mediaObject );
       
   139  * Description:
       
   140  * @param XAThreadSafetyMediaObjects mediaObject
       
   141  * @return
       
   142  */
       
   143 XAresult XAThreadSafety_TryLock( XAThreadSafetyMediaObjects mediaObject )
       
   144 {
       
   145     XAresult ret = XA_RESULT_SUCCESS;
       
   146     DEBUG_API("->XAThreadSafety_TryLock");
       
   147 
       
   148     if ( threadSafety )
       
   149     {
       
   150         if ( threadSafety->tsEnabled )
       
   151         {
       
   152             ret = XAImpl_TryLockMutex( threadSafety->mutexTable[mediaObject]);
       
   153             if ( ret == XA_RESULT_SUCCESS)
       
   154             {
       
   155                 DEBUG_INFO_A2("Locked %s:%x",MEDIAOBJECTNAME(mediaObject), threadSafety->mutexTable[mediaObject] );
       
   156             }
       
   157         }
       
   158     }
       
   159     else
       
   160     {
       
   161         DEBUG_INFO("Thread safety: disabled.");
       
   162     }
       
   163 
       
   164     DEBUG_API("<-XAThreadSafety_TryLock");
       
   165     return ret;
       
   166 }
       
   167