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 elmenet in a static struct
34  * or global
35  */
36 #define METAL_MUTEX_INIT(m) { NULL }
37 /*
38  * METAL_MUTEX_DEFINE - used for defining and initializing a global or
39  * static singleton mutex
40  */
41 #define METAL_MUTEX_DEFINE(m) metal_mutex_t m = METAL_MUTEX_INIT(m)
42 
43 static inline void __metal_mutex_init(metal_mutex_t *mutex)
44 {
45  metal_assert(mutex);
46  mutex->m = xSemaphoreCreateMutex();
47  metal_assert(mutex->m != NULL);
48 }
49 
50 static inline void __metal_mutex_deinit(metal_mutex_t *mutex)
51 {
52  metal_assert(mutex && mutex->m != NULL);
53  vSemaphoreDelete(mutex->m);
54  mutex->m=NULL;
55 }
56 
57 static inline int __metal_mutex_try_acquire(metal_mutex_t *mutex)
58 {
59  metal_assert(mutex && mutex->m != NULL);
60  return xSemaphoreTake(mutex->m, ( TickType_t ) 0 );
61 }
62 
63 static inline void __metal_mutex_acquire(metal_mutex_t *mutex)
64 {
65  metal_assert(mutex && mutex->m != NULL);
66  xSemaphoreTake(mutex->m, portMAX_DELAY);
67 }
68 
69 static inline void __metal_mutex_release(metal_mutex_t *mutex)
70 {
71  metal_assert(mutex && mutex->m != NULL);
72  xSemaphoreGive(mutex->m);
73 }
74 
75 static inline int __metal_mutex_is_acquired(metal_mutex_t *mutex)
76 {
77  metal_assert(mutex && mutex->m != NULL);
78  return (NULL == xSemaphoreGetMutexHolder(mutex->m)) ? 0 : 1;
79 }
80 
81 #ifdef __cplusplus
82 }
83 #endif
84 
85 #endif /* __METAL_FREERTOS_MUTEX__H__ */
static void __metal_mutex_deinit(metal_mutex_t *mutex)
Definition: mutex.h:50
static void __metal_mutex_release(metal_mutex_t *mutex)
Definition: mutex.h:69
#define metal_assert(cond)
Assertion macro.
Definition: assert.h:21
static void __metal_mutex_acquire(metal_mutex_t *mutex)
Definition: mutex.h:63
Definition: mutex.h:28
static int __metal_mutex_is_acquired(metal_mutex_t *mutex)
Definition: mutex.h:75
struct k_sem metal_mutex_t
Definition: mutex.h:26
SemaphoreHandle_t m
Definition: mutex.h:29
static int __metal_mutex_try_acquire(metal_mutex_t *mutex)
Definition: mutex.h:57
static void __metal_mutex_init(metal_mutex_t *mutex)
Definition: mutex.h:43