
#include "iLPlatform.h"
#include "iLOperatingSystem.h"


// Returns the original value pointed to by a_ptr
// If that value is the same as a_comparand, then *a_ptr is assigned a_val
int32_t iLAtomic_CompareAndSwap32(iLAtomic32* a_ptr, int32_t a_comparand, int32_t a_val)
{
#if defined(IL_PLATFORM_WINDOWS)
  return InterlockedCompareExchange(a_ptr, a_val, a_comparand);
#elif defined(IL_OPERATING_SYSTEM_MACOSX)
  int32_t ret = *a_ptr;
  OSAtomicCompareAndSwap32(a_comparand, a_val, a_ptr);
  return ret;
#elif defined(IL_PLATFORM_UNIX)
  return __sync_val_compare_and_swap(a_ptr, a_comparand, a_val);
#endif
}


int64_t iLAtomic_CompareAndSwap64(iLAtomic64* a_ptr, int64_t a_comparand, int64_t a_val)
{
#if defined(IL_PLATFORM_WINDOWS)
  return InterlockedCompareExchange64(a_ptr, a_val, a_comparand);
#elif defined(IL_OPERATING_SYSTEM_MACOSX)
  int64_t ret = *a_ptr;
  OSAtomicCompareAndSwap64(a_comparand, a_val, a_ptr);
  return ret;
#elif defined(IL_PLATFORM_UNIX)
  return __sync_val_compare_and_swap(a_ptr, a_comparand, a_val);
#endif
}


