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;
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;
101 }
103 Int GateMutex_delete(GateMutex_Handle * handle)
104 {
105 GateMutex_Object * obj = (GateMutex_Object *)*handle;
107 if (handle == NULL) {
108 return GateMutex_E_INVALIDARG;
109 }
110 if (*handle == NULL) {
111 return GateMutex_E_INVALIDARG;
112 }
113 pthread_mutex_destroy(&(obj->mutex));
114 free(*handle);
115 *handle = NULL;
117 return GateMutex_S_SUCCESS;
118 }
120 IArg GateMutex_enter (GateMutex_Handle gmHandle)
121 {
122 GateMutex_Object * obj = (GateMutex_Object *)gmHandle;
123 int ret;
125 ret = pthread_mutex_lock(&(obj->mutex));
126 assert(ret == 0);
128 return (IArg)ret;
129 }
131 Void GateMutex_leave (GateMutex_Handle gmHandle, IArg key)
132 {
133 GateMutex_Object * obj = (GateMutex_Object *)gmHandle;
134 int ret;
136 ret = pthread_mutex_unlock(&(obj->mutex));
137 assert(ret == 0);
138 }
141 #if defined (__cplusplus)
142 }
143 #endif /* defined (__cplusplus) */