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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c9b550b was 8ed4014, checked in by Martin Decky <martin@…>, 17 years ago

proper printf formatting

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