瀏覽代碼

* lazy thread initialization support

git-svn-id: trunk@47308 -
ondrej 4 年之前
父節點
當前提交
c64429cdd0
共有 7 個文件被更改,包括 50 次插入2 次删除
  1. 2 0
      rtl/beos/bethreads.pp
  2. 28 0
      rtl/inc/thread.inc
  3. 4 0
      rtl/inc/threadh.inc
  4. 2 0
      rtl/netware/systhrd.inc
  5. 2 0
      rtl/netwlibc/systhrd.inc
  6. 6 1
      rtl/unix/cthreads.pp
  7. 6 1
      rtl/win/systhrd.inc

+ 2 - 0
rtl/beos/bethreads.pp

@@ -168,6 +168,8 @@ Uses
            pthread_key_create(@TLSKey,nil);
            pthread_key_create(@TLSKey,nil);
            InitThreadVars(@CRelocateThreadvar);
            InitThreadVars(@CRelocateThreadvar);
 {$endif HASTHREADVAR}
 {$endif HASTHREADVAR}
+          { lazy initialize thread support }
+           LazyInitThreading;
            IsMultiThread:=true;
            IsMultiThread:=true;
          end;
          end;
         { the only way to pass data to the newly created thread
         { the only way to pass data to the newly created thread

+ 28 - 0
rtl/inc/thread.inc

@@ -349,6 +349,34 @@ begin
   currenttm.RTLEventWaitForTimeout(state,timeout);
   currenttm.RTLEventWaitForTimeout(state,timeout);
 end;
 end;
 
 
+{ ---------------------------------------------------------------------
+    lazy thread initialization support
+  ---------------------------------------------------------------------}
+
+var
+  LazyInitThreadingProcs : array of TProcedure = nil;
+procedure RegisterLazyInitThreadingProc(const proc: TProcedure);
+begin
+  if IsMultiThread then
+    begin
+    { multithreading is already enabled - execute directly }
+    proc();
+    end
+  else
+    begin
+    SetLength(LazyInitThreadingProcs,Length(LazyInitThreadingProcs)+1);
+    LazyInitThreadingProcs[high(LazyInitThreadingProcs)]:=proc;
+    end;
+end;
+
+procedure LazyInitThreading;
+var
+  i: Integer;
+begin
+  for i:=0 to high(LazyInitThreadingProcs) do
+    LazyInitThreadingProcs[i]();
+end;
+
 { ---------------------------------------------------------------------
 { ---------------------------------------------------------------------
     ThreadManager which gives run-time error. Use if no thread support.
     ThreadManager which gives run-time error. Use if no thread support.
   ---------------------------------------------------------------------}
   ---------------------------------------------------------------------}

+ 4 - 0
rtl/inc/threadh.inc

@@ -183,3 +183,7 @@ procedure RTLEventResetEvent(state:pRTLEvent);
 procedure RTLEventWaitFor(state:pRTLEvent);
 procedure RTLEventWaitFor(state:pRTLEvent);
 procedure RTLEventWaitFor(state:pRTLEvent;timeout : longint);
 procedure RTLEventWaitFor(state:pRTLEvent;timeout : longint);
 
 
+{ lazy thread initialization support }
+procedure RegisterLazyInitThreadingProc(const proc: TProcedure);
+{ do not call LazyInitThreading directly}
+procedure LazyInitThreading;

+ 2 - 0
rtl/netware/systhrd.inc

@@ -156,6 +156,8 @@ function SysBeginThread(sa : Pointer;stacksize : SizeUInt;
      if not IsMultiThread then
      if not IsMultiThread then
      begin
      begin
        InitThreadVars(@SysRelocateThreadvar);
        InitThreadVars(@SysRelocateThreadvar);
+      { lazy initialize thread support }
+       LazyInitThreading;
        IsMultithread:=true;
        IsMultithread:=true;
      end;
      end;
      { the only way to pass data to the newly created thread }
      { the only way to pass data to the newly created thread }

+ 2 - 0
rtl/netwlibc/systhrd.inc

@@ -133,6 +133,8 @@
           { We're still running in single thread mode, setup the TLS }
           { We're still running in single thread mode, setup the TLS }
            pthread_key_create(@TLSKey,nil);
            pthread_key_create(@TLSKey,nil);
            InitThreadVars(@SysRelocateThreadvar);
            InitThreadVars(@SysRelocateThreadvar);
+          { lazy initialize thread support }
+           LazyInitThreading;
            IsMultiThread:=true;
            IsMultiThread:=true;
          end;
          end;
         { the only way to pass data to the newly created thread
         { the only way to pass data to the newly created thread

+ 6 - 1
rtl/unix/cthreads.pp

@@ -357,7 +357,12 @@ Type  PINTRTLEvent = ^TINTRTLEvent;
       { Initialize multithreading if not done }
       { Initialize multithreading if not done }
       if not TLSInitialized then
       if not TLSInitialized then
         InitCTLS;
         InitCTLS;
-      IsMultiThread:=true;
+      if not IsMultiThread then
+        begin
+          { We're still running in single thread mode, lazy initialize thread support }
+           LazyInitThreading;
+           IsMultiThread:=true;
+        end;
 
 
       { the only way to pass data to the newly created thread
       { the only way to pass data to the newly created thread
         in a MT safe way, is to use the heap }
         in a MT safe way, is to use the heap }

+ 6 - 1
rtl/win/systhrd.inc

@@ -256,7 +256,12 @@ var
 {$endif DEBUG_MT}
 {$endif DEBUG_MT}
         { Initialize multithreading if not done }
         { Initialize multithreading if not done }
         SysInitTLS;
         SysInitTLS;
-        IsMultiThread:=true;
+        if not IsMultiThread then
+          begin
+           { lazy initialize thread support }
+            LazyInitThreading;
+            IsMultiThread:=true;
+          end;
 
 
         { the only way to pass data to the newly created thread
         { the only way to pass data to the newly created thread
           in a MT safe way, is to use the heap }
           in a MT safe way, is to use the heap }