| 1 | // |
| 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 | |
| 20 | namespace 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) { \ |
| 29 | T result; \ |
| 30 | flag.internal.Read(&result, &flags_internal::FlagOps<T>); \ |
| 31 | return result; \ |
| 32 | } |
| 33 | #else |
| 34 | #define ABSL_FLAGS_ATOMIC_GET(T) \ |
| 35 | T GetFlag(const absl::Flag<T>& flag) { \ |
| 36 | const int64_t r = flag.internal.atomic.load(std::memory_order_acquire); \ |
| 37 | if (r != flags_internal::CommandLineFlag::kAtomicInit) { \ |
| 38 | T t; \ |
| 39 | memcpy(&t, &r, sizeof(T)); \ |
| 40 | return t; \ |
| 41 | } \ |
| 42 | T result; \ |
| 43 | flag.internal.Read(&result, &flags_internal::FlagOps<T>); \ |
| 44 | return result; \ |
| 45 | } |
| 46 | #endif |
| 47 | |
| 48 | ABSL_FLAGS_INTERNAL_FOR_EACH_LOCK_FREE(ABSL_FLAGS_ATOMIC_GET) |
| 49 | |
| 50 | #undef ABSL_FLAGS_ATOMIC_GET |
| 51 | |
| 52 | } // namespace absl |
| 53 | |