Refactoring TFuture

This commit is contained in:
Michael Schimmel
2025-06-03 13:11:59 +02:00
parent 74a7cd71e9
commit 38c9e2830d
9 changed files with 708 additions and 65 deletions
+1 -1
View File
@@ -37,7 +37,7 @@ constructor TMycInitStateFuncFuture<T>.Create( const ATaskManager: IMycTaskManag
begin
inherited Create;
FDone := State.CreateLatch( 1 );
FDone := TState.CreateLatch( 1 );
// Subscribe the job execution to AGate.
// The job will run when AGate notifies the subscriber returned by Run.
+5
View File
@@ -138,6 +138,8 @@ begin
end;
for var i := 0 to High(FWorkThreads) do
FWorkThreads[i].Start;
FWaitSemaphores.Push( TSemaphore.Create(nil, 0, 1, '') );
end;
destructor TMycTaskFactory.Destroy;
@@ -147,6 +149,9 @@ begin
for var i := High(FWorkThreads) downto 0 do
FWorkThreads[i].Free;
if Assigned(FException) then
FException.Free;
FWorkGate.Free;
FWorkStack.Clear;
inherited Destroy;
+15 -15
View File
@@ -19,7 +19,7 @@ type
property Done: IMycState read GetDone;
end;
Future<T> = record
TFuture<T> = record
strict private
FFuture: IMycFuture<T>;
function GetDone: IMycState; inline;
@@ -30,8 +30,8 @@ type
public
constructor Create(const AFuture: IMycFuture<T>);
class operator Implicit(const A: IMycFuture<T>): Future<T>; overload;
class operator Implicit(const A: Future<T>): IMycFuture<T>; overload;
class operator Implicit(const A: IMycFuture<T>): TFuture<T>; overload;
class operator Implicit(const A: TFuture<T>): IMycFuture<T>; overload;
class function Construct(const Proc: TFunc<T>): IMycFuture<T>; overload; static;
class function Construct(const Gate: IMycState; const Proc: TFunc<T>): IMycFuture<T>; overload; static;
@@ -49,64 +49,64 @@ implementation
uses
Myc.Core.Futures, Myc.Core.Tasks;
constructor Future<T>.Create(const AFuture: IMycFuture<T>);
constructor TFuture<T>.Create(const AFuture: IMycFuture<T>);
begin
FFuture := AFuture;
end;
class constructor Future<T>.CreateClass;
class constructor TFuture<T>.CreateClass;
begin
TMycTaskFactory.AquireTaskManager;
end;
class destructor Future<T>.DestroyClass;
class destructor TFuture<T>.DestroyClass;
begin
TMycTaskFactory.ReleaseTaskManager;
end;
function Future<T>.Chain<S>(const Proc: TFunc<T, S>): IMycFuture<S>;
function TFuture<T>.Chain<S>(const Proc: TFunc<T, S>): IMycFuture<S>;
begin
var Cap := FFuture;
Result := Future<S>.Construct( FFuture.Done,
Result := TFuture<S>.Construct( FFuture.Done,
function: S
begin
Result := Proc( Cap.Result );
end );
end;
class function Future<T>.Construct(const Proc: TFunc<T>): IMycFuture<T>;
class function TFuture<T>.Construct(const Proc: TFunc<T>): IMycFuture<T>;
begin
Result := TMycInitStateFuncFuture<T>.Create( TaskManager, nil, Proc );
end;
class function Future<T>.Construct(const Gate: IMycState; const Proc: TFunc<T>): IMycFuture<T>;
class function TFuture<T>.Construct(const Gate: IMycState; const Proc: TFunc<T>): IMycFuture<T>;
begin
Result := TMycInitStateFuncFuture<T>.Create( TaskManager, Gate, Proc );
end;
function Future<T>.GetDone: IMycState;
function TFuture<T>.GetDone: IMycState;
begin
Result := FFuture.Done;
end;
function Future<T>.GetResult: T;
function TFuture<T>.GetResult: T;
begin
Result := FFuture.Result;
end;
function Future<T>.WaitFor: T;
function TFuture<T>.WaitFor: T;
begin
TaskManager.WaitFor( FFuture.Done );
Result := FFuture.Result;
end;
class operator Future<T>.Implicit(const A: IMycFuture<T>): Future<T>;
class operator TFuture<T>.Implicit(const A: IMycFuture<T>): TFuture<T>;
begin
Result.Create( A );
end;
class operator Future<T>.Implicit(const A: Future<T>): IMycFuture<T>;
class operator TFuture<T>.Implicit(const A: TFuture<T>): IMycFuture<T>;
begin
Result := A.FFuture;
end;
+38 -17
View File
@@ -7,7 +7,7 @@ uses
type
IMycSubscriber = interface
// Interface for an entity that can be notified by a State or State.
// Interface for an entity that can be notified by a TState or TState.
// Returns true if this subscriber expects further notifications from the source, false otherwise.
function Notify: Boolean;
end;
@@ -21,21 +21,21 @@ type
function GetState: IMycState;
public
constructor Create( const AState: IMycState; ATag: Pointer );
// Unsubscribes from the associated State.
// Unsubscribes from the associated TState.
procedure Unsubscribe;
class operator Initialize( out Dest: TMycSubscription );
property State: IMycState read GetState;
property TState: IMycState read GetState;
end;
IMycState = interface
// Represents a subscribable state that can be queried.
// Represents a subscribable TState that can be queried.
{$REGION 'property access'}
function GetIsSet: Boolean;
{$ENDREGION}
// Subscribes a given subscriber to this State.
// Subscribes a given subscriber to this TState.
function Subscribe( Subscriber: IMycSubscriber ): TMycSubscription;
procedure Unsubscribe( Tag: Pointer );
// IsSet is true if the state has been reached or the condition is met.
// IsSet is true if the TState has been reached or the condition is met.
property IsSet: Boolean read GetIsSet;
end;
@@ -48,25 +48,29 @@ type
// Inherits IMycSubscriber and State property from IMycFlag. No new members.
end;
// IMycDirty represents a resettable flag, typically indicating if a state is "dirty" (requiring attention) or "clean".
// It inherits from IMycFlag, meaning it has a state, can be notified, and subscribed to.
// IMycDirty represents a resettable flag, typically indicating if a State is "dirty" (requiring attention) or "clean".
// It inherits from IMycFlag, meaning it has a State, can be notified, and subscribed to.
IMycDirty = interface( IMycSubscriber )
// Provides access to the IMycState interface of the flag.
function GetState: IMycState;
// Resets the flag to its "clean" (not set / not dirty) state.
// Resets the flag to its "clean" (not set / not dirty) State.
// Returns true if the flag was actually dirty before this reset, false otherwise.
function Reset: Boolean;
property State: IMycState read GetState;
end;
State = record
TState = record
private
FState: IMycState;
class function GetNull: IMycState; static;
public
constructor Create(const AState: IMycState);
class function CreateLatch( Count: Integer ): IMycLatch; static;
class function CreateDirty: IMycDirty; static;
class function All( const States: TArray<IMycState> ): IMycState; static;
class function Any( const States: TArray<IMycState> ): IMycState; static;
class operator Implicit(const A: IMycState): TState; overload;
class operator Implicit(const A: TState): IMycState; overload;
class property Null: IMycState read GetNull;
end;
@@ -103,14 +107,21 @@ begin
Dest.FTag := nil;
end;
{ State }
{ TState }
class function State.CreateLatch( Count: Integer ): IMycLatch;
constructor TState.Create(const AState: IMycState);
begin
FState := AState;
if not Assigned(FState) then
FState := TMycState.Null;
end;
class function TState.CreateLatch( Count: Integer ): IMycLatch;
begin
Result := TMycLatch.CreateLatch( Count );
end;
class function State.All( const States: TArray<IMycState> ): IMycState;
class function TState.All( const States: TArray<IMycState> ): IMycState;
var
Latch: IMycLatch;
begin
@@ -120,13 +131,13 @@ begin
Result := Latch.State;
end;
class function State.Any( const States: TArray<IMycState> ): IMycState;
class function TState.Any( const States: TArray<IMycState> ): IMycState;
var
Latch: IMycLatch;
begin
if Length( States ) = 0 then
begin
Result := State.Null;
Result := TState.Null;
end
else
begin
@@ -137,14 +148,24 @@ begin
end;
end;
class function State.CreateDirty: IMycDirty;
class function TState.CreateDirty: IMycDirty;
begin
Result := TMycDirty.CreateDirty;
end;
class function State.GetNull: IMycState;
class function TState.GetNull: IMycState;
begin
Result := TMycState.Null;
end;
class operator TState.Implicit(const A: TState): IMycState;
begin
Result := A.FState;
end;
class operator TState.Implicit(const A: IMycState): TState;
begin
Result.Create( A );
end;
end.
+64
View File
@@ -8,6 +8,7 @@ uses
type
IMycTaskManager = interface
// TOD Dokumentation
function CreateTask( const Gate: IMycState; const Proc: TProc ): TMycSubscription;
// Waits for the operation associated with State to complete.
@@ -21,6 +22,69 @@ type
var
TaskManager: IMycTaskManager;
procedure SetupTaskManagerMock;
implementation
uses
System.Generics.Collections;
type
TMycExecMock = class(TInterfacedObject, IMycSubscriber)
private
FProc: TProc;
public
constructor Create(const AProc: TProc);
function Notify: Boolean;
end;
TMycTaskManagerMock = class(TInterfacedObject, IMycTaskManager)
public
// IMycTaskManager
function CreateTask(const Gate: IMycState; const Proc: TProc): TMycSubscription;
procedure WaitFor(State: IMycState);
constructor Create;
end;
{ TMycExecMock }
constructor TMycExecMock.Create(const AProc: TProc);
begin
inherited Create;
FProc := AProc;
end;
function TMycExecMock.Notify: Boolean;
begin
if Assigned(FProc) then
begin
FProc();
FProc := nil;
end;
end;
{ TMycTaskManagerMock }
constructor TMycTaskManagerMock.Create;
begin
inherited Create;
end;
function TMycTaskManagerMock.CreateTask(const Gate: IMycState; const Proc: TProc): TMycSubscription;
begin
var s: IMycState := TState(Gate);
Result := s.Subscribe( TMycExecMock.Create( Proc ) );
end;
procedure TMycTaskManagerMock.WaitFor(State: IMycState);
begin
Assert( State.IsSet );
end;
procedure SetupTaskManagerMock;
begin
TaskManager := TMycTaskManagerMock.Create;
end;
end.