TWriteable<T>

This commit is contained in:
Michael Schimmel
2025-06-24 19:11:29 +02:00
parent 3797507d95
commit a65a5f2b0a
4 changed files with 140 additions and 48 deletions
+19 -8
View File
@@ -36,7 +36,7 @@ type
constructor Create(const AChanged: TSignal; const AProc: TFunc<T>);
end;
TMycWriteableMutable<T> = class(TInterfacedObject, TMutable<T>.IMutable, TMutable<T>.IWriteable)
TMycWriteableMutable<T> = class(TInterfacedObject, TMutable<T>.IMutable, TWriteable<T>.IWriteable)
private
FValue: T;
FChanged: TEvent;
@@ -77,7 +77,7 @@ type
constructor Create(const AChanged: TSignal.ISignal; const AProc: TFunc<T>);
end;
TMycProtectedWriteableMutable<T> = class(TInterfacedObject, TMutable<T>.IMutable, TMutable<T>.IWriteable)
TMycProtectedWriteableMutable<T> = class(TInterfacedObject, TMutable<T>.IMutable, TWriteable<T>.IWriteable)
private
FValue: T;
FChanged: TEvent;
@@ -85,6 +85,8 @@ type
protected
function GetChanged: TSignal;
function GetValue: T;
procedure Lock; inline;
procedure Release; inline;
public
constructor Create(const AValue: T);
procedure SetValue(const Value: T);
@@ -234,24 +236,33 @@ end;
function TMycProtectedWriteableMutable<T>.GetValue: T;
begin
while AtomicExchange(FLock, 1) = 1 do
YieldProcessor;
Lock;
try
Result := FValue;
finally
AtomicExchange(FLock, 0);
Release;
end;
end;
procedure TMycProtectedWriteableMutable<T>.Lock;
begin
while AtomicExchange(FLock, 1) = 1 do
YieldProcessor;
end;
procedure TMycProtectedWriteableMutable<T>.Release;
begin
AtomicExchange(FLock, 0);
end;
procedure TMycProtectedWriteableMutable<T>.SetValue(const Value: T);
begin
while AtomicExchange(FLock, 1) = 1 do
YieldProcessor;
Lock;
try
FValue := Value;
FChanged.Notify;
finally
AtomicExchange(FLock, 0);
Release;
end;
end;