Skip to content

Commit

Permalink
Migrate the C use of SbAtomic to compiler built-ins.
Browse files Browse the repository at this point in the history
b/310724224

Change-Id: I3b2dbbac4455e1aa2da6df1db368d7f59beeefac
  • Loading branch information
y4vor committed Sep 23, 2024
1 parent 948d0bf commit 2ada162
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 28 deletions.
4 changes: 4 additions & 0 deletions chrome/updater/configurator.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
#include "components/update_client/configurator.h"
#include "components/update_client/persisted_data.h"

#if defined(STARBOARD)
#include "starboard/atomic.h"
#endif

class GURL;
class PrefService;

Expand Down
4 changes: 4 additions & 0 deletions starboard/atomic.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
#include "starboard/configuration.h"
#include "starboard/types.h"

#ifndef __cplusplus
#error "This header should not be included in C source code"
#endif

#ifdef __cplusplus
extern "C" {
#endif
Expand Down
1 change: 0 additions & 1 deletion starboard/nplb/include_all.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

// Includes all headers in a C context to make sure they compile as C files.

#include "starboard/atomic.h"
#include "starboard/audio_sink.h"
#include "starboard/configuration.h"
#include "starboard/cpu_features.h"
Expand Down
3 changes: 1 addition & 2 deletions third_party/boringssl/src/crypto/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@
#ifdef STARBOARD
#include <pthread.h>

#include "starboard/atomic.h"
#include "starboard/thread.h"
#endif

Expand Down Expand Up @@ -606,7 +605,7 @@ OPENSSL_EXPORT int CRYPTO_refcount_dec_and_test_zero(CRYPTO_refcount_t *count);

#if defined(STARBOARD)
struct CRYPTO_STATIC_MUTEX {
SbAtomic32 initialized;
uint32_t initialized;
CRYPTO_MUTEX mutex;
};
#define CRYPTO_STATIC_MUTEX_INIT { 0 }
Expand Down
18 changes: 6 additions & 12 deletions third_party/boringssl/src/crypto/refcount_starboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,20 @@

#if defined(STARBOARD)

#include "starboard/atomic.h"

void CRYPTO_refcount_inc(CRYPTO_refcount_t *count) {
CRYPTO_refcount_t expected = SbAtomicNoBarrier_Load(count);
CRYPTO_refcount_t expected = __atomic_load_n(count, __ATOMIC_RELAXED);

while (expected != CRYPTO_REFCOUNT_MAX) {
CRYPTO_refcount_t new_value = expected + 1;
CRYPTO_refcount_t old_value = SbAtomicNoBarrier_CompareAndSwap(
count, expected, new_value);
if (old_value == expected) {
if (__atomic_compare_exchange_n(count, &expected, new_value, false,
__ATOMIC_RELAXED, __ATOMIC_RELAXED)) {
break;
}
expected = old_value;
}
}

int CRYPTO_refcount_dec_and_test_zero(CRYPTO_refcount_t *count) {
CRYPTO_refcount_t expected = SbAtomicNoBarrier_Load(count);
CRYPTO_refcount_t expected = __atomic_load_n(count, __ATOMIC_RELAXED);

for (;;) {
if (expected == 0) {
Expand All @@ -44,12 +40,10 @@ int CRYPTO_refcount_dec_and_test_zero(CRYPTO_refcount_t *count) {
return 0;
} else {
CRYPTO_refcount_t new_value = expected - 1;
CRYPTO_refcount_t old_value = SbAtomicNoBarrier_CompareAndSwap(
count, expected, new_value);
if (old_value == expected) {
if (__atomic_compare_exchange_n(count, &expected, new_value, false,
__ATOMIC_RELAXED, __ATOMIC_RELAXED)) {
return new_value == 0;
}
expected = old_value;
}
}
}
Expand Down
12 changes: 7 additions & 5 deletions third_party/boringssl/src/crypto/thread_starboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,18 @@ void EnsureInitialized(struct CRYPTO_STATIC_MUTEX* lock) {
kInitialized
};

if (SbAtomicNoBarrier_Load(&lock->initialized) == kInitialized) {
if (__atomic_load_n(&lock->initialized, __ATOMIC_RELAXED) == kInitialized) {
return;
}
if (SbAtomicNoBarrier_CompareAndSwap(&lock->initialized,
kUninitialized, kInitializing) == kUninitialized) {

uint32_t expected = kUninitialized;
if (__atomic_compare_exchange_n(&lock->initialized, &expected, kInitializing,
false, __ATOMIC_RELAXED, __ATOMIC_RELAXED)) {
CRYPTO_MUTEX_init(&lock->mutex);
SbAtomicNoBarrier_Store(&lock->initialized, kInitialized);
__atomic_store_n(&lock->initialized, kInitialized, __ATOMIC_RELAXED);
return;
}
while (SbAtomicNoBarrier_Load(&lock->initialized) != kInitialized) {
while (__atomic_load_n(&lock->initialized, __ATOMIC_RELAXED) != kInitialized) {
usleep(1000); // 1ms
}
}
Expand Down
8 changes: 0 additions & 8 deletions third_party/boringssl/src/include/openssl/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,6 @@

#include <openssl/base.h>

#ifdef STARBOARD
#include "starboard/atomic.h"
#endif

#if defined(__cplusplus)
extern "C" {
#endif
Expand Down Expand Up @@ -114,11 +110,7 @@ typedef union crypto_mutex_st {
// as C code that might not set -std=c11. So, in practice, it's not possible to
// do that. Instead we statically assert that the size and native alignment of
// a plain uint32_t and an _Atomic uint32_t are equal in refcount_c11.c.
#if defined(STARBOARD)
typedef SbAtomic32 CRYPTO_refcount_t;
#else
typedef uint32_t CRYPTO_refcount_t;
#endif


// Deprecated functions.
Expand Down

0 comments on commit 2ada162

Please sign in to comment.