summaryrefslogtreecommitdiffstats
path: root/cpukit/score/include/rtems/score/isrlock.h
blob: d36b6040474b1d69581e22980e32287e7eab9e8b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/**
 * @file
 *
 * @ingroup ScoreISRLocks
 *
 * @brief ISR Locks
 */

/*
 * Copyright (c) 2013 embedded brains GmbH.  All rights reserved.
 *
 *  embedded brains GmbH
 *  Dornierstr. 4
 *  82178 Puchheim
 *  Germany
 *  <rtems@embedded-brains.de>
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rtems.org/license/LICENSE.
 */

#ifndef _RTEMS_SCORE_ISR_LOCK_H
#define _RTEMS_SCORE_ISR_LOCK_H

#include <rtems/score/isrlevel.h>
#include <rtems/score/smplock.h>

#ifdef __cplusplus
extern "C" {
#endif

/**
 * @defgroup ScoreISRLocks ISR Locks
 *
 * @ingroup ScoreISR
 *
 * @brief Low-level lock to protect critical sections accessed by threads and
 * interrupt service routines.
 *
 * On single processor configurations the ISR locks degrade to simple ISR
 * disable/enable sequences.  No additional storage or objects are required.
 *
 * This synchronization primitive is supported on SMP configurations.  Here SMP
 * locks are used.
 *
 * @{
 */

/**
 * @brief ISR lock control.
 */
typedef struct {
#if defined( RTEMS_SMP )
  SMP_lock_Control lock;
#endif
} ISR_lock_Control;

/**
 * @brief Local ISR lock context for acquire and release pairs.
 */
typedef struct {
#if defined( RTEMS_SMP )
  SMP_lock_Context lock_context;
#else
  ISR_Level isr_level;
#endif
} ISR_lock_Context;

/**
 * @brief Initializer for static initialization of ISR locks.
 */
#if defined( RTEMS_SMP )
  #define ISR_LOCK_INITIALIZER( name ) \
    { SMP_LOCK_INITIALIZER( name ) }
#else
  #define ISR_LOCK_INITIALIZER( name ) \
    { }
#endif

/**
 * @brief Initializes an ISR lock.
 *
 * Concurrent initialization leads to unpredictable results.
 *
 * @param[in,out] lock The ISR lock control.
 * @param[in] name The name for the ISR lock.  This name must be persistent
 * throughout the life time of this lock.
 */
static inline void _ISR_lock_Initialize(
  ISR_lock_Control *lock,
  const char *name
)
{
#if defined( RTEMS_SMP )
  _SMP_lock_Initialize( &lock->lock, name );
#else
  (void) lock;
  (void) name;
#endif
}

/**
 * @brief Destroys an ISR lock.
 *
 * Concurrent destruction leads to unpredictable results.
 *
 * @param[in,out] lock The ISR lock control.
 */
static inline void _ISR_lock_Destroy( ISR_lock_Control *lock )
{
#if defined( RTEMS_SMP )
  _SMP_lock_Destroy( &lock->lock );
#else
  (void) lock;
#endif
}

/**
 * @brief Acquires an ISR lock.
 *
 * Interrupts will be disabled.  On SMP configurations this function acquires
 * an SMP lock.
 *
 * This function can be used in thread and interrupt context.
 *
 * @param[in,out] lock The ISR lock control.
 * @param[in,out] context The local ISR lock context for an acquire and release
 * pair.
 *
 * @see _ISR_lock_Release_and_ISR_enable().
 */
static inline void _ISR_lock_ISR_disable_and_acquire(
  ISR_lock_Control *lock,
  ISR_lock_Context *context
)
{
#if defined( RTEMS_SMP )
  _SMP_lock_ISR_disable_and_acquire( &lock->lock, &context->lock_context );
#else
  (void) lock;
  _ISR_Disable( context->isr_level );
#endif
}

/**
 * @brief Releases an ISR lock.
 *
 * The interrupt status will be restored.  On SMP configurations this function
 * releases an SMP lock.
 *
 * This function can be used in thread and interrupt context.
 *
 * @param[in,out] lock The ISR lock control.
 * @param[in,out] context The local ISR lock context for an acquire and release
 * pair.
 *
 * @see _ISR_lock_ISR_disable_and_acquire().
 */
static inline void _ISR_lock_Release_and_ISR_enable(
  ISR_lock_Control *lock,
  ISR_lock_Context *context
)
{
#if defined( RTEMS_SMP )
  _SMP_lock_Release_and_ISR_enable( &lock->lock, &context->lock_context );
#else
  (void) lock;
  _ISR_Enable( context->isr_level );
#endif
}

/**
 * @brief Acquires an ISR lock inside an ISR disabled section.
 *
 * The interrupt status will remain unchanged.  On SMP configurations this
 * function acquires an SMP lock.
 *
 * In case the executing context can be interrupted by higher priority
 * interrupts and these interrupts enter the critical section protected by this
 * lock, then the result is unpredictable.
 *
 * @param[in,out] lock The ISR lock control.
 * @param[in,out] context The local ISR lock context for an acquire and release
 * pair.
 *
 * @see _ISR_lock_Release().
 */
static inline void _ISR_lock_Acquire(
  ISR_lock_Control *lock,
  ISR_lock_Context *context
)
{
#if defined( RTEMS_SMP )
  _SMP_lock_Acquire( &lock->lock, &context->lock_context );
#else
  (void) lock;
  (void) context;
#endif
}

/**
 * @brief Releases an ISR lock inside an ISR disabled section.
 *
 * The interrupt status will remain unchanged.  On SMP configurations this
 * function releases an SMP lock.
 *
 * @param[in,out] lock The ISR lock control.
 * @param[in,out] context The local ISR lock context for an acquire and release
 * pair.
 *
 * @see _ISR_lock_Acquire().
 */
static inline void _ISR_lock_Release(
  ISR_lock_Control *lock,
  ISR_lock_Context *context
)
{
#if defined( RTEMS_SMP )
  _SMP_lock_Release( &lock->lock, &context->lock_context );
#else
  (void) lock;
  (void) context;
#endif
}

/** @} */

#ifdef __cplusplus
}
#endif

#endif /* _RTEMS_SCORE_ISR_LOCK_H */