]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - ipc/ipcdev.git/blob - linux/src/api/gates/GateMutex.c
Linux: Update user AF_RPMSG define for 5.15+ kernels
[ipc/ipcdev.git] / linux / src / api / gates / GateMutex.c
1 /*
2  * Copyright (c) 2013-2015, 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;
87     (Void)params;
88     (Void)eb;
90     GateMutex_Object * obj = (GateMutex_Object *)calloc(1,
91         sizeof(GateMutex_Object));
92     if (obj == NULL) {
93         return NULL;
94     }
96     memset(obj, 0, sizeof(GateMutex_Object));
97     IGateProvider_ObjectInitializer(obj, GateMutex);
98     pthread_mutexattr_init(&attr);
99     pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
100     pthread_mutex_init(&(obj->mutex), &attr);
102     return (GateMutex_Handle)obj;
105 Int GateMutex_delete(GateMutex_Handle * handle)
107     GateMutex_Object * obj = NULL;
109     if (handle == NULL) {
110         return GateMutex_E_INVALIDARG;
111     }
112     if (*handle == NULL) {
113         return GateMutex_E_INVALIDARG;
114     }
116     obj = (GateMutex_Object *)*handle;
117     pthread_mutex_destroy(&(obj->mutex));
118     free(*handle);
119     *handle = NULL;
121     return GateMutex_S_SUCCESS;
124 IArg GateMutex_enter (GateMutex_Handle gmHandle)
126     GateMutex_Object * obj = (GateMutex_Object *)gmHandle;
127     int ret;
129     ret = pthread_mutex_lock(&(obj->mutex));
130     assert(ret == 0);
132     return (IArg)ret;
135 Void GateMutex_leave (GateMutex_Handle gmHandle, IArg key)
137     GateMutex_Object * obj = (GateMutex_Object *)gmHandle;
138     int ret;
139     (Void)key;
141     ret = pthread_mutex_unlock(&(obj->mutex));
142     assert(ret == 0);
146 #if defined (__cplusplus)
148 #endif /* defined (__cplusplus) */