blob: a02d069e9b00a4dfa5d50f363fbc7c7d170aa2e6 [file] [log] [blame]
Austin Schuh36244a12019-09-21 17:52:38 -07001//
2// Copyright 2019 The Abseil Authors.
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// https://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16#include "absl/flags/flag.h"
17
18#include <cstring>
19
20namespace absl {
21
22// We want to validate the type mismatch between type definition and
23// declaration. The lock-free implementation does not allow us to do it,
24// so in debug builds we always use the slower implementation, which always
25// validates the type.
26#ifndef NDEBUG
27#define ABSL_FLAGS_ATOMIC_GET(T) \
28 T GetFlag(const absl::Flag<T>& flag) { return flag.Get(); }
29#else
30#define ABSL_FLAGS_ATOMIC_GET(T) \
31 T GetFlag(const absl::Flag<T>& flag) { \
32 T result; \
33 if (flag.AtomicGet(&result)) { \
34 return result; \
35 } \
36 return flag.Get(); \
37 }
38#endif
39
40ABSL_FLAGS_INTERNAL_FOR_EACH_LOCK_FREE(ABSL_FLAGS_ATOMIC_GET)
41
42#undef ABSL_FLAGS_ATOMIC_GET
43
44// This global nutex protects on-demand construction of flag objects in MSVC
45// builds.
46#if defined(_MSC_VER)
47
48namespace flags_internal {
49
50ABSL_CONST_INIT static absl::Mutex construction_guard(absl::kConstInit);
51
52void LockGlobalConstructionGuard() { construction_guard.Lock(); }
53
54void UnlockGlobalConstructionGuard() { construction_guard.Unlock(); }
55
56} // namespace flags_internal
57
58#endif
59
60} // namespace absl