folly/folly/synchronization/detail/Hardware.cpp

/*
 * Copyright (c) Meta Platforms, Inc. and 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.
 */

#include <folly/synchronization/detail/Hardware.h>

#include <atomic>
#include <cstdlib>
#include <stdexcept>
#include <utility>

#include <boost/preprocessor/repetition/repeat.hpp>

#include <folly/CppAttributes.h>
#include <folly/lang/Assume.h>
#include <folly/lang/Exception.h>

#if FOLLY_X64 && defined(__RTM__)
#include <immintrin.h>
#define FOLLY_RTM_SUPPORT
#else
#define FOLLY_RTM_SUPPORT
#endif

#if FOLLY_RTM_SUPPORT
#if defined(__GNUC__) || defined(__clang__)
#include <cpuid.h>
#elif defined(_MSC_VER)
#include <intrin.h>
#endif
#endif

namespace folly {

static bool rtmEnabledImpl() {}

bool rtmEnabled() {}

#if FOLLY_RTM_SUPPORT
#define FOLLY_RTM_DISABLED_NORETURN
#else
#define FOLLY_RTM_DISABLED_NORETURN
#endif

FOLLY_RTM_DISABLED_NORETURN static unsigned rtmBeginFunc() {}

FOLLY_RTM_DISABLED_NORETURN static void rtmEndFunc() {}

FOLLY_RTM_DISABLED_NORETURN static bool rtmTestFunc() {}

[[noreturn]] FOLLY_DISABLE_SANITIZERS static void rtmAbortFunc(
    [[maybe_unused]] uint8_t status) {}

namespace detail {

unsigned rtmBeginDisabled() {}
void rtmEndDisabled() {}
bool rtmTestDisabled() {}
[[noreturn]] void rtmAbortDisabled(uint8_t) {}

static void rewrite() {}

unsigned rtmBeginVE() {}
void rtmEndVE() {}
bool rtmTestVE() {}
void rtmAbortVE(uint8_t status) {}

std::atomic<unsigned (*)()> rtmBeginV{};
std::atomic<void (*)()> rtmEndV{};
std::atomic<bool (*)()> rtmTestV{};
std::atomic<void (*)(uint8_t)> rtmAbortV{};

} // namespace detail

} // namespace folly