Skip to content

Commit

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

Change-Id: I3b2dbbac4455e1aa2da6df1db368d7f59beeefac
  • Loading branch information
y4vor committed Sep 24, 2024
1 parent 948d0bf commit d648f24
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 79 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
1 change: 0 additions & 1 deletion third_party/boringssl/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ if (!use_cobalt_customizations) {
sources += [
"src/crypto/cpu-starboard.c",
"src/crypto/rand_extra/starboard.c",
"src/crypto/refcount_starboard.c",
"src/crypto/thread_starboard.cc",
]
public -= [ "src/include/openssl/opensslconf.h" ]
Expand Down
8 changes: 1 addition & 7 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 @@ -563,18 +562,13 @@ OPENSSL_EXPORT void CRYPTO_once(CRYPTO_once_t *once, void (*init)(void));

// Automatically enable C11 atomics if implemented.
#if !defined(OPENSSL_C11_ATOMIC) && defined(OPENSSL_THREADS) && \
!defined(STARBOARD) && \
!defined(__STDC_NO_ATOMICS__) && defined(__STDC_VERSION__) && \
__STDC_VERSION__ >= 201112L
#define OPENSSL_C11_ATOMIC
#endif

// CRYPTO_REFCOUNT_MAX is the value at which the reference count saturates.
#if defined(STARBOARD)
#define CRYPTO_REFCOUNT_MAX 0x7fffffff
#else
#define CRYPTO_REFCOUNT_MAX 0xffffffff
#endif

// CRYPTO_refcount_inc atomically increments the value at |*count| unless the
// value would overflow. It's safe for multiple threads to concurrently call
Expand Down Expand Up @@ -606,7 +600,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
57 changes: 0 additions & 57 deletions third_party/boringssl/src/crypto/refcount_starboard.c

This file was deleted.

14 changes: 9 additions & 5 deletions third_party/boringssl/src/crypto/thread_starboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#if defined(STARBOARD)
#include <pthread.h>
#include <stdatomic.h>
#include <unistd.h>

#include <openssl/mem.h>
Expand Down Expand Up @@ -62,16 +63,19 @@ void EnsureInitialized(struct CRYPTO_STATIC_MUTEX* lock) {
kInitialized
};

if (SbAtomicNoBarrier_Load(&lock->initialized) == kInitialized) {
if (atomic_load((_Atomic uint32_t *)&lock->initialized) == kInitialized) {
return;
}
if (SbAtomicNoBarrier_CompareAndSwap(&lock->initialized,
kUninitialized, kInitializing) == kUninitialized) {

uint32_t expected = kUninitialized;

if (atomic_compare_exchange_weak((_Atomic uint32_t *)&lock->initialized,
&expected, kInitializing)) {
CRYPTO_MUTEX_init(&lock->mutex);
SbAtomicNoBarrier_Store(&lock->initialized, kInitialized);
atomic_store((_Atomic uint32_t *)&lock->initialized, kInitialized);
return;
}
while (SbAtomicNoBarrier_Load(&lock->initialized) != kInitialized) {
while (atomic_load((_Atomic uint32_t *)&lock->initialized) != 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 d648f24

Please sign in to comment.