Firmware
px4_atomic.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2019 PX4 Development Team. 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
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
54 #pragma once
55 
56 #ifdef __cplusplus
57 
58 #include <stdbool.h>
59 #include <stdint.h>
60 
61 namespace px4
62 {
63 
64 template <typename T>
65 class atomic
66 {
67 public:
68 
69 #ifdef __PX4_NUTTX
70  // Ensure that all operations are lock-free, so that 'atomic' can be used from
71  // IRQ handlers. This might not be required everywhere though.
72  static_assert(__atomic_always_lock_free(sizeof(T), 0), "atomic is not lock-free for the given type T");
73 #endif
74 
75 
76  explicit atomic(T value) : _value(value) {}
77 
81  inline T load() const
82  {
83  return __atomic_load_n(&_value, __ATOMIC_SEQ_CST);
84  }
85 
89  inline void store(T value)
90  {
91  __atomic_store(&_value, &value, __ATOMIC_SEQ_CST);
92  }
93 
98  inline T fetch_add(T num)
99  {
100  return __atomic_fetch_add(&_value, num, __ATOMIC_SEQ_CST);
101  }
102 
107  inline T fetch_sub(T num)
108  {
109  return __atomic_fetch_sub(&_value, num, __ATOMIC_SEQ_CST);
110  }
111 
116  inline T fetch_and(T num)
117  {
118  return __atomic_fetch_and(&_value, num, __ATOMIC_SEQ_CST);
119  }
120 
125  inline T fetch_xor(T num)
126  {
127  return __atomic_fetch_xor(&_value, num, __ATOMIC_SEQ_CST);
128  }
129 
134  inline T fetch_or(T num)
135  {
136  return __atomic_fetch_or(&_value, num, __ATOMIC_SEQ_CST);
137  }
138 
143  inline T fetch_nand(T num)
144  {
145  return __atomic_fetch_nand(&_value, num, __ATOMIC_SEQ_CST);
146  }
147 
156  inline bool compare_exchange(T *expected, T num)
157  {
158  return __atomic_compare_exchange(&_value, expected, num, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
159  }
160 
161 private:
162  T _value;
163 };
164 
165 using atomic_int = atomic<int>;
166 using atomic_int32_t = atomic<int32_t>;
167 using atomic_bool = atomic<bool>;
168 
169 } /* namespace px4 */
170 
171 #endif /* __cplusplus */
Definition: px4_nuttx_impl.cpp:44