[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
|
---|
[b60c582] | 35 | * @brief Spinlocks.
|
---|
[cf26ba9] | 36 | */
|
---|
[b60c582] | 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 |
|
---|
[5f85c91] | 47 | #ifdef CONFIG_SMP
|
---|
[f761f1eb] | 48 |
|
---|
[5e04b48d] | 49 | /** Initialize spinlock
|
---|
| 50 | *
|
---|
| 51 | * @param sl Pointer to spinlock_t structure.
|
---|
[90c8b8d] | 52 | *
|
---|
[5e04b48d] | 53 | */
|
---|
[a000878c] | 54 | void spinlock_initialize(spinlock_t *lock, const char *name)
|
---|
[f761f1eb] | 55 | {
|
---|
[90c8b8d] | 56 | atomic_set(&lock->val, 0);
|
---|
[2d93f1f9] | 57 | #ifdef CONFIG_DEBUG_SPINLOCK
|
---|
[90c8b8d] | 58 | lock->name = name;
|
---|
| 59 | #endif
|
---|
[f761f1eb] | 60 | }
|
---|
| 61 |
|
---|
[90c8b8d] | 62 | #ifdef CONFIG_DEBUG_SPINLOCK
|
---|
| 63 |
|
---|
[5e04b48d] | 64 | /** Lock spinlock
|
---|
| 65 | *
|
---|
| 66 | * Lock spinlock.
|
---|
| 67 | * This version has limitted ability to report
|
---|
| 68 | * possible occurence of deadlock.
|
---|
| 69 | *
|
---|
[90c8b8d] | 70 | * @param lock Pointer to spinlock_t structure.
|
---|
| 71 | *
|
---|
[5e04b48d] | 72 | */
|
---|
[90c8b8d] | 73 | void spinlock_lock_debug(spinlock_t *lock)
|
---|
[f761f1eb] | 74 | {
|
---|
[98000fb] | 75 | size_t i = 0;
|
---|
[05e2a7ad] | 76 | bool deadlock_reported = false;
|
---|
[90c8b8d] | 77 |
|
---|
[c842f04] | 78 | preemption_disable();
|
---|
[90c8b8d] | 79 | while (test_and_set(&lock->val)) {
|
---|
[d9cf9d5f] | 80 | /*
|
---|
[90c8b8d] | 81 | * We need to be careful about particular locks
|
---|
| 82 | * which are directly used to report deadlocks
|
---|
| 83 | * via printf() (and recursively other functions).
|
---|
| 84 | * This conserns especially printf_lock and the
|
---|
| 85 | * framebuffer lock.
|
---|
[d9cf9d5f] | 86 | *
|
---|
[90c8b8d] | 87 | * Any lock whose name is prefixed by "*" will be
|
---|
| 88 | * ignored by this deadlock detection routine
|
---|
| 89 | * as this might cause an infinite recursion.
|
---|
[d9cf9d5f] | 90 | * We trust our code that there is no possible deadlock
|
---|
[90c8b8d] | 91 | * caused by these locks (except when an exception
|
---|
| 92 | * is triggered for instance by printf()).
|
---|
| 93 | *
|
---|
| 94 | * We encountered false positives caused by very
|
---|
| 95 | * slow framebuffer interaction (especially when
|
---|
[d9cf9d5f] | 96 | * run in a simulator) that caused problems with both
|
---|
[90c8b8d] | 97 | * printf_lock and the framebuffer lock.
|
---|
[d9cf9d5f] | 98 | *
|
---|
| 99 | */
|
---|
[90c8b8d] | 100 | if (lock->name[0] == '*')
|
---|
[d9cf9d5f] | 101 | continue;
|
---|
[90c8b8d] | 102 |
|
---|
[14df080] | 103 | if (i++ > DEADLOCK_THRESHOLD) {
|
---|
[e16e0d59] | 104 | printf("cpu%u: looping on spinlock %" PRIp ":%s, "
|
---|
[90c8b8d] | 105 | "caller=%" PRIp "(%s)\n", CPU->id, lock, lock->name,
|
---|
[e16e0d59] | 106 | CALLER, symtab_fmt_name_lookup(CALLER));
|
---|
[44814b8] | 107 |
|
---|
[f761f1eb] | 108 | i = 0;
|
---|
[05e2a7ad] | 109 | deadlock_reported = true;
|
---|
[f761f1eb] | 110 | }
|
---|
| 111 | }
|
---|
[90c8b8d] | 112 |
|
---|
[05e2a7ad] | 113 | if (deadlock_reported)
|
---|
[8ed4014] | 114 | printf("cpu%u: not deadlocked\n", CPU->id);
|
---|
[90c8b8d] | 115 |
|
---|
[5e04b48d] | 116 | /*
|
---|
| 117 | * Prevent critical section code from bleeding out this way up.
|
---|
| 118 | */
|
---|
[7dd56f1] | 119 | CS_ENTER_BARRIER();
|
---|
[f761f1eb] | 120 | }
|
---|
[90c8b8d] | 121 |
|
---|
[13108f24] | 122 | /** Unlock spinlock
|
---|
| 123 | *
|
---|
| 124 | * Unlock spinlock.
|
---|
| 125 | *
|
---|
| 126 | * @param sl Pointer to spinlock_t structure.
|
---|
| 127 | */
|
---|
| 128 | void spinlock_unlock_debug(spinlock_t *lock)
|
---|
| 129 | {
|
---|
[2b4a9f26] | 130 | ASSERT_SPINLOCK(atomic_get(&lock->val) != 0, lock);
|
---|
[13108f24] | 131 |
|
---|
| 132 | /*
|
---|
| 133 | * Prevent critical section code from bleeding out this way down.
|
---|
| 134 | */
|
---|
| 135 | CS_LEAVE_BARRIER();
|
---|
| 136 |
|
---|
| 137 | atomic_set(&lock->val, 0);
|
---|
| 138 | preemption_enable();
|
---|
| 139 | }
|
---|
| 140 |
|
---|
[f761f1eb] | 141 | #endif
|
---|
| 142 |
|
---|
[5e04b48d] | 143 | /** Lock spinlock conditionally
|
---|
| 144 | *
|
---|
[2b4a9f26] | 145 | * Lock spinlock conditionally. If the spinlock is not available
|
---|
| 146 | * at the moment, signal failure.
|
---|
[5e04b48d] | 147 | *
|
---|
[90c8b8d] | 148 | * @param lock Pointer to spinlock_t structure.
|
---|
[5e04b48d] | 149 | *
|
---|
| 150 | * @return Zero on failure, non-zero otherwise.
|
---|
[90c8b8d] | 151 | *
|
---|
[5e04b48d] | 152 | */
|
---|
[90c8b8d] | 153 | int spinlock_trylock(spinlock_t *lock)
|
---|
[f761f1eb] | 154 | {
|
---|
[c842f04] | 155 | preemption_disable();
|
---|
[90c8b8d] | 156 | int rc = !test_and_set(&lock->val);
|
---|
| 157 |
|
---|
[5e04b48d] | 158 | /*
|
---|
| 159 | * Prevent critical section code from bleeding out this way up.
|
---|
| 160 | */
|
---|
[7dd56f1] | 161 | CS_ENTER_BARRIER();
|
---|
[90c8b8d] | 162 |
|
---|
[c842f04] | 163 | if (!rc)
|
---|
| 164 | preemption_enable();
|
---|
[7dd56f1] | 165 |
|
---|
| 166 | return rc;
|
---|
[f761f1eb] | 167 | }
|
---|
| 168 |
|
---|
| 169 | #endif
|
---|
[b45c443] | 170 |
|
---|
[cc73a8a1] | 171 | /** @}
|
---|
[b45c443] | 172 | */
|
---|