1 | /*
|
---|
2 | * Copyright (c) 2001-2004 Jakub Jermar
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup sync
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | */
|
---|
34 |
|
---|
35 | #ifndef KERN_SPINLOCK_H_
|
---|
36 | #define KERN_SPINLOCK_H_
|
---|
37 |
|
---|
38 | #include <arch/types.h>
|
---|
39 | #include <arch/barrier.h>
|
---|
40 | #include <preemption.h>
|
---|
41 | #include <atomic.h>
|
---|
42 | #include <debug.h>
|
---|
43 |
|
---|
44 | #ifdef CONFIG_SMP
|
---|
45 |
|
---|
46 | typedef struct {
|
---|
47 | atomic_t val;
|
---|
48 |
|
---|
49 | #ifdef CONFIG_DEBUG_SPINLOCK
|
---|
50 | char *name;
|
---|
51 | #endif
|
---|
52 | } spinlock_t;
|
---|
53 |
|
---|
54 | /*
|
---|
55 | * SPINLOCK_DECLARE is to be used for dynamically allocated spinlocks,
|
---|
56 | * where the lock gets initialized in run time.
|
---|
57 | */
|
---|
58 | #define SPINLOCK_DECLARE(lock_name) spinlock_t lock_name
|
---|
59 | #define SPINLOCK_EXTERN(lock_name) extern spinlock_t lock_name
|
---|
60 |
|
---|
61 | /*
|
---|
62 | * SPINLOCK_INITIALIZE is to be used for statically allocated spinlocks.
|
---|
63 | * It declares and initializes the lock.
|
---|
64 | */
|
---|
65 | #ifdef CONFIG_DEBUG_SPINLOCK
|
---|
66 |
|
---|
67 | #define SPINLOCK_INITIALIZE_NAME(lock_name, desc_name) \
|
---|
68 | spinlock_t lock_name = { \
|
---|
69 | .name = desc_name, \
|
---|
70 | .val = { 0 } \
|
---|
71 | }
|
---|
72 |
|
---|
73 | #define SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, desc_name) \
|
---|
74 | static spinlock_t lock_name = { \
|
---|
75 | .name = desc_name, \
|
---|
76 | .val = { 0 } \
|
---|
77 | }
|
---|
78 |
|
---|
79 | #define spinlock_lock(lock) spinlock_lock_debug(lock)
|
---|
80 |
|
---|
81 | #else
|
---|
82 |
|
---|
83 | #define SPINLOCK_INITIALIZE_NAME(lock_name, desc_name) \
|
---|
84 | spinlock_t lock_name = { \
|
---|
85 | .val = { 0 } \
|
---|
86 | }
|
---|
87 |
|
---|
88 | #define SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, desc_name) \
|
---|
89 | static spinlock_t lock_name = { \
|
---|
90 | .val = { 0 } \
|
---|
91 | }
|
---|
92 |
|
---|
93 | #define spinlock_lock(lock) atomic_lock_arch(&(lock)->val)
|
---|
94 |
|
---|
95 | #endif
|
---|
96 |
|
---|
97 | #define SPINLOCK_INITIALIZE(lock_name) \
|
---|
98 | SPINLOCK_INITIALIZE_NAME(lock_name, #lock_name)
|
---|
99 |
|
---|
100 | #define SPINLOCK_STATIC_INITIALIZE(lock_name) \
|
---|
101 | SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, #lock_name)
|
---|
102 |
|
---|
103 | extern void spinlock_initialize(spinlock_t *lock, char *name);
|
---|
104 | extern int spinlock_trylock(spinlock_t *lock);
|
---|
105 | extern void spinlock_lock_debug(spinlock_t *lock);
|
---|
106 |
|
---|
107 | /** Unlock spinlock
|
---|
108 | *
|
---|
109 | * Unlock spinlock.
|
---|
110 | *
|
---|
111 | * @param sl Pointer to spinlock_t structure.
|
---|
112 | */
|
---|
113 | static inline void spinlock_unlock(spinlock_t *lock)
|
---|
114 | {
|
---|
115 | ASSERT(atomic_get(&lock->val) != 0);
|
---|
116 |
|
---|
117 | /*
|
---|
118 | * Prevent critical section code from bleeding out this way down.
|
---|
119 | */
|
---|
120 | CS_LEAVE_BARRIER();
|
---|
121 |
|
---|
122 | atomic_set(&lock->val, 0);
|
---|
123 | preemption_enable();
|
---|
124 | }
|
---|
125 |
|
---|
126 | #ifdef CONFIG_DEBUG_SPINLOCK
|
---|
127 |
|
---|
128 | #include <print.h>
|
---|
129 |
|
---|
130 | #define DEADLOCK_THRESHOLD 100000000
|
---|
131 |
|
---|
132 | #define DEADLOCK_PROBE_INIT(pname) size_t pname = 0
|
---|
133 |
|
---|
134 | #define DEADLOCK_PROBE(pname, value) \
|
---|
135 | if ((pname)++ > (value)) { \
|
---|
136 | (pname) = 0; \
|
---|
137 | printf("Deadlock probe %s: exceeded threshold %u\n", \
|
---|
138 | "cpu%u: function=%s, line=%u\n", \
|
---|
139 | #pname, (value), CPU->id, __func__, __LINE__); \
|
---|
140 | }
|
---|
141 |
|
---|
142 | #else
|
---|
143 |
|
---|
144 | #define DEADLOCK_PROBE_INIT(pname)
|
---|
145 | #define DEADLOCK_PROBE(pname, value)
|
---|
146 |
|
---|
147 | #endif
|
---|
148 |
|
---|
149 | #else /* CONFIG_SMP */
|
---|
150 |
|
---|
151 | /* On UP systems, spinlocks are effectively left out. */
|
---|
152 |
|
---|
153 | #define SPINLOCK_DECLARE(name)
|
---|
154 | #define SPINLOCK_EXTERN(name)
|
---|
155 |
|
---|
156 | #define SPINLOCK_INITIALIZE(name)
|
---|
157 | #define SPINLOCK_STATIC_INITIALIZE(name)
|
---|
158 |
|
---|
159 | #define SPINLOCK_INITIALIZE_NAME(name, desc_name)
|
---|
160 | #define SPINLOCK_STATIC_INITIALIZE_NAME(name, desc_name)
|
---|
161 |
|
---|
162 | #define spinlock_initialize(lock, name)
|
---|
163 |
|
---|
164 | #define spinlock_lock(lock) preemption_disable()
|
---|
165 | #define spinlock_trylock(lock) (preemption_disable(), 1)
|
---|
166 | #define spinlock_unlock(lock) preemption_enable()
|
---|
167 |
|
---|
168 | #define DEADLOCK_PROBE_INIT(pname)
|
---|
169 | #define DEADLOCK_PROBE(pname, value)
|
---|
170 |
|
---|
171 | #endif
|
---|
172 |
|
---|
173 | #endif
|
---|
174 |
|
---|
175 | /** @}
|
---|
176 | */
|
---|