StateOS
Loading...
Searching...
No Matches
osspinlock.h
1/******************************************************************************
2
3 @file StateOS: osspinlock.h
4 @author Rajmund Szymanski
5 @date 18.11.2025
6 @brief This file contains definitions for StateOS.
7
8 ******************************************************************************
9
10 Copyright (c) 2018-2022 Rajmund Szymanski. All rights reserved.
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to
14 deal in the Software without restriction, including without limitation the
15 rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16 sell copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included
20 in all copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28 IN THE SOFTWARE.
29
30 ******************************************************************************/
31
32#ifndef __STATEOS_SPN_H
33#define __STATEOS_SPN_H
34
35#include "oskernel.h"
36#include "oscriticalsection.h"
37
43
47
48typedef uint_fast8_t spn_t;
49typedef uint_fast8_t spn_id [];
50
51/******************************************************************************
52 *
53 * Name : _SPN_INIT
54 *
55 * Description : create and initialize a spin lock object
56 *
57 * Parameters : none
58 *
59 * Return : spin lock object
60 *
61 * Note : for internal use
62 *
63 ******************************************************************************/
64
65#define _SPN_INIT() 0
66
73#define OS_SPN( spn ) \
74 spn_t spn[] = { _SPN_INIT() }
75
76/** static alias of OS_SPN() */
77#define static_SPN( spn ) \
78 static spn_t spn[] = { _SPN_INIT() }
79
87
88#ifndef __cplusplus
89#define SPN_INIT() \
90 _SPN_INIT()
91#endif
92
100
101#ifndef __cplusplus
102#define SPN_CREATE() \
103 { SPN_INIT () }
104/** alias of SPN_CREATE() */
105#define SPN_NEW \
106 SPN_CREATE
107#endif
108
109#ifdef __cplusplus
110extern "C" {
111#endif
112
113/******************************************************************************
114 *
115 * Name : core_spn_tryLock
116 *
117 * Description : try lock the spin lock object
118 *
119 * Parameters
120 * spn : pointer to spin lock object
121 *
122 * Return
123 * true : spin lock object was successfully locked or OS_ATOMICS is zero
124 * false : otherwise
125 *
126 * Note : for internal use
127 *
128 ******************************************************************************/
129
130__STATIC_INLINE
131bool core_spn_tryLock( spn_t *spn )
132{
133#if OS_ATOMICS
134 return __STD atomic_exchange((__STD atomic_uint_fast8_t *)spn, 1) == 0;
135#else
136 (void) spn;
137 return true;
138#endif
139}
140
141/******************************************************************************
142 *
143 * Name : core_spn_lock
144 *
145 * Description : lock the spin lock object
146 * (wait indefinitely if the spin lock object can't be locked immediately)
147 * or do nothing if OS_ATOMICS is zero
148 *
149 * Parameters
150 * spn : pointer to spin lock object
151 *
152 * Return : none
153 *
154 * Note : for internal use
155 *
156 ******************************************************************************/
157
158__STATIC_INLINE
159void core_spn_lock( spn_t *spn )
160{
161#if OS_ATOMICS
162 while (!core_spn_tryLock(spn));
163#else
164 (void) spn;
165#endif
166}
167
168/******************************************************************************
169 *
170 * Name : core_spn_unlock
171 *
172 * Description : unlock the spin lock object
173 * or do nothing if OS_ATOMICS is zero
174 *
175 * Parameters
176 * spn : pointer to spin lock object
177 *
178 * Return : none
179 *
180 * Note : for internal use
181 *
182 ******************************************************************************/
183
184__STATIC_INLINE
185void core_spn_unlock( spn_t *spn )
186{
187#if OS_ATOMICS
188 __STD atomic_store((__STD atomic_uint_fast8_t *)spn, 0);
189#else
190 (void) spn;
191#endif
192}
193
201
202__STATIC_INLINE
203void spn_init( spn_t *spn )
204{
205 *spn = 0;
206}
207
220#define spn_lock(spn) \
221 sys_lock(); core_spn_lock(spn)
222
235#define spn_unlock(spn) \
236 core_spn_unlock(spn); sys_unlock()
237
238#ifdef __cplusplus
239}
240#endif
241
242/* -------------------------------------------------------------------------- */
243
244#if defined(__cplusplus) && (__cplusplus >= 201103L) && !defined(__STATEOS_STDCXX)
245namespace stateos {
246
254struct SpinLock : private CriticalSection
255{
257 explicit
258 SpinLock( spn_t *_spn ): spn_{ _spn} { core_spn_lock(spn_); }
260 explicit
261 SpinLock( spn_t& _spn ): spn_{&_spn} { core_spn_lock(spn_); }
262
263 ~SpinLock() { core_spn_unlock(spn_); }
264
265 SpinLock( SpinLock&& ) = delete;
266 SpinLock( const SpinLock& ) = delete;
267 SpinLock& operator=( SpinLock&& ) = delete;
268 SpinLock& operator=( const SpinLock& ) = delete;
269
270 private:
271 spn_t *spn_;
272};
273
274} // namespace
275#endif//__cplusplus
276
277/* -------------------------------------------------------------------------- */
278
280
281#endif//__STATEOS_SPN_H
void spn_init(spn_t *spn)
initialize a spin lock object
Definition osspinlock.h:202
uint_fast8_t spn_t
spin lock
Definition osspinlock.h:48
CriticalSection()
constructor, see the constructor parameters of the class
Definition oscriticalsection.h:181
use a spin lock object
Definition osspinlock.h:254
SpinLock(spn_t *_spn)
constructor, see the constructor parameters of the class
Definition osspinlock.h:257