forked from electronicarts/CnC_Generals_Zero_Hour
-
Notifications
You must be signed in to change notification settings - Fork 78
[GEN][ZH] Implement interlocked compat helper functions #887
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Mauller
wants to merge
1
commit into
TheSuperHackers:main
Choose a base branch
from
Mauller:implement-interlocked-compat
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
/* | ||
** Command & Conquer Generals Zero Hour(tm) | ||
** Copyright 2025 TheSuperHackers | ||
** | ||
** This program is free software: you can redistribute it and/or modify | ||
** it under the terms of the GNU General Public License as published by | ||
** the Free Software Foundation, either version 3 of the License, or | ||
** (at your option) any later version. | ||
** | ||
** This program is distributed in the hope that it will be useful, | ||
** but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
** GNU General Public License for more details. | ||
** | ||
** You should have received a copy of the GNU General Public License | ||
** along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
// This file contains macros to help with simple atomic integer interlocked operations. | ||
#pragma once | ||
|
||
#include <Utility/stdint_adapter.h> | ||
|
||
|
||
// Interlocked helper function data types | ||
#if defined(__linux__) || defined(__CYGWIN__) | ||
typedef int16_t AtomicType16; | ||
typedef int32_t AtomicType32; | ||
typedef int64_t AtomicType64; | ||
|
||
#elif defined(__APPLE__) | ||
typedef SInt16 AtomicType16; | ||
typedef SInt32 AtomicType32; | ||
typedef SInt64 AtomicType64; | ||
|
||
#elif defined(__OpenBSD__) | ||
typedef uint16_t AtomicType16; | ||
typedef uint32_t AtomicType32; | ||
typedef uint64_t AtomicType64; | ||
|
||
#elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__) | ||
typedef uint16_t AtomicType16; | ||
typedef uint32_t AtomicType32; | ||
typedef uint64_t AtomicType64; | ||
|
||
#elif defined(_WIN32) || defined(_WIN64) | ||
typedef int16_t AtomicType16; | ||
typedef long AtomicType32; | ||
typedef int64_t AtomicType64; | ||
|
||
#else | ||
#error platform not supported | ||
#endif | ||
|
||
|
||
#if defined(__linux__) || defined(__CYGWIN__) | ||
inline AtomicType16 interlockedIncrement16(volatile AtomicType16 *addend) { return __sync_fetch_and_add(addend, AtomicType16{ 1 }); } | ||
inline AtomicType32 interlockedIncrement32(volatile AtomicType32 *addend) { return __sync_fetch_and_add(addend, AtomicType32{ 1 }); } | ||
inline AtomicType64 interlockedIncrement64(volatile AtomicType64 *addend) { return __sync_fetch_and_add(addend, AtomicType64{ 1 }); } | ||
|
||
inline AtomicType16 interlockedDecrement16(volatile AtomicType16 *addend) { return __sync_fetch_and_sub(addend, AtomicType16{ 1 }); } | ||
inline AtomicType32 interlockedDecrement32(volatile AtomicType32 *addend) { return __sync_fetch_and_sub(addend, AtomicType32{ 1 }); } | ||
inline AtomicType64 interlockedDecrement64(volatile AtomicType64 *addend) { return __sync_fetch_and_sub(addend, AtomicType64{ 1 }); } | ||
|
||
#elif defined(__APPLE__) | ||
#include <OSAtomic.h> | ||
inline AtomicType16 interlockedIncrement16(volatile AtomicType16 *addend) { return OSIncrementAtomic16(addend); } | ||
inline AtomicType32 interlockedIncrement32(volatile AtomicType32 *addend) { return OSIncrementAtomic(addend); } | ||
inline AtomicType64 interlockedIncrement64(volatile AtomicType64 *addend) { return OSIncrementAtomic64(addend); } | ||
|
||
inline AtomicType16 interlockedIncrement16(volatile AtomicType16 *addend) { return OSDecrementAtomic16(addend); } | ||
inline AtomicType32 interlockedIncrement32(volatile AtomicType32 *addend) { return OSDecrementAtomic(addend); } | ||
inline AtomicType64 interlockedIncrement64(volatile AtomicType64 *addend) { return OSDecrementAtomic64(addend); } | ||
|
||
#elif defined(__OpenBSD__) | ||
#include <sys/atomic.h> | ||
// Appears to have no native atomic operations for 16 bit integers. | ||
inline AtomicType32 interlockedIncrement32(volatile AtomicType32 *addend) { return atomic_inc_int_nv(addend); } | ||
inline AtomicType64 interlockedIncrement64(volatile AtomicType64 *addend) { return atomic_inc_long_nv(addend); } | ||
|
||
inline AtomicType32 interlockedIncrement32(volatile AtomicType32 *addend) { return atomic_dec_int_nv(addend); } | ||
inline AtomicType64 interlockedIncrement64(volatile AtomicType64 *addend) { return atomic_dec_long_nv(addend); } | ||
|
||
#elif defined(__FreeBSD__) || defined(__DragonFly__) | ||
#include <machine/atomic.h> | ||
// Appears to have no native atomic operations for 16, 64 bit integers. | ||
inline AtomicType32 interlockedIncrement32(volatile AtomicType32 *addend) { return atomic_fetchadd_32(addend, AtomicType32{ 1 }); } | ||
|
||
inline AtomicType32 interlockedDecrement32(volatile AtomicType32 *addend) { return atomic_fetchadd_32(addend, ~AtomicType32{ 0 }); } | ||
|
||
#elif defined(__NetBSD__) | ||
#include <sys/atomic.h> | ||
// Appears to have no native atomic operations for 16 bit integers. | ||
inline AtomicType32 interlockedIncrement32(volatile AtomicType32 *addend) { return atomic_inc_32_nv(addend); } | ||
inline AtomicType64 interlockedIncrement64(volatile AtomicType64 *addend) { return atomic_inc_64_nv(addend); } | ||
|
||
inline AtomicType32 interlockedDecrement32(volatile AtomicType32 *addend) { return atomic_dec_32_nv(addend); } | ||
inline AtomicType32 interlockedDecrement64(volatile AtomicType64 *addend) { return atomic_dec_64_nv(addend); } | ||
|
||
#elif defined(_WIN32) || defined(_WIN64) | ||
#include <windows.h> | ||
#if !(defined(_MSC_VER) && _MSC_VER < 1300) | ||
inline AtomicType16 interlockedIncrement16(volatile AtomicType16 *addend) { return InterlockedIncrement16(addend); } | ||
inline AtomicType32 interlockedIncrement32(volatile AtomicType32 *addend) { return InterlockedIncrement(addend); } | ||
inline AtomicType64 interlockedIncrement64(volatile AtomicType64 *addend) { return InterlockedIncrement64(addend); } | ||
|
||
inline AtomicType16 interlockedDecrement16(volatile AtomicType16 *addend) { return InterlockedDecrement16(addend); } | ||
inline AtomicType32 interlockedDecrement32(volatile AtomicType32 *addend) { return InterlockedDecrement(addend); } | ||
inline AtomicType64 interlockedDecrement64(volatile AtomicType64 *addend) { return InterlockedDecrement64(addend); } | ||
#else | ||
inline AtomicType16 interlockedIncrement16(volatile AtomicType16* ptr) { | ||
__asm { | ||
mov ecx, ptr ; Load address of the value into ECX | ||
mov ax, 1 ; Value to add (1) | ||
lock xadd [ecx], ax ; Atomically add and exchange | ||
inc ax ; Increment the value in AX | ||
movzx eax, ax ; Zero extend AX to EAX | ||
} | ||
} | ||
inline AtomicType32 interlockedIncrement32(volatile AtomicType32 *addend) { return InterlockedIncrement(addend); } | ||
// 64 bit interlockedIncrement64 does not handle carryover into the upper 32bits | ||
inline AtomicType64 interlockedIncrement64(volatile AtomicType64* ptr) { | ||
__asm { | ||
mov eax, ptr ; Load address of the value into EAX | ||
mov ecx, 1 ; Value to add (1) | ||
lock xadd [eax], ecx ; Atomically add and exchange the lower 32 bits | ||
mov edx, [eax + 4] ; Load the upper 32 bits | ||
add edx, 0 ; Add 0 to the upper 32 bits (no change) | ||
mov [eax + 4], edx ; Store the upper 32 bits back | ||
mov eax, [ptr] ; Move the new value (after increment) into EAX | ||
add eax, 1 ; Increment EAX to return the new value | ||
} | ||
} | ||
|
||
inline AtomicType16 interlockedDecrement16(volatile AtomicType16* ptr) { | ||
__asm { | ||
mov ecx, ptr ; Load address of the value into ECX | ||
mov ax, 1 ; Value to subtract (1) | ||
neg ax ; Negate the value to make it -1 | ||
lock xadd [ecx], ax ; Atomically add and exchange | ||
dec ax ; Decrement the value in AX | ||
movzx eax, ax ; Zero extend AX to EAX | ||
} | ||
} | ||
inline AtomicType32 interlockedDecrement32(volatile AtomicType32 *addend) { return InterlockedDecrement(addend); } | ||
// 64 bit interlockedDecrement64 does not handle borrow from the upper 32bits | ||
inline AtomicType64 interlockedDecrement64(volatile AtomicType64* ptr) { | ||
__asm { | ||
mov eax, ptr ; Load address of the value into EAX | ||
mov ecx, 1 ; Value to subtract (1) | ||
lock xadd [eax], ecx ; Atomically add and exchange the lower 32 bits | ||
neg ecx ; Negate ECX to get -1 | ||
lock xadd [eax], ecx ; Atomically subtract 1 from the lower 32 bits | ||
mov edx, [eax + 4] ; Load the upper 32 bits | ||
add edx, 0 ; Add 0 to the upper 32 bits (no change) | ||
mov [eax + 4], edx ; Store the upper 32 bits back | ||
mov eax, [ptr] ; Move the new value (after decrement) into EAX | ||
sub eax, 1 ; Decrement EAX to return the new value | ||
} | ||
} | ||
#endif // !(_MSC_VER < 1300) | ||
|
||
#else | ||
#error platform not supported | ||
#endif | ||
|
||
|
||
// c++ template Interlocked helper functions, prevent compiler errors when implementation is not present and function is unused for a missing type | ||
template<typename Type> inline Type interlockedIncrement(volatile Type *addend); | ||
Mauller marked this conversation as resolved.
Show resolved
Hide resolved
|
||
template<typename Type> inline Type interlockedDecrement(volatile Type *addend); | ||
|
||
template<> inline AtomicType16 interlockedIncrement<AtomicType16>(volatile AtomicType16 *addend) { return interlockedIncrement16(addend); } | ||
template<> inline AtomicType32 interlockedIncrement<AtomicType32>(volatile AtomicType32 *addend) { return interlockedIncrement32(addend); } | ||
template<> inline AtomicType64 interlockedIncrement<AtomicType64>(volatile AtomicType64 *addend) { return interlockedIncrement64(addend); } | ||
|
||
template<> inline AtomicType16 interlockedDecrement<AtomicType16>(volatile AtomicType16 *addend) { return interlockedDecrement16(addend); } | ||
template<> inline AtomicType32 interlockedDecrement<AtomicType32>(volatile AtomicType32 *addend) { return interlockedDecrement32(addend); } | ||
template<> inline AtomicType64 interlockedDecrement<AtomicType64>(volatile AtomicType64 *addend) { return interlockedDecrement64(addend); } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.