Skip to content

Commit

Permalink
Fix release build (#46)
Browse files Browse the repository at this point in the history
* Perf critial structures needs to be C-array instead of c++ array

* Temp fix only disable optimization in Core

* fix a buffer overflow

* Fix build on Linux
  • Loading branch information
howard0su authored Dec 5, 2023
1 parent a8ed3a0 commit e2f3183
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 104 deletions.
92 changes: 48 additions & 44 deletions Source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ set(CMAKE_CXX_STANDARD 20)

#TODO: Separate SDL Options
list(APPEND plat_main )
## if DEBUG is added to the build_daedalus script it will execute teh Debug section otherwise by default do release.
if(DEBUG)
set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION FALSE)
add_compile_definitions(DAEDALUS_CONFIG_VERSION="Dev") ## Perhaps we can remove this. It's only used in the PSP UI and model doesn't matter as much
option(DAEDALUS_ENABLE_ASSERTS "Enable Static Asserts" ON)
option(DAEDALUS_DEBUG_CONSOLE "Enable Debug Console" ON)
option(DAEDALUS_DEBUG_DISPLAYLIST "Options for debugging the display list, useful for determining texture issues" OFF)
option(DAEDALUS_LOG "Enable various logging" ON)
add_compile_options("-Wall")
else()
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
add_compile_definitions(DAEDALUS_CONFIG_VERSION="Release") ## Perhaps we can remove this. It's only used in the PSP UI and model doesn't matter as much
add_compile_options("-O3")
endif()

## if DEBUG is added to the build_daedalus script it will execute teh Debug section otherwise by default do release.
if(DEBUG)
set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION FALSE)
add_compile_definitions(DAEDALUS_CONFIG_VERSION="Dev") ## Perhaps we can remove this. It's only used in the PSP UI and model doesn't matter as much
option(DAEDALUS_ENABLE_ASSERTS "Enable Static Asserts" ON)
option(DAEDALUS_DEBUG_CONSOLE "Enable Debug Console" ON)
option(DAEDALUS_DEBUG_DISPLAYLIST "Options for debugging the display list, useful for determining texture issues" OFF)
option(DAEDALUS_LOG "Enable various logging" ON)
add_compile_options("-Wall")
else()
set(CMAKE_BUILD_TYPE RelWithDebInfo)
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
add_compile_definitions(DAEDALUS_CONFIG_VERSION="Release") ## Perhaps we can remove this. It's only used in the PSP UI and model doesn't matter as much
endif()
#Determine Dynarec compatibility and link in appropriate Dynarec
#Todo: Move all relevant Dynarec to the Dynarec/arch folder and enable here when correct platform is detected
if(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "arm64" )
Expand All @@ -46,7 +46,7 @@ list(APPEND plat_main )
else()
message("Platform unknown, Dynarec disabled")
option(DAEDALUS_ENABLE_DYNAREC "Enable Dynarec" OFF)
endif()
endif()

#Check the Endian of the host platform and apply endian rules.
include (TestBigEndian)
Expand Down Expand Up @@ -134,32 +134,36 @@ list(APPEND plat_main )
include_directories( BEFORE ${include_dirs})


## Generic defines for certain things that Windows requires.
if(NOT WIN32)
add_compile_definitions(DAEDALUS_THREAD_CALL_TYPE=${})
add_compile_definitions(DAEDALUS_VARARG_CALL_TYPE=${})
add_compile_options("-g")
endif()
## Generic defines for certain things that Windows requires.
if(NOT WIN32)
add_compile_definitions(DAEDALUS_THREAD_CALL_TYPE=${})
add_compile_definitions(DAEDALUS_VARARG_CALL_TYPE=${})
add_compile_options("-g")
add_compile_options("-ffast-math")
endif()


if (APPLE)
list(APPEND sys_libraries "-framework OpenGL -framework AudioToolbox -framework Cocoa -framework IOKit -framework CoreVideo")
## Homebrew uses a separate path on Apple Silicon
##Ugh is there a better way??
if(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "arm64" )
include_directories(/opt/homebrew/include)
link_directories(/opt/homebrew/lib)
else()
include_directories(/usr/local/include)
link_directories(/usr/local/lib)
endif()
endif (APPLE)
if(CTR)
if(UNIX)
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION FALSE)
endif(UNIX)

if (APPLE)
list(APPEND sys_libraries "-framework OpenGL -framework AudioToolbox -framework Cocoa -framework IOKit -framework CoreVideo")
## Homebrew uses a separate path on Apple Silicon
##Ugh is there a better way??
if(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "arm64" )
include_directories(/opt/homebrew/include)
link_directories(/opt/homebrew/lib)
else()
include_directories(/usr/local/include)
link_directories(/usr/local/lib)
endif()
endif (APPLE)
if(CTR)
include_directories($ENV{DEVKITPRO}/portlibs/3ds/include)
include_directories($ENV{DEVKITPRO}/picaGL/include)
link_directories($ENV{DEVKITPRO}/picaGL/lib)
include_directories($ENV{DEVKITPRO}/portlibs/3ds/lib)
endif(CTR)
endif(CTR)

if(WIN32)
set(CMAKE_TOOLCHAIN_FILE ${VCPKG_TOOLCHAIN})
Expand All @@ -179,13 +183,12 @@ if(WIN32)
#Build and Link Executable

add_executable(daedalus SysW32/main.cpp)
target_link_libraries(daedalus LINK_PUBLIC
Core Config Debug Graphics HLEAudio HLEGraphics Interface Math OSHLE RomFile System Utility
Graphics_GL HLEGraphics_GL Input_GL Interface_GL imgui
Win_Debug Win_Audio Win_Utility X86_Dynarec)
target_link_libraries(daedalus LINK_PUBLIC
Core Config Debug Graphics HLEAudio HLEGraphics Interface Math OSHLE RomFile System Utility
Graphics_GL HLEGraphics_GL Input_GL Interface_GL imgui
Win_Debug Win_Audio Win_Utility)
endif (WIN32)


## Daedalus Core Build
add_subdirectory(Config)
add_subdirectory(Core)
Expand Down Expand Up @@ -220,8 +223,9 @@ if(WIN32)
add_executable(daedalus ${plat_main})
list(APPEND default_libraries png z) ## Required Libraries

message(" ${daed_libs} ${default_libraries} ${sys_libraries} " )
target_include_directories(daedalus BEFORE PUBLIC ${include_dirs})
target_link_libraries(daedalus PRIVATE ${daed_libs} ${default_libraries} ${sys_libraries} )
target_link_libraries(daedalus PRIVATE ${daed_libs} ${default_libraries} ${sys_libraries})

# --- Post Build ---
if(PSP)
Expand Down Expand Up @@ -279,4 +283,4 @@ if(WIN32)
install(DIRECTORY DESTINATION DaedalusX64)
install(DIRECTORY DESTINATION DaedalusX64/Roms)
install(DIRECTORY DESTINATION DaedalusX64/SaveGames)
install(DIRECTORY DESTINATION DaedalusX64/SaveStates)
install(DIRECTORY DESTINATION DaedalusX64/SaveStates)
3 changes: 3 additions & 0 deletions Source/Core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,8 @@ add_library(Core OBJECT
TLB.cpp
)

# Cannot compile with -O2 or above
set_source_files_properties(R4300.cpp PROPERTIES COMPILE_FLAGS -O0)

list(APPEND daed_libs "Core")
set(daed_libs ${daed_libs} PARENT_SCOPE)
2 changes: 1 addition & 1 deletion Source/Core/Cheats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ bool CheatCodes_Read(const char *rom_name, const char *file, u8 countryID)
}

codegroupcount = 0;
while(codegroupcount < numberofgroups && fgets(line, 32767, stream) && strlen(line) > 8) // 32767 makes sure the entire line is read
while(codegroupcount < numberofgroups && fgets(line, sizeof(line), stream) && strlen(line) > 8)
{
// Codes for the group are in the string line[]
for(c1 = 0; line[c1] != '=' && line[c1] != '\0'; c1++) codegrouplist[codegroupcount].name[c1] = line[c1];
Expand Down
22 changes: 4 additions & 18 deletions Source/Core/Memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

#include "Base/Types.h"

#include <array>
#include <algorithm>

#include "Core/CPU.h"
Expand Down Expand Up @@ -63,8 +62,7 @@ static void MemoryUpdatePIF();

static void Memory_InitTables();

// const u32 MemoryRegionSizes[NUM_MEM_BUFFERS] =
std::array<const u32, NUM_MEM_BUFFERS> MemoryRegionSizes =
const u32 MemoryRegionSizes[NUM_MEM_BUFFERS] =
{
0x04, // This seems enough (Salvy)
kMaximumMemSize, // RD_RAM
Expand Down Expand Up @@ -108,12 +106,9 @@ bool g_RomWritten;
u8 * g_pu8RamBase_8000 = nullptr;
//u8 * g_pu8RamBase_A000 = nullptr;

// std::array<MemFuncRead, 0x4000> g_MemoryLookupTableRead;
std::array<MemFuncWrite, 0x4000> g_MemoryLookupTableWrite;
std::array<void *, NUM_MEM_BUFFERS> g_pMemoryBuffers;
MemFuncRead g_MemoryLookupTableRead[0x4000];
// MemFuncWrite g_MemoryLookupTableWrite[0x4000];
// void * g_pMemoryBuffers[NUM_MEM_BUFFERS];
MemFuncWrite g_MemoryLookupTableWrite[0x4000];
void * g_pMemoryBuffers[NUM_MEM_BUFFERS];


#include "Memory_Read.inl"
Expand Down Expand Up @@ -223,9 +218,7 @@ void Memory_Fini(void)

g_pu8RamBase_8000 = nullptr;
//g_pu8RamBase_A000 = nullptr;
// std::fill(g_pMemoryBuffers.begin(), g_pMemoryBuffers.end(), 0);
g_pMemoryBuffers = {};
// memset( g_pMemoryBuffers, 0, sizeof( g_pMemoryBuffers ) );
memset( g_pMemoryBuffers, 0xff, sizeof( g_pMemoryBuffers ) );
}

bool Memory_Reset()
Expand Down Expand Up @@ -330,13 +323,6 @@ static void Memory_InitFunc(u32 start, u32 size, const u32 ReadRegion, const u32

void Memory_InitTables()
{
// g_MemoryLookupTableRead = {};
g_MemoryLookupTableWrite = {};
// g_MemoryLookupTableRead.fill();
// g_MemoryLookupTableWrite.fill(0);
memset(g_MemoryLookupTableRead, 0, sizeof(MemFuncRead) * 0x4000);
// memset(g_MemoryLookupTableWrite, 0, sizeof(MemFuncWrite) * 0x4000);

u32 i = 0;
for (i = 0; i < (0x10000 >> 2); i++)
{
Expand Down
10 changes: 3 additions & 7 deletions Source/Core/Memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,9 @@ extern u32 gRamSize;
extern u32 gTLBReadHit;
extern u32 gTLBWriteHit;
#endif
// extern void * g_pMemoryBuffers[NUM_MEM_BUFFERS];
// extern const u32 MemoryRegionSizes[NUM_MEM_BUFFERS];

extern std::array<void *, NUM_MEM_BUFFERS> g_pMemoryBuffers;
extern std::array< const u32, NUM_MEM_BUFFERS> MemoryRegionSizes;
extern void * g_pMemoryBuffers[NUM_MEM_BUFFERS];
extern const u32 MemoryRegionSizes[NUM_MEM_BUFFERS];

bool Memory_Init();
void Memory_Fini();
Expand All @@ -97,10 +95,8 @@ using MemWriteValueFunction = void (*)(u32 address, u32 value);
using InternalMemFastFunction = bool (*)(u32 address, void ** p_translated);
#endif

// extern std::array<MemFuncRead, 0x4000> g_MemoryLookupTableRead;
extern std::array<MemFuncWrite, 0x4000> g_MemoryLookupTableWrite;
extern MemFuncRead g_MemoryLookupTableRead[0x4000];
// extern MemFuncWrite g_MemoryLookupTableWrite[0x4000];
extern MemFuncWrite g_MemoryLookupTableWrite[0x4000];

// Fast memory access
inline void* const ReadAddress( u32 address )
Expand Down
8 changes: 3 additions & 5 deletions Source/Core/R4300.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

#include "Core/R4300OpCode.h"
#include "Core/R4300Instruction.h"
#include <array>

void R4300_SetSR( u32 new_value );

// extern CPU_Instruction R4300Instruction[64];
extern std::array<CPU_Instruction, 64> R4300Instruction;
extern std::array< CPU_Instruction, 64> R4300Cop1DInstruction;
// extern CPU_Instruction R4300Cop1DInstruction[64];
extern CPU_Instruction R4300Instruction[64];
extern CPU_Instruction R4300Cop1DInstruction[64];

inline CPU_Instruction R4300_GetDInstructionHandler( OpCode op_code )
{
Expand Down
29 changes: 9 additions & 20 deletions Source/Core/R4300_Jump.inl
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

// Opcode Jump Table
// CPU_Instruction R4300Instruction[64] = {
std::array<CPU_Instruction, 64> R4300Instruction
{
CPU_Instruction R4300Instruction[64] = {
R4300_Special, R4300_RegImm, R4300_J, R4300_JAL, R4300_BEQ, R4300_BNE, R4300_BLEZ, R4300_BGTZ,
R4300_ADDI, R4300_ADDIU, R4300_SLTI, R4300_SLTIU, R4300_ANDI, R4300_ORI, R4300_XORI, R4300_LUI,
R4300_CoPro0, R4300_CoPro1, R4300_Unk, R4300_Unk, R4300_BEQL, R4300_BNEL, R4300_BLEZL, R4300_BGTZL,
Expand Down Expand Up @@ -75,8 +73,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.


// SpecialOpCode Jump Table
// CPU_Instruction R4300SpecialInstruction[64] = {
std::array<CPU_Instruction, 64> R4300SpecialInstruction {
CPU_Instruction R4300SpecialInstruction[64] = {
R4300_Special_SLL, R4300_Special_Unk, R4300_Special_SRL, R4300_Special_SRA, R4300_Special_SLLV, R4300_Special_Unk, R4300_Special_SRLV, R4300_Special_SRAV,
R4300_Special_JR, R4300_Special_JALR, R4300_Special_Unk, R4300_Special_Unk, R4300_Special_SYSCALL, R4300_Special_BREAK, R4300_Special_Unk, R4300_Special_SYNC,
R4300_Special_MFHI, R4300_Special_MTHI, R4300_Special_MFLO, R4300_Special_MTLO, R4300_Special_DSLLV, R4300_Special_Unk, R4300_Special_DSRLV, R4300_Special_DSRAV,
Expand All @@ -100,9 +97,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
hi |-------|-------|-------|-------|-------|-------|-------|-------|
*/

// CPU_Instruction R4300RegImmInstruction[32] = {
std::array<CPU_Instruction, 32> R4300RegImmInstruction {

CPU_Instruction R4300RegImmInstruction[32] = {
R4300_RegImm_BLTZ, R4300_RegImm_BGEZ, R4300_RegImm_BLTZL, R4300_RegImm_BGEZL, R4300_RegImm_Unk, R4300_RegImm_Unk, R4300_RegImm_Unk, R4300_RegImm_Unk,
R4300_RegImm_TGEI, R4300_RegImm_TGEIU, R4300_RegImm_TLTI, R4300_RegImm_TLTIU, R4300_RegImm_TEQI, R4300_RegImm_Unk, R4300_RegImm_TNEI, R4300_RegImm_Unk,
R4300_RegImm_BLTZAL, R4300_RegImm_BGEZAL, R4300_RegImm_BLTZALL, R4300_RegImm_BGEZALL, R4300_RegImm_Unk, R4300_RegImm_Unk, R4300_RegImm_Unk, R4300_RegImm_Unk,
Expand All @@ -125,8 +120,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

// COP0 Jump Table
std::array<CPU_Instruction, 32> R4300Cop0Instruction {
// CPU_Instruction R4300Cop0Instruction[32] = {
CPU_Instruction R4300Cop0Instruction[32] = {
R4300_Cop0_MFC0, R4300_Cop0_Unk, R4300_Cop0_Unk, R4300_Cop0_Unk, R4300_Cop0_MTC0, R4300_Cop0_Unk, R4300_Cop0_Unk, R4300_Cop0_Unk,
R4300_Cop0_Unk, R4300_Cop0_Unk, R4300_Cop0_Unk, R4300_Cop0_Unk, R4300_Cop0_Unk, R4300_Cop0_Unk, R4300_Cop0_Unk, R4300_Cop0_Unk,
R4300_Cop0_TLB, R4300_Cop0_Unk, R4300_Cop0_Unk, R4300_Cop0_Unk, R4300_Cop0_Unk, R4300_Cop0_Unk, R4300_Cop0_Unk, R4300_Cop0_Unk,
Expand Down Expand Up @@ -154,8 +148,7 @@ std::array<CPU_Instruction, 32> R4300Cop0Instruction {
*/

// TLBOpCode Jump Table
// CPU_Instruction R4300TLBInstruction[64] = {
std::array<CPU_Instruction, 64> R4300TLBInstruction {
CPU_Instruction R4300TLBInstruction[64] = {
R4300_TLB_Unk, R4300_TLB_TLBR, R4300_TLB_TLBWI, R4300_TLB_Unk, R4300_TLB_Unk, R4300_TLB_Unk, R4300_TLB_TLBWR, R4300_TLB_Unk,
R4300_TLB_TLBP, R4300_TLB_Unk, R4300_TLB_Unk, R4300_TLB_Unk, R4300_TLB_Unk, R4300_TLB_Unk, R4300_TLB_Unk, R4300_TLB_Unk,
R4300_TLB_Unk, R4300_TLB_Unk, R4300_TLB_Unk, R4300_TLB_Unk, R4300_TLB_Unk, R4300_TLB_Unk, R4300_TLB_Unk, R4300_TLB_Unk,
Expand Down Expand Up @@ -184,8 +177,7 @@ std::array<CPU_Instruction, 32> R4300Cop0Instruction {
*/

// COP1 Jump Table
// CPU_Instruction R4300Cop1Instruction[32] = {
std::array<CPU_Instruction,32> R4300Cop1Instruction {
CPU_Instruction R4300Cop1Instruction[32] = {
R4300_Cop1_MFC1, R4300_Cop1_DMFC1, R4300_Cop1_CFC1, R4300_Cop1_Unk, R4300_Cop1_MTC1, R4300_Cop1_DMTC1, R4300_Cop1_CTC1, R4300_Cop1_Unk,
R4300_Cop1_BCInstr, R4300_Cop1_Unk, R4300_Cop1_Unk, R4300_Cop1_Unk, R4300_Cop1_Unk, R4300_Cop1_Unk, R4300_Cop1_Unk, R4300_Cop1_Unk,
R4300_Cop1_SInstr, R4300_Cop1_DInstr, R4300_Cop1_Unk, R4300_Cop1_Unk, R4300_Cop1_WInstr, R4300_Cop1_LInstr, R4300_Cop1_Unk, R4300_Cop1_Unk,
Expand All @@ -204,8 +196,7 @@ std::array<CPU_Instruction, 32> R4300Cop0Instruction {
nd |-------|-------|
*/

// CPU_Instruction R4300Cop1BC1Instruction[4] = {
std::array<CPU_Instruction, 4> R4300Cop1BC1Instruction {
CPU_Instruction R4300Cop1BC1Instruction[4] = {
R4300_BC1_BC1F, R4300_BC1_BC1T, R4300_BC1_BC1FL, R4300_BC1_BC1TL
};

Expand All @@ -229,8 +220,7 @@ std::array<CPU_Instruction, 32> R4300Cop0Instruction {
*/

// Single Jump Table
// CPU_Instruction R4300Cop1SInstruction[64] = {
std::array<CPU_Instruction, 64> R4300Cop1SInstruction {
CPU_Instruction R4300Cop1SInstruction[64] = {
R4300_Cop1_S_ADD, R4300_Cop1_S_SUB, R4300_Cop1_S_MUL, R4300_Cop1_S_DIV, R4300_Cop1_S_SQRT, R4300_Cop1_S_ABS, R4300_Cop1_S_MOV, R4300_Cop1_S_NEG,
R4300_Cop1_S_ROUND_L, R4300_Cop1_S_TRUNC_L, R4300_Cop1_S_CEIL_L, R4300_Cop1_S_FLOOR_L, R4300_Cop1_S_ROUND_W, R4300_Cop1_S_TRUNC_W, R4300_Cop1_S_CEIL_W, R4300_Cop1_S_FLOOR_W,
R4300_Cop1_S_Unk, R4300_Cop1_S_Unk, R4300_Cop1_S_Unk, R4300_Cop1_S_Unk, R4300_Cop1_S_Unk, R4300_Cop1_S_Unk, R4300_Cop1_S_Unk, R4300_Cop1_S_Unk,
Expand Down Expand Up @@ -260,8 +250,7 @@ std::array<CPU_Instruction, 32> R4300Cop0Instruction {
*/

// Double Jump Table
std::array<CPU_Instruction, 64> R4300Cop1DInstruction
// CPU_Instruction R4300Cop1DInstruction[64] =
CPU_Instruction R4300Cop1DInstruction[64] =
{
R4300_Cop1_D_ADD, R4300_Cop1_D_SUB, R4300_Cop1_D_MUL, R4300_Cop1_D_DIV, R4300_Cop1_D_SQRT, R4300_Cop1_D_ABS, R4300_Cop1_D_MOV, R4300_Cop1_D_NEG,
R4300_Cop1_D_ROUND_L, R4300_Cop1_D_TRUNC_L, R4300_Cop1_D_CEIL_L, R4300_Cop1_D_FLOOR_L, R4300_Cop1_D_ROUND_W, R4300_Cop1_D_TRUNC_W, R4300_Cop1_D_CEIL_W, R4300_Cop1_D_FLOOR_W,
Expand Down
5 changes: 1 addition & 4 deletions Source/SysPosix/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,5 @@ install(FILES ${PROJECT_BINARY_DIR}/../Data/roms.ini DESTINATION DaedalusX64)
install(FILES ${PROJECT_BINARY_DIR}/../Data/readme.txt DESTINATION DaedalusX64)
install(FILES ${PROJECT_BINARY_DIR}/../Data/copying.txt DESTINATION DaedalusX64)




set(daed_libs ${daed_libs} PARENT_SCOPE)
set(plat_main SysPosix/main.cpp PARENT_SCOPE)
set(plat_main SysPosix/main.cpp PARENT_SCOPE)
15 changes: 10 additions & 5 deletions Source/System/AtomicPrimitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,16 @@ inline u32 AtomicBitSet( volatile u32 * ptr, u32 and_bits, u32 or_bits )

inline u32 AtomicBitSet( volatile u32 * ptr, u32 and_bits, u32 or_bits )
{
u32 r = *ptr;
r &= and_bits;
r |= or_bits;
*ptr = r;
return r;
u32 new_value;
u32 orig_value;

do {
orig_value = __sync_fetch_and_or(ptr, or_bits); // Atomically OR or_bits into *ptr
new_value = (orig_value & and_bits) | or_bits; // Calculate the new value

} while (__sync_val_compare_and_swap(ptr, orig_value, new_value) != orig_value);

return new_value;
}

#endif
Expand Down

0 comments on commit e2f3183

Please sign in to comment.