libmetal
Loading...
Searching...
No Matches
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
25extern "C" {
26#endif
27
28typedef 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 }; \
42DO_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
53static 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
60static 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
68{
69 metal_assert(mutex && mutex->m);
70 return xSemaphoreTake(mutex->m, (TickType_t)0);
71}
72
73static inline void __metal_mutex_acquire(metal_mutex_t *mutex)
74{
75 metal_assert(mutex && mutex->m);
76 xSemaphoreTake(mutex->m, portMAX_DELAY);
77}
78
79static inline void __metal_mutex_release(metal_mutex_t *mutex)
80{
81 metal_assert(mutex && mutex->m);
82 xSemaphoreGive(mutex->m);
83}
84
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__ */
#define metal_assert(cond)
Assertion macro.
Definition: assert.h:21
Definition: mutex.h:28
SemaphoreHandle_t m
Definition: mutex.h:29
static void __metal_mutex_release(metal_mutex_t *mutex)
Definition: mutex.h:79
static void __metal_mutex_deinit(metal_mutex_t *mutex)
Definition: mutex.h:60
static int __metal_mutex_try_acquire(metal_mutex_t *mutex)
Definition: mutex.h:67
static int __metal_mutex_is_acquired(metal_mutex_t *mutex)
Definition: mutex.h:85
static void __metal_mutex_acquire(metal_mutex_t *mutex)
Definition: mutex.h:73
static void __metal_mutex_init(metal_mutex_t *mutex)
Definition: mutex.h:53