source: mainline/kernel/generic/src/synch/spinlock.c@ 95658c9

ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 95658c9 was 95658c9, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 2 years ago

Put irq_spinlock_*() functions in a separate file

  • Property mode set to 100644
File size: 4.8 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 kernel_sync
30 * @{
31 */
32
33/**
34 * @file
35 * @brief Spinlocks.
36 */
37
38#include <synch/spinlock.h>
39#include <atomic.h>
40#include <barrier.h>
41#include <arch.h>
42#include <preemption.h>
43#include <stdio.h>
44#include <debug.h>
45#include <symtab.h>
46#include <stacktrace.h>
47#include <cpu.h>
48
49#ifdef CONFIG_SMP
50
51/** Initialize spinlock
52 *
53 * @param sl Pointer to spinlock_t structure.
54 *
55 */
56void spinlock_initialize(spinlock_t *lock, const char *name)
57{
58 atomic_flag_clear_explicit(&lock->flag, memory_order_relaxed);
59#ifdef CONFIG_DEBUG_SPINLOCK
60 lock->name = name;
61#endif
62}
63
64#ifdef CONFIG_DEBUG_SPINLOCK
65
66/** Lock spinlock
67 *
68 * Lock spinlock.
69 * This version has limitted ability to report
70 * possible occurence of deadlock.
71 *
72 * @param lock Pointer to spinlock_t structure.
73 *
74 */
75void spinlock_lock_debug(spinlock_t *lock)
76{
77 size_t i = 0;
78 bool deadlock_reported = false;
79
80 preemption_disable();
81 while (atomic_flag_test_and_set_explicit(&lock->flag, memory_order_acquire)) {
82 /*
83 * We need to be careful about particular locks
84 * which are directly used to report deadlocks
85 * via printf() (and recursively other functions).
86 * This conserns especially printf_lock and the
87 * framebuffer lock.
88 *
89 * Any lock whose name is prefixed by "*" will be
90 * ignored by this deadlock detection routine
91 * as this might cause an infinite recursion.
92 * We trust our code that there is no possible deadlock
93 * caused by these locks (except when an exception
94 * is triggered for instance by printf()).
95 *
96 * We encountered false positives caused by very
97 * slow framebuffer interaction (especially when
98 * run in a simulator) that caused problems with both
99 * printf_lock and the framebuffer lock.
100 */
101 if (lock->name[0] == '*')
102 continue;
103
104 if (i++ > DEADLOCK_THRESHOLD) {
105 printf("cpu%u: looping on spinlock %p:%s, "
106 "caller=%p (%s)\n", CPU->id, lock, lock->name,
107 (void *) CALLER, symtab_fmt_name_lookup(CALLER));
108 stack_trace();
109
110 i = 0;
111 deadlock_reported = true;
112 }
113 }
114
115 if (deadlock_reported)
116 printf("cpu%u: not deadlocked\n", CPU->id);
117}
118
119/** Unlock spinlock
120 *
121 * Unlock spinlock.
122 *
123 * @param sl Pointer to spinlock_t structure.
124 */
125void spinlock_unlock_debug(spinlock_t *lock)
126{
127 ASSERT_SPINLOCK(spinlock_locked(lock), lock);
128
129 atomic_flag_clear_explicit(&lock->flag, memory_order_release);
130 preemption_enable();
131}
132
133#endif
134
135/** Lock spinlock conditionally
136 *
137 * Lock spinlock conditionally. If the spinlock is not available
138 * at the moment, signal failure.
139 *
140 * @param lock Pointer to spinlock_t structure.
141 *
142 * @return Zero on failure, non-zero otherwise.
143 *
144 */
145bool spinlock_trylock(spinlock_t *lock)
146{
147 preemption_disable();
148 bool ret = !atomic_flag_test_and_set_explicit(&lock->flag, memory_order_acquire);
149
150 if (!ret)
151 preemption_enable();
152
153 return ret;
154}
155
156/** Find out whether the spinlock is currently locked.
157 *
158 * @param lock Spinlock.
159 * @return True if the spinlock is locked, false otherwise.
160 */
161bool spinlock_locked(spinlock_t *lock)
162{
163 // NOTE: Atomic flag doesn't support simple atomic read (by design),
164 // so instead we test_and_set and then clear if necessary.
165 // This function is only used inside assert, so we don't need
166 // any preemption_disable/enable here.
167
168 bool ret = atomic_flag_test_and_set_explicit(&lock->flag, memory_order_relaxed);
169 if (!ret)
170 atomic_flag_clear_explicit(&lock->flag, memory_order_relaxed);
171 return ret;
172}
173
174#endif
175
176/** @}
177 */
Note: See TracBrowser for help on using the repository browser.