1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @ingroup ScoreSMPLockCPU |
---|
5 | * |
---|
6 | * @brief CPU SMP Lock Implementation |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | * Copyright (c) 2013 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.com/license/LICENSE. |
---|
15 | */ |
---|
16 | |
---|
17 | #ifndef _RTEMS_SCORE_NO_CPU_SMPLOCK_H |
---|
18 | #define _RTEMS_SCORE_NO_CPU_SMPLOCK_H |
---|
19 | |
---|
20 | #include <rtems/score/cpu.h> |
---|
21 | |
---|
22 | #ifdef __cplusplus |
---|
23 | extern "C" { |
---|
24 | #endif /* __cplusplus */ |
---|
25 | |
---|
26 | /** |
---|
27 | * @defgroup ScoreSMPLockCPU CPU SMP Locks |
---|
28 | * |
---|
29 | * @ingroup ScoreSMPLock |
---|
30 | * |
---|
31 | * This example will implement a ticket lock. |
---|
32 | * |
---|
33 | * @{ |
---|
34 | */ |
---|
35 | |
---|
36 | /** |
---|
37 | * @brief CPU SMP lock control. |
---|
38 | */ |
---|
39 | typedef struct { |
---|
40 | unsigned int next_ticket; |
---|
41 | unsigned int now_serving; |
---|
42 | } CPU_SMP_lock_Control; |
---|
43 | |
---|
44 | /** |
---|
45 | * @brief CPU SMP lock control initializer for static initialization. |
---|
46 | */ |
---|
47 | #define CPU_SMP_LOCK_INITIALIZER { 0, 0 } |
---|
48 | |
---|
49 | /** |
---|
50 | * @brief Initializes a CPU SMP lock control. |
---|
51 | * |
---|
52 | * @param[out] lock The CPU SMP lock control. |
---|
53 | */ |
---|
54 | static inline void _CPU_SMP_lock_Initialize( CPU_SMP_lock_Control *lock ) |
---|
55 | { |
---|
56 | lock->next_ticket = 0; |
---|
57 | lock->now_serving = 0; |
---|
58 | } |
---|
59 | |
---|
60 | /** |
---|
61 | * @brief Acquires a CPU SMP lock. |
---|
62 | * |
---|
63 | * @param[in,out] lock The CPU SMP lock control. |
---|
64 | */ |
---|
65 | static inline void _CPU_SMP_lock_Acquire( CPU_SMP_lock_Control *lock ) |
---|
66 | { |
---|
67 | unsigned int my_ticket = _Atomic_Fetch_and_increment( &lock->next_ticket ); |
---|
68 | |
---|
69 | while ( _Atomic_Load_and_acquire( &lock->now_serving ) != my_ticket ) { |
---|
70 | _Wait_some_time(); |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | /** |
---|
75 | * @brief Releases a CPU SMP lock. |
---|
76 | * |
---|
77 | * @param[in,out] lock The CPU SMP lock control. |
---|
78 | */ |
---|
79 | static inline void _CPU_SMP_lock_Release( CPU_SMP_lock_Control *lock ) |
---|
80 | { |
---|
81 | _Atomic_Store_and_release( &lock->now_serving, lock->now_serving + 1 ); |
---|
82 | } |
---|
83 | |
---|
84 | /** |
---|
85 | * @brief Disables interrupts and acquires the CPU SMP lock. |
---|
86 | * |
---|
87 | * @param[in,out] lock The CPU SMP lock control. |
---|
88 | * @param[out] isr_cookie The ISR cookie. |
---|
89 | */ |
---|
90 | #define _CPU_SMP_lock_ISR_disable_and_acquire( lock, isr_cookie ) \ |
---|
91 | do { \ |
---|
92 | _CPU_ISR_Disable( isr_cookie ); \ |
---|
93 | _CPU_SMP_lock_Acquire( lock ); \ |
---|
94 | } while (0) |
---|
95 | |
---|
96 | /** |
---|
97 | * @brief Releases the CPU SMP lock and enables interrupts. |
---|
98 | * |
---|
99 | * @param[in,out] lock The CPU SMP lock control. |
---|
100 | * @param[in] isr_cookie The ISR cookie. |
---|
101 | */ |
---|
102 | #define _CPU_SMP_lock_Release_and_ISR_enable( lock, isr_cookie ) \ |
---|
103 | do { \ |
---|
104 | _CPU_SMP_lock_Release( lock ); \ |
---|
105 | _CPU_ISR_Enable( isr_cookie ); \ |
---|
106 | } while (0) |
---|
107 | |
---|
108 | /**@}*/ |
---|
109 | |
---|
110 | #ifdef __cplusplus |
---|
111 | } |
---|
112 | #endif /* __cplusplus */ |
---|
113 | |
---|
114 | #endif /* _RTEMS_SCORE_NO_CPU_SMPLOCK_H */ |
---|