Skip to content

Commit

Permalink
feat: Added Critical Sections for the lists
Browse files Browse the repository at this point in the history
  • Loading branch information
gcarreno committed Sep 25, 2023
1 parent 20ace6a commit 4f2d69c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
15 changes: 10 additions & 5 deletions src/dispatching/polykerma.dispatching.dispatcher.pas
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ interface
{ TDispatcher }
TDispatcher = class(TObject)
private
FMessagesCriticalSection: TCriticalSection;
FMessageList: TFPObjectList;
FChannelsCriticalSection: TCriticalSection;
FChannelList: TFPHashObjectList;
Expand All @@ -40,6 +39,7 @@ TDispatcher = class(TObject)
function Register(const AChannel: String; const AModule: TModule): Boolean;
procedure Run(const WaitFor: Boolean);
procedure Terminate;

published
end;
TDispatcherClass = class of TDispatcher;
Expand All @@ -52,7 +52,6 @@ constructor TDispatcher.Create;
begin
Debug({$I %FILE%}, {$I %LINE%}, 'Dispatcher Create');
// Messages
FMessagesCriticalSection:= TCriticalSection.Create;
FMessageList:= TFPObjectList.Create;
FMessageList.OwnsObjects:= True;
// Channels
Expand Down Expand Up @@ -83,7 +82,6 @@ destructor TDispatcher.Destroy;
// Messages
FMessageList.Clear;
FMessageList.Free;
FMessagesCriticalSection.Free;
inherited Destroy;
end;

Expand Down Expand Up @@ -123,7 +121,9 @@ function TDispatcher.Register(const AChannel: String;
except
on E: Exception do
begin
Error({$I %FILE%}, {$I %LINE%}, 'Dispatcher Register');
Error({$I %FILE%}, {$I %LINE%}, Format('Dispatcher Register: %s', [
E.Message
]));
Result:= False;
end;
end;
Expand All @@ -145,7 +145,12 @@ procedure TDispatcher.Post(const AMessage: TMessage);
Debug({$I %FILE%}, {$I %LINE%}, Format('Dispatcher Post: %s', [
AMessage.Channel
]));
FMessageList.Add(AMessage);
FThreadProcessMessages.MessagesCriticalSection.Acquire;
try
FMessageList.Add(AMessage);
finally
FThreadProcessMessages.MessagesCriticalSection.Release;
end;
end;

end.
Expand Down
13 changes: 8 additions & 5 deletions src/threading/polykerma.threading.threadprocessmessages.pas
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ interface
TThreadProcessMessages = class(TThread)
private
FProcedureProcessMessages: TProcedureProcessMessages;
FListCriticalSection: TCriticalSection;
FMessagesCriticalSection: TCriticalSection;
FMessageList: TFPObjectList;
protected
procedure Execute; override;
Expand All @@ -35,6 +35,9 @@ TThreadProcessMessages = class(TThread)
const CreateSuspended: Boolean
);
destructor Destroy; override;

property MessagesCriticalSection: TCriticalSection
read FMessagesCriticalSection;
published
end;
TThreadProcessingMessagesClass = class of TThreadProcessMessages;
Expand All @@ -51,15 +54,15 @@ constructor TThreadProcessMessages.Create(
begin
Debug({$I %FILE%}, {$I %LINE%}, 'Thread Process Messages Create');
FProcedureProcessMessages:= AProcedureProcessMessages;
FListCriticalSection:= TCriticalSection.Create;
FMessagesCriticalSection:= TCriticalSection.Create;
FMessageList:= AMessageList;
inherited Create(CreateSuspended);
end;

destructor TThreadProcessMessages.Destroy;
begin
Debug({$I %FILE%}, {$I %LINE%}, 'Thread Process Messages Destroy');
FListCriticalSection.Free;
FMessagesCriticalSection.Free;
inherited Destroy;
end;

Expand All @@ -70,7 +73,7 @@ procedure TThreadProcessMessages.Execute;
Debug({$I %FILE%}, {$I %LINE%}, 'Thread Process Messages Execute');
while not Terminated do
begin
FListCriticalSection.Acquire;
FMessagesCriticalSection.Acquire;
try
if FMessageList.Count > 0 then
begin
Expand All @@ -81,7 +84,7 @@ procedure TThreadProcessMessages.Execute;
end;
Sleep(1);
finally
FListCriticalSection.Release;
FMessagesCriticalSection.Release;
end;
end;
end;
Expand Down

0 comments on commit 4f2d69c

Please sign in to comment.