From d648f24d5e6506027e22952e1589083e36ad90de Mon Sep 17 00:00:00 2001 From: Yavor Goulishev Date: Mon, 23 Sep 2024 17:18:01 +0000 Subject: [PATCH] Migrate the C use of SbAtomic to c11. b/310724224 Change-Id: I3b2dbbac4455e1aa2da6df1db368d7f59beeefac --- chrome/updater/configurator.h | 4 ++ starboard/atomic.h | 4 ++ starboard/nplb/include_all.c | 1 - third_party/boringssl/BUILD.gn | 1 - third_party/boringssl/src/crypto/internal.h | 8 +-- .../boringssl/src/crypto/refcount_starboard.c | 57 ------------------- .../boringssl/src/crypto/thread_starboard.cc | 14 +++-- .../boringssl/src/include/openssl/thread.h | 8 --- 8 files changed, 18 insertions(+), 79 deletions(-) delete mode 100644 third_party/boringssl/src/crypto/refcount_starboard.c diff --git a/chrome/updater/configurator.h b/chrome/updater/configurator.h index 9478dc709ec5..5b056f25d41e 100644 --- a/chrome/updater/configurator.h +++ b/chrome/updater/configurator.h @@ -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; diff --git a/starboard/atomic.h b/starboard/atomic.h index 036eb3492dd8..f5f1632a1965 100644 --- a/starboard/atomic.h +++ b/starboard/atomic.h @@ -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 diff --git a/starboard/nplb/include_all.c b/starboard/nplb/include_all.c index 6f576ef72662..d5ae969fa402 100644 --- a/starboard/nplb/include_all.c +++ b/starboard/nplb/include_all.c @@ -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" diff --git a/third_party/boringssl/BUILD.gn b/third_party/boringssl/BUILD.gn index dc4c34a43df5..f1f2025412ec 100644 --- a/third_party/boringssl/BUILD.gn +++ b/third_party/boringssl/BUILD.gn @@ -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" ] diff --git a/third_party/boringssl/src/crypto/internal.h b/third_party/boringssl/src/crypto/internal.h index 52787aa882f4..6c57947619ed 100644 --- a/third_party/boringssl/src/crypto/internal.h +++ b/third_party/boringssl/src/crypto/internal.h @@ -121,7 +121,6 @@ #ifdef STARBOARD #include -#include "starboard/atomic.h" #include "starboard/thread.h" #endif @@ -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 @@ -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 } diff --git a/third_party/boringssl/src/crypto/refcount_starboard.c b/third_party/boringssl/src/crypto/refcount_starboard.c deleted file mode 100644 index 27c5e54bce57..000000000000 --- a/third_party/boringssl/src/crypto/refcount_starboard.c +++ /dev/null @@ -1,57 +0,0 @@ -// Copyright 2019 The Cobalt Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -// Based on refcount_c11.c implementation. - -#include "internal.h" - -#if defined(STARBOARD) - -#include "starboard/atomic.h" - -void CRYPTO_refcount_inc(CRYPTO_refcount_t *count) { - CRYPTO_refcount_t expected = SbAtomicNoBarrier_Load(count); - - 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) { - break; - } - expected = old_value; - } -} - -int CRYPTO_refcount_dec_and_test_zero(CRYPTO_refcount_t *count) { - CRYPTO_refcount_t expected = SbAtomicNoBarrier_Load(count); - - for (;;) { - if (expected == 0) { - SbSystemBreakIntoDebugger(); - } else if (expected == CRYPTO_REFCOUNT_MAX) { - 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) { - return new_value == 0; - } - expected = old_value; - } - } -} - -#endif // defined(STARBOARD) diff --git a/third_party/boringssl/src/crypto/thread_starboard.cc b/third_party/boringssl/src/crypto/thread_starboard.cc index 413615d5d9f9..b96e04438ce2 100644 --- a/third_party/boringssl/src/crypto/thread_starboard.cc +++ b/third_party/boringssl/src/crypto/thread_starboard.cc @@ -18,6 +18,7 @@ #if defined(STARBOARD) #include +#include #include #include @@ -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 } } diff --git a/third_party/boringssl/src/include/openssl/thread.h b/third_party/boringssl/src/include/openssl/thread.h index 6e4b72ff1e07..738bfa38f12d 100644 --- a/third_party/boringssl/src/include/openssl/thread.h +++ b/third_party/boringssl/src/include/openssl/thread.h @@ -61,10 +61,6 @@ #include -#ifdef STARBOARD -#include "starboard/atomic.h" -#endif - #if defined(__cplusplus) extern "C" { #endif @@ -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.