[redcode/crythread-2nd-pass] removed MultiThread.h along with external CryInterlocked* definitions

Signed-off-by: AMZN-ScottR <24445312+AMZN-ScottR@users.noreply.github.com>
This commit is contained in:
AMZN-ScottR
2021-08-11 16:38:54 -07:00
parent 337ea488b6
commit c173dee2bc
6 changed files with 1 additions and 419 deletions
-277
View File
@@ -1,277 +0,0 @@
/*
* Copyright (c) Contributors to the Open 3D Engine Project.
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/
#pragma once
#if defined(APPLE) || defined(LINUX)
#include <sched.h>
#endif
#include <AzCore/std/parallel/mutex.h>
#include "CryAssert.h"
// Section dictionary
#if defined(AZ_RESTRICTED_PLATFORM)
#define MULTITHREAD_H_SECTION_TRAITS 1
#define MULTITHREAD_H_SECTION_DEFINE_CRYINTERLOCKEXCHANGE 2
#define MULTITHREAD_H_SECTION_IMPLEMENT_CRYSPINLOCK 3
#define MULTITHREAD_H_SECTION_IMPLEMENT_CRYINTERLOCKEDADD 4
#define MULTITHREAD_H_SECTION_IMPLEMENT_CRYINTERLOCKEDADDSIZE 5
#define MULTITHREAD_H_SECTION_CRYINTERLOCKEDFLUSHSLIST_PT1 6
#define MULTITHREAD_H_SECTION_CRYINTERLOCKEDFLUSHSLIST_PT2 7
#define MULTITHREAD_H_SECTION_IMPLEMENT_CRYINTERLOCKEDCOMPAREEXCHANGE64 8
#endif
#define WRITE_LOCK_VAL (1 << 16)
// Traits
#if defined(AZ_RESTRICTED_PLATFORM)
#define AZ_RESTRICTED_SECTION MULTITHREAD_H_SECTION_TRAITS
#include AZ_RESTRICTED_FILE(MultiThread_h)
#endif
void CrySpinLock(volatile int* pLock, int checkVal, int setVal);
void CryReleaseSpinLock (volatile int*, int);
LONG CryInterlockedIncrement(int volatile* lpAddend);
LONG CryInterlockedDecrement(int volatile* lpAddend);
LONG CryInterlockedOr(LONG volatile* Destination, LONG Value);
LONG CryInterlockedExchangeAdd(LONG volatile* lpAddend, LONG Value);
LONG CryInterlockedCompareExchange(LONG volatile* dst, LONG exchange, LONG comperand);
void* CryInterlockedCompareExchangePointer(void* volatile* dst, void* exchange, void* comperand);
void* CryInterlockedExchangePointer (void* volatile* dst, void* exchange);
#if defined(AZ_RESTRICTED_PLATFORM)
#define AZ_RESTRICTED_SECTION MULTITHREAD_H_SECTION_DEFINE_CRYINTERLOCKEXCHANGE
#include AZ_RESTRICTED_FILE(MultiThread_h)
#endif
ILINE void CrySpinLock(volatile int* pLock, int checkVal, int setVal)
{
#ifdef _CPU_X86
# ifdef __GNUC__
int val;
__asm__ __volatile__ (
"0: mov %[checkVal], %%eax\n"
" lock cmpxchg %[setVal], (%[pLock])\n"
" jnz 0b"
: "=m" (*pLock)
: [pLock] "r" (pLock), "m" (*pLock),
[checkVal] "m" (checkVal),
[setVal] "r" (setVal)
: "eax", "cc", "memory"
);
# else //!__GNUC__
__asm
{
mov edx, setVal
mov ecx, pLock
Spin:
// Trick from Intel Optimizations guide
#ifdef _CPU_SSE
pause
#endif
mov eax, checkVal
lock cmpxchg [ecx], edx
jnz Spin
}
# endif //!__GNUC__
#else // !_CPU_X86
# if defined(AZ_RESTRICTED_PLATFORM)
#define AZ_RESTRICTED_SECTION MULTITHREAD_H_SECTION_IMPLEMENT_CRYSPINLOCK
#include AZ_RESTRICTED_FILE(MultiThread_h)
# endif
# if defined(AZ_RESTRICTED_SECTION_IMPLEMENTED)
# undef AZ_RESTRICTED_SECTION_IMPLEMENTED
# elif defined(APPLE) || defined(LINUX)
// register int val;
// __asm__ __volatile__ (
// "0: mov %[checkVal], %%eax\n"
// " lock cmpxchg %[setVal], (%[pLock])\n"
// " jnz 0b"
// : "=m" (*pLock)
// : [pLock] "r" (pLock), "m" (*pLock),
// [checkVal] "m" (checkVal),
// [setVal] "r" (setVal)
// : "eax", "cc", "memory"
// );
//while(CryInterlockedCompareExchange((volatile long*)pLock,setVal,checkVal)!=checkVal) ;
uint loops = 0;
while (__sync_val_compare_and_swap((volatile int32_t*)pLock, (int32_t)checkVal, (int32_t)setVal) != checkVal)
{
# if !defined (ANDROID) && !defined(IOS)
_mm_pause();
# endif
if (!(++loops & 0x7F))
{
usleep(1); // give threads with other prio chance to run
}
else if (!(loops & 0x3F))
{
sched_yield(); // give threads with same prio chance to run
}
}
# else
// NOTE: The code below will fail on 64bit architectures!
while (_InterlockedCompareExchange((volatile LONG*)pLock, setVal, checkVal) != checkVal)
{
_mm_pause();
}
# endif
#endif
}
ILINE void CryReleaseSpinLock(volatile int* pLock, int setVal)
{
*pLock = setVal;
}
//////////////////////////////////////////////////////////////////////////
ILINE void CryInterlockedAdd(volatile int* pVal, int iAdd)
{
#ifdef _CPU_X86
# ifdef __GNUC__
__asm__ __volatile__ (
" lock add %[iAdd], (%[pVal])\n"
: "=m" (*pVal)
: [pVal] "r" (pVal), "m" (*pVal), [iAdd] "r" (iAdd)
);
# else
__asm
{
mov edx, pVal
mov eax, iAdd
lock add [edx], eax
}
# endif
#else
// NOTE: The code below will fail on 64bit architectures!
#if defined(_WIN64)
_InterlockedExchangeAdd((volatile LONG*)pVal, iAdd);
#define AZ_RESTRICTED_SECTION_IMPLEMENTED
#elif defined(AZ_RESTRICTED_PLATFORM)
#define AZ_RESTRICTED_SECTION MULTITHREAD_H_SECTION_IMPLEMENT_CRYINTERLOCKEDADD
#include AZ_RESTRICTED_FILE(MultiThread_h)
#endif
#if defined(AZ_RESTRICTED_SECTION_IMPLEMENTED)
#undef AZ_RESTRICTED_SECTION_IMPLEMENTED
#elif defined(APPLE) || defined(LINUX)
CryInterlockedExchangeAdd((volatile LONG*)pVal, iAdd);
#elif defined(APPLE)
OSAtomicAdd32(iAdd, (volatile LONG*)pVal);
#else
InterlockedExchangeAdd((volatile LONG*)pVal, iAdd);
#endif
#endif
}
ILINE void CryInterlockedAddSize(volatile size_t* pVal, ptrdiff_t iAdd)
{
#if defined(PLATFORM_64BIT)
#if defined(_WIN64)
_InterlockedExchangeAdd64((volatile __int64*)pVal, iAdd);
#define AZ_RESTRICTED_SECTION_IMPLEMENTED
#elif defined(AZ_RESTRICTED_PLATFORM)
#define AZ_RESTRICTED_SECTION MULTITHREAD_H_SECTION_IMPLEMENT_CRYINTERLOCKEDADDSIZE
#include AZ_RESTRICTED_FILE(MultiThread_h)
#endif
#if defined(AZ_RESTRICTED_SECTION_IMPLEMENTED)
#undef AZ_RESTRICTED_SECTION_IMPLEMENTED
#elif defined(WIN32)
InterlockedExchangeAdd64((volatile LONG64*)pVal, iAdd);
#elif defined(APPLE) || defined(LINUX)
(void)__sync_fetch_and_add((int64_t*)pVal, (int64_t)iAdd);
#else
int64 x, n;
do
{
x = (int64) * pVal;
n = x + iAdd;
}
while (CryInterlockedCompareExchange64((volatile int64*)pVal, n, x) != x);
#endif
#else
CryInterlockedAdd((volatile int*)pVal, (int)iAdd);
#endif
}
//////////////////////////////////////////////////////////////////////////
ILINE void CryWriteLock(volatile int* rw)
{
CrySpinLock(rw, 0, WRITE_LOCK_VAL);
}
ILINE void CryReleaseWriteLock(volatile int* rw)
{
CryInterlockedAdd(rw, -WRITE_LOCK_VAL);
}
//////////////////////////////////////////////////////////////////////////
struct WriteLock
{
ILINE WriteLock(volatile int& rw) { CryWriteLock(&rw); prw = &rw; }
~WriteLock() { CryReleaseWriteLock(prw); }
private:
volatile int* prw;
};
//////////////////////////////////////////////////////////////////////////
struct WriteLockCond
{
ILINE WriteLockCond(volatile int& rw, int bActive = 1)
{
if (bActive)
{
CrySpinLock(&rw, 0, iActive = WRITE_LOCK_VAL);
}
else
{
iActive = 0;
}
prw = &rw;
}
ILINE WriteLockCond() { prw = &(iActive = 0); }
~WriteLockCond()
{
CryInterlockedAdd(prw, -iActive);
}
void SetActive(int bActive = 1) { iActive = -bActive & WRITE_LOCK_VAL; }
void Release() { CryInterlockedAdd(prw, -iActive); }
volatile int* prw;
int iActive;
};
#if defined(LINUX) || defined(APPLE)
ILINE int64 CryInterlockedCompareExchange64(volatile int64* addr, int64 exchange, int64 comperand)
{
return __sync_val_compare_and_swap(addr, comperand, exchange);
// This is OK, because long is signed int64 on Linux x86_64
//return CryInterlockedCompareExchange((volatile long*)addr, (long)exchange, (long)comperand);
}
#else
ILINE int64 CryInterlockedCompareExchange64(volatile int64* addr, int64 exchange, int64 compare)
{
// forward to system call
#if defined(AZ_RESTRICTED_PLATFORM)
#define AZ_RESTRICTED_SECTION MULTITHREAD_H_SECTION_IMPLEMENT_CRYINTERLOCKEDCOMPAREEXCHANGE64
#include AZ_RESTRICTED_FILE(MultiThread_h)
#endif
#if defined(AZ_RESTRICTED_SECTION_IMPLEMENTED)
#undef AZ_RESTRICTED_SECTION_IMPLEMENTED
#else
return _InterlockedCompareExchange64((volatile int64*)addr, exchange, compare);
#endif
}
#endif
+1 -84
View File
@@ -1292,90 +1292,7 @@ short CryGetAsyncKeyState(int vKey)
return 0;
}
#if defined(LINUX) || defined(APPLE) || defined(DEFINE_CRY_INTERLOCKED_INCREMENT)
//[K01]: http://www.memoryhole.net/kyle/2007/05/atomic_incrementing.html
//http://forums.devx.com/archive/index.php/t-160558.html
//////////////////////////////////////////////////////////////////////////
DLL_EXPORT LONG CryInterlockedIncrement(LONG volatile* lpAddend)
{
/*int r;
__asm__ __volatile__ (
"lock ; xaddl %0, (%1) \n\t"
: "=r" (r)
: "r" (lpAddend), "0" (1)
: "memory"
);
return (LONG) (r + 1); */// add, since we get the original value back.
return __sync_fetch_and_add(lpAddend, 1) + 1;
}
//////////////////////////////////////////////////////////////////////////
DLL_EXPORT LONG CryInterlockedDecrement(LONG volatile* lpAddend)
{
/*int r;
__asm__ __volatile__ (
"lock ; xaddl %0, (%1) \n\t"
: "=r" (r)
: "r" (lpAddend), "0" (-1)
: "memory"
);
return (LONG) (r - 1); */// subtract, since we get the original value back.
return __sync_fetch_and_sub(lpAddend, 1) - 1;
}
//////////////////////////////////////////////////////////////////////////
DLL_EXPORT LONG CryInterlockedExchangeAdd(LONG volatile* lpAddend, LONG Value)
{
/* LONG r;
__asm__ __volatile__ (
#if defined(LINUX64) || defined(APPLE) // long is 64 bits on amd64.
"lock ; xaddq %0, (%1) \n\t"
#else
"lock ; xaddl %0, (%1) \n\t"
#endif
: "=r" (r)
: "r" (lpAddend), "0" (Value)
: "memory"
);
return r;*/
return __sync_fetch_and_add(lpAddend, Value);
}
DLL_EXPORT LONG CryInterlockedOr(LONG volatile* Destination, LONG Value)
{
return __sync_fetch_and_or(Destination, Value);
}
DLL_EXPORT LONG CryInterlockedCompareExchange(LONG volatile* dst, LONG exchange, LONG comperand)
{
return __sync_val_compare_and_swap(dst, comperand, exchange);
/*LONG r;
__asm__ __volatile__ (
#if defined(LINUX64) || defined(APPLE) // long is 64 bits on amd64.
"lock ; cmpxchgq %2, (%1) \n\t"
#else
"lock ; cmpxchgl %2, (%1) \n\t"
#endif
: "=a" (r)
: "r" (dst), "r" (exchange), "0" (comperand)
: "memory"
);
return r;*/
}
DLL_EXPORT void* CryInterlockedCompareExchangePointer(void* volatile* dst, void* exchange, void* comperand)
{
return __sync_val_compare_and_swap(dst, comperand, exchange);
//return (void*)CryInterlockedCompareExchange((long volatile*)dst, (long)exchange, (long)comperand);
}
DLL_EXPORT void* CryInterlockedExchangePointer(void* volatile* dst, void* exchange)
{
__sync_synchronize();
return __sync_lock_test_and_set(dst, exchange);
//return (void*)CryInterlockedCompareExchange((long volatile*)dst, (long)exchange, (long)comperand);
}
#if defined(LINUX) || defined(APPLE)
threadID CryGetCurrentThreadId()
{
@@ -96,7 +96,6 @@ set(FILES
LegacyAllocator.h
MetaUtils.h
MiniQueue.h
MultiThread.h
MultiThread_Containers.h
NullAudioSystem.h
PNoise3.h
-4
View File
@@ -730,10 +730,6 @@ enum ETriState
typedef int socklen_t;
#endif
// Include MultiThreading support.
#include "MultiThread.h"
// In RELEASE disable printf and fprintf
#if defined(_RELEASE) && !defined(RELEASE_LOGGING)
#if defined(AZ_RESTRICTED_PLATFORM)
-52
View File
@@ -308,58 +308,6 @@ short CryGetAsyncKeyState([[maybe_unused]] int vKey)
#endif
}
//////////////////////////////////////////////////////////////////////////
LONG CryInterlockedIncrement(int volatile* lpAddend)
{
return InterlockedIncrement((volatile LONG*)lpAddend);
}
//////////////////////////////////////////////////////////////////////////
LONG CryInterlockedDecrement(int volatile* lpAddend)
{
return InterlockedDecrement((volatile LONG*)lpAddend);
}
//////////////////////////////////////////////////////////////////////////
LONG CryInterlockedExchangeAdd(LONG volatile* lpAddend, LONG Value)
{
return InterlockedExchangeAdd(lpAddend, Value);
}
LONG CryInterlockedOr(LONG volatile* Destination, LONG Value)
{
return InterlockedOr(Destination, Value);
}
LONG CryInterlockedCompareExchange(LONG volatile* dst, LONG exchange, LONG comperand)
{
return InterlockedCompareExchange(dst, exchange, comperand);
}
void* CryInterlockedCompareExchangePointer(void* volatile* dst, void* exchange, void* comperand)
{
return InterlockedCompareExchangePointer(dst, exchange, comperand);
}
void* CryInterlockedExchangePointer(void* volatile* dst, void* exchange)
{
return InterlockedExchangePointer(dst, exchange);
}
void CryInterlockedAdd(volatile size_t* pVal, ptrdiff_t iAdd)
{
#if defined (PLATFORM_64BIT)
#if !defined(NDEBUG)
size_t v = (size_t)
#endif
InterlockedAdd64((volatile int64*)pVal, iAdd);
#else
size_t v = (size_t)CryInterlockedExchangeAdd((volatile long*)pVal, (long)iAdd);
v += iAdd;
#endif
assert((iAdd == 0) || (iAdd < 0 && v < v - (size_t)iAdd) || (iAdd > 0 && v > v - (size_t)iAdd));
}
//////////////////////////////////////////////////////////////////////////
uint32 CryGetFileAttributes(const char* lpFileName)
{
-1
View File
@@ -10,7 +10,6 @@
#pragma once
#include <ILog.h>
#include <MultiThread.h>
#include <MultiThread_Containers.h>
//////////////////////////////////////////////////////////////////////