From 4f2d69c30d86940cc592afcf70c3d965e1a9f299 Mon Sep 17 00:00:00 2001 From: Gustavo Carreno Date: Mon, 25 Sep 2023 19:21:57 +0100 Subject: [PATCH] feat: Added Critical Sections for the lists --- .../polykerma.dispatching.dispatcher.pas | 15 ++++++++++----- .../polykerma.threading.threadprocessmessages.pas | 13 ++++++++----- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/dispatching/polykerma.dispatching.dispatcher.pas b/src/dispatching/polykerma.dispatching.dispatcher.pas index 00a319b..357d596 100644 --- a/src/dispatching/polykerma.dispatching.dispatcher.pas +++ b/src/dispatching/polykerma.dispatching.dispatcher.pas @@ -24,7 +24,6 @@ interface { TDispatcher } TDispatcher = class(TObject) private - FMessagesCriticalSection: TCriticalSection; FMessageList: TFPObjectList; FChannelsCriticalSection: TCriticalSection; FChannelList: TFPHashObjectList; @@ -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; @@ -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 @@ -83,7 +82,6 @@ destructor TDispatcher.Destroy; // Messages FMessageList.Clear; FMessageList.Free; - FMessagesCriticalSection.Free; inherited Destroy; end; @@ -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; @@ -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. diff --git a/src/threading/polykerma.threading.threadprocessmessages.pas b/src/threading/polykerma.threading.threadprocessmessages.pas index d210534..7a1b0a6 100644 --- a/src/threading/polykerma.threading.threadprocessmessages.pas +++ b/src/threading/polykerma.threading.threadprocessmessages.pas @@ -24,7 +24,7 @@ interface TThreadProcessMessages = class(TThread) private FProcedureProcessMessages: TProcedureProcessMessages; - FListCriticalSection: TCriticalSection; + FMessagesCriticalSection: TCriticalSection; FMessageList: TFPObjectList; protected procedure Execute; override; @@ -35,6 +35,9 @@ TThreadProcessMessages = class(TThread) const CreateSuspended: Boolean ); destructor Destroy; override; + + property MessagesCriticalSection: TCriticalSection + read FMessagesCriticalSection; published end; TThreadProcessingMessagesClass = class of TThreadProcessMessages; @@ -51,7 +54,7 @@ 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; @@ -59,7 +62,7 @@ constructor TThreadProcessMessages.Create( destructor TThreadProcessMessages.Destroy; begin Debug({$I %FILE%}, {$I %LINE%}, 'Thread Process Messages Destroy'); - FListCriticalSection.Free; + FMessagesCriticalSection.Free; inherited Destroy; end; @@ -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 @@ -81,7 +84,7 @@ procedure TThreadProcessMessages.Execute; end; Sleep(1); finally - FListCriticalSection.Release; + FMessagesCriticalSection.Release; end; end; end;