From 564ebf9799563dfd1019fbcc9121339b7f94b936 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D0=BE=D0=BD=D0=B4=D0=B0=D1=80=D0=B5=D0=B2=20=D0=98?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD?= Date: Tue, 25 Feb 2020 20:46:33 +0100 Subject: [PATCH] thread safe FixPointer (without locking) --- TestSuite/pointers12.pas | 1 - bin/Lib/PABCSystem.pas | 21 ++++++++++----------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/TestSuite/pointers12.pas b/TestSuite/pointers12.pas index 37dfab666..44efaa7c5 100644 --- a/TestSuite/pointers12.pas +++ b/TestSuite/pointers12.pas @@ -40,5 +40,4 @@ begin p := @x[2]; p^.name := 'def'; assert(p^.name = 'def'); - readln; end. \ No newline at end of file diff --git a/bin/Lib/PABCSystem.pas b/bin/Lib/PABCSystem.pas index 53fe3d307..99471197a 100644 --- a/bin/Lib/PABCSystem.pas +++ b/bin/Lib/PABCSystem.pas @@ -12507,32 +12507,31 @@ begin result := 0; end; +type NotPinnableWrapper = class + [ThreadStatic] + static notPinnableTypes: HashSet<&Type>; +end; + // ----------------------------------------------------- // Internal procedures for PABCRTL.dll: implementation // ----------------------------------------------------- var __initialized := false; - nonPinnableLock := new object; - notPinnableTypes: HashSet<&Type>; - + [System.Diagnostics.DebuggerStepThrough] function __FixPointer(obj: object): GCHandle; begin if obj <> nil then begin try - if notPinnableTypes.Contains(obj.GetType()) then + if (NotPinnableWrapper.notPinnableTypes <> nil) and NotPinnableWrapper.notPinnableTypes.Contains(obj.GetType()) then Result := GCHandle.Alloc(obj) else Result := GCHandle.Alloc(obj, GCHandleType.Pinned); except - lock nonPinnableLock do - begin - if notPinnableTypes = nil then - notPinnableTypes := new HashSet<&Type>; - notPinnableTypes.Add(obj.GetType()); - end; - + if NotPinnableWrapper.notPinnableTypes = nil then + NotPinnableWrapper.notPinnableTypes := new HashSet<&Type>; + NotPinnableWrapper.notPinnableTypes.Add(obj.GetType()); Result := GCHandle.Alloc(obj); end; end;