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

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

Indentaion and formatting changes even Martin will like :-)

  • Property mode set to 100644
File size: 4.1 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
33/**
34 * @file
35 * @brief Spinlocks.
36 */
37
38#include <synch/spinlock.h>
39#include <atomic.h>
40#include <arch/barrier.h>
41#include <arch.h>
42#include <preemption.h>
43#include <print.h>
44#include <debug.h>
45#include <symtab.h>
46
47#ifdef CONFIG_FB
48#include <genarch/fb/fb.h>
49#endif
50
51#ifdef CONFIG_SMP
52
53/** Initialize spinlock
54 *
55 * Initialize spinlock.
56 *
57 * @param sl Pointer to spinlock_t structure.
58 */
59void spinlock_initialize(spinlock_t *sl, char *name)
60{
61 atomic_set(&sl->val, 0);
62#ifdef CONFIG_DEBUG_SPINLOCK
63 sl->name = name;
64#endif
65}
66
67/** Lock spinlock
68 *
69 * Lock spinlock.
70 * This version has limitted ability to report
71 * possible occurence of deadlock.
72 *
73 * @param sl Pointer to spinlock_t structure.
74 */
75#ifdef CONFIG_DEBUG_SPINLOCK
76#define DEADLOCK_THRESHOLD 100000000
77void spinlock_lock_debug(spinlock_t *sl)
78{
79 count_t i = 0;
80 char *symbol;
81 bool deadlock_reported = false;
82
83 preemption_disable();
84 while (test_and_set(&sl->val)) {
85
86 /*
87 * We need to be careful about printflock and fb_lock.
88 * Both of them are used to report deadlocks via
89 * printf() and fb_putchar().
90 *
91 * We trust our code that there is no possible deadlock
92 * caused by these two locks (except when an exception
93 * is triggered for instance by printf() or fb_putchar()).
94 * However, we encountered false positives caused by very
95 * slow VESA framebuffer interaction (especially when
96 * run in a simulator) that caused problems with both
97 * printflock and fb_lock.
98 *
99 * Possible deadlocks on both printflock and fb_lock
100 * are therefore not reported as they would cause an
101 * infinite recursion.
102 */
103 if (sl == &printflock)
104 continue;
105#ifdef CONFIG_FB
106 if (sl == &fb_lock)
107 continue;
108#endif
109 if (i++ > DEADLOCK_THRESHOLD) {
110 printf("cpu%d: looping on spinlock %.*p:%s, "
111 "caller=%.*p", CPU->id, sizeof(uintptr_t) * 2, sl,
112 sl->name, sizeof(uintptr_t) * 2, CALLER);
113 symbol = get_symtab_entry(CALLER);
114 if (symbol)
115 printf("(%s)", symbol);
116 printf("\n");
117 i = 0;
118 deadlock_reported = true;
119 }
120 }
121
122 if (deadlock_reported)
123 printf("cpu%d: not deadlocked\n", CPU->id);
124
125 /*
126 * Prevent critical section code from bleeding out this way up.
127 */
128 CS_ENTER_BARRIER();
129}
130#endif
131
132/** Lock spinlock conditionally
133 *
134 * Lock spinlock conditionally.
135 * If the spinlock is not available at the moment,
136 * signal failure.
137 *
138 * @param sl Pointer to spinlock_t structure.
139 *
140 * @return Zero on failure, non-zero otherwise.
141 */
142int spinlock_trylock(spinlock_t *sl)
143{
144 int rc;
145
146 preemption_disable();
147 rc = !test_and_set(&sl->val);
148
149 /*
150 * Prevent critical section code from bleeding out this way up.
151 */
152 CS_ENTER_BARRIER();
153
154 if (!rc)
155 preemption_enable();
156
157 return rc;
158}
159
160#endif
161
162/** @}
163 */
Note: See TracBrowser for help on using the repository browser.