libmetal
mutex.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, Xilinx Inc. and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 /*
8  * @file freertos/mutex.h
9  * @brief FreeRTOS mutex primitives for libmetal.
10  */
11 
12 #ifndef __METAL_MUTEX__H__
13 #error "Include metal/mutex.h instead of metal/freertos/mutex.h"
14 #endif
15 
16 #ifndef __METAL_FREERTOS_MUTEX__H__
17 #define __METAL_FREERTOS_MUTEX__H__
18 
19 #include <metal/assert.h>
20 #include "FreeRTOS.h"
21 #include "semphr.h"
22 
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 typedef struct {
29  SemaphoreHandle_t m;
31 
32 /*
33  * METAL_MUTEX_INIT - used for initializing an mutex element in a static struct
34  * or global
35  */
36 #if defined(__GNUC__)
37 #define METAL_MUTEX_INIT(m) { NULL }; \
38 _Pragma("GCC warning\"static initialisation of the mutex is deprecated\"")
39 #elif defined(__ICCARM__)
40 #define DO_PRAGMA(x) _Pragma(#x)
41 #define METAL_MUTEX_INIT(m) { NULL }; \
42 DO_PRAGMA(message("Warning: static initialisation of the mutex is deprecated"))
43 #else
44 #define METAL_MUTEX_INIT(m) { NULL }
45 #endif
46 
47 /*
48  * METAL_MUTEX_DEFINE - used for defining and initializing a global or
49  * static singleton mutex
50  */
51 #define METAL_MUTEX_DEFINE(m) metal_mutex_t m = METAL_MUTEX_INIT(m)
52 
53 static inline void __metal_mutex_init(metal_mutex_t *mutex)
54 {
55  metal_assert(mutex);
56  mutex->m = xSemaphoreCreateMutex();
57  metal_assert(mutex->m);
58 }
59 
60 static inline void __metal_mutex_deinit(metal_mutex_t *mutex)
61 {
62  metal_assert(mutex && mutex->m);
63  vSemaphoreDelete(mutex->m);
64  mutex->m = NULL;
65 }
66 
67 static inline int __metal_mutex_try_acquire(metal_mutex_t *mutex)
68 {
69  metal_assert(mutex && mutex->m);
70  return xSemaphoreTake(mutex->m, (TickType_t)0);
71 }
72 
73 static inline void __metal_mutex_acquire(metal_mutex_t *mutex)
74 {
75  metal_assert(mutex && mutex->m);
76  xSemaphoreTake(mutex->m, portMAX_DELAY);
77 }
78 
79 static inline void __metal_mutex_release(metal_mutex_t *mutex)
80 {
81  metal_assert(mutex && mutex->m);
82  xSemaphoreGive(mutex->m);
83 }
84 
85 static inline int __metal_mutex_is_acquired(metal_mutex_t *mutex)
86 {
87  metal_assert(mutex && mutex->m);
88  return (!xSemaphoreGetMutexHolder(mutex->m)) ? 0 : 1;
89 }
90 
91 #ifdef __cplusplus
92 }
93 #endif
94 
95 #endif /* __METAL_FREERTOS_MUTEX__H__ */
__metal_mutex_release
static void __metal_mutex_release(metal_mutex_t *mutex)
Definition: mutex.h:79
__metal_mutex_try_acquire
static int __metal_mutex_try_acquire(metal_mutex_t *mutex)
Definition: mutex.h:67
metal_assert
#define metal_assert(cond)
Assertion macro.
Definition: assert.h:21
metal_mutex_t::m
SemaphoreHandle_t m
Definition: mutex.h:29
__metal_mutex_is_acquired
static int __metal_mutex_is_acquired(metal_mutex_t *mutex)
Definition: mutex.h:85
__metal_mutex_deinit
static void __metal_mutex_deinit(metal_mutex_t *mutex)
Definition: mutex.h:60
metal_mutex_t
mutex_t metal_mutex_t
Definition: mutex.h:25
__metal_mutex_init
static void __metal_mutex_init(metal_mutex_t *mutex)
Definition: mutex.h:53
__metal_mutex_acquire
static void __metal_mutex_acquire(metal_mutex_t *mutex)
Definition: mutex.h:73
metal_mutex_t
Definition: mutex.h:28