1
0
forked from 0ad/0ad

Remove platform-dependant atomic operations

This commit is contained in:
Nicolas Auvray 2024-09-16 23:05:10 +02:00
parent 4f082560fa
commit 61bf270873
9 changed files with 7 additions and 291 deletions

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2015 Wildfire Games.
/* Copyright (C) 2024 Wildfire Games.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
@ -28,21 +28,6 @@
#include "lib/sysdep/cpu.h"
intptr_t cpu_AtomicAdd(volatile intptr_t* location, intptr_t increment)
{
return __sync_fetch_and_add(location, increment);
}
bool cpu_CAS(volatile intptr_t* location, intptr_t expected, intptr_t newValue)
{
return __sync_bool_compare_and_swap(location, expected, newValue);
}
bool cpu_CAS64(volatile i64* location, i64 expected, i64 newValue)
{
return __sync_bool_compare_and_swap(location, expected, newValue);
}
const char* cpu_IdentifierString()
{
return "unknown"; // TODO

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2010 Wildfire Games.
/* Copyright (C) 2024 Wildfire Games.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
@ -24,7 +24,6 @@
#if ARCH_AMD64
#include "lib/sysdep/cpu.h"
#include "lib/sysdep/arch/amd64/amd64.h"
@ -34,70 +33,5 @@ void cpu_ConfigureFloatingPoint()
// don't need to change the FPU control word.
}
#if MSC_VERSION
// VC 2008 and ICC 12 differ in their declaration of _Interlocked*
#if ICC_VERSION
typedef __int64* P64;
#else
typedef volatile __int64* P64;
#endif
bool cpu_CAS(volatile intptr_t* location, intptr_t expected, intptr_t newValue)
{
const intptr_t initial = _InterlockedCompareExchange64((P64)location, newValue, expected);
return initial == expected;
}
bool cpu_CAS64(volatile i64* location, i64 expected, i64 newValue)
{
const i64 initial = _InterlockedCompareExchange64((P64)location, newValue, expected);
return initial == expected;
}
intptr_t cpu_AtomicAdd(volatile intptr_t* location, intptr_t increment)
{
return _InterlockedExchangeAdd64((P64)location, increment);
}
#elif OS_MACOSX
#include <libkern/OSAtomic.h>
intptr_t cpu_AtomicAdd(volatile intptr_t* location, intptr_t increment)
{
cassert(sizeof(intptr_t) == sizeof(int64_t));
return OSAtomicAdd64Barrier(increment, (volatile int64_t*)location);
}
bool cpu_CAS(volatile intptr_t* location, intptr_t expected, intptr_t newValue)
{
cassert(sizeof(intptr_t) == sizeof(void*));
return OSAtomicCompareAndSwapPtrBarrier((void*)expected, (void*)newValue, (void* volatile*)location);
}
bool cpu_CAS64(volatile i64* location, i64 expected, i64 newValue)
{
return OSAtomicCompareAndSwap64Barrier(expected, newValue, location);
}
#elif GCC_VERSION
intptr_t cpu_AtomicAdd(volatile intptr_t* location, intptr_t increment)
{
return __sync_fetch_and_add(location, increment);
}
bool cpu_CAS(volatile intptr_t* location, intptr_t expected, intptr_t newValue)
{
return __sync_bool_compare_and_swap(location, expected, newValue);
}
bool cpu_CAS64(volatile i64* location, i64 expected, i64 newValue)
{
return __sync_bool_compare_and_swap(location, expected, newValue);
}
#endif
#endif // ARCH_AMD64

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2012 Wildfire Games.
/* Copyright (C) 2024 Wildfire Games.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
@ -28,21 +28,6 @@
#include "lib/sysdep/cpu.h"
intptr_t cpu_AtomicAdd(volatile intptr_t* location, intptr_t increment)
{
return __sync_fetch_and_add(location, increment);
}
bool cpu_CAS(volatile intptr_t* location, intptr_t expected, intptr_t newValue)
{
return __sync_bool_compare_and_swap(location, expected, newValue);
}
bool cpu_CAS64(volatile i64* location, i64 expected, i64 newValue)
{
return __sync_bool_compare_and_swap(location, expected, newValue);
}
const char* cpu_IdentifierString()
{
return "unknown"; // TODO

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2020 Wildfire Games.
/* Copyright (C) 2024 Wildfire Games.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
@ -28,21 +28,6 @@
#include "lib/sysdep/cpu.h"
intptr_t cpu_AtomicAdd(volatile intptr_t* location, intptr_t increment)
{
return __sync_fetch_and_add(location, increment);
}
bool cpu_CAS(volatile intptr_t* location, intptr_t expected, intptr_t newValue)
{
return __sync_bool_compare_and_swap(location, expected, newValue);
}
bool cpu_CAS64(volatile i64* location, i64 expected, i64 newValue)
{
return __sync_bool_compare_and_swap(location, expected, newValue);
}
const char* cpu_IdentifierString()
{
return __builtin_cpu_name();

View File

@ -1,98 +0,0 @@
/* Copyright (C) 2011 Wildfire Games.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*
* routines specific to IA-32
*/
#include "precompiled.h"
#include "lib/sysdep/cpu.h"
#include "lib/sysdep/arch/ia32/ia32.h"
#if MSC_VERSION
// VC 2008 and ICC 12 differ in their declaration of _Interlocked*
#if ICC_VERSION
typedef long* P32;
typedef __int64* P64;
#else
typedef volatile long* P32;
typedef volatile __int64* P64;
#endif
bool cpu_CAS(volatile intptr_t* location, intptr_t expected, intptr_t newValue)
{
const intptr_t initial = _InterlockedCompareExchange((P32)location, newValue, expected);
return initial == expected;
}
bool cpu_CAS64(volatile i64* location, i64 expected, i64 newValue)
{
const i64 initial = _InterlockedCompareExchange64((P64)location, newValue, expected);
return initial == expected;
}
intptr_t cpu_AtomicAdd(volatile intptr_t* location, intptr_t increment)
{
return _InterlockedExchangeAdd((P32)location, increment);
}
#elif OS_MACOSX
#include <libkern/OSAtomic.h>
intptr_t cpu_AtomicAdd(volatile intptr_t* location, intptr_t increment)
{
cassert(sizeof(intptr_t) == sizeof(int32_t));
return OSAtomicAdd32Barrier(increment, (volatile int32_t*)location);
}
bool cpu_CAS(volatile intptr_t* location, intptr_t expected, intptr_t newValue)
{
cassert(sizeof(intptr_t) == sizeof(void*));
return OSAtomicCompareAndSwapPtrBarrier((void*)expected, (void*)newValue, (void* volatile*)location);
}
bool cpu_CAS64(volatile i64* location, i64 expected, i64 newValue)
{
return OSAtomicCompareAndSwap64Barrier(expected, newValue, location);
}
#elif GCC_VERSION
intptr_t cpu_AtomicAdd(volatile intptr_t* location, intptr_t increment)
{
return __sync_fetch_and_add(location, increment);
}
bool cpu_CAS(volatile intptr_t* location, intptr_t expected, intptr_t newValue)
{
return __sync_bool_compare_and_swap(location, expected, newValue);
}
bool cpu_CAS64(volatile i64* location, i64 expected, i64 newValue)
{
return __sync_bool_compare_and_swap(location, expected, newValue);
}
#endif

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2021 Wildfire Games.
/* Copyright (C) 2024 Wildfire Games.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
@ -28,21 +28,6 @@
#include "lib/sysdep/cpu.h"
intptr_t cpu_AtomicAdd(volatile intptr_t* location, intptr_t increment)
{
return __sync_fetch_and_add(location, increment);
}
bool cpu_CAS(volatile intptr_t* location, intptr_t expected, intptr_t newValue)
{
return __sync_bool_compare_and_swap(location, expected, newValue);
}
bool cpu_CAS64(volatile i64* location, i64 expected, i64 newValue)
{
return __sync_bool_compare_and_swap(location, expected, newValue);
}
const char* cpu_IdentifierString()
{
// TODO

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2010 Wildfire Games.
/* Copyright (C) 2024 Wildfire Games.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
@ -45,26 +45,3 @@ cassert(sizeof(void*) == 8);
cassert(sizeof(i64) == sizeof(intptr_t));
#endif
cassert(sizeof(void*) == sizeof(intptr_t));
static void TestCAS64()
{
volatile i64 var = 1;
cpu_CAS64(&var, 1ull, 2ull);
ENSURE(var == 2ull);
}
static void TestAtomicAdd()
{
volatile intptr_t i1 = 1;
intptr_t prev = cpu_AtomicAdd(&i1, 1);
ENSURE(prev == 1);
ENSURE(i1 == 2);
}
void cpu_Test()
{
TestCAS64();
TestAtomicAdd();
}

View File

@ -49,42 +49,6 @@ namespace ERR
const char* cpu_IdentifierString();
//-----------------------------------------------------------------------------
// lock-free support routines
/**
* add a signed value to a variable without the possibility of interference
* from other threads/CPUs.
*
* @return the previous value.
**/
intptr_t cpu_AtomicAdd(volatile intptr_t* location, intptr_t increment);
/**
* atomic "compare and swap".
*
* @param location address of the word to compare and possibly overwrite
* @param expected its expected value
* @param newValue the value with which to replace it
* @return false if the target word doesn't match the expected value,
* otherwise true (also overwriting the contents of location)
**/
bool cpu_CAS(volatile intptr_t* location, intptr_t expected, intptr_t newValue);
bool cpu_CAS64(volatile i64* location, i64 expected, i64 newValue);
/**
* specialization of cpu_CAS for pointer types. this avoids error-prone
* casting in user code.
**/
template<typename T>
inline bool cpu_CAS(volatile T* location, T expected, T new_value)
{
return cpu_CAS((volatile intptr_t*)location, (intptr_t)expected, (intptr_t)new_value);
}
void cpu_Test();
/**
* pause in spin-wait loops, as a performance optimisation.
**/

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2014 Wildfire Games.
/* Copyright (C) 2024 Wildfire Games.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
@ -24,7 +24,6 @@
#include "lib/lib.h"
#include "lib/secure_crt.h"
#include "lib/sysdep/cpu.h"
#include "lib/sysdep/filesystem.h"
#include "lib/sysdep/sysdep.h"