Skip to content

Commit

Permalink
Migrate the C use of SbAtomic to c11 (#4145)
Browse files Browse the repository at this point in the history
b/310724224
  • Loading branch information
y4vor authored Sep 27, 2024
1 parent 5f4785c commit ab487e5
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 101 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/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,7 @@ 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",
"src/crypto/thread_starboard.c",
]
public -= [ "src/include/openssl/opensslconf.h" ]
public_deps = [
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.

Original file line number Diff line number Diff line change
Expand Up @@ -18,71 +18,72 @@

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

#include <openssl/mem.h>

#include "starboard/thread.h"

namespace {

// Each enum of thread_local_data_t corresponds to a ThreadLocalEntry.
struct ThreadLocalEntry {
typedef struct ThreadLocalEntry {
thread_local_destructor_t destructor;
void* value;
};
} ThreadLocalEntry;

// One thread local key will point to an array of ThreadLocalEntry.
static pthread_key_t g_thread_local_key = 0;
static int g_thread_local_key_created = 0;

// Control the creation of the global thread local key.
pthread_once_t g_thread_local_once_control = PTHREAD_ONCE_INIT;
static pthread_once_t g_thread_local_once_control = PTHREAD_ONCE_INIT;

void ThreadLocalDestructor(void* value) {
static void ThreadLocalDestructor(void* value) {
if (value) {
ThreadLocalEntry* thread_locals = static_cast<ThreadLocalEntry*>(value);
ThreadLocalEntry* thread_locals = (ThreadLocalEntry*)(value);
for (int i = 0; i < NUM_OPENSSL_THREAD_LOCALS; ++i) {
if (thread_locals[i].destructor != nullptr) {
if (thread_locals[i].destructor != NULL) {
thread_locals[i].destructor(thread_locals[i].value);
}
}
OPENSSL_free(value);
}
}

void ThreadLocalInit() {
static void ThreadLocalInit() {
g_thread_local_key_created = pthread_key_create(&g_thread_local_key, &ThreadLocalDestructor) == 0;
}

void EnsureInitialized(struct CRYPTO_STATIC_MUTEX* lock) {
static void EnsureInitialized(struct CRYPTO_STATIC_MUTEX* lock) {
enum {
kUninitialized = 0,
kInitializing,
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
}
}

} // namespace

void CRYPTO_MUTEX_init(CRYPTO_MUTEX* lock) {
if (pthread_mutex_init(&lock->mutex, nullptr) != 0) {
if (pthread_mutex_init(&lock->mutex, NULL) != 0) {
SbSystemBreakIntoDebugger();
}
if (pthread_cond_init(&lock->condition, nullptr) != 0) {
if (pthread_cond_init(&lock->condition, NULL) != 0) {
SbSystemBreakIntoDebugger();
}
lock->readers = 0;
Expand Down Expand Up @@ -183,13 +184,13 @@ void CRYPTO_STATIC_MUTEX_unlock_write(struct CRYPTO_STATIC_MUTEX* lock) {
void* CRYPTO_get_thread_local(thread_local_data_t index) {
pthread_once(&g_thread_local_once_control, &ThreadLocalInit);
if (!g_thread_local_key_created) {
return nullptr;
return NULL;
}

ThreadLocalEntry* thread_locals = static_cast<ThreadLocalEntry*>(
ThreadLocalEntry* thread_locals = (ThreadLocalEntry*)(
pthread_getspecific(g_thread_local_key));
if (thread_locals == nullptr) {
return nullptr;
if (thread_locals == NULL) {
return NULL;
}

return thread_locals[index].value;
Expand All @@ -203,14 +204,14 @@ int CRYPTO_set_thread_local(thread_local_data_t index, void *value,
return 0;
}

ThreadLocalEntry* thread_locals = static_cast<ThreadLocalEntry*>(
ThreadLocalEntry* thread_locals = (ThreadLocalEntry*)(
pthread_getspecific(g_thread_local_key));
if (thread_locals == nullptr) {
if (thread_locals == NULL) {
size_t thread_locals_size =
sizeof(ThreadLocalEntry) * NUM_OPENSSL_THREAD_LOCALS;
thread_locals = static_cast<ThreadLocalEntry*>(
thread_locals = (ThreadLocalEntry*)(
OPENSSL_malloc(thread_locals_size));
if (thread_locals == nullptr) {
if (thread_locals == NULL) {
destructor(value);
return 0;
}
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 ab487e5

Please sign in to comment.