khronosfws/openmax_al/src/engine/xathreadsyncitf.c
branchRCL_3
changeset 46 0ac9a5310753
parent 45 095bea5f582e
child 50 999b2818a0eb
equal deleted inserted replaced
45:095bea5f582e 46: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: ThreadSync Itf Implementation
       
    15  *
       
    16  */
       
    17 
       
    18 #include <stdio.h>
       
    19 #include <stdlib.h>
       
    20 #include <assert.h>
       
    21 
       
    22 #include "xathreadsyncitf.h"
       
    23 
       
    24 /**
       
    25  * Base interface XAThreadSyncItf Implementation
       
    26  */
       
    27 XAresult XAThreadSyncItfImpl_EnterCriticalSection(XAThreadSyncItf self)
       
    28     {
       
    29     XAresult ret = XA_RESULT_SUCCESS;
       
    30     XAThreadSyncItfImpl* impl = NULL;
       
    31 
       
    32     DEBUG_API("->XAThreadSyncItfImpl_EnterCriticalSection");
       
    33 
       
    34     impl = (XAThreadSyncItfImpl*) (*self);
       
    35     if (!impl || impl != impl->self)
       
    36         {
       
    37         DEBUG_ERR("XA_RESULT_PARAMETER_INVALID");
       
    38         DEBUG_API("<-XAThreadSyncItfImpl_EnterCriticalSection");
       
    39         return XA_RESULT_PARAMETER_INVALID;
       
    40         }
       
    41 
       
    42     if (impl->engInCritical)
       
    43         {
       
    44         /* The calling context must not already be in
       
    45          critical section state. */
       
    46         DEBUG_ERR("XA_RESULT_PRECONDITIONS_VIOLATED");
       
    47         ret = XA_RESULT_PRECONDITIONS_VIOLATED;
       
    48         }
       
    49     else
       
    50         {
       
    51         ret = XAImpl_LockMutex(impl->engCriticalSection);
       
    52         if (ret == XA_RESULT_SUCCESS)
       
    53             {
       
    54             impl->engInCritical = XA_BOOLEAN_TRUE;
       
    55             }
       
    56         }
       
    57     DEBUG_API("<-XAThreadSyncItfImpl_EnterCriticalSection");
       
    58     return ret;
       
    59     }
       
    60 
       
    61 XAresult XAThreadSyncItfImpl_ExitCriticalSection(XAThreadSyncItf self)
       
    62     {
       
    63     XAThreadSyncItfImpl* impl = NULL;
       
    64     XAresult ret = XA_RESULT_SUCCESS;
       
    65 
       
    66     impl = (XAThreadSyncItfImpl*) (*self);
       
    67     DEBUG_API("->XAThreadSyncItfImpl_ExitCriticalSection");
       
    68 
       
    69     if (!impl || impl != impl->self)
       
    70         {
       
    71         DEBUG_ERR("XA_RESULT_PARAMETER_INVALID");
       
    72         DEBUG_API("<-XAThreadSyncItfImpl_ExitCriticalSection");
       
    73         return XA_RESULT_PARAMETER_INVALID;
       
    74         }
       
    75 
       
    76     if (impl->engInCritical)
       
    77         {
       
    78         ret = XAImpl_UnlockMutex(impl->engCriticalSection);
       
    79         if (ret == XA_RESULT_SUCCESS)
       
    80             {
       
    81             impl->engInCritical = XA_BOOLEAN_FALSE;
       
    82             }
       
    83         }
       
    84     else
       
    85         {
       
    86         /* The engine must be in critical section state */
       
    87         DEBUG_ERR("XA_RESULT_PRECONDITIONS_VIOLATED");
       
    88         ret = XA_RESULT_PRECONDITIONS_VIOLATED;
       
    89         }
       
    90     DEBUG_API("<-XAThreadSyncItfImpl_ExitCriticalSection");
       
    91     return ret;
       
    92     }
       
    93 
       
    94 /**
       
    95  * XAThreadSyncItfImpl -specific methods
       
    96  **/
       
    97 
       
    98 XAThreadSyncItfImpl* XAThreadSyncItfImpl_Create()
       
    99     {
       
   100     XAThreadSyncItfImpl *self = NULL;
       
   101     DEBUG_API("->XAThreadSyncItfImpl_Create");
       
   102 
       
   103     self = (XAThreadSyncItfImpl*) calloc(1, sizeof(XAThreadSyncItfImpl));
       
   104 
       
   105     if (self)
       
   106         {
       
   107         /* init itf default implementation */
       
   108         self->itf.EnterCriticalSection
       
   109                 = XAThreadSyncItfImpl_EnterCriticalSection;
       
   110         self->itf.ExitCriticalSection
       
   111                 = XAThreadSyncItfImpl_ExitCriticalSection;
       
   112 
       
   113         if (XAImpl_CreateMutex(&(self->engCriticalSection))
       
   114                 != XA_RESULT_SUCCESS)
       
   115             {
       
   116             DEBUG_ERR("Mutex creation failed, abort");
       
   117             free(self);
       
   118             DEBUG_API("<-XAThreadSyncItfImpl_Create");
       
   119             return NULL;
       
   120             }
       
   121 
       
   122         self->self = self;
       
   123         }
       
   124     DEBUG_API("<-XAThreadSyncItfImpl_Create");
       
   125     return self;
       
   126     }
       
   127 
       
   128 void XAThreadSyncItfImpl_Free(XAThreadSyncItfImpl* self)
       
   129     {
       
   130     DEBUG_API("->XAThreadSyncItfImpl_Free");
       
   131     if(self)
       
   132         {
       
   133         assert(self==self->self);
       
   134         XAImpl_DeleteMutex(self->engCriticalSection);
       
   135         free(self);
       
   136         }
       
   137     DEBUG_API("<-XAThreadSyncItfImpl_Free");
       
   138     }
       
   139