2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2009 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2013-04-18 03:09:55 +00:00
|
|
|
// Refer to the license.txt file included.
|
2010-02-19 17:05:26 +00:00
|
|
|
|
2014-02-20 03:11:52 +00:00
|
|
|
// IWYU pragma: private, include "Common/Atomic.h"
|
|
|
|
|
2014-02-10 18:54:46 +00:00
|
|
|
#pragma once
|
2010-02-19 17:05:26 +00:00
|
|
|
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Common/Common.h"
|
2015-09-28 15:57:16 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2010-02-19 17:05:26 +00:00
|
|
|
|
|
|
|
// Atomic operations are performed in a single step by the CPU. It is
|
|
|
|
// impossible for other threads to see the operation "half-done."
|
|
|
|
//
|
|
|
|
// Some atomic operations can be combined with different types of memory
|
|
|
|
// barriers called "Acquire semantics" and "Release semantics", defined below.
|
|
|
|
//
|
|
|
|
// Acquire semantics: Future memory accesses cannot be relocated to before the
|
|
|
|
// operation.
|
|
|
|
//
|
|
|
|
// Release semantics: Past memory accesses cannot be relocated to after the
|
|
|
|
// operation.
|
|
|
|
//
|
|
|
|
// These barriers affect not only the compiler, but also the CPU.
|
|
|
|
|
|
|
|
namespace Common
|
|
|
|
{
|
2014-08-30 20:14:56 +00:00
|
|
|
inline void AtomicAdd(volatile u32& target, u32 value)
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
__sync_add_and_fetch(&target, value);
|
2010-02-19 17:05:26 +00:00
|
|
|
}
|
|
|
|
|
2014-08-30 20:14:56 +00:00
|
|
|
inline void AtomicAnd(volatile u32& target, u32 value)
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
__sync_and_and_fetch(&target, value);
|
2010-02-19 17:05:26 +00:00
|
|
|
}
|
|
|
|
|
2014-08-30 20:14:56 +00:00
|
|
|
inline void AtomicDecrement(volatile u32& target)
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
__sync_add_and_fetch(&target, -1);
|
2010-08-10 08:29:15 +00:00
|
|
|
}
|
|
|
|
|
2014-08-30 20:14:56 +00:00
|
|
|
inline void AtomicIncrement(volatile u32& target)
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
__sync_add_and_fetch(&target, 1);
|
2010-02-19 17:05:26 +00:00
|
|
|
}
|
|
|
|
|
2014-08-30 20:14:56 +00:00
|
|
|
inline void AtomicOr(volatile u32& target, u32 value)
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
__sync_or_and_fetch(&target, value);
|
2010-02-19 17:05:26 +00:00
|
|
|
}
|
2013-09-22 20:07:45 +00:00
|
|
|
|
2013-09-23 19:00:53 +00:00
|
|
|
#ifndef __ATOMIC_RELAXED
|
|
|
|
#error __ATOMIC_RELAXED not defined; your compiler version is too old.
|
|
|
|
#endif
|
|
|
|
|
2013-09-22 20:07:45 +00:00
|
|
|
template <typename T>
|
2014-08-30 20:14:56 +00:00
|
|
|
inline T AtomicLoad(volatile T& src)
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
return __atomic_load_n(&src, __ATOMIC_RELAXED);
|
2013-09-22 20:07:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2014-08-30 20:14:56 +00:00
|
|
|
inline T AtomicLoadAcquire(volatile T& src)
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
return __atomic_load_n(&src, __ATOMIC_ACQUIRE);
|
2010-02-19 17:05:26 +00:00
|
|
|
}
|
|
|
|
|
2013-09-22 20:07:45 +00:00
|
|
|
template <typename T, typename U>
|
2014-08-30 20:14:56 +00:00
|
|
|
inline void AtomicStore(volatile T& dest, U value)
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
__atomic_store_n(&dest, value, __ATOMIC_RELAXED);
|
2010-02-19 17:05:26 +00:00
|
|
|
}
|
2013-09-22 20:07:45 +00:00
|
|
|
|
|
|
|
template <typename T, typename U>
|
2014-08-30 20:14:56 +00:00
|
|
|
inline void AtomicStoreRelease(volatile T& dest, U value)
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
__atomic_store_n(&dest, value, __ATOMIC_RELEASE);
|
2013-09-22 20:07:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename U>
|
2014-08-30 20:14:56 +00:00
|
|
|
inline T* AtomicExchangeAcquire(T* volatile& loc, U newval)
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
return __atomic_exchange_n(&loc, newval, __ATOMIC_ACQ_REL);
|
2010-02-19 17:05:26 +00:00
|
|
|
}
|
2019-05-05 23:48:12 +00:00
|
|
|
} // namespace Common
|