Skip to content

Commit

Permalink
Removed the use of weak references.
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentparrett committed Mar 14, 2023
1 parent e1f726c commit b897221
Show file tree
Hide file tree
Showing 11 changed files with 315 additions and 890 deletions.
8 changes: 1 addition & 7 deletions VSoft.Messaging.dspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"metadata": {
"id": "VSoft.Messaging",
"version": "0.3.5",
"version": "0.3.6",
"description": "VSoft.Messaging is a libary that provides an internal synchronous/asynchronous publish/subscribe messaging system for Delphi applications.",
"authors": "Vincent Parrett",
"projectUrl": "https://github.com/VSoftTechnologies/VSoft.SemanticVersion",
Expand Down Expand Up @@ -79,12 +79,6 @@
"templates": [
{
"name": "default",
"dependencies": [
{
"id": "VSoft.WeakReference",
"version": "[0.1.3,]"
}
],
"source": [
{
"src": "src\\**\\*.pas",
Expand Down
567 changes: 150 additions & 417 deletions demos/Vcl/MsgDemo.dproj

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions demos/Vcl/MsgDemoMainForm.dfm
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ object Form2: TForm2
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
inline rx1: TMsgRecieverFrame
Left = 16
Expand Down
2 changes: 1 addition & 1 deletion demos/Vcl/MsgDemoMainForm.pas
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ procedure TForm2.thrdSendButtonClick(Sender: TObject);
FChannel.Queue.PostMessage(msg)
else
FChannel.Queue.SendMessage(msg);
// TThread.Sleep(40);
TThread.Sleep(1);
end;
end;
TThread.CreateAnonymousThread(proc).Start;
Expand Down
3 changes: 2 additions & 1 deletion demos/Vcl/MsgFrame.pas
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ procedure TMsgRecieverFrame.Init(const channel: IMessageChannel);

procedure TMsgRecieverFrame.Progress(var msg: TProgressMessage);
begin
ProgressBar1.Position := msg.Progress;
if ProgressBar1.Position <> msg.Progress then
ProgressBar1.Position := msg.Progress;

end;

Expand Down
10 changes: 5 additions & 5 deletions packages/Rad Studio 10.0 Seattle/VSoft.MessagingR.dproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@
<CfgParent>Base</CfgParent>
<Base>true</Base>
</PropertyGroup>
<PropertyGroup Condition="'$(Config)'=='Release' or '$(Cfg_2)'!=''">
<Cfg_2>true</Cfg_2>
<CfgParent>Base</CfgParent>
<Base>true</Base>
</PropertyGroup>
<PropertyGroup Condition="('$(Platform)'=='Win32' and '$(Cfg_1)'=='true') or '$(Cfg_1_Win32)'!=''">
<Cfg_1_Win32>true</Cfg_1_Win32>
<CfgParent>Cfg_1</CfgParent>
<Cfg_1>true</Cfg_1>
<Base>true</Base>
</PropertyGroup>
<PropertyGroup Condition="'$(Config)'=='Release' or '$(Cfg_2)'!=''">
<Cfg_2>true</Cfg_2>
<CfgParent>Base</CfgParent>
<Base>true</Base>
</PropertyGroup>
<PropertyGroup Condition="'$(Base)'!=''">
<SanitizedProjectName>VSoft_MessagingR</SanitizedProjectName>
<VerInfo_Locale>3081</VerInfo_Locale>
Expand Down
10 changes: 5 additions & 5 deletions packages/Rad Studio 11.0 Alexandria/VSoft.MessagingR.dpk
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ package VSoft.MessagingR;
{$EXTENDEDSYNTAX ON}
{$IMPORTEDDATA ON}
{$IOCHECKS ON}
{$LOCALSYMBOLS ON}
{$LOCALSYMBOLS OFF}
{$LONGSTRINGS ON}
{$OPENSTRINGS ON}
{$OPTIMIZATION OFF}
{$OPTIMIZATION ON}
{$OVERFLOWCHECKS OFF}
{$RANGECHECKS OFF}
{$REFERENCEINFO ON}
{$REFERENCEINFO OFF}
{$SAFEDIVIDE OFF}
{$STACKFRAMES ON}
{$STACKFRAMES OFF}
{$TYPEDADDRESS OFF}
{$VARSTRINGCHECKS ON}
{$WRITEABLECONST OFF}
{$MINENUMSIZE 1}
{$IMAGEBASE $400000}
{$DEFINE DEBUG}
{$DEFINE RELEASE}
{$ENDIF IMPLICITBUILDING}
{$DESCRIPTION 'VSoft.Messaging Runtime for 10.4'}
{$LIBSUFFIX AUTO}
Expand Down
561 changes: 137 additions & 424 deletions packages/Rad Studio 11.0 Alexandria/VSoft.MessagingR.dproj

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions src/VSoft.Messaging.Channel.pas
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ interface
uses
classes,
Generics.Collections,
VSoft.WeakReference,
VSoft.Messaging.Internal,
VSoft.Messaging;

Expand All @@ -41,7 +40,7 @@ interface
procedure PostMessage(const message : IMessage);
end;

TVSoftMessageChannel = class(TWeakReferencedObject,IMessageChannel, IMessageChannelPost)
TVSoftMessageChannel = class(TInterfacedObject,IMessageChannel, IMessageChannelPost)
private
FDispatchersLock : TObject;
FDispatchers : TList<IMessageDispatcher>;
Expand Down
37 changes: 12 additions & 25 deletions src/VSoft.Messaging.Dispatchers.pas
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ interface
System.SysUtils,
Generics.Collections,
System.SyncObjs,
VSoft.WeakReference,
VSoft.Messaging.Internal,
VSoft.Messaging;

Expand All @@ -49,7 +48,7 @@ TMessageDispatcherBase = class(TInterfacedObject,IMessageDispatcher,IMessageDi
FOwnerThreadId : TThreadID;
FTarget : TObject;
FQueue : TQueue<IMessage>;
FChannel : IWeakReference<IMessageChannel>;
FChannel : IMessageChannel;
FQueueLock : TCriticalSection;
FEnabled : boolean;
FIncludeFilter : TDictionary<Cardinal,byte>;
Expand Down Expand Up @@ -217,13 +216,9 @@ procedure TMessageDispatcherThread.Execute;
except
on e : Exception do
begin
if (FDispatcher.FChannel <> nil) and FDispatcher.FChannel.IsAlive then
begin
//this does an addref to make sure we don't get the rug pulled out.
channel := FDispatcher.FChannel.Data;
if channel <> nil then
channel := FDispatcher.FChannel; //keep the instance alive.
if (channel <> nil) then
channel.HandleError(e.Message);
end;
end;
end;
msgs[i] := nil;
Expand Down Expand Up @@ -255,7 +250,8 @@ constructor TMessageDispatcherBase.Create(const target : TObject = nil);
FQueueLock := TCriticalSection.Create;
FEnabled := True;
FChannel := nil;
FOwnerThreadId := TThread.Current.ThreadID;

FOwnerThreadId := TThread.CurrentThread.ThreadID;
end;

function TMessageDispatcherBase.DequeueAtMost(const count: integer): TArray<IMessage>;
Expand Down Expand Up @@ -305,7 +301,7 @@ procedure TMessageDispatcherBase.DoSendMessage(const Message: IMessage);

function TMessageDispatcherBase.GetChannel: IMessageChannel;
begin
result := FChannel.Data;
result := FChannel;
end;


Expand Down Expand Up @@ -348,23 +344,14 @@ procedure TMessageDispatcherBase.SetChannel(const value: IMessageChannel);
var
oldChannel : IMessageChannel;
begin
if ((FChannel <> nil) and (FChannel.Data <> value)) or ((FChannel = nil) and (value <> nil)) then
oldChannel := FChannel;
if (oldChannel <> nil) or (value <> nil) then
begin
if (oldChannel <> nil) then
oldChannel.UnSubscribe(Self);
FChannel := value;
if FChannel <> nil then
begin
if FChannel.IsAlive then
begin
oldChannel := FChannel.Data;
if oldChannel <> nil then
oldChannel.UnSubscribe(Self);
end;
end;
if value <> nil then
FChannel := TWeakReference<IMessageChannel>.Create(value)
else
FChannel := nil;
if FChannel <> nil then
FChannel.Data.Subscribe(Self);
FChannel.Subscribe(Self);
end;
end;

Expand Down
2 changes: 1 addition & 1 deletion src/VSoft.Messaging.Internal.pas
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ constructor TVSMessageWrapper<T>.Create(const AMsgData: T);
inherited Create;
Assert(SizeOf(AMsgData) > 12);
FMsgData := AMsgData;
FThreadID := TThread.Current.ThreadID;
FThreadID := TThread.CurrentThread.ThreadID;
end;

destructor TVSMessageWrapper<T>.Destroy;
Expand Down

0 comments on commit b897221

Please sign in to comment.