RTEMS 6.1-rc5
Loading...
Searching...
No Matches
smplockticket.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: BSD-2-Clause */
2
12/*
13 * Copyright (C) 2013, 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_SMPLOCKTICKET_H
38#define _RTEMS_SCORE_SMPLOCKTICKET_H
39
40#include <rtems/score/cpuopts.h>
41
42#if defined(RTEMS_SMP)
43
44#include <rtems/score/atomic.h>
46
47#ifdef __cplusplus
48extern "C" {
49#endif /* __cplusplus */
50
60typedef struct {
61 Atomic_Uint next_ticket;
62 Atomic_Uint now_serving;
63} SMP_ticket_lock_Control;
64
68#define SMP_TICKET_LOCK_INITIALIZER \
69 { \
70 ATOMIC_INITIALIZER_UINT( 0U ), \
71 ATOMIC_INITIALIZER_UINT( 0U ) \
72 }
73
81static inline void _SMP_ticket_lock_Initialize(
82 SMP_ticket_lock_Control *lock
83)
84{
85 _Atomic_Init_uint( &lock->next_ticket, 0U );
86 _Atomic_Init_uint( &lock->now_serving, 0U );
87}
88
96static inline void _SMP_ticket_lock_Destroy( SMP_ticket_lock_Control *lock )
97{
98 (void) lock;
99}
100
108static inline void _SMP_ticket_lock_Do_acquire(
109 SMP_ticket_lock_Control *lock
110#if defined(RTEMS_PROFILING)
111 ,
112 SMP_lock_Stats *stats,
113 SMP_lock_Stats_context *stats_context
114#endif
115)
116{
117 unsigned int my_ticket;
118 unsigned int now_serving;
119#if defined(RTEMS_PROFILING)
120 unsigned int initial_queue_length;
121 SMP_lock_Stats_acquire_context acquire_context;
122
123 _SMP_lock_Stats_acquire_begin( &acquire_context );
124#endif
125
126 my_ticket =
127 _Atomic_Fetch_add_uint( &lock->next_ticket, 1U, ATOMIC_ORDER_RELAXED );
128
129#if defined(RTEMS_PROFILING)
130 now_serving =
131 _Atomic_Load_uint( &lock->now_serving, ATOMIC_ORDER_ACQUIRE );
132 initial_queue_length = my_ticket - now_serving;
133
134 if ( initial_queue_length > 0 ) {
135#endif
136
137 do {
138 now_serving =
139 _Atomic_Load_uint( &lock->now_serving, ATOMIC_ORDER_ACQUIRE );
140 } while ( now_serving != my_ticket );
141
142#if defined(RTEMS_PROFILING)
143 }
144
145 _SMP_lock_Stats_acquire_end(
146 &acquire_context,
147 stats,
148 stats_context,
149 initial_queue_length
150 );
151#endif
152}
153
165#if defined(RTEMS_PROFILING)
166 #define _SMP_ticket_lock_Acquire( lock, stats, stats_context ) \
167 _SMP_ticket_lock_Do_acquire( lock, stats, stats_context )
168#else
169 #define _SMP_ticket_lock_Acquire( lock, stats, stats_context ) \
170 _SMP_ticket_lock_Do_acquire( lock )
171#endif
172
179static inline void _SMP_ticket_lock_Do_release(
180 SMP_ticket_lock_Control *lock
181#if defined(RTEMS_PROFILING)
182 ,
183 const SMP_lock_Stats_context *stats_context
184#endif
185)
186{
187 unsigned int current_ticket =
188 _Atomic_Load_uint( &lock->now_serving, ATOMIC_ORDER_RELAXED );
189 unsigned int next_ticket = current_ticket + 1U;
190
191#if defined(RTEMS_PROFILING)
192 _SMP_lock_Stats_release_update( stats_context );
193#endif
194
195 _Atomic_Store_uint( &lock->now_serving, next_ticket, ATOMIC_ORDER_RELEASE );
196}
197
204#if defined(RTEMS_PROFILING)
205 #define _SMP_ticket_lock_Release( lock, stats_context ) \
206 _SMP_ticket_lock_Do_release( lock, stats_context )
207#else
208 #define _SMP_ticket_lock_Release( lock, stats_context ) \
209 _SMP_ticket_lock_Do_release( lock )
210#endif
211
214#ifdef __cplusplus
215}
216#endif /* __cplusplus */
217
218#endif /* RTEMS_SMP */
219
220#endif /* _RTEMS_SCORE_SMPLOCKTICKET_H */
This header file provides the interfaces of the Atomic Operations.
This header file provides the interfaces of the SMP Locks related to lock statistics.