]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - ipc/ipcdev.git/blob - linux/src/api/gates/GateMutex.c
Linux: Update to autotools files for GateMP addition
[ipc/ipcdev.git] / linux / src / api / gates / GateMutex.c
1 /*
2  * Copyright (c) 2013-2014, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * *  Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * *  Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * *  Neither the name of Texas Instruments Incorporated nor the names of
17  *    its contributors may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 /*
33  *  ======== GateMutex.c ========
34  */
37 /* Standard headers */
38 #include <ti/ipc/Std.h>
40 /* Linux headers */
41 #include <stdlib.h>
42 #include <pthread.h>
43 #include <string.h>
44 #include <assert.h>
46 #include <IGateProvider.h>
48 /*
49  * TODO: does this belong in ti/ipc/Std.h? We should consider getting rid of
50  *       error blocks from the GateMutex.h interface.
51  */
52 typedef UInt32            Error_Block;
53 #include <GateMutex.h>
56 #if defined (__cplusplus)
57 extern "C" {
58 #endif
61 /* -----------------------------------------------------------------------------
62  *  Structs & Enums
63  * -----------------------------------------------------------------------------
64  */
65 struct GateMutex_Object {
66     IGateProvider_SuperObject;   /* For inheritance from IGateProvider */
67     pthread_mutex_t mutex;       /* Mutex lock */
68 };
71 /* -----------------------------------------------------------------------------
72  *  Forward declarations
73  * -----------------------------------------------------------------------------
74  */
75 IArg GateMutex_enter(GateMutex_Handle gmhandle);
76 Void GateMutex_leave(GateMutex_Handle gmhandle, IArg key);
79 /* -----------------------------------------------------------------------------
80  *  APIs
81  * -----------------------------------------------------------------------------
82  */
83 GateMutex_Handle GateMutex_create(const GateMutex_Params * params,
84     Error_Block *eb)
85 {
86     pthread_mutexattr_t attr;
88     GateMutex_Object * obj = (GateMutex_Object *)calloc(1,
89         sizeof(GateMutex_Object));
90     if (obj == NULL) {
91         return NULL;
92     }
94     memset(obj, 0, sizeof(GateMutex_Object));
95     IGateProvider_ObjectInitializer(obj, GateMutex);
96     pthread_mutexattr_init(&attr);
97     pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
98     pthread_mutex_init(&(obj->mutex), &attr);
100     return (GateMutex_Handle)obj;
103 Int GateMutex_delete(GateMutex_Handle * handle)
105     if (handle == NULL) {
106         return GateMutex_E_INVALIDARG;
107     }
108     if (*handle == NULL) {
109         return GateMutex_E_INVALIDARG;
110     }
111     free(*handle);
112     *handle = NULL;
114     return GateMutex_S_SUCCESS;
117 IArg GateMutex_enter (GateMutex_Handle gmHandle)
119     GateMutex_Object * obj = (GateMutex_Object *)gmHandle;
120     int ret;
122     ret = pthread_mutex_lock(&(obj->mutex));
123     assert(ret == 0);
125     return (IArg)ret;
128 Void GateMutex_leave (GateMutex_Handle gmHandle, IArg key)
130     GateMutex_Object * obj = (GateMutex_Object *)gmHandle;
131     int ret;
133     ret = pthread_mutex_unlock(&(obj->mutex));
134     assert(ret == 0);
138 #if defined (__cplusplus)
140 #endif /* defined (__cplusplus) */