Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ sentry_target_sources_cwd(sentry
sentry_session.h
sentry_slice.c
sentry_slice.h
sentry_spinlock.h
sentry_string.c
sentry_string.h
sentry_symbolizer.h
Expand Down Expand Up @@ -98,7 +99,6 @@ if(WIN32)
)
elseif(NX OR PROSPERO)
sentry_target_sources_cwd(sentry
sentry_unix_spinlock.h
path/sentry_path_unix.c
process/sentry_process_none.c
)
Expand All @@ -108,7 +108,6 @@ else()
sentry_random.h
sentry_unix_pageallocator.c
sentry_unix_pageallocator.h
sentry_unix_spinlock.h
symbolizer/sentry_symbolizer_unix.c
path/sentry_path_unix.c
process/sentry_process_unix.c
Expand Down
63 changes: 63 additions & 0 deletions src/sentry_spinlock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#ifndef SENTRY_SPINLOCK_H_INCLUDED
#define SENTRY_SPINLOCK_H_INCLUDED

#include "sentry_boot.h"
#include "sentry_cpu_relax.h"

typedef volatile long sentry_spinlock_t;
typedef bool (*sentry_spinlock_wait_func_t)(int attempt, void *data);

#define SENTRY__SPINLOCK_INIT 0

static inline bool
sentry__spinlock_try_lock(sentry_spinlock_t *spinlock)
{
#ifdef SENTRY_PLATFORM_WINDOWS
# if SIZEOF_LONG == 8
return InterlockedCompareExchange64((volatile LONG64 *)spinlock, 1, 0) == 0;
# else
return InterlockedCompareExchange((volatile LONG *)spinlock, 1, 0) == 0;
Comment thread
sentry[bot] marked this conversation as resolved.
# endif
#else
long unlocked = 0;
return __atomic_compare_exchange_n(
spinlock, &unlocked, 1, false, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED);
#endif
}

static inline void
sentry__spinlock_lock(sentry_spinlock_t *spinlock)
{
while (!sentry__spinlock_try_lock(spinlock)) {
sentry__cpu_relax();
}
}

static inline bool
sentry__spinlock_wait(sentry_spinlock_t *spinlock,
sentry_spinlock_wait_func_t wait_func, void *data)
{
int attempts = 0;
while (!sentry__spinlock_try_lock(spinlock)) {
if (!wait_func || !wait_func(++attempts, data)) {
return false;
}
}
return true;
}

static inline void
sentry__spinlock_unlock(sentry_spinlock_t *spinlock)
{
#ifdef SENTRY_PLATFORM_WINDOWS
# if SIZEOF_LONG == 8
InterlockedExchange64((volatile LONG64 *)spinlock, 0);
# else
InterlockedExchange((volatile LONG *)spinlock, 0);
# endif
#else
__atomic_store_n(spinlock, 0, __ATOMIC_RELEASE);
#endif
}

#endif
2 changes: 1 addition & 1 deletion src/sentry_unix_pageallocator.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "sentry_unix_pageallocator.h"
#include "sentry_core.h"
#include "sentry_unix_spinlock.h"
#include "sentry_spinlock.h"

#include <sys/mman.h>
#include <unistd.h>
Expand Down
28 changes: 0 additions & 28 deletions src/sentry_unix_spinlock.h

This file was deleted.

32 changes: 32 additions & 0 deletions tests/unit/test_sync.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "sentry_core.h"
#include "sentry_spinlock.h"
#include "sentry_sync.h"
#include "sentry_testsupport.h"
#include "sentry_utils.h"
Expand Down Expand Up @@ -669,3 +670,34 @@ SENTRY_TEST(cond_wait_timeout_overflow)
pthread_cond_destroy(&cond);
#endif
}

static long g_spin_waits = 0;

static bool
spin_wait(int UNUSED(attempt), void *UNUSED(data))
{
sentry__atomic_fetch_and_add(&g_spin_waits, 1);
return sentry__atomic_fetch(&g_spin_waits) < 2;
}

SENTRY_TEST(spinlock)
{
sentry_spinlock_t lock = SENTRY__SPINLOCK_INIT;
sentry__atomic_store(&g_spin_waits, 0);

TEST_CHECK(sentry__spinlock_try_lock(&lock));
TEST_CHECK_INT_EQUAL(sentry__atomic_fetch(&lock), 1);
TEST_CHECK(!sentry__spinlock_try_lock(&lock));
sentry__spinlock_unlock(&lock);
TEST_CHECK_INT_EQUAL(sentry__atomic_fetch(&lock), 0);

sentry__spinlock_lock(&lock);
TEST_CHECK_INT_EQUAL(sentry__atomic_fetch(&lock), 1);
TEST_CHECK(!sentry__spinlock_wait(&lock, spin_wait, NULL));
TEST_CHECK_INT_EQUAL(sentry__atomic_fetch(&g_spin_waits), 2);
sentry__spinlock_unlock(&lock);
TEST_CHECK_INT_EQUAL(sentry__atomic_fetch(&lock), 0);
TEST_CHECK(sentry__spinlock_wait(&lock, spin_wait, NULL));
TEST_CHECK_INT_EQUAL(sentry__atomic_fetch(&g_spin_waits), 2);
sentry__spinlock_unlock(&lock);
}
1 change: 1 addition & 0 deletions tests/unit/tests.inc
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ XX(span_data_n)
XX(span_tagging)
XX(span_tagging_n)
XX(spans_on_scope)
XX(spinlock)
XX(stack_guarantee)
XX(stack_guarantee_auto_init)
XX(strict_continuation_asymmetric_lenient_continues)
Expand Down
Loading