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