Changeset f43d8ce in mainline for kernel/generic/src/synch/spinlock.c
- Timestamp:
- 2023-02-02T21:58:36Z (15 months ago)
- Branches:
- master, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 2b264c4
- Parents:
- 95658c9
- git-author:
- Jiří Zárevúcky <zarevucky.jiri@…> (2023-01-19 22:40:04)
- git-committer:
- Jiří Zárevúcky <zarevucky.jiri@…> (2023-02-02 21:58:36)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
kernel/generic/src/synch/spinlock.c
r95658c9 rf43d8ce 1 1 /* 2 2 * Copyright (c) 2001-2004 Jakub Jermar 3 * Copyright (c) 2023 Jiří Zárevúcky 3 4 * All rights reserved. 4 5 * … … 47 48 #include <cpu.h> 48 49 49 #ifdef CONFIG_SMP50 51 50 /** Initialize spinlock 52 51 * … … 56 55 void spinlock_initialize(spinlock_t *lock, const char *name) 57 56 { 57 #ifdef CONFIG_SMP 58 58 atomic_flag_clear_explicit(&lock->flag, memory_order_relaxed); 59 59 #ifdef CONFIG_DEBUG_SPINLOCK 60 60 lock->name = name; 61 61 #endif 62 #endif 62 63 } 63 64 64 #ifdef CONFIG_DEBUG_SPINLOCK65 66 65 /** Lock spinlock 67 *68 * Lock spinlock.69 * This version has limitted ability to report70 * possible occurence of deadlock.71 66 * 72 67 * @param lock Pointer to spinlock_t structure. 73 68 * 74 69 */ 75 void spinlock_lock _debug(spinlock_t *lock)70 void spinlock_lock(spinlock_t *lock) 76 71 { 72 preemption_disable(); 73 74 #ifdef CONFIG_SMP 75 bool deadlock_reported = false; 77 76 size_t i = 0; 78 bool deadlock_reported = false;79 77 80 preemption_disable();81 78 while (atomic_flag_test_and_set_explicit(&lock->flag, memory_order_acquire)) { 79 #ifdef CONFIG_DEBUG_SPINLOCK 82 80 /* 83 81 * We need to be careful about particular locks … … 111 109 deadlock_reported = true; 112 110 } 111 #endif 113 112 } 113 114 /* Avoid compiler warning with debug disabled. */ 115 (void) i; 114 116 115 117 if (deadlock_reported) 116 118 printf("cpu%u: not deadlocked\n", CPU->id); 119 120 #endif 117 121 } 118 122 119 123 /** Unlock spinlock 120 124 * 121 * Unlock spinlock.122 *123 125 * @param sl Pointer to spinlock_t structure. 124 126 */ 125 void spinlock_unlock _debug(spinlock_t *lock)127 void spinlock_unlock(spinlock_t *lock) 126 128 { 129 #ifdef CONFIG_SMP 130 #ifdef CONFIG_DEBUG_SPINLOCK 127 131 ASSERT_SPINLOCK(spinlock_locked(lock), lock); 132 #endif 128 133 129 134 atomic_flag_clear_explicit(&lock->flag, memory_order_release); 135 #endif 136 130 137 preemption_enable(); 131 138 } 132 139 133 #endif 134 135 /** Lock spinlock conditionally 136 * 140 /** 137 141 * Lock spinlock conditionally. If the spinlock is not available 138 142 * at the moment, signal failure. … … 140 144 * @param lock Pointer to spinlock_t structure. 141 145 * 142 * @return Zero on failure, non-zero otherwise.146 * @return true on success. 143 147 * 144 148 */ … … 146 150 { 147 151 preemption_disable(); 152 153 #ifdef CONFIG_SMP 148 154 bool ret = !atomic_flag_test_and_set_explicit(&lock->flag, memory_order_acquire); 149 155 … … 152 158 153 159 return ret; 160 #else 161 return true; 162 #endif 154 163 } 155 164 … … 161 170 bool spinlock_locked(spinlock_t *lock) 162 171 { 172 #ifdef CONFIG_SMP 163 173 // NOTE: Atomic flag doesn't support simple atomic read (by design), 164 174 // so instead we test_and_set and then clear if necessary. … … 170 180 atomic_flag_clear_explicit(&lock->flag, memory_order_relaxed); 171 181 return ret; 182 #else 183 return true; 184 #endif 172 185 } 173 174 #endif175 186 176 187 /** @}
Note:
See TracChangeset
for help on using the changeset viewer.