2023-03-14 08:02:43 +03:00
|
|
|
|
using System.Threading;
|
|
|
|
|
|
2023-03-16 19:09:10 +03:00
|
|
|
|
namespace DotRecast.Core
|
|
|
|
|
{
|
2023-05-06 05:44:57 +03:00
|
|
|
|
public class RcAtomicInteger
|
2023-03-16 19:48:49 +03:00
|
|
|
|
{
|
|
|
|
|
private volatile int _location;
|
2023-03-16 19:09:10 +03:00
|
|
|
|
|
2023-05-06 05:44:57 +03:00
|
|
|
|
public RcAtomicInteger() : this(0)
|
2023-03-16 19:48:49 +03:00
|
|
|
|
{
|
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
|
|
2023-05-06 05:44:57 +03:00
|
|
|
|
public RcAtomicInteger(int location)
|
2023-03-16 19:48:49 +03:00
|
|
|
|
{
|
|
|
|
|
_location = location;
|
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
|
public int IncrementAndGet()
|
|
|
|
|
{
|
|
|
|
|
return Interlocked.Increment(ref _location);
|
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
|
public int GetAndIncrement()
|
|
|
|
|
{
|
|
|
|
|
var next = Interlocked.Increment(ref _location);
|
|
|
|
|
return next - 1;
|
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
|
|
|
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
|
public int DecrementAndGet()
|
|
|
|
|
{
|
|
|
|
|
return Interlocked.Decrement(ref _location);
|
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
|
public int Read()
|
|
|
|
|
{
|
|
|
|
|
return _location;
|
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
|
public int GetSoft()
|
|
|
|
|
{
|
|
|
|
|
return _location;
|
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
|
public int Exchange(int exchange)
|
|
|
|
|
{
|
|
|
|
|
return Interlocked.Exchange(ref _location, exchange);
|
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
|
public int Decrease(int value)
|
|
|
|
|
{
|
|
|
|
|
return Interlocked.Add(ref _location, -value);
|
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
|
public int CompareExchange(int value, int comparand)
|
|
|
|
|
{
|
|
|
|
|
return Interlocked.CompareExchange(ref _location, value, comparand);
|
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
|
public int Add(int value)
|
|
|
|
|
{
|
|
|
|
|
return Interlocked.Add(ref _location, value);
|
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
|
}
|
|
|
|
|
}
|