Skip to content

Commit bbcdb40

Browse files
NickGerlemanfacebook-github-bot
authored andcommitted
C++ 17 style critical sections
Summary: C++ 17 added `std::scoped_lock`, which effectively supersedes `std::lock_guard`. See https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#cp21-use-stdlock-or-stdscoped_lock-to-acquire-multiple-mutexes for why it was added. This diff replaces usages of `std::lock_guard` with `std::scoped_lock` and CTAD. Changelog: [Internal] Reviewed By: mdvacca Differential Revision: D48244308 fbshipit-source-id: d0a090d92c4c7276fdeea7fb1275900e5e0d3617
1 parent 30e2345 commit bbcdb40

File tree

26 files changed

+89
-88
lines changed

26 files changed

+89
-88
lines changed

packages/react-native/Libraries/WebPerformance/PerformanceEntryReporter.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ void PerformanceEntryReporter::stopReporting() {
6666
}
6767

6868
GetPendingEntriesResult PerformanceEntryReporter::popPendingEntries() {
69-
std::lock_guard<std::mutex> lock(entriesMutex_);
69+
std::scoped_lock lock(entriesMutex_);
7070
GetPendingEntriesResult res = {
7171
std::vector<RawPerformanceEntry>(), droppedEntryCount_};
7272
for (auto &buffer : buffers_) {
@@ -99,7 +99,7 @@ void PerformanceEntryReporter::logEntry(const RawPerformanceEntry &entry) {
9999
return;
100100
}
101101

102-
std::lock_guard<std::mutex> lock(entriesMutex_);
102+
std::scoped_lock lock(entriesMutex_);
103103

104104
auto &buffer = buffers_[entry.entryType];
105105

@@ -367,7 +367,7 @@ EventTag PerformanceEntryReporter::onEventStart(const char *name) {
367367

368368
auto timeStamp = getCurrentTimeStamp();
369369
{
370-
std::lock_guard<std::mutex> lock(eventsInFlightMutex_);
370+
std::scoped_lock lock(eventsInFlightMutex_);
371371
eventsInFlight_.emplace(std::make_pair(
372372
sCurrentEventTag_, EventEntry{reportedName, timeStamp, 0.0}));
373373
}
@@ -380,7 +380,7 @@ void PerformanceEntryReporter::onEventDispatch(EventTag tag) {
380380
}
381381
auto timeStamp = getCurrentTimeStamp();
382382
{
383-
std::lock_guard<std::mutex> lock(eventsInFlightMutex_);
383+
std::scoped_lock lock(eventsInFlightMutex_);
384384
auto it = eventsInFlight_.find(tag);
385385
if (it != eventsInFlight_.end()) {
386386
it->second.dispatchTime = timeStamp;
@@ -394,7 +394,7 @@ void PerformanceEntryReporter::onEventEnd(EventTag tag) {
394394
}
395395
auto timeStamp = getCurrentTimeStamp();
396396
{
397-
std::lock_guard<std::mutex> lock(eventsInFlightMutex_);
397+
std::scoped_lock lock(eventsInFlightMutex_);
398398
auto it = eventsInFlight_.find(tag);
399399
if (it == eventsInFlight_.end()) {
400400
return;

packages/react-native/ReactAndroid/src/main/jni/react/fabric/EventBeatManager.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,18 @@ jni::local_ref<EventBeatManager::jhybriddata> EventBeatManager::initHybrid(
2222

2323
void EventBeatManager::addObserver(
2424
EventBeatManagerObserver const &observer) const {
25-
std::lock_guard<std::mutex> lock(mutex_);
25+
std::scoped_lock lock(mutex_);
2626
observers_.insert(&observer);
2727
}
2828

2929
void EventBeatManager::removeObserver(
3030
EventBeatManagerObserver const &observer) const {
31-
std::lock_guard<std::mutex> lock(mutex_);
31+
std::scoped_lock lock(mutex_);
3232
observers_.erase(&observer);
3333
}
3434

3535
void EventBeatManager::tick() {
36-
std::lock_guard<std::mutex> lock(mutex_);
36+
std::scoped_lock lock(mutex_);
3737

3838
for (auto observer : observers_) {
3939
observer->tick();

packages/react-native/ReactAndroid/src/main/jni/react/fabric/FabricMountingManager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ void FabricMountingManager::executeMount(
250250
const MountingTransaction &transaction) {
251251
SystraceSection section("FabricMountingManager::executeMount");
252252

253-
std::lock_guard<std::recursive_mutex> lock(commitMutex_);
253+
std::scoped_lock lock(commitMutex_);
254254
auto finishTransactionStartTime = telemetryTimePointNow();
255255

256256
auto env = jni::Environment::current();

packages/react-native/ReactAndroid/src/main/jni/react/jni/JMessageQueueThread.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ void JMessageQueueThread::runOnQueueSync(std::function<void()> &&runnable) {
8888
bool runnableComplete = false;
8989

9090
runOnQueue([&]() mutable {
91-
std::lock_guard<std::mutex> lock(signalMutex);
91+
std::scoped_lock lock(signalMutex);
9292

9393
runnable();
9494
runnableComplete = true;

packages/react-native/ReactCommon/cxxreact/Instance.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ void Instance::initializeBridge(
5959
*/
6060
jsCallInvoker_->setNativeToJsBridgeAndFlushCalls(nativeToJsBridge_);
6161

62-
std::lock_guard<std::mutex> lock(m_syncMutex);
62+
std::scoped_lock lock(m_syncMutex);
6363
m_syncReady = true;
6464
m_syncCV.notify_all();
6565
});
@@ -248,7 +248,7 @@ Instance::getDecoratedNativeMethodCallInvoker(
248248

249249
void Instance::JSCallInvoker::setNativeToJsBridgeAndFlushCalls(
250250
std::weak_ptr<NativeToJsBridge> nativeToJsBridge) {
251-
std::lock_guard<std::mutex> guard(m_mutex);
251+
std::scoped_lock guard(m_mutex);
252252

253253
m_shouldBuffer = false;
254254
m_nativeToJsBridge = nativeToJsBridge;
@@ -265,7 +265,7 @@ void Instance::JSCallInvoker::invokeSync(std::function<void()> &&work) {
265265
}
266266

267267
void Instance::JSCallInvoker::invokeAsync(std::function<void()> &&work) {
268-
std::lock_guard<std::mutex> guard(m_mutex);
268+
std::scoped_lock guard(m_mutex);
269269

270270
/**
271271
* Why is is necessary to queue up async work?

packages/react-native/ReactCommon/hermes/inspector/Inspector.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ Inspector::Inspector(
117117
std::make_shared<jsi::StringBuffer>(src), "__tickleJsHackUrl");
118118

119119
{
120-
std::lock_guard<std::mutex> lock(mutex_);
120+
std::scoped_lock lock(mutex_);
121121

122122
if (pauseOnFirstStatement) {
123123
awaitingDebuggerOnStart_ = true;
@@ -289,10 +289,11 @@ ScriptInfo Inspector::getScriptInfoFromTopCallFrame() {
289289

290290
void Inspector::addCurrentScriptToLoadedScripts() {
291291
ScriptInfo info = getScriptInfoFromTopCallFrame();
292+
auto fileId = info.fileId;
292293

293-
if (!loadedScripts_.count(info.fileId)) {
294-
loadedScriptIdByName_[info.fileName] = info.fileId;
295-
loadedScripts_[info.fileId] = LoadedScriptInfo{std::move(info), false};
294+
if (!loadedScripts_.count(fileId)) {
295+
loadedScriptIdByName_[info.fileName] = fileId;
296+
loadedScripts_[fileId] = LoadedScriptInfo{std::move(info), false};
296297
}
297298
}
298299

@@ -538,7 +539,7 @@ void Inspector::transition(std::unique_ptr<InspectorState> nextState) {
538539

539540
void Inspector::disableOnExecutor(
540541
std::shared_ptr<folly::Promise<Unit>> promise) {
541-
std::lock_guard<std::mutex> lock(mutex_);
542+
std::scoped_lock lock(mutex_);
542543

543544
debugger_.setIsDebuggerAttached(false);
544545

@@ -547,7 +548,7 @@ void Inspector::disableOnExecutor(
547548

548549
void Inspector::enableOnExecutor(
549550
std::shared_ptr<folly::Promise<Unit>> promise) {
550-
std::lock_guard<std::mutex> lock(mutex_);
551+
std::scoped_lock lock(mutex_);
551552

552553
auto result = state_->enable();
553554

@@ -575,7 +576,7 @@ void Inspector::executeIfEnabledOnExecutor(
575576
const std::string &description,
576577
folly::Function<void(const debugger::ProgramState &)> func,
577578
std::shared_ptr<folly::Promise<Unit>> promise) {
578-
std::lock_guard<std::mutex> lock(mutex_);
579+
std::scoped_lock lock(mutex_);
579580

580581
if (!state_->isPaused() && !state_->isRunning()) {
581582
promise->setException(InvalidStateException(
@@ -601,7 +602,7 @@ void Inspector::setBreakpointOnExecutor(
601602
debugger::SourceLocation loc,
602603
std::optional<std::string> condition,
603604
std::shared_ptr<folly::Promise<debugger::BreakpointInfo>> promise) {
604-
std::lock_guard<std::mutex> lock(mutex_);
605+
std::scoped_lock lock(mutex_);
605606

606607
bool pushed = state_->pushPendingFunc([this, loc, condition, promise] {
607608
debugger::BreakpointID id = debugger_.setBreakpoint(loc);
@@ -625,7 +626,7 @@ void Inspector::setBreakpointOnExecutor(
625626
void Inspector::removeBreakpointOnExecutor(
626627
debugger::BreakpointID breakpointId,
627628
std::shared_ptr<folly::Promise<folly::Unit>> promise) {
628-
std::lock_guard<std::mutex> lock(mutex_);
629+
std::scoped_lock lock(mutex_);
629630

630631
bool pushed = state_->pushPendingFunc([this, breakpointId, promise] {
631632
debugger_.deleteBreakpoint(breakpointId);
@@ -640,7 +641,7 @@ void Inspector::removeBreakpointOnExecutor(
640641
void Inspector::logOnExecutor(
641642
ConsoleMessageInfo info,
642643
std::shared_ptr<folly::Promise<folly::Unit>> promise) {
643-
std::lock_guard<std::mutex> lock(mutex_);
644+
std::scoped_lock lock(mutex_);
644645

645646
state_->pushPendingFunc([this, info = std::move(info)] {
646647
observer_.onMessageAdded(*this, info);
@@ -652,13 +653,13 @@ void Inspector::logOnExecutor(
652653
void Inspector::setPendingCommandOnExecutor(
653654
debugger::Command command,
654655
std::shared_ptr<folly::Promise<Unit>> promise) {
655-
std::lock_guard<std::mutex> lock(mutex_);
656+
std::scoped_lock lock(mutex_);
656657

657658
state_->setPendingCommand(std::move(command), promise);
658659
}
659660

660661
void Inspector::pauseOnExecutor(std::shared_ptr<folly::Promise<Unit>> promise) {
661-
std::lock_guard<std::mutex> lock(mutex_);
662+
std::scoped_lock lock(mutex_);
662663

663664
bool canPause = state_->pause();
664665

@@ -675,7 +676,7 @@ void Inspector::evaluateOnExecutor(
675676
std::shared_ptr<folly::Promise<debugger::EvalResult>> promise,
676677
folly::Function<void(const facebook::hermes::debugger::EvalResult &)>
677678
resultTransformer) {
678-
std::lock_guard<std::mutex> lock(mutex_);
679+
std::scoped_lock lock(mutex_);
679680

680681
state_->pushPendingEval(
681682
frameIndex, src, promise, std::move(resultTransformer));
@@ -684,7 +685,7 @@ void Inspector::evaluateOnExecutor(
684685
void Inspector::setPauseOnExceptionsOnExecutor(
685686
const debugger::PauseOnThrowMode &mode,
686687
std::shared_ptr<folly::Promise<folly::Unit>> promise) {
687-
std::lock_guard<std::mutex> local(mutex_);
688+
std::scoped_lock local(mutex_);
688689

689690
state_->pushPendingFunc([this, mode, promise] {
690691
debugger_.setPauseOnThrowMode(mode);

packages/react-native/ReactCommon/hermes/inspector/chrome/Connection.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ std::string Connection::Impl::getTitle() const {
222222

223223
bool Connection::Impl::connect(std::unique_ptr<IRemoteConnection> remoteConn) {
224224
assert(remoteConn);
225-
std::lock_guard<std::mutex> lock(connectionMutex_);
225+
std::scoped_lock lock(connectionMutex_);
226226

227227
if (connected_) {
228228
return false;
@@ -237,7 +237,7 @@ bool Connection::Impl::connect(std::unique_ptr<IRemoteConnection> remoteConn) {
237237
}
238238

239239
bool Connection::Impl::disconnect() {
240-
std::lock_guard<std::mutex> lock(connectionMutex_);
240+
std::scoped_lock lock(connectionMutex_);
241241

242242
if (!connected_) {
243243
return false;
@@ -350,7 +350,7 @@ void Connection::Impl::onPause(
350350
note.reason = "other";
351351
note.hitBreakpoints = std::vector<m::debugger::BreakpointId>();
352352

353-
std::lock_guard<std::mutex> lock(virtualBreakpointMutex_);
353+
std::scoped_lock lock(virtualBreakpointMutex_);
354354
for (auto &bp :
355355
virtualBreakpoints_[kBeforeScriptWithSourceMapExecution]) {
356356
note.hitBreakpoints->emplace_back(bp);
@@ -398,7 +398,7 @@ void Connection::Impl::onScriptParsed(
398398
if (!info.sourceMappingUrl.empty()) {
399399
note.sourceMapURL = info.sourceMappingUrl;
400400

401-
std::lock_guard<std::mutex> lock(virtualBreakpointMutex_);
401+
std::scoped_lock lock(virtualBreakpointMutex_);
402402
if (hasVirtualBreakpoint(kBeforeScriptWithSourceMapExecution)) {
403403
// We are precariously relying on the fact that onScriptParsed
404404
// is invoked immediately before the pause load mode is checked.
@@ -409,7 +409,7 @@ void Connection::Impl::onScriptParsed(
409409
}
410410

411411
{
412-
std::lock_guard<std::mutex> lock(parsedScriptsMutex_);
412+
std::scoped_lock lock(parsedScriptsMutex_);
413413
parsedScripts_.push_back(info.fileName);
414414
}
415415

@@ -1182,7 +1182,7 @@ void Connection::Impl::handle(const m::debugger::PauseRequest &req) {
11821182

11831183
void Connection::Impl::handle(const m::debugger::RemoveBreakpointRequest &req) {
11841184
if (isVirtualBreakpointId(req.breakpointId)) {
1185-
std::lock_guard<std::mutex> lock(virtualBreakpointMutex_);
1185+
std::scoped_lock lock(virtualBreakpointMutex_);
11861186
if (!removeVirtualBreakpoint(req.breakpointId)) {
11871187
sendErrorToClientViaExecutor(
11881188
req.id, "Unknown breakpoint ID: " + req.breakpointId);
@@ -1238,7 +1238,7 @@ void Connection::Impl::handle(
12381238
debugger::SourceLocation loc;
12391239

12401240
{
1241-
std::lock_guard<std::mutex> lock(parsedScriptsMutex_);
1241+
std::scoped_lock lock(parsedScriptsMutex_);
12421242
setHermesLocation(loc, req, parsedScripts_);
12431243
}
12441244

@@ -1307,7 +1307,7 @@ void Connection::Impl::handle(
13071307

13081308
// The act of creating and registering the breakpoint ID is enough
13091309
// to "set" it. We merely check for the existence of them later.
1310-
std::lock_guard<std::mutex> lock(virtualBreakpointMutex_);
1310+
std::scoped_lock lock(virtualBreakpointMutex_);
13111311
m::debugger::SetInstrumentationBreakpointResponse resp;
13121312
resp.id = req.id;
13131313
resp.breakpointId = createVirtualBreakpoint(req.instrumentation);

packages/react-native/ReactCommon/hermes/inspector/chrome/ConnectionDemux.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ ConnectionDemux::~ConnectionDemux() = default;
6565
DebugSessionToken ConnectionDemux::enableDebugging(
6666
std::unique_ptr<RuntimeAdapter> adapter,
6767
const std::string &title) {
68-
std::lock_guard<std::mutex> lock(mutex_);
68+
std::scoped_lock lock(mutex_);
6969

7070
// TODO(#22976087): workaround for ComponentScript contexts never being
7171
// destroyed.
@@ -91,7 +91,7 @@ DebugSessionToken ConnectionDemux::enableDebugging(
9191
}
9292

9393
void ConnectionDemux::disableDebugging(DebugSessionToken session) {
94-
std::lock_guard<std::mutex> lock(mutex_);
94+
std::scoped_lock lock(mutex_);
9595
if (conns_.find(session) == conns_.end()) {
9696
return;
9797
}

packages/react-native/ReactCommon/hermes/inspector/chrome/tests/ConnectionDemuxTests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class TestRemoteConnection : public IRemoteConnection {
5858
}
5959

6060
void setDisconnected() {
61-
std::lock_guard<std::mutex> lock(mutex_);
61+
std::scoped_lock lock(mutex_);
6262
connected_ = false;
6363
cv_.notify_one();
6464
}

packages/react-native/ReactCommon/hermes/inspector/chrome/tests/SyncConnection.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ void SyncConnection::waitForNotification(
115115
void SyncConnection::onReply(const std::string &message) {
116116
LOG(INFO) << "SyncConnection::onReply got message: " << prettify(message);
117117

118-
std::lock_guard<std::mutex> lock(mutex_);
118+
std::scoped_lock lock(mutex_);
119119

120120
folly::dynamic obj = folly::parseJson(message);
121121
if (obj.count("id")) {

packages/react-native/ReactCommon/hermes/inspector/detail/SerialExecutor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ SerialExecutor::SerialExecutor(const std::string &name)
1717

1818
SerialExecutor::~SerialExecutor() {
1919
{
20-
std::lock_guard<std::mutex> lock(mutex_);
20+
std::scoped_lock lock(mutex_);
2121
finish_ = true;
2222
wakeup_.notify_one();
2323
}
@@ -26,7 +26,7 @@ SerialExecutor::~SerialExecutor() {
2626
}
2727

2828
void SerialExecutor::add(folly::Func func) {
29-
std::lock_guard<std::mutex> lock(mutex_);
29+
std::scoped_lock lock(mutex_);
3030
funcs_.push(std::move(func));
3131
wakeup_.notify_one();
3232
}

packages/react-native/ReactCommon/jsinspector/InspectorInterfaces.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ int InspectorImpl::addPage(
4747
const std::string &title,
4848
const std::string &vm,
4949
ConnectFunc connectFunc) {
50-
std::lock_guard<std::mutex> lock(mutex_);
50+
std::scoped_lock lock(mutex_);
5151

5252
int pageId = nextPageId_++;
5353
titles_[pageId] = std::make_tuple(title, vm);
@@ -57,14 +57,14 @@ int InspectorImpl::addPage(
5757
}
5858

5959
void InspectorImpl::removePage(int pageId) {
60-
std::lock_guard<std::mutex> lock(mutex_);
60+
std::scoped_lock lock(mutex_);
6161

6262
titles_.erase(pageId);
6363
connectFuncs_.erase(pageId);
6464
}
6565

6666
std::vector<InspectorPage> InspectorImpl::getPages() const {
67-
std::lock_guard<std::mutex> lock(mutex_);
67+
std::scoped_lock lock(mutex_);
6868

6969
std::vector<InspectorPage> inspectorPages;
7070
for (auto &it : titles_) {
@@ -81,7 +81,7 @@ std::unique_ptr<ILocalConnection> InspectorImpl::connect(
8181
IInspector::ConnectFunc connectFunc;
8282

8383
{
84-
std::lock_guard<std::mutex> lock(mutex_);
84+
std::scoped_lock lock(mutex_);
8585

8686
auto it = connectFuncs_.find(pageId);
8787
if (it != connectFuncs_.end()) {

0 commit comments

Comments
 (0)