Added thread-safe random class

This commit is contained in:
Thorsten Sommer 2024-07-30 20:22:43 +02:00
parent 2cea53d2a9
commit b74fd644ff
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
5 changed files with 93 additions and 4 deletions

View File

@ -1,6 +1,7 @@
using AIStudio.Chat;
using AIStudio.Provider;
using AIStudio.Settings;
using AIStudio.Tools;
using Microsoft.AspNetCore.Components;
@ -15,7 +16,7 @@ public abstract partial class AssistantBase : ComponentBase
protected IJSRuntime JsRuntime { get; init; } = null!;
[Inject]
protected Random RNG { get; set; } = null!;
protected ThreadSafeRandom RNG { get; init; } = null!;
protected abstract string Title { get; }

View File

@ -22,7 +22,7 @@ public partial class Workspaces : ComponentBase
private IDialogService DialogService { get; set; } = null!;
[Inject]
public Random RNG { get; set; } = null!;
private ThreadSafeRandom RNG { get; init; } = null!;
[Parameter]
public ChatThread? CurrentChatThread { get; set; }

View File

@ -25,7 +25,7 @@ public partial class Chat : MSGComponentBase, IAsyncDisposable
public IJSRuntime JsRuntime { get; init; } = null!;
[Inject]
public Random RNG { get; set; } = null!;
private ThreadSafeRandom RNG { get; init; } = null!;
[Inject]
public IDialogService DialogService { get; set; } = null!;

View File

@ -29,7 +29,7 @@ builder.Services.AddSingleton(MessageBus.INSTANCE);
builder.Services.AddSingleton<Rust>();
builder.Services.AddMudMarkdownClipboardService<MarkdownClipboardService>();
builder.Services.AddSingleton<SettingsManager>();
builder.Services.AddSingleton<Random>();
builder.Services.AddSingleton<ThreadSafeRandom>();
builder.Services.AddHostedService<UpdateService>();
builder.Services.AddHostedService<TemporaryChatService>();
builder.Services.AddRazorComponents()

View File

@ -0,0 +1,88 @@
namespace AIStudio.Tools;
/// <inheritdoc />
public sealed class ThreadSafeRandom : Random
{
private static readonly object LOCK = new();
#region Overrides of Random
/// <inheritdoc />
public override int Next()
{
lock (LOCK)
return base.Next();
}
/// <inheritdoc />
public override int Next(int maxValue)
{
lock (LOCK)
return base.Next(maxValue);
}
/// <inheritdoc />
public override int Next(int minValue, int maxValue)
{
lock (LOCK)
return base.Next(minValue, maxValue);
}
/// <inheritdoc />
public override void NextBytes(byte[] buffer)
{
lock (LOCK)
base.NextBytes(buffer);
}
/// <inheritdoc />
public override void NextBytes(Span<byte> buffer)
{
lock (LOCK)
base.NextBytes(buffer);
}
/// <inheritdoc />
public override double NextDouble()
{
lock (LOCK)
return base.NextDouble();
}
/// <inheritdoc />
public override long NextInt64()
{
lock (LOCK)
return base.NextInt64();
}
/// <inheritdoc />
public override long NextInt64(long maxValue)
{
lock (LOCK)
return base.NextInt64(maxValue);
}
/// <inheritdoc />
public override long NextInt64(long minValue, long maxValue)
{
lock (LOCK)
return base.NextInt64(minValue, maxValue);
}
/// <inheritdoc />
public override float NextSingle()
{
lock (LOCK)
return base.NextSingle();
}
/// <inheritdoc />
protected override double Sample()
{
lock (LOCK)
return base.Sample();
}
#endregion
}