RTEMS
smplockseq.h
Go to the documentation of this file.
1 
9 /*
10  * Copyright (c) 2016 embedded brains GmbH
11  *
12  * The license and distribution terms for this file may be
13  * found in the file LICENSE in this distribution or at
14  * http://www.rtems.org/license/LICENSE.
15  */
16 
17 #ifndef _RTEMS_SCORE_SMPLOCKSEQ_H
18 #define _RTEMS_SCORE_SMPLOCKSEQ_H
19 
20 #include <rtems/score/cpuopts.h>
21 
22 #if defined(RTEMS_SMP)
23 
24 #include <rtems/score/assert.h>
25 #include <rtems/score/atomic.h>
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif /* __cplusplus */
30 
50 typedef struct {
56  Atomic_Uint sequence;
58 
62 #define SMP_SEQUENCE_LOCK_INITIALIZER { ATOMIC_INITIALIZER_UINT( 0 ) }
63 
72 {
73  _Atomic_Init_uint( &lock->sequence, 0 );
74 }
75 
84 {
85  (void) lock;
86 }
87 
99 static inline unsigned int _SMP_sequence_lock_Write_begin(
101 )
102 {
103  unsigned int seq;
104 
105  seq = _Atomic_Load_uint( &lock->sequence, ATOMIC_ORDER_RELAXED );
106  _Assert( seq % 2 == 0 );
107 
108  _Atomic_Store_uint( &lock->sequence, seq + 1, ATOMIC_ORDER_RELAXED );
109 
110  /* There is no atomic store with acquire/release semantics */
111  _Atomic_Fence( ATOMIC_ORDER_ACQ_REL );
112 
113  return seq;
114 }
115 
122 static inline void _SMP_sequence_lock_Write_end(
124  unsigned int seq
125 )
126 {
127  _Atomic_Store_uint( &lock->sequence, seq + 2, ATOMIC_ORDER_RELEASE );
128 }
129 
139 static inline unsigned int _SMP_sequence_lock_Read_begin(
140  const SMP_sequence_lock_Control *lock
141 )
142 {
143  return _Atomic_Load_uint( &lock->sequence, ATOMIC_ORDER_ACQUIRE );
144 }
145 
157 static inline bool _SMP_sequence_lock_Read_retry(
159  unsigned int seq
160 )
161 {
162  unsigned int seq2;
163 
164  seq2 = _Atomic_Fetch_add_uint( &lock->sequence, 0, ATOMIC_ORDER_RELEASE );
165  return seq != seq2 || seq % 2 != 0;
166 }
167 
170 #ifdef __cplusplus
171 }
172 #endif /* __cplusplus */
173 
174 #endif /* RTEMS_SMP */
175 
176 #endif /* _RTEMS_SCORE_SMPLOCKSEQ_H */
static void _SMP_sequence_lock_Initialize(SMP_sequence_lock_Control *lock)
Initializes an SMP sequence lock.
Definition: smplockseq.h:71
static unsigned int _SMP_sequence_lock_Write_begin(SMP_sequence_lock_Control *lock)
Begins an SMP sequence lock write operation.
Definition: smplockseq.h:99
Atomic_Uint sequence
The sequence number.
Definition: smplockseq.h:56
static unsigned int _SMP_sequence_lock_Read_begin(const SMP_sequence_lock_Control *lock)
Begins an SMP sequence lock read operation.
Definition: smplockseq.h:139
Information for the Assert Handler.
static void _SMP_sequence_lock_Write_end(SMP_sequence_lock_Control *lock, unsigned int seq)
Ends an SMP sequence lock write operation.
Definition: smplockseq.h:122
static bool _SMP_sequence_lock_Read_retry(SMP_sequence_lock_Control *lock, unsigned int seq)
Ends an SMP sequence lock read operation and indicates if a retry is necessary.
Definition: smplockseq.h:157
SMP sequence lock control.
Definition: smplockseq.h:50
Atomic Operations API.
#define _Assert(_e)
Assertion similar to assert() controlled via RTEMS_DEBUG instead of NDEBUG.
Definition: assert.h:100
static void _SMP_sequence_lock_Destroy(SMP_sequence_lock_Control *lock)
Destroys an SMP sequence lock.
Definition: smplockseq.h:83