Skip to content

Commit

Permalink
spinlock: Add try_acquire()
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmacete authored and oleavr committed May 10, 2024
1 parent ee86e66 commit f147df7
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 0 deletions.
14 changes: 14 additions & 0 deletions gum/backend-arm/gumspinlock-arm.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2010-2021 Ole André Vadla Ravnås <[email protected]>
* Copyright (C) 2024 Francesco Tamagni <[email protected]>
*
* Licence: wxWindows Library Licence, Version 3.1
*/
Expand Down Expand Up @@ -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)
{
Expand Down
14 changes: 14 additions & 0 deletions gum/backend-arm64/gumspinlock-arm64.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2014-2019 Ole André Vadla Ravnås <[email protected]>
* Copyright (C) 2024 Francesco Tamagni <[email protected]>
*
* Licence: wxWindows Library Licence, Version 3.1
*/
Expand Down Expand Up @@ -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)
{
Expand Down
14 changes: 14 additions & 0 deletions gum/backend-mips/gumspinlock-mips.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2014-2019 Ole André Vadla Ravnås <[email protected]>
* Copyright (C) 2024 Francesco Tamagni <[email protected]>
*
* Licence: wxWindows Library Licence, Version 3.1
*/
Expand Down Expand Up @@ -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)
{
Expand Down
14 changes: 14 additions & 0 deletions gum/backend-x86/gumspinlock-amd64-msc.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2010-2019 Ole André Vadla Ravnås <[email protected]>
* Copyright (C) 2024 Francesco Tamagni <[email protected]>
*
* Licence: wxWindows Library Licence, Version 3.1
*/
Expand Down Expand Up @@ -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)
{
Expand Down
14 changes: 14 additions & 0 deletions gum/backend-x86/gumspinlock-ia32-msc.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2010-2019 Ole André Vadla Ravnås <[email protected]>
* Copyright (C) 2024 Francesco Tamagni <[email protected]>
*
* Licence: wxWindows Library Licence, Version 3.1
*/
Expand Down Expand Up @@ -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)
{
Expand Down
14 changes: 14 additions & 0 deletions gum/backend-x86/gumspinlock-x86.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2010-2019 Ole André Vadla Ravnås <[email protected]>
* Copyright (C) 2024 Francesco Tamagni <[email protected]>
*
* Licence: wxWindows Library Licence, Version 3.1
*/
Expand Down Expand Up @@ -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)
{
Expand Down
2 changes: 2 additions & 0 deletions gum/gumspinlock.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2010-2019 Ole André Vadla Ravnås <[email protected]>
* Copyright (C) 2024 Francesco Tamagni <[email protected]>
*
* Licence: wxWindows Library Licence, Version 3.1
*/
Expand All @@ -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
Expand Down

0 comments on commit f147df7

Please sign in to comment.