Changed various locking mechanism.

This commit is contained in:
Tom
2025-03-29 20:18:09 +00:00
parent c80f1f2aa0
commit c7904f120d
10 changed files with 282 additions and 143 deletions

View File

@@ -14,7 +14,7 @@ namespace HermesSocketServer.Socket.Handlers
private readonly long[] _array;
private readonly ILogger _logger;
private readonly object _lock;
private readonly Mutex _lock;
private int _index;
public ChatterHandler(Database database, ILogger logger)
@@ -24,7 +24,7 @@ namespace HermesSocketServer.Socket.Handlers
_chatters = new HashSet<long>(CHATTER_BUFFER_SIZE);
_array = new long[CHATTER_BUFFER_SIZE];
_index = -1;
_lock = new object();
_lock = new Mutex();
}
public async Task Execute<T>(WebSocketUser sender, T message, HermesSocketManager sockets)
@@ -32,8 +32,9 @@ namespace HermesSocketServer.Socket.Handlers
if (message is not ChatterMessage data || sender.Id == null)
return;
lock (_lock)
try
{
_lock.WaitOne();
if (_chatters.Contains(data.Id))
return;
@@ -43,11 +44,16 @@ namespace HermesSocketServer.Socket.Handlers
_index = -1;
var previous = _array[++_index];
if (previous != 0) {
if (previous != 0)
{
_chatters.Remove(previous);
}
_array[_index] = data.Id;
}
finally
{
_lock.ReleaseMutex();
}
try
{