From f147df7552ed5ada91c56bb0ae321b0b972323cf 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 | 14 ++++++++++++++ gum/backend-arm64/gumspinlock-arm64.c | 14 ++++++++++++++ gum/backend-mips/gumspinlock-mips.c | 14 ++++++++++++++ gum/backend-x86/gumspinlock-amd64-msc.c | 14 ++++++++++++++ gum/backend-x86/gumspinlock-ia32-msc.c | 14 ++++++++++++++ gum/backend-x86/gumspinlock-x86.c | 14 ++++++++++++++ gum/gumspinlock.h | 2 ++ 7 files changed, 86 insertions(+) diff --git a/gum/backend-arm/gumspinlock-arm.c b/gum/backend-arm/gumspinlock-arm.c index e6fbc9233..59802b76c 100644 --- a/gum/backend-arm/gumspinlock-arm.c +++ b/gum/backend-arm/gumspinlock-arm.c @@ -1,5 +1,6 @@ /* * Copyright (C) 2010-2021 Ole André Vadla Ravnås + * Copyright (C) 2024 Francesco Tamagni * * Licence: wxWindows Library Licence, Version 3.1 */ @@ -35,6 +36,19 @@ gum_spinlock_acquire (GumSpinlock * spinlock) #endif } +gboolean +gum_spinlock_try_acquire (GumSpinlock * spinlock) +{ + GumSpinlockImpl * self = (GumSpinlockImpl *) spinlock; + + if (self->is_held) + return FALSE; + + gum_spinlock_acquire (spinlock); + + return TRUE; +} + void gum_spinlock_release (GumSpinlock * spinlock) { diff --git a/gum/backend-arm64/gumspinlock-arm64.c b/gum/backend-arm64/gumspinlock-arm64.c index 46b7bc643..5a1ba4c9e 100644 --- a/gum/backend-arm64/gumspinlock-arm64.c +++ b/gum/backend-arm64/gumspinlock-arm64.c @@ -1,5 +1,6 @@ /* * Copyright (C) 2014-2019 Ole André Vadla Ravnås + * Copyright (C) 2024 Francesco Tamagni * * Licence: wxWindows Library Licence, Version 3.1 */ @@ -30,6 +31,19 @@ gum_spinlock_acquire (GumSpinlock * spinlock) ; } +gboolean +gum_spinlock_try_acquire (GumSpinlock * spinlock) +{ + GumSpinlockImpl * self = (GumSpinlockImpl *) spinlock; + + if (self->is_held) + return FALSE; + + gum_spinlock_acquire (spinlock); + + return TRUE; +} + void gum_spinlock_release (GumSpinlock * spinlock) { diff --git a/gum/backend-mips/gumspinlock-mips.c b/gum/backend-mips/gumspinlock-mips.c index 46b7bc643..5a1ba4c9e 100644 --- a/gum/backend-mips/gumspinlock-mips.c +++ b/gum/backend-mips/gumspinlock-mips.c @@ -1,5 +1,6 @@ /* * Copyright (C) 2014-2019 Ole André Vadla Ravnås + * Copyright (C) 2024 Francesco Tamagni * * Licence: wxWindows Library Licence, Version 3.1 */ @@ -30,6 +31,19 @@ gum_spinlock_acquire (GumSpinlock * spinlock) ; } +gboolean +gum_spinlock_try_acquire (GumSpinlock * spinlock) +{ + GumSpinlockImpl * self = (GumSpinlockImpl *) spinlock; + + if (self->is_held) + return FALSE; + + gum_spinlock_acquire (spinlock); + + return TRUE; +} + void gum_spinlock_release (GumSpinlock * spinlock) { diff --git a/gum/backend-x86/gumspinlock-amd64-msc.c b/gum/backend-x86/gumspinlock-amd64-msc.c index 5874c93a2..918536938 100644 --- a/gum/backend-x86/gumspinlock-amd64-msc.c +++ b/gum/backend-x86/gumspinlock-amd64-msc.c @@ -1,5 +1,6 @@ /* * Copyright (C) 2010-2019 Ole André Vadla Ravnås + * Copyright (C) 2024 Francesco Tamagni * * Licence: wxWindows Library Licence, Version 3.1 */ @@ -30,6 +31,19 @@ gum_spinlock_acquire (GumSpinlock * spinlock) ; } +gboolean +gum_spinlock_try_acquire (GumSpinlock * spinlock) +{ + GumSpinlockImpl * self = (GumSpinlockImpl *) spinlock; + + if (self->is_held) + return FALSE; + + gum_spinlock_acquire (spinlock); + + return TRUE; +} + void gum_spinlock_release (GumSpinlock * spinlock) { diff --git a/gum/backend-x86/gumspinlock-ia32-msc.c b/gum/backend-x86/gumspinlock-ia32-msc.c index 433c304f9..912a63255 100644 --- a/gum/backend-x86/gumspinlock-ia32-msc.c +++ b/gum/backend-x86/gumspinlock-ia32-msc.c @@ -1,5 +1,6 @@ /* * Copyright (C) 2010-2019 Ole André Vadla Ravnås + * Copyright (C) 2024 Francesco Tamagni * * Licence: wxWindows Library Licence, Version 3.1 */ @@ -35,6 +36,19 @@ gum_spinlock_acquire (GumSpinlock * spinlock) } } +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; +} + __declspec (naked) void gum_spinlock_release (GumSpinlock * spinlock) { diff --git a/gum/backend-x86/gumspinlock-x86.c b/gum/backend-x86/gumspinlock-x86.c index 79f5647cb..ac882e4f4 100644 --- a/gum/backend-x86/gumspinlock-x86.c +++ b/gum/backend-x86/gumspinlock-x86.c @@ -1,5 +1,6 @@ /* * Copyright (C) 2010-2019 Ole André Vadla Ravnås + * Copyright (C) 2024 Francesco Tamagni * * Licence: wxWindows Library Licence, Version 3.1 */ @@ -30,6 +31,19 @@ gum_spinlock_acquire (GumSpinlock * spinlock) ; } +gboolean +gum_spinlock_try_acquire (GumSpinlock * spinlock) +{ + GumSpinlockImpl * self = (GumSpinlockImpl *) spinlock; + + if (self->is_held) + return FALSE; + + gum_spinlock_acquire (spinlock); + + return TRUE; +} + void gum_spinlock_release (GumSpinlock * spinlock) { diff --git a/gum/gumspinlock.h b/gum/gumspinlock.h index c9d7f8199..c0874c779 100644 --- a/gum/gumspinlock.h +++ b/gum/gumspinlock.h @@ -1,5 +1,6 @@ /* * Copyright (C) 2010-2019 Ole André Vadla Ravnås + * Copyright (C) 2024 Francesco Tamagni * * Licence: wxWindows Library Licence, Version 3.1 */ @@ -23,6 +24,7 @@ struct _GumSpinlock void gum_spinlock_init (GumSpinlock * spinlock); void gum_spinlock_acquire (GumSpinlock * spinlock); +gboolean gum_spinlock_try_acquire (GumSpinlock * spinlock); void gum_spinlock_release (GumSpinlock * spinlock); G_END_DECLS