/* * Copyright (c) Facebook, Inc. and its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include #include #include #include #include #include #ifdef _WIN32 #include #endif namespace folly { namespace detail { constexpr std::memory_order atomic_compare_exchange_succ( bool cond, std::memory_order succ, std::memory_order fail) { constexpr auto const relaxed = std::memory_order_relaxed; constexpr auto const release = std::memory_order_release; constexpr auto const acq_rel = std::memory_order_acq_rel; assert(fail != release); assert(fail != acq_rel); // Clang TSAN ignores the passed failure order and infers failure order from // success order in atomic compare-exchange operations, which is broken for // cases like success-release/failure-acquire, so return a success order with // the failure order mixed in. auto const bump = succ == release ? acq_rel : succ; auto const high = fail < bump ? bump : fail; return !cond || fail == relaxed ? succ : high; } constexpr std::memory_order atomic_compare_exchange_succ( std::memory_order succ, std::memory_order fail) { constexpr auto const cond = kIsSanitizeThread && kIsClang; return atomic_compare_exchange_succ(cond, succ, fail); } } // namespace detail template