Skip to content

Commit

Permalink
Migrate C++ code from starboard/atomic.h to std::atomic (#4199)
Browse files Browse the repository at this point in the history
Migrate C++ code from starboard/atomic.h to std::atomic

b/301268304

Change-Id: I67161b7dac0a087b2b930218d12cc0e6790db291
  • Loading branch information
iuriionishchenko authored Oct 15, 2024
1 parent e863eab commit 55a1d8c
Show file tree
Hide file tree
Showing 45 changed files with 101 additions and 496 deletions.
20 changes: 0 additions & 20 deletions starboard/android/arm/atomic_public.h

This file was deleted.

20 changes: 0 additions & 20 deletions starboard/android/arm64/atomic_public.h

This file was deleted.

1 change: 0 additions & 1 deletion starboard/android/shared/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ static_library("starboard_platform") {
"application_android.h",
"asset_manager.cc",
"asset_manager.h",
"atomic_public.h",
"audio_decoder.cc",
"audio_decoder.h",
"audio_decoder_passthrough.h",
Expand Down
6 changes: 3 additions & 3 deletions starboard/android/shared/application_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ void ApplicationAndroid::ProcessAndroidCommand() {

// Remember the Android activity state to sync to when we have a window.
case AndroidCommand::kStop:
SbAtomicBarrier_Increment(&android_stop_count_, -1);
android_stop_count_.fetch_sub(1, std::memory_order_seq_cst);
// Intentional fall-through.
case AndroidCommand::kStart:
case AndroidCommand::kResume:
Expand Down Expand Up @@ -393,7 +393,7 @@ void ApplicationAndroid::ProcessAndroidCommand() {

// If there's an outstanding "stop" command, then don't update the app state
// since it'll be overridden by the upcoming "stop" state.
if (SbAtomicAcquire_Load(&android_stop_count_) > 0) {
if (android_stop_count_.load(std::memory_order_acquire) > 0) {
return;
}

Expand Down Expand Up @@ -436,7 +436,7 @@ void ApplicationAndroid::SendAndroidCommand(AndroidCommand::CommandType type,
}
break;
case AndroidCommand::kStop:
SbAtomicBarrier_Increment(&android_stop_count_, 1);
android_stop_count_.fetch_add(1, std::memory_order_seq_cst);
break;
default:
break;
Expand Down
3 changes: 1 addition & 2 deletions starboard/android/shared/application_android.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include "game-activity/GameActivity.h"
#include "starboard/android/shared/input_events_generator.h"
#include "starboard/android/shared/jni_env_ext.h"
#include "starboard/atomic.h"
#include "starboard/common/condition_variable.h"
#include "starboard/common/mutex.h"
#include "starboard/configuration.h"
Expand Down Expand Up @@ -138,7 +137,7 @@ class ApplicationAndroid

// Track queued "stop" commands to avoid starting the app when Android has
// already requested it be stopped.
SbAtomic32 android_stop_count_ = 0;
std::atomic<int32_t> android_stop_count_{0};

// Set to true in the destructor to ensure other threads stop waiting.
std::atomic_bool application_destroying_{false};
Expand Down
26 changes: 0 additions & 26 deletions starboard/android/shared/atomic_public.h

This file was deleted.

1 change: 0 additions & 1 deletion starboard/android/shared/audio_renderer_passthrough.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include "starboard/android/shared/audio_decoder.h"
#include "starboard/android/shared/audio_track_bridge.h"
#include "starboard/android/shared/drm_system.h"
#include "starboard/atomic.h"
#include "starboard/common/mutex.h"
#include "starboard/common/ref_counted.h"
#include "starboard/drm.h"
Expand Down
1 change: 0 additions & 1 deletion starboard/android/shared/player_components_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#include "starboard/android/shared/media_capabilities_cache.h"
#include "starboard/android/shared/media_common.h"
#include "starboard/android/shared/video_decoder.h"
#include "starboard/atomic.h"
#include "starboard/common/log.h"
#include "starboard/common/media.h"
#include "starboard/common/ref_counted.h"
Expand Down
1 change: 0 additions & 1 deletion starboard/android/shared/video_decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include "starboard/android/shared/media_decoder.h"
#include "starboard/android/shared/video_frame_tracker.h"
#include "starboard/android/shared/video_window.h"
#include "starboard/atomic.h"
#include "starboard/common/condition_variable.h"
#include "starboard/common/optional.h"
#include "starboard/common/ref_counted.h"
Expand Down
20 changes: 0 additions & 20 deletions starboard/android/x86/atomic_public.h

This file was deleted.

4 changes: 0 additions & 4 deletions starboard/atomic.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,4 @@ SbAtomicRelease_LoadPtr(volatile const SbAtomicPtr* ptr) {
} // extern "C"
#endif

// Include the platform definitions of these functions, which should be defined
// as inlined. This macro is defined on the command line by GN.
#include STARBOARD_ATOMIC_INCLUDE

#endif // STARBOARD_ATOMIC_H_
1 change: 0 additions & 1 deletion starboard/build/config/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ config("starboard") {
}

defines += [
"STARBOARD_ATOMIC_INCLUDE=\"$starboard_path/atomic_public.h\"",
"STARBOARD_CONFIGURATION_INCLUDE=\"$starboard_path/configuration_public.h\"",
]
}
Expand Down
18 changes: 9 additions & 9 deletions starboard/common/experimental/concurrency_debug.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
#if SB_ENABLE_CONCURRENCY_DEBUG

#include <algorithm>
#include <atomic>
#include <string>
#include <vector>

#include "starboard/atomic.h"
#include "starboard/common/log.h"
#include "starboard/common/mutex.h"
#include "starboard/system.h"
Expand All @@ -39,15 +39,15 @@ const int64_t kMinimumWaitToLog = 5000; // 5ms
const int64_t kLoggingInterval = 5 * 1'000'000; // 5s
const int kStackTraceDepth = 0;

volatile SbAtomic32 s_mutex_acquire_call_counter = 0;
volatile SbAtomic32 s_mutex_acquire_contention_counter = 0;
volatile SbAtomic32 s_mutex_max_contention_time = 0;
volatile std::atomic<int32_t> s_mutex_acquire_call_counter{0};
volatile std::atomic<int32_t> s_mutex_acquire_contention_counter{0};
volatile std::atomic<int32_t> s_mutex_max_contention_time{0};

} // namespace

ScopedMutexWaitTracker::ScopedMutexWaitTracker(SbMutex* mutex)
: acquired_(SbMutexAcquireTry(mutex) == kSbMutexAcquired) {
SbAtomicNoBarrier_Increment(&s_mutex_acquire_call_counter, 1);
s_mutex_acquire_call_counter.fetch_add(1, std::memory_order_relaxed);
if (!acquired_) {
wait_start_ = starboard::CurrentMonotonicTime();
}
Expand All @@ -57,20 +57,20 @@ ScopedMutexWaitTracker::~ScopedMutexWaitTracker() {
if (kMinimumWaitToLog == 0 || acquired_) {
return;
}
if (SbAtomicNoBarrier_Increment(&s_mutex_acquire_contention_counter, 1) <
if ((s_mutex_acquire_contention_counter.fetch_add(1, std::memory_order_relaxed) + 1) <
kNumberOfInitialContentionsToIgnore) {
return;
}

auto elapsed = starboard::CurrentMonotonicTime() - wait_start_;

for (;;) {
SbAtomic32 old_value = s_mutex_max_contention_time;
int32_t old_value = s_mutex_max_contention_time;
if (elapsed <= old_value) {
break;
}
if (SbAtomicNoBarrier_CompareAndSwap(&s_mutex_max_contention_time,
old_value, elapsed) == old_value) {
if (s_mutex_max_contention_time.compare_exchange_weak(old_value, elapsed,
std::memory_order_release, std::memory_order_relaxed)) { old_value, elapsed) == old_value) {
break;
}
}
Expand Down
23 changes: 12 additions & 11 deletions starboard/common/instance_counter.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
#ifndef STARBOARD_COMMON_INSTANCE_COUNTER_H_
#define STARBOARD_COMMON_INSTANCE_COUNTER_H_

#include "starboard/atomic.h"
#include <atomic>

#include "starboard/common/log.h"

#if defined(COBALT_BUILD_TYPE_GOLD)
Expand All @@ -28,23 +29,23 @@

#define DECLARE_INSTANCE_COUNTER(class_name) \
namespace { \
SbAtomic32 s_##class_name##_instance_count = 0; \
std::atomic<int32_t> s_##class_name##_instance_count = 0; \
}

#define ON_INSTANCE_CREATED(class_name) \
{ \
SB_LOG(INFO) << "New instance of " << #class_name \
<< " is created. We have " \
<< (SbAtomicNoBarrier_Increment( \
&s_##class_name##_instance_count, 1)) \
<< " instances in total."; \
#define ON_INSTANCE_CREATED(class_name) \
{ \
SB_LOG(INFO) << "New instance of " << #class_name \
<< " is created. We have " \
<< s_##class_name##_instance_count. \
fetch_add(1, std::memory_order_relaxed) \
<< " instances in total."; \
}

#define ON_INSTANCE_RELEASED(class_name) \
{ \
SB_LOG(INFO) << "Instance of " << #class_name << " is released. We have " \
<< (SbAtomicNoBarrier_Increment( \
&s_##class_name##_instance_count, -1)) \
<< s_##class_name##_instance_count. \
fetch_sub(1, std::memory_order_relaxed) \
<< " instances in total."; \
}
#endif // defined(COBALT_BUILD_TYPE_GOLD)
Expand Down
15 changes: 8 additions & 7 deletions starboard/common/spin_lock.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@

namespace starboard {

void SpinLockAcquire(SbAtomic32* atomic) {
while (SbAtomicAcquire_CompareAndSwap(atomic, kSpinLockStateReleased,
kSpinLockStateAcquired) ==
kSpinLockStateAcquired) {
void SpinLockAcquire(std::atomic<int32_t>* atomic) {
int expected{kSpinLockStateReleased};
while (!atomic->compare_exchange_weak(expected,
kSpinLockStateAcquired, std::memory_order_acquire)) {
expected = kSpinLockStateReleased;
sched_yield();
}
}

void SpinLockRelease(SbAtomic32* atomic) {
SbAtomicRelease_Store(atomic, kSpinLockStateReleased);
void SpinLockRelease(std::atomic<int32_t>* atomic) {
atomic->store(kSpinLockStateReleased, std::memory_order_release);
}

SpinLock::SpinLock() : atomic_(kSpinLockStateReleased) {}
Expand All @@ -42,7 +43,7 @@ void SpinLock::Release() {
SpinLockRelease(&atomic_);
}

ScopedSpinLock::ScopedSpinLock(SbAtomic32* atomic) : atomic_(atomic) {
ScopedSpinLock::ScopedSpinLock(std::atomic<int32_t>* atomic) : atomic_(atomic) {
SpinLockAcquire(atomic_);
}

Expand Down
16 changes: 8 additions & 8 deletions starboard/common/spin_lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@
#ifndef STARBOARD_COMMON_SPIN_LOCK_H_
#define STARBOARD_COMMON_SPIN_LOCK_H_

#include "starboard/atomic.h"
#include <atomic>

namespace starboard {

const SbAtomic32 kSpinLockStateReleased = 0;
const SbAtomic32 kSpinLockStateAcquired = 1;
constexpr int32_t kSpinLockStateReleased{0};
constexpr int32_t kSpinLockStateAcquired{1};

void SpinLockAcquire(SbAtomic32* atomic);
void SpinLockRelease(SbAtomic32* atomic);
void SpinLockAcquire(std::atomic<int32_t>* atomic);
void SpinLockRelease(std::atomic<int32_t>* atomic);

class SpinLock {
public:
Expand All @@ -39,18 +39,18 @@ class SpinLock {
void Release();

private:
SbAtomic32 atomic_;
std::atomic<int32_t> atomic_;
friend class ScopedSpinLock;
};

class ScopedSpinLock {
public:
explicit ScopedSpinLock(SbAtomic32* atomic);
explicit ScopedSpinLock(std::atomic<int32_t>* atomic);
explicit ScopedSpinLock(SpinLock& spin_lock);
~ScopedSpinLock();

private:
SbAtomic32* atomic_;
std::atomic<int32_t>* atomic_;
};

} // namespace starboard
Expand Down
Loading

0 comments on commit 55a1d8c

Please sign in to comment.