Rev 3343 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1 | jermar | 1 | /* |
| 2071 | jermar | 2 | * Copyright (c) 2001-2004 Jakub Jermar |
| 1 | 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 | |||
| 1757 | jermar | 29 | /** @addtogroup sync |
| 1702 | cejka | 30 | * @{ |
| 31 | */ |
||
| 32 | |||
| 1264 | jermar | 33 | /** |
| 1702 | cejka | 34 | * @file |
| 1264 | jermar | 35 | * @brief Spinlocks. |
| 36 | */ |
||
| 37 | |||
| 383 | jermar | 38 | #include <synch/spinlock.h> |
| 1104 | jermar | 39 | #include <atomic.h> |
| 153 | jermar | 40 | #include <arch/barrier.h> |
| 383 | jermar | 41 | #include <arch.h> |
| 223 | jermar | 42 | #include <preemption.h> |
| 195 | vana | 43 | #include <print.h> |
| 383 | jermar | 44 | #include <debug.h> |
| 552 | palkovsky | 45 | #include <symtab.h> |
| 3862 | rimsky | 46 | #include <arch/asm.h> |
| 1 | jermar | 47 | |
| 1313 | jermar | 48 | #ifdef CONFIG_FB |
| 49 | #include <genarch/fb/fb.h> |
||
| 50 | #endif |
||
| 51 | |||
| 458 | decky | 52 | #ifdef CONFIG_SMP |
| 1 | jermar | 53 | |
| 383 | jermar | 54 | /** Initialize spinlock |
| 55 | * |
||
| 56 | * Initialize spinlock. |
||
| 57 | * |
||
| 58 | * @param sl Pointer to spinlock_t structure. |
||
| 59 | */ |
||
| 552 | palkovsky | 60 | void spinlock_initialize(spinlock_t *sl, char *name) |
| 1 | jermar | 61 | { |
| 625 | palkovsky | 62 | atomic_set(&sl->val, 0); |
| 552 | palkovsky | 63 | #ifdef CONFIG_DEBUG_SPINLOCK |
| 64 | sl->name = name; |
||
| 65 | #endif |
||
| 1 | jermar | 66 | } |
| 67 | |||
| 383 | jermar | 68 | /** Lock spinlock |
| 69 | * |
||
| 70 | * Lock spinlock. |
||
| 71 | * This version has limitted ability to report |
||
| 72 | * possible occurence of deadlock. |
||
| 73 | * |
||
| 74 | * @param sl Pointer to spinlock_t structure. |
||
| 75 | */ |
||
| 1100 | palkovsky | 76 | #ifdef CONFIG_DEBUG_SPINLOCK |
| 77 | void spinlock_lock_debug(spinlock_t *sl) |
||
| 1 | jermar | 78 | { |
| 557 | jermar | 79 | count_t i = 0; |
| 552 | palkovsky | 80 | char *symbol; |
| 557 | jermar | 81 | bool deadlock_reported = false; |
| 1 | jermar | 82 | |
| 223 | jermar | 83 | preemption_disable(); |
| 1 | jermar | 84 | while (test_and_set(&sl->val)) { |
| 1313 | jermar | 85 | |
| 86 | /* |
||
| 2211 | decky | 87 | * We need to be careful about printf_lock and fb_lock. |
| 1313 | jermar | 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 |
||
| 2211 | decky | 97 | * printf_lock and fb_lock. |
| 1313 | jermar | 98 | * |
| 2211 | decky | 99 | * Possible deadlocks on both printf_lock and fb_lock |
| 1313 | jermar | 100 | * are therefore not reported as they would cause an |
| 101 | * infinite recursion. |
||
| 102 | */ |
||
| 2211 | decky | 103 | if (sl == &printf_lock) |
| 1313 | jermar | 104 | continue; |
| 105 | #ifdef CONFIG_FB |
||
| 106 | if (sl == &fb_lock) |
||
| 107 | continue; |
||
| 108 | #endif |
||
| 1667 | jermar | 109 | if (i++ > DEADLOCK_THRESHOLD) { |
| 3059 | decky | 110 | printf("cpu%u: looping on spinlock %" PRIp ":%s, caller=%" PRIp, |
| 111 | CPU->id, sl, sl->name, CALLER); |
||
| 622 | palkovsky | 112 | symbol = get_symtab_entry(CALLER); |
| 552 | palkovsky | 113 | if (symbol) |
| 114 | printf("(%s)", symbol); |
||
| 115 | printf("\n"); |
||
| 1 | jermar | 116 | i = 0; |
| 557 | jermar | 117 | deadlock_reported = true; |
| 1 | jermar | 118 | } |
| 119 | } |
||
| 383 | jermar | 120 | |
| 557 | jermar | 121 | if (deadlock_reported) |
| 3059 | decky | 122 | printf("cpu%u: not deadlocked\n", CPU->id); |
| 557 | jermar | 123 | |
| 383 | jermar | 124 | /* |
| 125 | * Prevent critical section code from bleeding out this way up. |
||
| 126 | */ |
||
| 153 | jermar | 127 | CS_ENTER_BARRIER(); |
| 1 | jermar | 128 | } |
| 129 | #endif |
||
| 130 | |||
| 383 | jermar | 131 | /** Lock spinlock conditionally |
| 132 | * |
||
| 133 | * Lock spinlock conditionally. |
||
| 134 | * If the spinlock is not available at the moment, |
||
| 135 | * signal failure. |
||
| 136 | * |
||
| 137 | * @param sl Pointer to spinlock_t structure. |
||
| 138 | * |
||
| 139 | * @return Zero on failure, non-zero otherwise. |
||
| 140 | */ |
||
| 1 | jermar | 141 | int spinlock_trylock(spinlock_t *sl) |
| 142 | { |
||
| 153 | jermar | 143 | int rc; |
| 144 | |||
| 223 | jermar | 145 | preemption_disable(); |
| 153 | jermar | 146 | rc = !test_and_set(&sl->val); |
| 383 | jermar | 147 | |
| 148 | /* |
||
| 149 | * Prevent critical section code from bleeding out this way up. |
||
| 150 | */ |
||
| 153 | jermar | 151 | CS_ENTER_BARRIER(); |
| 223 | jermar | 152 | |
| 153 | if (!rc) |
||
| 154 | preemption_enable(); |
||
| 153 | jermar | 155 | |
| 156 | return rc; |
||
| 1 | jermar | 157 | } |
| 158 | |||
| 159 | #endif |
||
| 1702 | cejka | 160 | |
| 1757 | jermar | 161 | /** @} |
| 1702 | cejka | 162 | */ |