Pārlūkot izejas kodu

* Implemented late thread manager initialization. Thread manager code is not included to executable if thread stuff are not used. WinCE executable size is reduced by 4KB, Win32 executable size is reduced by 3KB. No testsuite regressions.

git-svn-id: trunk@9557 -
yury 17 gadi atpakaļ
vecāks
revīzija
0dcad4d822

+ 0 - 1
rtl/amiga/system.pp

@@ -385,7 +385,6 @@ begin
   InOutRes:=0;
 { Arguments }
   GenerateArgs;
-  InitSystemThreads;
   initvariantmanager;
   initwidestringmanager;
 end.

+ 0 - 1
rtl/beos/system.pp

@@ -411,7 +411,6 @@ begin
   SysInitStdIO;
 { Reset IO Error }
   InOutRes:=0;
-  InitSystemThreads;
 {$ifdef HASVARIANT}
   initvariantmanager;
 {$endif HASVARIANT}

+ 0 - 2
rtl/bsd/system.pp

@@ -294,8 +294,6 @@ Begin
   InOutRes:=0;
   { Arguments }
   SetupCmdLine;
-  { threading }
-  InitSystemThreads;
   initvariantmanager;
   initwidestringmanager;
 End.

+ 0 - 5
rtl/embedded/system.pp

@@ -298,11 +298,6 @@ begin
   InOutRes:=0;
 {$endif FPC_HAS_FEATURE_CONSOLEIO}
 
-{$ifdef FPC_HAS_FEATURE_THREADING}
-  { threading }
-  InitSystemThreads;
-{$endif FPC_HAS_FEATURE_THREADING}
-
 {$ifdef FPC_HAS_FEATURE_VARIANTS}
   initvariantmanager;
 {$endif FPC_HAS_FEATURE_VARIANTS}

+ 0 - 2
rtl/emx/system.pas

@@ -577,8 +577,6 @@ begin
     { no I/O-Error }
     inoutres:=0;
 
-    InitSystemThreads;
-
     InitVariantManager;
 
 {$ifdef HASWIDESTRING}

+ 0 - 2
rtl/gba/system.pp

@@ -146,7 +146,5 @@ begin
   SysInitStdIO;
 { Reset IO Error }
   InOutRes:=0;
-{ Arguments }
-  InitSystemThreads;
   initvariantmanager;
 end.

+ 0 - 3
rtl/go32v2/system.pp

@@ -656,9 +656,6 @@ Begin
    AllFilesMask := '*.*';
 { Reset IO Error }
   InOutRes:=0;
-{$ifdef FPC_HAS_FEATURE_THREADING}
-  InitSystemThreads;
-{$endif}
 {$ifdef  EXCEPTIONS_IN_SYSTEM}
   InitDPMIExcp;
   InstallDefaultHandlers;

+ 30 - 14
rtl/inc/heap.inc

@@ -395,6 +395,22 @@ procedure finish_waitvarlist(loc_freelists: pfreelists); forward;
 function  try_finish_waitfixedlist(loc_freelists: pfreelists): boolean; forward;
 procedure try_finish_waitvarlist(loc_freelists: pfreelists); forward;
 
+
+var
+  fTM: TThreadManager; external name 'fCurrentTM';
+
+procedure do_heap_lock;
+begin
+  if Assigned(fTM.EnterCriticalSection) then
+    fTM.EnterCriticalSection(heap_lock);
+end;
+
+procedure do_heap_unlock;
+begin
+  if Assigned(fTM.LeaveCriticalSection) then
+    fTM.LeaveCriticalSection(heap_lock);
+end;
+
 {*****************************************************************************
                                 List adding/removal
 *****************************************************************************}
@@ -734,7 +750,7 @@ begin
   if not assigned(poc) and (assigned(orphaned_freelists.waitfixed) 
       or assigned(orphaned_freelists.waitvar) or (orphaned_freelists.oscount > 0)) then
     begin
-      entercriticalsection(heap_lock);
+      do_heap_lock;
       finish_waitfixedlist(@orphaned_freelists);
       finish_waitvarlist(@orphaned_freelists);
       if orphaned_freelists.oscount > 0 then
@@ -758,7 +774,7 @@ begin
               loc_freelists^.oslist_all := poc;
             end;
         end;
-      leavecriticalsection(heap_lock);
+      do_heap_unlock;
     end;
   if poc = nil then
     begin
@@ -1019,18 +1035,18 @@ end;
 
 procedure waitfree_fixed(pmc: pmemchunk_fixed; poc: poschunk);
 begin
-  entercriticalsection(heap_lock);
+  do_heap_lock;
   pmc^.next_fixed := poc^.freelists^.waitfixed;
   poc^.freelists^.waitfixed := pmc;
-  leavecriticalsection(heap_lock);
+  do_heap_unlock;
 end;
 
 procedure waitfree_var(pmcv: pmemchunk_var);
 begin
-  entercriticalsection(heap_lock);
+  do_heap_lock;
   pmcv^.next_var := pmcv^.freelists^.waitvar;
   pmcv^.freelists^.waitvar := pmcv;
-  leavecriticalsection(heap_lock);
+  do_heap_unlock;
 end;
 
 function SysFreeMem_Fixed(loc_freelists: pfreelists; pmc: pmemchunk_fixed): ptruint;
@@ -1141,9 +1157,9 @@ function try_finish_waitfixedlist(loc_freelists: pfreelists): boolean;
 begin
   if loc_freelists^.waitfixed = nil then 
     exit(false);
-  entercriticalsection(heap_lock);
+  do_heap_lock;
   finish_waitfixedlist(loc_freelists);
-  leavecriticalsection(heap_lock);
+  do_heap_unlock;
   result := true;
 end;
 
@@ -1165,9 +1181,9 @@ procedure try_finish_waitvarlist(loc_freelists: pfreelists);
 begin
   if loc_freelists^.waitvar = nil then 
     exit;
-  entercriticalsection(heap_lock);
+  do_heap_lock;
   finish_waitvarlist(loc_freelists);
-  leavecriticalsection(heap_lock);
+  do_heap_unlock;
 end;
 
 {*****************************************************************************
@@ -1431,7 +1447,7 @@ begin
   loc_freelists := @freelists;
   if main_relo_freelists <> nil then
   begin
-    entercriticalsection(heap_lock);
+    do_heap_lock;
     finish_waitfixedlist(loc_freelists);
     finish_waitvarlist(loc_freelists);
 {$ifdef HAS_SYSOSFREE}
@@ -1463,9 +1479,9 @@ begin
         orphaned_freelists.oslist_all := loc_freelists^.oslist_all;
       end;
     end;
-    leavecriticalsection(heap_lock);
-    if main_relo_freelists = loc_freelists then
-      donecriticalsection(heap_lock);
+    do_heap_unlock;
+    if (main_relo_freelists = loc_freelists) and Assigned(fTM.DoneCriticalSection) then
+      fTM.DoneCriticalSection(heap_lock);
   end;
 {$ifdef SHOW_MEM_USAGE}
   writeln('Max heap used/size: ', loc_freelists^.internal_status.maxheapused, '/', 

+ 15 - 6
rtl/inc/thread.inc

@@ -15,7 +15,16 @@
 
 
 Var
-  CurrentTM : TThreadManager;
+  fCurrentTM : TThreadManager; public;
+  
+    Procedure InitSystemThreads; forward;
+    
+    function CurrentTM : TThreadManager;
+      begin
+        if not Assigned(fCurrentTM.BeginThread) then
+          InitSystemThreads;
+        Result:=fCurrentTM;
+      end;
 
 {*****************************************************************************
                            Threadvar initialization
@@ -212,13 +221,13 @@ Function SetThreadManager(Const NewTM : TThreadManager) : Boolean;
 
 begin
   Result:=True;
-  If Assigned(CurrentTM.DoneManager) then
-    Result:=CurrentTM.DoneManager();
+  If Assigned(fCurrentTM.DoneManager) then
+    Result:=fCurrentTM.DoneManager();
   If Result then
     begin
-    CurrentTM:=NewTM;
-    If Assigned(CurrentTM.InitManager) then
-      Result:=CurrentTM.InitManager();
+    fCurrentTM:=NewTM;
+    If Assigned(fCurrentTM.InitManager) then
+      Result:=fCurrentTM.InitManager();
     end;
 end;
 

+ 0 - 2
rtl/linux/system.pp

@@ -333,8 +333,6 @@ begin
   SysInitExecPath;
   { Reset IO Error }
   InOutRes:=0;
-  { threading }
-  InitSystemThreads;
   initvariantmanager;
   initwidestringmanager;
 end.

+ 0 - 1
rtl/macos/system.pp

@@ -553,7 +553,6 @@ begin
   { Reset IO Error }
   InOutRes:=0;
   errno:=0;
-  InitSystemThreads;
   initvariantmanager;
   initwidestringmanager;
 

+ 0 - 1
rtl/morphos/system.pp

@@ -417,7 +417,6 @@ begin
   InOutRes:=0;
 { Arguments }
   GenerateArgs;
-  InitSystemThreads;
   initvariantmanager;
   initwidestringmanager;
 end.

+ 0 - 2
rtl/nds/system.pp

@@ -173,7 +173,5 @@ begin
   SysInitStdIO;
 { Reset IO Error }
   InOutRes:=0;
-{ Arguments }
-  InitSystemThreads;
   initvariantmanager;
 end.

+ 0 - 1
rtl/netware/system.pp

@@ -476,7 +476,6 @@ Begin
   IsLibrary := FALSE;
   IsConsole := TRUE;
   ExitCode  := 0;
-  InitSystemThreads;
   initvariantmanager;
   initwidestringmanager;
 End.

+ 0 - 1
rtl/netwlibc/system.pp

@@ -546,7 +546,6 @@ Begin
 {Delphi Compatible}
   IsConsole := TRUE;
   ExitCode  := 0;
-  InitSystemThreads;
   initvariantmanager;
   initwidestringmanager;
 End.

+ 0 - 1
rtl/os2/system.pas

@@ -1189,7 +1189,6 @@ begin
     DefaultCreator := '';
     DefaultFileType := '';
 
-    InitSystemThreads;
     InitVariantManager;
 
 {$ifdef HASWIDESTRING}

+ 0 - 1
rtl/solaris/system.pp

@@ -229,7 +229,6 @@ Begin
   InOutRes:=0;
 { Arguments }
   SetupCmdLine;
-  InitSystemThreads;
   initvariantmanager;
   initwidestringmanager;
 End.

+ 0 - 2
rtl/win32/system.pp

@@ -1219,8 +1219,6 @@ begin
   { Reset IO Error }
   InOutRes:=0;
   ProcessID := GetCurrentProcessID;
-  { threading }
-  InitSystemThreads;
   { Reset internal error variable }
   errno:=0;
   initvariantmanager;

+ 0 - 2
rtl/win64/system.pp

@@ -1170,8 +1170,6 @@ begin
   { Reset IO Error }
   InOutRes:=0;
   ProcessID := GetCurrentProcessID;
-  { threading }
-  InitSystemThreads;
   { Reset internal error variable }
   errno:=0;
   initvariantmanager;

+ 0 - 2
rtl/wince/system.pp

@@ -1812,8 +1812,6 @@ initialization
   { Reset IO Error }
   InOutRes:=0;
   ProcessID := GetCurrentProcessID;
-  { threading }
-  InitSystemThreads;
   { Reset internal error variable }
   errno:=0;
   initvariantmanager;