source: mainline/kernel/generic/include/synch/spinlock.h@ 9150781

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9150781 was 13108f24, checked in by Jakub Jermar <jakub@…>, 15 years ago

Split spinlock_unlock() into a debug and non-debug version.

The non-debug version, spinlock_unlock_nondebug(), will likely get inlined just
as its counterpart spinlock_lock_arch(). It will not do any sanity checking.

The debug version, spinlock_unlock_debug(), will, on the other hand, not be
inlined, aiding thus the readibility of the generated assembly code. It will be
easier to spot the corresponding spinlock_lock_debug() and
spinlock_unlock_debug() pairs. It is meant to perform thorough sanity checking.

  • Property mode set to 100644
File size: 4.7 KB
Line 
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 <typedefs.h>
39#include <arch/barrier.h>
40#include <preemption.h>
41#include <atomic.h>
42#include <debug.h>
43
44#ifdef CONFIG_SMP
45
46typedef struct {
47 atomic_t val;
48
49#ifdef CONFIG_DEBUG_SPINLOCK
50 const 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#define spinlock_unlock(lock) spinlock_unlock_debug((lock))
81
82#else
83
84#define SPINLOCK_INITIALIZE_NAME(lock_name, desc_name) \
85 spinlock_t lock_name = { \
86 .val = { 0 } \
87 }
88
89#define SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, desc_name) \
90 static spinlock_t lock_name = { \
91 .val = { 0 } \
92 }
93
94#define spinlock_lock(lock) atomic_lock_arch(&(lock)->val)
95#define spinlock_unlock(lock) spinlock_unlock_nondebug((lock))
96
97#endif
98
99#define SPINLOCK_INITIALIZE(lock_name) \
100 SPINLOCK_INITIALIZE_NAME(lock_name, #lock_name)
101
102#define SPINLOCK_STATIC_INITIALIZE(lock_name) \
103 SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, #lock_name)
104
105extern void spinlock_initialize(spinlock_t *lock, const char *name);
106extern int spinlock_trylock(spinlock_t *lock);
107extern void spinlock_lock_debug(spinlock_t *lock);
108extern void spinlock_unlock_debug(spinlock_t *lock);
109
110/** Unlock spinlock
111 *
112 * Unlock spinlock for non-debug kernels.
113 *
114 * @param sl Pointer to spinlock_t structure.
115 */
116static inline void spinlock_unlock_nondebug(spinlock_t *lock)
117{
118 /*
119 * Prevent critical section code from bleeding out this way down.
120 */
121 CS_LEAVE_BARRIER();
122
123 atomic_set(&lock->val, 0);
124 preemption_enable();
125}
126
127#ifdef CONFIG_DEBUG_SPINLOCK
128
129#include <print.h>
130
131#define DEADLOCK_THRESHOLD 100000000
132
133#define DEADLOCK_PROBE_INIT(pname) size_t pname = 0
134
135#define DEADLOCK_PROBE(pname, value) \
136 if ((pname)++ > (value)) { \
137 (pname) = 0; \
138 printf("Deadlock probe %s: exceeded threshold %u\n", \
139 "cpu%u: function=%s, line=%u\n", \
140 #pname, (value), CPU->id, __func__, __LINE__); \
141 }
142
143#else
144
145#define DEADLOCK_PROBE_INIT(pname)
146#define DEADLOCK_PROBE(pname, value)
147
148#endif
149
150#else /* CONFIG_SMP */
151
152/* On UP systems, spinlocks are effectively left out. */
153
154#define SPINLOCK_DECLARE(name)
155#define SPINLOCK_EXTERN(name)
156
157#define SPINLOCK_INITIALIZE(name)
158#define SPINLOCK_STATIC_INITIALIZE(name)
159
160#define SPINLOCK_INITIALIZE_NAME(name, desc_name)
161#define SPINLOCK_STATIC_INITIALIZE_NAME(name, desc_name)
162
163#define spinlock_initialize(lock, name)
164
165#define spinlock_lock(lock) preemption_disable()
166#define spinlock_trylock(lock) (preemption_disable(), 1)
167#define spinlock_unlock(lock) preemption_enable()
168
169#define DEADLOCK_PROBE_INIT(pname)
170#define DEADLOCK_PROBE(pname, value)
171
172#endif
173
174#endif
175
176/** @}
177 */
Note: See TracBrowser for help on using the repository browser.