초기커밋
This commit is contained in:
54
ServerCore/Sync/Counter.cs
Normal file
54
ServerCore/Sync/Counter.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
||||
|
||||
namespace ServerCore;
|
||||
|
||||
public class Counter
|
||||
{
|
||||
private uint m_count = 0;
|
||||
|
||||
private readonly uint m_min;
|
||||
private readonly uint m_max;
|
||||
|
||||
public Counter(uint min = uint.MinValue, uint max = uint.MaxValue)
|
||||
{
|
||||
m_min = min;
|
||||
m_max = max;
|
||||
}
|
||||
|
||||
public uint incCount()
|
||||
{
|
||||
var value = Volatile.Read(ref m_count);
|
||||
if(value >= m_max)
|
||||
{
|
||||
m_count = m_max;
|
||||
return m_count;
|
||||
}
|
||||
|
||||
return Interlocked.Increment(ref m_count);
|
||||
}
|
||||
|
||||
public uint decCount()
|
||||
{
|
||||
var value = Volatile.Read(ref m_count);
|
||||
if (value <= m_min)
|
||||
{
|
||||
m_count = m_min;
|
||||
return m_count;
|
||||
}
|
||||
|
||||
return Interlocked.Decrement(ref m_count);
|
||||
}
|
||||
|
||||
public uint getCount()
|
||||
{
|
||||
return Volatile.Read(ref m_count);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user