RTEMS 6.1-rc2
Loading...
Searching...
No Matches
smplockseq.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: BSD-2-Clause */
2
12/*
13 * Copyright (c) 2016 embedded brains GmbH & Co. KG
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#ifndef _RTEMS_SCORE_SMPLOCKSEQ_H
38#define _RTEMS_SCORE_SMPLOCKSEQ_H
39
40#include <rtems/score/cpuopts.h>
41
42#if defined(RTEMS_SMP)
43
44#include <rtems/score/assert.h>
45#include <rtems/score/atomic.h>
46
47#ifdef __cplusplus
48extern "C" {
49#endif /* __cplusplus */
50
70typedef struct {
76 Atomic_Uint sequence;
77} SMP_sequence_lock_Control;
78
82#define SMP_SEQUENCE_LOCK_INITIALIZER { ATOMIC_INITIALIZER_UINT( 0 ) }
83
91static inline void _SMP_sequence_lock_Initialize( SMP_sequence_lock_Control *lock )
92{
93 _Atomic_Init_uint( &lock->sequence, 0 );
94}
95
103static inline void _SMP_sequence_lock_Destroy( SMP_sequence_lock_Control *lock )
104{
105 (void) lock;
106}
107
119static inline unsigned int _SMP_sequence_lock_Write_begin(
120 SMP_sequence_lock_Control *lock
121)
122{
123 unsigned int seq;
124
125 seq = _Atomic_Load_uint( &lock->sequence, ATOMIC_ORDER_RELAXED );
126 _Assert( seq % 2 == 0 );
127
128 _Atomic_Store_uint( &lock->sequence, seq + 1, ATOMIC_ORDER_RELAXED );
129
130 /* There is no atomic store with acquire/release semantics */
131 _Atomic_Fence( ATOMIC_ORDER_ACQ_REL );
132
133 return seq;
134}
135
142static inline void _SMP_sequence_lock_Write_end(
143 SMP_sequence_lock_Control *lock,
144 unsigned int seq
145)
146{
147 _Atomic_Store_uint( &lock->sequence, seq + 2, ATOMIC_ORDER_RELEASE );
148}
149
159static inline unsigned int _SMP_sequence_lock_Read_begin(
160 const SMP_sequence_lock_Control *lock
161)
162{
163 return _Atomic_Load_uint( &lock->sequence, ATOMIC_ORDER_ACQUIRE );
164}
165
177static inline bool _SMP_sequence_lock_Read_retry(
178 SMP_sequence_lock_Control *lock,
179 unsigned int seq
180)
181{
182 unsigned int seq2;
183
184 seq2 = _Atomic_Fetch_add_uint( &lock->sequence, 0, ATOMIC_ORDER_RELEASE );
185 return seq != seq2 || seq % 2 != 0;
186}
187
190#ifdef __cplusplus
191}
192#endif /* __cplusplus */
193
194#endif /* RTEMS_SMP */
195
196#endif /* _RTEMS_SCORE_SMPLOCKSEQ_H */
This header file provides the interfaces of the Assert Handler.
#define _Assert(_e)
Assertion similar to assert() controlled via RTEMS_DEBUG instead of NDEBUG and static analysis runs.
Definition: assert.h:96
This header file provides the interfaces of the Atomic Operations.