Skip to content

Commit

Permalink
spinlock: Add try_acquire
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmacete committed Apr 11, 2024
1 parent ffda5fc commit fc995f2
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 0 deletions.
12 changes: 12 additions & 0 deletions gum/backend-arm/gumspinlock-arm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
12 changes: 12 additions & 0 deletions gum/backend-arm64/gumspinlock-arm64.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
12 changes: 12 additions & 0 deletions gum/backend-mips/gumspinlock-mips.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
12 changes: 12 additions & 0 deletions gum/backend-x86/gumspinlock-amd64-msc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
12 changes: 12 additions & 0 deletions gum/backend-x86/gumspinlock-ia32-msc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
12 changes: 12 additions & 0 deletions gum/backend-x86/gumspinlock-x86.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 2 additions & 0 deletions gum/gumspinlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit fc995f2

Please sign in to comment.