diff --git a/source/lib/sysdep/arch/aarch64/aarch64.cpp b/source/lib/sysdep/arch/aarch64/aarch64.cpp index 690b34660a..cb92570abc 100644 --- a/source/lib/sysdep/arch/aarch64/aarch64.cpp +++ b/source/lib/sysdep/arch/aarch64/aarch64.cpp @@ -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 diff --git a/source/lib/sysdep/arch/amd64/amd64.cpp b/source/lib/sysdep/arch/amd64/amd64.cpp index a036fe7565..2e1837ddf7 100644 --- a/source/lib/sysdep/arch/amd64/amd64.cpp +++ b/source/lib/sysdep/arch/amd64/amd64.cpp @@ -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 - -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 diff --git a/source/lib/sysdep/arch/arm/arm.cpp b/source/lib/sysdep/arch/arm/arm.cpp index 8fd9f85522..026fe8f0ce 100644 --- a/source/lib/sysdep/arch/arm/arm.cpp +++ b/source/lib/sysdep/arch/arm/arm.cpp @@ -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 diff --git a/source/lib/sysdep/arch/e2k/e2k.cpp b/source/lib/sysdep/arch/e2k/e2k.cpp index 39f9b5ef45..d1330682ab 100644 --- a/source/lib/sysdep/arch/e2k/e2k.cpp +++ b/source/lib/sysdep/arch/e2k/e2k.cpp @@ -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(); diff --git a/source/lib/sysdep/arch/ia32/ia32.cpp b/source/lib/sysdep/arch/ia32/ia32.cpp deleted file mode 100644 index e5e314d0bf..0000000000 --- a/source/lib/sysdep/arch/ia32/ia32.cpp +++ /dev/null @@ -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 - -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 diff --git a/source/lib/sysdep/arch/ppc64/ppc64.cpp b/source/lib/sysdep/arch/ppc64/ppc64.cpp index 9381397973..2273c6df5f 100644 --- a/source/lib/sysdep/arch/ppc64/ppc64.cpp +++ b/source/lib/sysdep/arch/ppc64/ppc64.cpp @@ -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 diff --git a/source/lib/sysdep/cpu.cpp b/source/lib/sysdep/cpu.cpp index 9a7a0bd0ea..fed1dacd84 100644 --- a/source/lib/sysdep/cpu.cpp +++ b/source/lib/sysdep/cpu.cpp @@ -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(); -} diff --git a/source/lib/sysdep/cpu.h b/source/lib/sysdep/cpu.h index 7ecb878013..3dc46097c5 100644 --- a/source/lib/sysdep/cpu.h +++ b/source/lib/sysdep/cpu.h @@ -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 -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. **/ diff --git a/source/lib/sysdep/tests/test_sysdep.h b/source/lib/sysdep/tests/test_sysdep.h index 80c336255d..5ce70fed16 100644 --- a/source/lib/sysdep/tests/test_sysdep.h +++ b/source/lib/sysdep/tests/test_sysdep.h @@ -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"