// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. #ifndef RUNTIME_PLATFORM_ATOMIC_WIN_H_ #define RUNTIME_PLATFORM_ATOMIC_WIN_H_ #if !defined RUNTIME_PLATFORM_ATOMIC_H_ #error Do not include atomic_win.h directly. Use atomic.h instead. #endif #if !defined(HOST_OS_WINDOWS) #error This file should only be included on Windows builds. #endif namespace dart { inline uintptr_t AtomicOperations::FetchAndIncrement(uintptr_t* p) { #if defined(HOST_ARCH_X64) return static_cast( InterlockedIncrement64(reinterpret_cast(p))) - 1; #elif defined(HOST_ARCH_IA32) return static_cast( InterlockedIncrement(reinterpret_cast(p))) - 1; #else #error Unsupported host architecture. #endif } inline intptr_t AtomicOperations::FetchAndIncrement(intptr_t* p) { #if defined(HOST_ARCH_X64) return static_cast( InterlockedIncrement64(reinterpret_cast(p))) - 1; #elif defined(HOST_ARCH_IA32) return static_cast( InterlockedIncrement(reinterpret_cast(p))) - 1; #else #error Unsupported host architecture. #endif } inline void AtomicOperations::IncrementBy(intptr_t* p, intptr_t value) { #if defined(HOST_ARCH_X64) InterlockedExchangeAdd64(reinterpret_cast(p), static_cast(value)); #elif defined(HOST_ARCH_IA32) InterlockedExchangeAdd(reinterpret_cast(p), static_cast(value)); #else #error Unsupported host architecture. #endif } inline void AtomicOperations::IncrementInt64By(int64_t* p, int64_t value) { #if defined(HOST_ARCH_IA32) || defined(HOST_ARCH_X64) InterlockedExchangeAdd64(reinterpret_cast(p), static_cast(value)); #else #error Unsupported host architecture. #endif } inline uintptr_t AtomicOperations::FetchAndDecrement(uintptr_t* p) { #if defined(HOST_ARCH_X64) return static_cast( InterlockedDecrement64(reinterpret_cast(p))) + 1; #elif defined(HOST_ARCH_IA32) return static_cast( InterlockedDecrement(reinterpret_cast(p))) + 1; #else #error Unsupported host architecture. #endif } inline intptr_t AtomicOperations::FetchAndDecrement(intptr_t* p) { #if defined(HOST_ARCH_X64) return static_cast( InterlockedDecrement64(reinterpret_cast(p))) + 1; #elif defined(HOST_ARCH_IA32) return static_cast( InterlockedDecrement(reinterpret_cast(p))) + 1; #else #error Unsupported host architecture. #endif } inline void AtomicOperations::DecrementBy(intptr_t* p, intptr_t value) { #if defined(HOST_ARCH_X64) InterlockedExchangeAdd64(reinterpret_cast(p), static_cast(-value)); #elif defined(HOST_ARCH_IA32) InterlockedExchangeAdd(reinterpret_cast(p), static_cast(-value)); #else #error Unsupported host architecture. #endif } inline uint32_t AtomicOperations::FetchOrRelaxedUint32(uint32_t* ptr, uint32_t value) { return static_cast(InterlockedOrNoFence( reinterpret_cast(ptr), static_cast(value))); } inline uint32_t AtomicOperations::FetchAndRelaxedUint32(uint32_t* ptr, uint32_t value) { return static_cast(InterlockedAndNoFence( reinterpret_cast(ptr), static_cast(value))); } inline uword AtomicOperations::CompareAndSwapWord(uword* ptr, uword old_value, uword new_value) { #if defined(HOST_ARCH_X64) return static_cast(InterlockedCompareExchange64( reinterpret_cast(ptr), static_cast(new_value), static_cast(old_value))); #elif defined(HOST_ARCH_IA32) return static_cast(InterlockedCompareExchange( reinterpret_cast(ptr), static_cast(new_value), static_cast(old_value))); #else #error Unsupported host architecture. #endif } inline uint32_t AtomicOperations::CompareAndSwapUint32(uint32_t* ptr, uint32_t old_value, uint32_t new_value) { #if (defined(HOST_ARCH_X64) || defined(HOST_ARCH_IA32)) return static_cast(InterlockedCompareExchange( reinterpret_cast(ptr), static_cast(new_value), static_cast(old_value))); #else #error Unsupported host architecture. #endif } template inline T AtomicOperations::LoadAcquire(T* ptr) { #if (defined(HOST_ARCH_X64) || defined(HOST_ARCH_IA32)) return *ptr; #else #error Unsupported host architecture. #endif } template inline void AtomicOperations::StoreRelease(T* ptr, T value) { #if (defined(HOST_ARCH_X64) || defined(HOST_ARCH_IA32)) *ptr = value; #else #error Unsupported host architecture. #endif } } // namespace dart #endif // RUNTIME_PLATFORM_ATOMIC_WIN_H_