TWriteable<T>
This commit is contained in:
+19
-8
@@ -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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user