Skip to content

Commit 70ba73e

Browse files
derekmaurocopybara-github
authored andcommitted
Use absl::NoDestructor an absl::Mutex instance in the flags
library to prevent some exit-time destructor warnings This is the convention in the rest of Abseil. PiperOrigin-RevId: 734551159 Change-Id: I4c8eb12ea8e2b7b42605d6b587dfef2235a29b7e
1 parent 4a2ba8c commit 70ba73e

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

Diff for: absl/flags/parse.cc

+8-4
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,13 @@ ABSL_CONST_INIT bool fromenv_needs_processing
7676
ABSL_CONST_INIT bool tryfromenv_needs_processing
7777
ABSL_GUARDED_BY(ProcessingChecksMutex()) = false;
7878

79-
ABSL_CONST_INIT absl::Mutex specified_flags_guard(absl::kConstInit);
79+
absl::Mutex* SpecifiedFlagsMutex() {
80+
static absl::NoDestructor<absl::Mutex> mutex;
81+
return mutex.get();
82+
}
83+
8084
ABSL_CONST_INIT std::vector<const CommandLineFlag*>* specified_flags
81-
ABSL_GUARDED_BY(specified_flags_guard) = nullptr;
85+
ABSL_GUARDED_BY(SpecifiedFlagsMutex()) = nullptr;
8286

8387
// Suggesting at most kMaxHints flags in case of misspellings.
8488
ABSL_CONST_INIT const size_t kMaxHints = 100;
@@ -640,7 +644,7 @@ void ReportUnrecognizedFlags(
640644
// --------------------------------------------------------------------
641645

642646
bool WasPresentOnCommandLine(absl::string_view flag_name) {
643-
absl::ReaderMutexLock l(&specified_flags_guard);
647+
absl::ReaderMutexLock l(SpecifiedFlagsMutex());
644648
ABSL_INTERNAL_CHECK(specified_flags != nullptr,
645649
"ParseCommandLine is not invoked yet");
646650

@@ -767,7 +771,7 @@ HelpMode ParseAbseilFlagsOnlyImpl(
767771
}
768772
positional_args.push_back(argv[0]);
769773

770-
absl::MutexLock l(&flags_internal::specified_flags_guard);
774+
absl::MutexLock l(flags_internal::SpecifiedFlagsMutex());
771775
if (specified_flags == nullptr) {
772776
specified_flags = new std::vector<const CommandLineFlag*>;
773777
} else {

0 commit comments

Comments
 (0)