From fc995f260db618014f1cdd6759d48342038f8650 Mon Sep 17 00:00:00 2001 From: Francesco Tamagni Date: Wed, 27 Mar 2024 16:49:35 +0100 Subject: [PATCH] spinlock: Add try_acquire --- gum/backend-arm/gumspinlock-arm.c | 12 ++++++++++++ gum/backend-arm64/gumspinlock-arm64.c | 12 ++++++++++++ gum/backend-mips/gumspinlock-mips.c | 12 ++++++++++++ gum/backend-x86/gumspinlock-amd64-msc.c | 12 ++++++++++++ gum/backend-x86/gumspinlock-ia32-msc.c | 12 ++++++++++++ gum/backend-x86/gumspinlock-x86.c | 12 ++++++++++++ gum/gumspinlock.h | 2 ++ 7 files changed, 74 insertions(+) diff --git a/gum/backend-arm/gumspinlock-arm.c b/gum/backend-arm/gumspinlock-arm.c index e6fbc9233a..c7277c2362 100644 --- a/gum/backend-arm/gumspinlock-arm.c +++ b/gum/backend-arm/gumspinlock-arm.c @@ -46,3 +46,15 @@ gum_spinlock_release (GumSpinlock * spinlock) g_atomic_int_set (&self->is_held, FALSE); #endif } + +gboolean +gum_spinlock_try_acquire (GumSpinlock * spinlock) +{ + GumSpinlockImpl * self = (GumSpinlockImpl *) spinlock; + if (self->is_held) + return FALSE; + + gum_spinlock_acquire (spinlock); + + return TRUE; +} diff --git a/gum/backend-arm64/gumspinlock-arm64.c b/gum/backend-arm64/gumspinlock-arm64.c index 46b7bc6436..95a25cc069 100644 --- a/gum/backend-arm64/gumspinlock-arm64.c +++ b/gum/backend-arm64/gumspinlock-arm64.c @@ -37,3 +37,15 @@ gum_spinlock_release (GumSpinlock * spinlock) __sync_lock_release (&self->is_held); } + +gboolean +gum_spinlock_try_acquire (GumSpinlock * spinlock) +{ + GumSpinlockImpl * self = (GumSpinlockImpl *) spinlock; + if (self->is_held) + return FALSE; + + gum_spinlock_acquire (spinlock); + + return TRUE; +} diff --git a/gum/backend-mips/gumspinlock-mips.c b/gum/backend-mips/gumspinlock-mips.c index 46b7bc6436..95a25cc069 100644 --- a/gum/backend-mips/gumspinlock-mips.c +++ b/gum/backend-mips/gumspinlock-mips.c @@ -37,3 +37,15 @@ gum_spinlock_release (GumSpinlock * spinlock) __sync_lock_release (&self->is_held); } + +gboolean +gum_spinlock_try_acquire (GumSpinlock * spinlock) +{ + GumSpinlockImpl * self = (GumSpinlockImpl *) spinlock; + if (self->is_held) + return FALSE; + + gum_spinlock_acquire (spinlock); + + return TRUE; +} diff --git a/gum/backend-x86/gumspinlock-amd64-msc.c b/gum/backend-x86/gumspinlock-amd64-msc.c index 5874c93a2d..0de5b4aac8 100644 --- a/gum/backend-x86/gumspinlock-amd64-msc.c +++ b/gum/backend-x86/gumspinlock-amd64-msc.c @@ -37,3 +37,15 @@ gum_spinlock_release (GumSpinlock * spinlock) g_atomic_int_set (&self->is_held, FALSE); } + +gboolean +gum_spinlock_try_acquire (GumSpinlock * spinlock) +{ + GumSpinlockImpl * self = (GumSpinlockImpl *) spinlock; + if (self->is_held) + return FALSE; + + gum_spinlock_acquire (spinlock); + + return TRUE; +} diff --git a/gum/backend-x86/gumspinlock-ia32-msc.c b/gum/backend-x86/gumspinlock-ia32-msc.c index 433c304f93..e8047d569f 100644 --- a/gum/backend-x86/gumspinlock-ia32-msc.c +++ b/gum/backend-x86/gumspinlock-ia32-msc.c @@ -45,3 +45,15 @@ gum_spinlock_release (GumSpinlock * spinlock) ret; } } + +gboolean +gum_spinlock_try_acquire (GumSpinlock * spinlock) +{ + volatile guint32 is_held = *(guint32*)spinlock; + if (is_held == 1) + return FALSE; + + gum_spinlock_acquire (spinlock); + + return TRUE; +} diff --git a/gum/backend-x86/gumspinlock-x86.c b/gum/backend-x86/gumspinlock-x86.c index 79f5647cb0..f610ba3753 100644 --- a/gum/backend-x86/gumspinlock-x86.c +++ b/gum/backend-x86/gumspinlock-x86.c @@ -37,3 +37,15 @@ gum_spinlock_release (GumSpinlock * spinlock) __sync_lock_release (&self->is_held); } + +gboolean +gum_spinlock_try_acquire (GumSpinlock * spinlock) +{ + GumSpinlockImpl * self = (GumSpinlockImpl *) spinlock; + if (self->is_held) + return FALSE; + + gum_spinlock_acquire (spinlock); + + return TRUE; +} diff --git a/gum/gumspinlock.h b/gum/gumspinlock.h index c9d7f81997..bd9cd23389 100644 --- a/gum/gumspinlock.h +++ b/gum/gumspinlock.h @@ -25,6 +25,8 @@ void gum_spinlock_init (GumSpinlock * spinlock); void gum_spinlock_acquire (GumSpinlock * spinlock); void gum_spinlock_release (GumSpinlock * spinlock); +gboolean gum_spinlock_try_acquire (GumSpinlock * spinlock); + G_END_DECLS #endif