Skip to content

Commit 22cab4b

Browse files
authored
refactor: Replace starboard::Mutex with std::mutex (youtube#6679)
This is part of efforts to deprecate `starboard::Mutex` and `starboard::ConditionVariable`. #vibe-coded Bug: 390503926
1 parent 06fc0d2 commit 22cab4b

File tree

4 files changed

+24
-24
lines changed

4 files changed

+24
-24
lines changed

starboard/shared/starboard/feature_list.cc

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "starboard/shared/starboard/feature_list.h"
1616

1717
#include <cstring>
18+
#include <mutex>
1819

1920
#include "starboard/common/log.h"
2021
#include "starboard/common/once.h"
@@ -52,7 +53,7 @@ void FeatureList::InitializeFeatureList(const SbFeature* features,
5253
SB_CHECK(instance)
5354
<< "Starboard FeatureList Instance has not been initialized.";
5455

55-
ScopedLock lock(instance->mutex_);
56+
std::lock_guard lock(instance->mutex_);
5657

5758
// Store the updated features inside of our instance's feature map.
5859
for (size_t i = 0; i < number_of_features; i++) {
@@ -146,7 +147,7 @@ void FeatureList::ValidateParam(const std::string& feature_name,
146147
// static
147148
bool FeatureList::IsEnabled(const SbFeature& feature) {
148149
FeatureList* instance = GetInstance();
149-
ScopedLock lock(instance->mutex_);
150+
std::lock_guard lock(instance->mutex_);
150151

151152
// IsEnabled can only be called after the FeatureList has been initialized.
152153
SB_CHECK(instance->IsInitialized())
@@ -164,7 +165,7 @@ bool FeatureList::IsEnabled(const SbFeature& feature) {
164165
template <>
165166
bool FeatureList::GetParam(const SbFeatureParamExt<bool>& param) {
166167
FeatureList* instance = GetInstance();
167-
ScopedLock lock(instance->mutex_);
168+
std::lock_guard lock(instance->mutex_);
168169
instance->ValidateParam(param.feature_name, param.param_name,
169170
SbFeatureParamTypeBool);
170171
return std::get<bool>(
@@ -174,7 +175,7 @@ bool FeatureList::GetParam(const SbFeatureParamExt<bool>& param) {
174175
template <>
175176
int FeatureList::GetParam(const SbFeatureParamExt<int>& param) {
176177
FeatureList* instance = GetInstance();
177-
ScopedLock lock(instance->mutex_);
178+
std::lock_guard lock(instance->mutex_);
178179
instance->ValidateParam(param.feature_name, param.param_name,
179180
SbFeatureParamTypeInt);
180181
return std::get<int>(
@@ -184,7 +185,7 @@ int FeatureList::GetParam(const SbFeatureParamExt<int>& param) {
184185
template <>
185186
double FeatureList::GetParam(const SbFeatureParamExt<double>& param) {
186187
FeatureList* instance = GetInstance();
187-
ScopedLock lock(instance->mutex_);
188+
std::lock_guard lock(instance->mutex_);
188189
instance->ValidateParam(param.feature_name, param.param_name,
189190
SbFeatureParamTypeDouble);
190191
return std::get<double>(
@@ -194,7 +195,7 @@ double FeatureList::GetParam(const SbFeatureParamExt<double>& param) {
194195
template <>
195196
std::string FeatureList::GetParam(const SbFeatureParamExt<std::string>& param) {
196197
FeatureList* instance = GetInstance();
197-
ScopedLock lock(instance->mutex_);
198+
std::lock_guard lock(instance->mutex_);
198199
instance->ValidateParam(param.feature_name, param.param_name,
199200
SbFeatureParamTypeString);
200201
return std::get<std::string>(
@@ -204,7 +205,7 @@ std::string FeatureList::GetParam(const SbFeatureParamExt<std::string>& param) {
204205
template <>
205206
int64_t FeatureList::GetParam(const SbFeatureParamExt<int64_t>& param) {
206207
FeatureList* instance = GetInstance();
207-
ScopedLock lock(instance->mutex_);
208+
std::lock_guard lock(instance->mutex_);
208209
instance->ValidateParam(param.feature_name, param.param_name,
209210
SbFeatureParamTypeTime);
210211
return std::get<int64_t>(

starboard/shared/starboard/feature_list.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
#ifndef STARBOARD_SHARED_STARBOARD_FEATURE_LIST_H_
1616
#define STARBOARD_SHARED_STARBOARD_FEATURE_LIST_H_
1717

18+
#include <mutex>
1819
#include <optional>
1920
#include <string>
2021
#include <type_traits>
2122
#include <unordered_map>
2223
#include <utility>
2324
#include <variant>
2425

25-
#include "starboard/common/mutex.h"
2626
#include "starboard/extension/features.h"
2727

2828
namespace starboard::features {
@@ -82,12 +82,12 @@ class FeatureList {
8282
// Mutex to ensure that in the rare chance that the FeatureList is
8383
// being accessed while it is initializing, we can let the instance
8484
// fully initialize before being accessed.
85-
Mutex mutex_;
85+
std::mutex mutex_;
8686

8787
// Starboard features will be stored in an std::unordered_map, where the keys
8888
// are the string representations of the features, and the values are the
8989
// associated boolean value of the feature.
90-
std::unordered_map<std::string, bool> features_;
90+
std::unordered_map<std::string, bool> features_; // Guarded by |mutex_|.
9191

9292
// Starboard feature parameters will be stored in a 2D std::unordered_map,
9393
// where the outer map's keys will be the string of the feature associated

starboard/shared/starboard/player/filter/testing/adaptive_audio_decoder_test.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
#include <deque>
2020
#include <functional>
2121
#include <memory>
22+
#include <mutex>
2223
#include <numeric>
2324
#include <queue>
2425
#include <string>
2526

26-
#include "starboard/common/mutex.h"
2727
#include "starboard/common/time.h"
2828
#include "starboard/configuration_constants.h"
2929
#include "starboard/shared/starboard/media/media_support_internal.h"
@@ -144,7 +144,7 @@ class AdaptiveAudioDecoderTest
144144
while (CurrentMonotonicTime() - start < kWaitForNextEventTimeOut) {
145145
job_queue_.RunUntilIdle();
146146
{
147-
ScopedLock scoped_lock(event_queue_mutex_);
147+
std::lock_guard lock(event_queue_mutex_);
148148
if (!event_queue_.empty()) {
149149
*event = event_queue_.front();
150150
event_queue_.pop_front();
@@ -199,16 +199,16 @@ class AdaptiveAudioDecoderTest
199199

200200
private:
201201
void OnOutput() {
202-
ScopedLock scoped_lock(event_queue_mutex_);
202+
std::lock_guard lock(event_queue_mutex_);
203203
event_queue_.push_back(kOutput);
204204
}
205205
void OnError() {
206-
ScopedLock scoped_lock(event_queue_mutex_);
206+
std::lock_guard lock(event_queue_mutex_);
207207
event_queue_.push_back(kError);
208208
}
209209

210210
void OnConsumed() {
211-
ScopedLock scoped_lock(event_queue_mutex_);
211+
std::lock_guard lock(event_queue_mutex_);
212212
event_queue_.push_back(kConsumed);
213213
}
214214

@@ -277,7 +277,7 @@ class AdaptiveAudioDecoderTest
277277
JobQueue job_queue_;
278278
std::unique_ptr<AudioDecoder> audio_decoder_;
279279

280-
Mutex event_queue_mutex_;
280+
std::mutex event_queue_mutex_;
281281
std::deque<Event> event_queue_;
282282
bool can_accept_more_input_ = true;
283283
};

starboard/shared/starboard/player/filter/testing/audio_decoder_test.cc

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,12 @@
2121
#include <functional>
2222
#include <map>
2323
#include <memory>
24+
#include <mutex>
2425
#include <string>
2526
#include <utility>
2627
#include <vector>
2728

28-
#include "starboard/common/condition_variable.h"
2929
#include "starboard/common/media.h"
30-
#include "starboard/common/mutex.h"
3130
#include "starboard/common/ref_counted.h"
3231
#include "starboard/common/time.h"
3332
#include "starboard/configuration_constants.h"
@@ -133,17 +132,17 @@ class AudioDecoderTest
133132
}
134133

135134
void OnOutput() {
136-
ScopedLock scoped_lock(event_queue_mutex_);
135+
std::lock_guard lock(event_queue_mutex_);
137136
event_queue_.push_back(kOutput);
138137
}
139138

140139
void OnError() {
141-
ScopedLock scoped_lock(event_queue_mutex_);
140+
std::lock_guard lock(event_queue_mutex_);
142141
event_queue_.push_back(kError);
143142
}
144143

145144
void OnConsumed() {
146-
ScopedLock scoped_lock(event_queue_mutex_);
145+
std::lock_guard lock(event_queue_mutex_);
147146
event_queue_.push_back(kConsumed);
148147
}
149148

@@ -152,7 +151,7 @@ class AudioDecoderTest
152151
while (CurrentMonotonicTime() - start < kWaitForNextEventTimeOut) {
153152
job_queue_.RunUntilIdle();
154153
{
155-
ScopedLock scoped_lock(event_queue_mutex_);
154+
std::lock_guard lock(event_queue_mutex_);
156155
if (!event_queue_.empty()) {
157156
*event = event_queue_.front();
158157
event_queue_.pop_front();
@@ -344,7 +343,7 @@ class AudioDecoderTest
344343
decoded_audio_sample_rate_ = 0;
345344
first_output_received_ = false;
346345
{
347-
ScopedLock scoped_lock(event_queue_mutex_);
346+
std::lock_guard lock(event_queue_mutex_);
348347
event_queue_.clear();
349348
}
350349
}
@@ -468,7 +467,7 @@ class AudioDecoderTest
468467
ASSERT_LE(abs(expected_output_frames - GetTotalFrames(decoded_audios_)), 1);
469468
}
470469

471-
Mutex event_queue_mutex_;
470+
std::mutex event_queue_mutex_;
472471
std::deque<Event> event_queue_;
473472

474473
// Test parameter for the filename to load with the VideoDmpReader.

0 commit comments

Comments
 (0)