mirror of
https://github.com/MikuLeaks/KianaBH3.git
synced 2025-12-13 21:34:43 +01:00
Init enter game
This commit is contained in:
68
KcpSharp/Base/KcpSendReceiveBufferItemCache.cs
Normal file
68
KcpSharp/Base/KcpSendReceiveBufferItemCache.cs
Normal file
@@ -0,0 +1,68 @@
|
||||
#if NEED_LINKEDLIST_SHIM
|
||||
using LinkedListOfBufferItem = KcpSharp.NetstandardShim.LinkedList<KcpSharp.KcpSendReceiveBufferItem>;
|
||||
using LinkedListNodeOfBufferItem = KcpSharp.NetstandardShim.LinkedListNode<KcpSharp.KcpSendReceiveBufferItem>;
|
||||
#else
|
||||
using LinkedListNodeOfBufferItem =
|
||||
System.Collections.Generic.LinkedListNode<KianaBH.KcpSharp.Base.KcpSendReceiveBufferItem>;
|
||||
using LinkedListOfBufferItem =
|
||||
System.Collections.Generic.LinkedList<KianaBH.KcpSharp.Base.KcpSendReceiveBufferItem>;
|
||||
#endif
|
||||
|
||||
namespace KianaBH.KcpSharp.Base;
|
||||
|
||||
internal struct KcpSendReceiveBufferItemCache
|
||||
{
|
||||
private LinkedListOfBufferItem _items;
|
||||
private SpinLock _lock;
|
||||
|
||||
public static KcpSendReceiveBufferItemCache Create()
|
||||
{
|
||||
return new KcpSendReceiveBufferItemCache
|
||||
{
|
||||
_items = new LinkedListOfBufferItem(),
|
||||
_lock = new SpinLock()
|
||||
};
|
||||
}
|
||||
|
||||
public LinkedListNodeOfBufferItem Allocate(in KcpSendReceiveBufferItem item)
|
||||
{
|
||||
var lockAcquired = false;
|
||||
try
|
||||
{
|
||||
_lock.Enter(ref lockAcquired);
|
||||
|
||||
var node = _items.First;
|
||||
if (node is null)
|
||||
{
|
||||
node = new LinkedListNodeOfBufferItem(item);
|
||||
}
|
||||
else
|
||||
{
|
||||
_items.Remove(node);
|
||||
node.ValueRef = item;
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (lockAcquired) _lock.Exit();
|
||||
}
|
||||
}
|
||||
|
||||
public void Return(LinkedListNodeOfBufferItem node)
|
||||
{
|
||||
var lockAcquired = false;
|
||||
try
|
||||
{
|
||||
_lock.Enter(ref lockAcquired);
|
||||
|
||||
node.ValueRef = default;
|
||||
_items.AddLast(node);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (lockAcquired) _lock.Exit();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user