Path: blob/main/contrib/llvm-project/llvm/lib/Support/Atomic.cpp
35233 views
//===-- Atomic.cpp - Atomic Operations --------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file implements atomic operations.9//10//===----------------------------------------------------------------------===//1112#include "llvm/Support/Atomic.h"13#include "llvm/Config/llvm-config.h"1415using namespace llvm;1617#if defined(_MSC_VER)18#include <intrin.h>1920// We must include windows.h after intrin.h.21#include <windows.h>22#undef MemoryFence23#endif2425#if defined(__GNUC__) || (defined(__IBMCPP__) && __IBMCPP__ >= 1210)26#define GNU_ATOMICS27#endif2829void sys::MemoryFence() {30#if LLVM_HAS_ATOMICS == 031return;32#else33# if defined(GNU_ATOMICS)34__sync_synchronize();35# elif defined(_MSC_VER)36MemoryBarrier();37# else38# error No memory fence implementation for your platform!39# endif40#endif41}4243sys::cas_flag sys::CompareAndSwap(volatile sys::cas_flag* ptr,44sys::cas_flag new_value,45sys::cas_flag old_value) {46#if LLVM_HAS_ATOMICS == 047sys::cas_flag result = *ptr;48if (result == old_value)49*ptr = new_value;50return result;51#elif defined(GNU_ATOMICS)52return __sync_val_compare_and_swap(ptr, old_value, new_value);53#elif defined(_MSC_VER)54return InterlockedCompareExchange(ptr, new_value, old_value);55#else56# error No compare-and-swap implementation for your platform!57#endif58}596061