1 /*
2  * Copyright (c) 2018, Pinecone Inc. and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 /*
8  * @file	nuttx/mutex.h
9  * @brief	NuttX mutex primitives for libmetal.
10  */
11 
12 #ifndef __METAL_MUTEX__H__
13 #error "Include metal/mutex.h instead of metal/nuttx/mutex.h"
14 #endif
15 
16 #ifndef __METAL_NUTTX_MUTEX__H__
17 #define __METAL_NUTTX_MUTEX__H__
18 
19 #include <nuttx/mutex.h>
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 typedef mutex_t metal_mutex_t;
26 
27 /*
28  * METAL_MUTEX_INIT - used for initializing an mutex element in a static struct
29  * or global
30  */
31 #define METAL_MUTEX_INIT(m) MUTEX_INITIALIZER
32 /*
33  * METAL_MUTEX_DEFINE - used for defining and initializing a global or
34  * static singleton mutex
35  */
36 #define METAL_MUTEX_DEFINE(m) metal_mutex_t m = MUTEX_INITIALIZER
37 
__metal_mutex_init(metal_mutex_t * mutex)38 static inline void __metal_mutex_init(metal_mutex_t *mutex)
39 {
40 	nxmutex_init(mutex);
41 }
42 
__metal_mutex_deinit(metal_mutex_t * mutex)43 static inline void __metal_mutex_deinit(metal_mutex_t *mutex)
44 {
45 	nxmutex_destroy(mutex);
46 }
47 
__metal_mutex_try_acquire(metal_mutex_t * mutex)48 static inline int __metal_mutex_try_acquire(metal_mutex_t *mutex)
49 {
50 	return nxmutex_trylock(mutex);
51 }
52 
__metal_mutex_acquire(metal_mutex_t * mutex)53 static inline void __metal_mutex_acquire(metal_mutex_t *mutex)
54 {
55 	nxmutex_lock(mutex);
56 }
57 
__metal_mutex_release(metal_mutex_t * mutex)58 static inline void __metal_mutex_release(metal_mutex_t *mutex)
59 {
60 	nxmutex_unlock(mutex);
61 }
62 
__metal_mutex_is_acquired(metal_mutex_t * mutex)63 static inline int __metal_mutex_is_acquired(metal_mutex_t *mutex)
64 {
65 	return nxmutex_is_locked(mutex);
66 }
67 
68 #ifdef __cplusplus
69 }
70 #endif
71 
72 #endif /* __METAL_NUTTX_MUTEX__H__ */
73