IntrOS
Loading...
Searching...
No Matches
osspinlock.h
1/******************************************************************************
2
3 @file IntrOS: osspinlock.h
4 @author Rajmund Szymanski
5 @date 18.11.2025
6 @brief This file contains definitions for IntrOS.
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 __INTROS_SPN_H
33#define __INTROS_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
199
200__STATIC_INLINE
201void spn_init( spn_t *spn )
202{
203 *spn = 0;
204}
205
217#define spn_lock(spn) \
218 sys_lock(); core_spn_lock(spn)
219
231#define spn_unlock(spn) \
232 core_spn_unlock(spn); sys_unlock()
233
234#ifdef __cplusplus
235}
236#endif
237
238/* -------------------------------------------------------------------------- */
239
240#if defined(__cplusplus)
241namespace intros {
242
250struct SpinLock : private CriticalSection
251{
253 explicit
254 SpinLock( spn_t *_spn ): spn_{ _spn} { core_spn_lock(spn_); }
256 explicit
257 SpinLock( spn_t& _spn ): spn_{&_spn} { core_spn_lock(spn_); }
258
259 ~SpinLock() { core_spn_unlock(spn_); }
260
261 SpinLock( SpinLock&& ) = delete;
262 SpinLock( const SpinLock& ) = delete;
263 SpinLock& operator=( SpinLock&& ) = delete;
264 SpinLock& operator=( const SpinLock& ) = delete;
265
266 private:
267 spn_t *spn_;
268};
269
270} // namespace
271#endif//__cplusplus
272
273/* -------------------------------------------------------------------------- */
274
276
277#endif//__INTROS_SPN_H
void spn_init(spn_t *spn)
initialize a spin lock object
Definition osspinlock.h:200
uint_fast8_t spn_t
spin lock
Definition osspinlock.h:48
CriticalSection()
constructor, see the constructor parameters of the class
Definition oscriticalsection.h:167
use a spin lock object
Definition osspinlock.h:250
SpinLock(spn_t *_spn)
constructor, see the constructor parameters of the class
Definition osspinlock.h:253