Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
erincatto committed Jul 23, 2023
1 parent a5aa7d0 commit f8f4fd5
Show file tree
Hide file tree
Showing 8 changed files with 241 additions and 157 deletions.
4 changes: 2 additions & 2 deletions samples/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ static void ScrollCallback(GLFWwindow* window, double dx, double dy)
}
}

BOX2D_API bool g_parallel;
BOX2D_API bool b2_parallel;

static void UpdateUI()
{
Expand All @@ -397,7 +397,7 @@ static void UpdateUI()
ImGui::Checkbox("Sleep", &s_settings.m_enableSleep);
ImGui::Checkbox("Warm Starting", &s_settings.m_enableWarmStarting);
ImGui::Checkbox("Continuous", &s_settings.m_enableContinuous);
ImGui::Checkbox("Parallel", &g_parallel);
ImGui::Checkbox("Parallel", &b2_parallel);

ImGui::Separator();

Expand Down
85 changes: 6 additions & 79 deletions src/atomic.inl
Original file line number Diff line number Diff line change
@@ -1,82 +1,9 @@
#include <stdint.h>

#if 0 && defined(_MSC_VER) && !defined(__clang__)

#include <intrin.h>

// Returns the initial value
static inline long atomic_fetch_add_long(volatile long* obj, long val)
{
return _InterlockedExchangeAdd(obj, val);
}

// Returns the initial value
static inline long atomic_fetch_sub_long(volatile long* obj, long val)
{
return _InterlockedExchangeAdd(obj, -val);
}

// Returns the initial value
static inline long atomic_store_long(volatile long* obj, long val)
{
return _InterlockedExchange(obj, val);
}

static inline long atomic_load_long(const volatile long* obj)
{
return *obj;
}

static inline bool atomic_compare_exchange_strong_long(volatile long* obj, long* expected, int32_t desired)
{
long current = _InterlockedCompareExchange(obj, desired, *expected);
if (current == *expected)
{
return true;
}

*expected = current;
return false;
}

static inline bool atomic_compare_exchange_weak_long(volatile long* obj, long* expected, int32_t desired)
{
long current = _InterlockedCompareExchange(obj, desired, *expected);
if (current == *expected)
{
return true;
}

*expected = current;
return false;
}

#else

#include <stdatomic.h>

#define atomic_fetch_add_long atomic_fetch_add
#define atomic_fetch_sub_long atomic_fetch_sub
#define atomic_store_long atomic_store
#define atomic_load_long atomic_load
#define atomic_compare_exchange_strong_long atomic_compare_exchange_strong
#define atomic_compare_exchange_weak_long atomic_compare_exchange_weak

#endif

// Atomically store the minimum two values
// *ptr = min(*ptr, b)
static inline void b2AtomicStoreMin(B2_ATOMIC long* ptr, long b)
{
long a = atomic_load_long(ptr);
while (a > b)
{
bool success = atomic_compare_exchange_weak_long(ptr, &a, b);
if (success)
{
return;
}

// otherwise `a` now holds the current value stored in `ptr`
}
}
//#define atomic_fetch_add_long atomic_fetch_add
//#define atomic_fetch_sub_long atomic_fetch_sub
//#define atomic_store_long atomic_store
//#define atomic_load_long atomic_load
//#define atomic_compare_exchange_strong_long atomic_compare_exchange_strong
//#define atomic_compare_exchange_weak_long atomic_compare_exchange_weak
Loading

0 comments on commit f8f4fd5

Please sign in to comment.