diff --git a/headers/private/kernel/smp.h b/headers/private/kernel/smp.h index 1fbc1cab44..9483adfada 100644 --- a/headers/private/kernel/smp.h +++ b/headers/private/kernel/smp.h @@ -41,6 +41,8 @@ typedef void (*smp_call_func)(uint32 data1, int32 currentCPU, uint32 data2, uint extern "C" { #endif +bool try_acquire_spinlock(spinlock* lock); + status_t smp_init(struct kernel_args *args); status_t smp_per_cpu_init(struct kernel_args *args, int32 cpu); status_t smp_init_post_generic_syscalls(void); @@ -71,10 +73,18 @@ int smp_intercpu_int_handler(int32 cpu); // {acquire,release}_spinlock(). #if !DEBUG_SPINLOCKS && !B_DEBUG_SPINLOCK_CONTENTION + +static inline bool +try_acquire_spinlock_inline(spinlock* lock) +{ + return atomic_or((int32*)lock, 1) == 0; +} + + static inline void acquire_spinlock_inline(spinlock* lock) { - if (atomic_or((int32*)lock, 1) == 0) + if (try_acquire_spinlock_inline(lock)) return; acquire_spinlock(lock); } @@ -87,8 +97,9 @@ release_spinlock_inline(spinlock* lock) } -#define acquire_spinlock(lock) acquire_spinlock_inline(lock) -#define release_spinlock(lock) release_spinlock_inline(lock) +#define try_acquire_spinlock(lock) try_acquire_spinlock_inline(lock) +#define acquire_spinlock(lock) acquire_spinlock_inline(lock) +#define release_spinlock(lock) release_spinlock_inline(lock) #endif // !DEBUG_SPINLOCKS && !B_DEBUG_SPINLOCK_CONTENTION diff --git a/src/system/kernel/smp.cpp b/src/system/kernel/smp.cpp index 3d7d58c0d9..d241ed0974 100644 --- a/src/system/kernel/smp.cpp +++ b/src/system/kernel/smp.cpp @@ -41,6 +41,7 @@ #endif +#undef try_acquire_spinlock #undef acquire_spinlock #undef release_spinlock @@ -292,6 +293,32 @@ process_all_pending_ici(int32 currentCPU) } +bool +try_acquire_spinlock(spinlock* lock) +{ +#if DEBUG_SPINLOCKS + if (are_interrupts_enabled()) { + panic("try_acquire_spinlock: attempt to acquire lock %p with " + "interrupts enabled", lock); + } +#endif + +#if B_DEBUG_SPINLOCK_CONTENTION + if (atomic_add(&lock->lock, 1) != 0) + return false; +#else + if (atomic_or((int32*)lock, 1) != 0) + return false; + +# if DEBUG_SPINLOCKS + push_lock_caller(arch_debug_get_caller(), lock); +# endif +#endif + + return true; +} + + void acquire_spinlock(spinlock* lock) {