1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * |
---|
6 | * @ingroup RTEMSScoreAtomic |
---|
7 | * |
---|
8 | * @brief This source file contains the implementation of |
---|
9 | * _Libatomic_Protect_start() and _Libatomic_Protect_end(). |
---|
10 | */ |
---|
11 | |
---|
12 | /* |
---|
13 | * Copyright (c) 2016 embedded brains GmbH. All rights reserved. |
---|
14 | * |
---|
15 | * embedded brains GmbH |
---|
16 | * Dornierstr. 4 |
---|
17 | * 82178 Puchheim |
---|
18 | * Germany |
---|
19 | * <rtems@embedded-brains.de> |
---|
20 | * |
---|
21 | * Redistribution and use in source and binary forms, with or without |
---|
22 | * modification, are permitted provided that the following conditions |
---|
23 | * are met: |
---|
24 | * 1. Redistributions of source code must retain the above copyright |
---|
25 | * notice, this list of conditions and the following disclaimer. |
---|
26 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
27 | * notice, this list of conditions and the following disclaimer in the |
---|
28 | * documentation and/or other materials provided with the distribution. |
---|
29 | * |
---|
30 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
31 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
32 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
33 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
34 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
35 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
36 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
37 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
38 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
39 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
40 | * POSSIBILITY OF SUCH DAMAGE. |
---|
41 | */ |
---|
42 | |
---|
43 | #ifdef HAVE_CONFIG_H |
---|
44 | #include "config.h" |
---|
45 | #endif |
---|
46 | |
---|
47 | #include <machine/_libatomic.h> |
---|
48 | |
---|
49 | #include <rtems/score/apimutex.h> |
---|
50 | #include <rtems/score/atomic.h> |
---|
51 | #include <rtems/score/isrlevel.h> |
---|
52 | |
---|
53 | #if defined(RTEMS_SMP) |
---|
54 | /* |
---|
55 | * For real SMP targets this is not useful. The main purpose is support for |
---|
56 | * testing on simulators like SIS. |
---|
57 | */ |
---|
58 | static Atomic_Flag _Libatomic_The_one_lock = ATOMIC_INITIALIZER_FLAG; |
---|
59 | #endif |
---|
60 | |
---|
61 | __uint32_t _Libatomic_Protect_start( void *ptr ) |
---|
62 | { |
---|
63 | ISR_Level isr_level; |
---|
64 | |
---|
65 | (void) ptr; |
---|
66 | _ISR_Local_disable( isr_level ); |
---|
67 | |
---|
68 | #if defined(RTEMS_SMP) |
---|
69 | while ( |
---|
70 | _Atomic_Flag_test_and_set( &_Libatomic_The_one_lock, ATOMIC_ORDER_SEQ_CST ) |
---|
71 | ) { |
---|
72 | /* Next try. Yes, a TAS spin lock implementation is stupid. */ |
---|
73 | } |
---|
74 | #endif |
---|
75 | |
---|
76 | return isr_level; |
---|
77 | } |
---|
78 | |
---|
79 | void _Libatomic_Protect_end( void *ptr, __uint32_t isr_level ) |
---|
80 | { |
---|
81 | (void) ptr; |
---|
82 | |
---|
83 | #if defined(RTEMS_SMP) |
---|
84 | _Atomic_Flag_clear( &_Libatomic_The_one_lock, ATOMIC_ORDER_SEQ_CST ); |
---|
85 | #endif |
---|
86 | |
---|
87 | _ISR_Local_enable( isr_level ); |
---|
88 | } |
---|
89 | |
---|
90 | static API_Mutex_Control _Libatomic_Mutex = |
---|
91 | API_MUTEX_INITIALIZER( "_Libatomic" ); |
---|
92 | |
---|
93 | void _Libatomic_Lock_n( void *ptr, __size_t n ) |
---|
94 | { |
---|
95 | (void) ptr; |
---|
96 | (void) n; |
---|
97 | _API_Mutex_Lock( &_Libatomic_Mutex ); |
---|
98 | } |
---|
99 | |
---|
100 | void _Libatomic_Unlock_n( void *ptr, __size_t n ) |
---|
101 | { |
---|
102 | (void) ptr; |
---|
103 | (void) n; |
---|
104 | _API_Mutex_Unlock( &_Libatomic_Mutex ); |
---|
105 | } |
---|