[apps/tidep0084.git] / tutorials / generic_sensor_tutorial / tutorial / SensorToCloud / components / common / src / mutex.c
1 /******************************************************************************
2 @file mutex.c
4 @brief TIMAC 2.0 API Mutex abstraction
6 Group: WCS LPC
7 $Target Devices: Linux: AM335x, Embedded Devices: CC1310, CC1350$
9 ******************************************************************************
10 $License: BSD3 2016 $
12 Copyright (c) 2015, Texas Instruments Incorporated
13 All rights reserved.
15 Redistribution and use in source and binary forms, with or without
16 modification, are permitted provided that the following conditions
17 are met:
19 * Redistributions of source code must retain the above copyright
20 notice, this list of conditions and the following disclaimer.
22 * Redistributions in binary form must reproduce the above copyright
23 notice, this list of conditions and the following disclaimer in the
24 documentation and/or other materials provided with the distribution.
26 * Neither the name of Texas Instruments Incorporated nor the names of
27 its contributors may be used to endorse or promote products derived
28 from this software without specific prior written permission.
30 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
31 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
32 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
35 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
36 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
37 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
38 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
39 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
40 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 ******************************************************************************
42 $Release Name: TI-15.4Stack Linux x64 SDK$
43 $Release Date: Jun 28, 2017 (2.02.00.03)$
44 *****************************************************************************/
46 #include "compiler.h"
47 #include "mutex.h"
48 #include "timer.h"
49 #include "log.h"
50 #include "threads.h"
52 #include <stdio.h>
53 #include <malloc.h>
54 #include <string.h>
55 #include <errno.h>
56 #include "hlos_specific.h"
58 static const int is_mutex = 'm';
60 /*
61 * @struct mutex - internal representation of a mutex
62 */
63 struct mutex {
64 /*! for debug purposes */
65 const char *dbg_name;
67 /*! for structure pointing checks */
68 const int *check_val;
70 /*! Underlying implimentation is a linux semaphore */
71 intptr_t m;
73 /*! Who owns this, requird for recursive mutexes */
74 intptr_t owner;
76 /*! non-zero recursive lock depth counter */
77 int recursive_lock;
78 };
80 /* get a safe debug name for this mutex */
81 static const char *_mutex_name(struct mutex *pM)
82 {
83 const char *cp;
85 cp = pM->dbg_name;
86 if(cp == NULL)
87 {
88 cp = "mutex-no-name";
89 }
90 return (cp);
91 }
93 /* convert the mutex handle to a mutex structure */
94 static struct mutex * h2m(intptr_t h)
95 {
96 struct mutex *pM;
97 pM = NULL;
98 if(h)
99 {
100 pM = (struct mutex *)(h);
101 if(pM->check_val != &is_mutex)
102 {
103 pM = NULL;
104 }
105 }
106 if(pM == NULL)
107 {
108 BUG_HERE("%s not a mutex (%p)\n",
109 THREAD_selfName(), (void *)(h));
110 }
111 return (pM);
112 }
114 /*
115 * Create a mutex
116 *
117 * Public function defined in mutex.h
118 */
119 intptr_t MUTEX_create(const char *name)
120 {
121 struct mutex *pM;
123 /* get space. */
124 pM = calloc(1, sizeof(struct mutex));
125 if(!pM)
126 {
127 if(name == NULL)
128 {
129 name = "mutex-no-name";
130 }
131 LOG_printf(LOG_ERROR,
132 "%s:(%s) MUTEX_Create() no memory\n",
133 THREAD_selfName(), name);
134 return (0);
135 }
136 /* set the checkvalue so we can verify the handle is a mutex later */
137 pM->check_val = &is_mutex;
138 pM->recursive_lock = 0;
140 pM->m = _ATOMIC_local_create();
141 if(pM->m == 0)
142 {
143 /* cannot create the semaphore? */
144 BUG_HERE("cannot create mutex\n");
145 }
147 if(name == NULL)
148 {
149 name = "mutex-no-name";
150 }
151 pM->dbg_name = strdup(name);
152 if(pM->dbg_name == NULL)
153 {
154 BUG_HERE("no memory\n");
155 }
157 /*LOG_printf(LOG_ALWAYS, "Create mutex: %s @ %p\n", pM->dbg_name, (void *)(pM)); */
158 return ((intptr_t)(pM));
159 }
161 /*
162 * Destroy a mutex
163 *
164 * Public function defined in mutex.h
165 */
166 void MUTEX_destroy(intptr_t h)
167 {
168 struct mutex *pM;
170 if(h)
171 {
172 /*LOG_printf(LOG_ALWAYS, "Destroy mutex: @ %p\n", (void *)(h)); */
173 }
175 /* recover our handle */
176 pM = h2m(h);
177 if(!pM)
178 {
179 return;
180 }
182 LOG_printf(LOG_DBG_MUTEX,
183 "%s(%s) MUTEX_Destroy()\n",
184 THREAD_selfName(), _mutex_name(pM));
185 /* destroy... destroy... */
186 if(pM->dbg_name)
187 {
188 free_const((const void *)(pM->dbg_name));
189 pM->dbg_name = NULL;
190 }
191 _ATOMIC_local_destroy(pM->m);
192 pM->m = 0;
193 memset((void *)(pM), 0, sizeof(*pM));
194 free((void *)(pM));
195 }
197 /*
198 * Lock a mutex
199 *
200 * Public function defined in mutex.h
201 */
202 int MUTEX_lock(intptr_t h, int timeout_mSecs)
203 {
204 struct mutex *pM;
205 int r;
206 bool need_lock;
207 intptr_t whoami;
208 uint32_t tstart, tend;
210 /* recover our mutex pointer */
211 pM = h2m(h);
212 if(pM == NULL)
213 {
214 return (-1);
215 }
217 /* inspect.. */
218 whoami = THREAD_self();
219 need_lock = true;
220 _ATOMIC_global_lock();
221 if(pM->recursive_lock)
222 {
223 if(pM->owner == whoami)
224 {
225 need_lock = false;
226 }
227 }
228 _ATOMIC_global_unlock();
229 tstart = TIMER_getNow();
230 r = 0;
231 if(need_lock)
232 {
233 r = _ATOMIC_local_lock(pM->m, timeout_mSecs);
234 }
235 if(r == 0)
236 {
237 /* success */
238 /* so we can detect recursive locks */
239 pM->owner = THREAD_self();
240 pM->recursive_lock += 1;
241 LOG_printf(LOG_DBG_MUTEX,
242 "%s: MUTEX_lock(%s) success (recursion=%d)\n",
243 THREAD_selfName(),
244 _mutex_name(pM),
245 pM->recursive_lock);
246 }
247 else
248 {
249 tend = TIMER_getNow();
250 LOG_printf(LOG_DBG_MUTEX, "%s: MUTEX_lock(%s) failed (timeout=%d), s=%d e=%d\n",
251 THREAD_selfName(),
252 _mutex_name(pM), timeout_mSecs, (int)tstart, (int)tend);
253 }
254 return (r);
255 }
257 /*
258 * Unlock a mutex
259 *
260 * Public function defined in mutex.h
261 */
262 int MUTEX_unLock(intptr_t h)
263 {
264 struct mutex *pM;
265 int r;
266 /* recover handle */
267 pM = h2m(h);
268 if(pM == NULL)
269 {
270 return (-1);
271 }
273 _ATOMIC_global_lock();
274 r = pM->recursive_lock;
275 if(r == 0)
276 {
277 BUG_HERE("mutex negative unlock!\n");
278 }
279 r--;
280 pM->recursive_lock = r;
281 if(r == 0)
282 {
283 pM->owner = 0; /* nobody owns this */
284 _ATOMIC_local_unlock(pM->m);
285 }
286 _ATOMIC_global_unlock();
287 if(r)
288 {
289 LOG_printf(LOG_DBG_MUTEX,
290 "%s: MUTEX_unLock(%s) still locked(%d)\n",
291 THREAD_selfName(), _mutex_name(pM), r);
292 }
293 else
294 {
295 LOG_printf(LOG_DBG_MUTEX,
296 "%s: MUTEX_unlock(%s) unlocked\n",
297 THREAD_selfName(), _mutex_name(pM));
298 }
299 return (0);
300 }
302 const char *MUTEX_lockerName(intptr_t h)
303 {
304 const char *cp;
305 struct mutex *pM;
307 pM = h2m(h);
309 cp = "none";
310 if(pM != NULL)
311 {
312 /* return the name of the locking thread */
313 cp = THREAD_name(pM->owner);
314 }
315 return (cp);
316 }
318 /*
319 * Determine if a mutex s locked
320 *
321 * Public function defined in mutex.h
322 */
323 bool MUTEX_isLocked(intptr_t h)
324 {
325 bool answer;
326 struct mutex *pM;
328 /* recover our handle */
329 pM = h2m(h);
330 if(pM == NULL)
331 {
332 return (false);
333 }
335 answer = (pM->recursive_lock != 0);
336 LOG_printf(LOG_DBG_MUTEX, "%s(%s) MUTEX_IsLocked() = %s\n",
337 THREAD_selfName(), _mutex_name(pM), answer ? "true" : "false");
338 return (answer);
339 }
341 /*
342 * ========================================
343 * Texas Instruments Micro Controller Style
344 * ========================================
345 * Local Variables:
346 * mode: c
347 * c-file-style: "bsd"
348 * tab-width: 4
349 * c-basic-offset: 4
350 * indent-tabs-mode: nil
351 * End:
352 * vim:set filetype=c tabstop=4 shiftwidth=4 expandtab=true
353 */