1 | /* |
---|
2 | * COPYRIGHT (c) 1989-2011. |
---|
3 | * On-Line Applications Research Corporation (OAR). |
---|
4 | * |
---|
5 | * The license and distribution terms for this file may be |
---|
6 | * found in the file LICENSE in this distribution or at |
---|
7 | * http://www.rtems.com/license/LICENSE. |
---|
8 | * |
---|
9 | * $Id$ |
---|
10 | */ |
---|
11 | |
---|
12 | #if HAVE_CONFIG_H |
---|
13 | #include "config.h" |
---|
14 | #endif |
---|
15 | |
---|
16 | #include <rtems/system.h> |
---|
17 | #include <rtems/score/smplock.h> |
---|
18 | #include <rtems/score/smp.h> |
---|
19 | |
---|
20 | #if defined (RTEMS_DEBUG) |
---|
21 | #include <rtems/bspIo.h> |
---|
22 | #endif |
---|
23 | |
---|
24 | void _SMP_lock_spinlock_simple_Initialize( |
---|
25 | SMP_lock_spinlock_simple_Control *lock |
---|
26 | ) |
---|
27 | { |
---|
28 | *lock = 0; |
---|
29 | } |
---|
30 | |
---|
31 | ISR_Level _SMP_lock_spinlock_simple_Obtain( |
---|
32 | SMP_lock_spinlock_simple_Control *lock |
---|
33 | ) |
---|
34 | { |
---|
35 | ISR_Level level; |
---|
36 | uint32_t value = 1; |
---|
37 | uint32_t previous; |
---|
38 | |
---|
39 | /* Note: Disable provides an implicit memory barrier. */ |
---|
40 | _ISR_Disable( level ); |
---|
41 | do { |
---|
42 | SMP_CPU_SWAP( lock, value, previous ); |
---|
43 | } while (previous == 1); |
---|
44 | |
---|
45 | return level; |
---|
46 | } |
---|
47 | |
---|
48 | void _SMP_lock_spinlock_simple_Release( |
---|
49 | SMP_lock_spinlock_simple_Control *lock, |
---|
50 | ISR_Level level |
---|
51 | ) |
---|
52 | { |
---|
53 | *lock = 0; |
---|
54 | _ISR_Enable( level ); |
---|
55 | } |
---|
56 | |
---|
57 | void _SMP_lock_spinlock_nested_Initialize( |
---|
58 | SMP_lock_spinlock_nested_Control *lock |
---|
59 | ) |
---|
60 | { |
---|
61 | lock->count = 0; |
---|
62 | lock->cpu_id = 0; |
---|
63 | } |
---|
64 | |
---|
65 | ISR_Level _SMP_lock_spinlock_nested_Obtain( |
---|
66 | SMP_lock_spinlock_nested_Control *lock |
---|
67 | ) |
---|
68 | { |
---|
69 | ISR_Level level = 0; |
---|
70 | uint32_t value = 1; |
---|
71 | uint32_t previous; |
---|
72 | int cpu_id; |
---|
73 | |
---|
74 | /* Note: Disable provides an implicit memory barrier. */ |
---|
75 | _ISR_Disable( level ); |
---|
76 | |
---|
77 | cpu_id = bsp_smp_processor_id(); |
---|
78 | |
---|
79 | /* Deal with nested calls from one cpu */ |
---|
80 | if ( (lock->count > 0) && (cpu_id == lock->cpu_id) ) { |
---|
81 | lock->count++; |
---|
82 | return level; |
---|
83 | } |
---|
84 | |
---|
85 | do { |
---|
86 | SMP_CPU_SWAP( lock, value, previous ); |
---|
87 | } while (previous == 1); |
---|
88 | |
---|
89 | lock->count++; |
---|
90 | lock->cpu_id = cpu_id; |
---|
91 | |
---|
92 | return level; |
---|
93 | } |
---|
94 | |
---|
95 | void _SMP_lock_spinlock_nested_Release( |
---|
96 | SMP_lock_spinlock_nested_Control *lock, |
---|
97 | ISR_Level level |
---|
98 | ) |
---|
99 | { |
---|
100 | #if defined(RTEMS_DEBUG) |
---|
101 | if ( lock->count == 0 ) |
---|
102 | printk ("Releasing spinlock when count is already zero?!?!\n"); |
---|
103 | #endif |
---|
104 | lock->count--; |
---|
105 | |
---|
106 | _ISR_Enable( level ); |
---|
107 | } |
---|