Contiki-NG
mutex.h
1 /*
2  * Copyright (c) 2017, George Oikonomou - http://www.spd.gr
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the copyright holder nor the names of its
15  * contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29  * OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 /*---------------------------------------------------------------------------*/
32 /**
33  * \addtogroup sys
34  * @{
35  *
36  * \defgroup mutex Mutexes
37  * @{
38  *
39  * This library provides an API and generic implementation of mutexes.
40  *
41  * Calling code should manipulate mutexes through the mutex_try_lock() and
42  * mutex_unlock() macros. By default, those macros will expand to the generic
43  * mutex manipulation implementations provided here. While these will work,
44  * they do reply on disabling the master interrupt in order to perform the
45  * lock/unlock operation.
46  *
47  * It is possible to override those generic implementation with CPU-specific
48  * implementations that exploit synchronisation instructions. To do so,
49  * create a CPU-specific header file. In this file, define mutex_try_lock()
50  * and mutex_unlock() to expand to the respective CPU function names. These
51  * can (but do not have to) be inlined. Then define MUTEX_CONF_ARCH_HEADER_PATH
52  * as this header's filename.
53  */
54 /*---------------------------------------------------------------------------*/
55 #ifndef MUTEX_H_
56 #define MUTEX_H_
57 /*---------------------------------------------------------------------------*/
58 #include "contiki.h"
59 
60 #include <stdint.h>
61 #include <stdbool.h>
62 /*---------------------------------------------------------------------------*/
63 #define MUTEX_STATUS_LOCKED 1 /** The mutex is locked */
64 #define MUTEX_STATUS_UNLOCKED 0 /** The mutex is not locked */
65 /*---------------------------------------------------------------------------*/
66 #ifdef MUTEX_CONF_ARCH_HEADER_PATH
67 #include MUTEX_CONF_ARCH_HEADER_PATH
68 #endif /* MUTEX_CONF_ARCH_HEADER_PATH */
69 /*---------------------------------------------------------------------------*/
70 #if !MUTEX_CONF_HAS_MUTEX_T
71 /**
72  * \brief Mutex data type
73  *
74  * It is possible for the platform to override this with its own typedef. In
75  * this scenario, make sure to also define MUTEX_CONF_HAS_MUTEX_T as 1.
76  */
77 typedef uint_fast8_t mutex_t;
78 #endif
79 /*---------------------------------------------------------------------------*/
80 #ifndef mutex_try_lock
81 /**
82  * \brief Try to lock a mutex
83  * \param m A pointer to the mutex to be locked
84  * \retval true Locking succeeded
85  * \retval false Locking failed (the mutex is already locked)
86  *
87  * This macro will expand to mutex_generic_try_lock() or to a CPU-provided
88  * implementation. Platform-independent code should use this macro instead
89  * of mutex_generic_try_lock().
90  */
91 #define mutex_try_lock(m) mutex_generic_try_lock(m)
92 #endif
93 
94 #ifndef mutex_unlock
95 /**
96  * \brief Unlock a previously acquired mutex
97  * \param m A pointer to the mutex to be unlocked
98  *
99  * This macro will expand to mutex_generic_unlock() or to a CPU-provided
100  * implementation. Platform-independent code should use this macro instead
101  * of mutex_generic_unlock().
102  */
103 #define mutex_unlock(m) mutex_generic_unlock(m)
104 #endif
105 /*---------------------------------------------------------------------------*/
106 /**
107  * \brief Try to lock a mutex
108  * \param mutex A pointer to the mutex to be locked
109  * \retval true Locking succeeded
110  * \retval false Locking failed (the mutex is already locked)
111  *
112  * Do not call this function directly. Use the mutex_try_lock() macro instead.
113  */
114 bool mutex_generic_try_lock(volatile mutex_t *mutex);
115 
116 /**
117  * \brief Unlock a previously acquired mutex
118  * \param mutex A pointer to the mutex to be unlocked
119  *
120  * Do not call this function directly. Use the mutex_unlock() macro instead.
121  */
122 void mutex_generic_unlock(volatile mutex_t *mutex);
123 /*---------------------------------------------------------------------------*/
124 #endif /* MUTEX_H_ */
125 /*---------------------------------------------------------------------------*/
126 /**
127  * @}
128  * @}
129  */
uint_fast8_t mutex_t
Mutex data type.
Definition: mutex.h:77
void mutex_generic_unlock(volatile mutex_t *mutex)
Unlock a previously acquired mutex.
Definition: mutex.c:61
bool mutex_generic_try_lock(volatile mutex_t *mutex)
Try to lock a mutex.
Definition: mutex.c:45