Jelajahi Sumber

* fix for Mantis #37252: apply patch by Bi0T1N to implement TThread.NameThreadForDebugging for Mac OS X 10.6 and newer (older versions don't provide the required functions)

git-svn-id: trunk@49323 -
svenbarth 4 tahun lalu
induk
melakukan
d0b1402e10
2 mengubah file dengan 41 tambahan dan 0 penghapusan
  1. 21 0
      rtl/darwin/pthread.inc
  2. 20 0
      rtl/unix/cthreads.pp

+ 21 - 0
rtl/darwin/pthread.inc

@@ -84,3 +84,24 @@ function pthread_mutexattr_destroy(_para1:Ppthread_mutexattr_t):cint;cdecl;exter
 function pthread_mutexattr_gettype(_para1:Ppthread_mutexattr_t; _para2:Pcint):cint;cdecl;external 'c' name 'pthread_mutexattr_gettype';
 function pthread_mutexattr_settype(_para1:Ppthread_mutexattr_t; _para2:cint):cint;cdecl;external 'c' name 'pthread_mutexattr_settype';
 function pthread_cond_timedwait(__cond:ppthread_cond_t; __mutex:ppthread_mutex_t; __abstime:ptimespec):cint; cdecl;external 'c' name 'pthread_cond_timedwait';
+
+var
+  // available in macOS 10.6 / iOS 3.2 and higher
+  pthread_setname_np: function(name: PAnsiChar):cint;cdecl;
+
+var
+  PthreadDLL: Pointer;
+
+function LoadPthreads: Boolean;
+begin
+  PThreadDLL:=DlOpen('libSystem.dylib',RTLD_LAZY);
+  Result:=PThreadDLL<>Nil;
+  if not Result then
+    exit;
+  Pointer(pthread_setname_np):=dlsym(PthreadDLL,'pthread_setname_np');
+end;
+
+function UnLoadPthreads: Boolean;
+begin
+  Result:=dlclose(PThreadDLL)=0;
+end;

+ 20 - 0
rtl/unix/cthreads.pp

@@ -60,6 +60,10 @@ interface
  {$endif darwin}
 {$endif}
 
+{$if defined(Darwin) or defined(iphonesim)}
+  {$define dynpthreads}
+{$endif darwin}
+
 {$define basicevents_with_pthread_cond}
 
 Procedure SetCThreadManager;
@@ -544,6 +548,15 @@ Type  PINTRTLEvent = ^TINTRTLEvent;
           pthread_setname_np(pthread_t(threadHandle), @CuttedName[1]);
         end;
       end;
+{$elseif defined(Darwin) or defined(iphonesim)}
+  {$ifdef dynpthreads}
+      if Assigned(pthread_setname_np) then
+  {$endif dynpthreads}
+      begin
+        // only allowed to set from within the thread
+        if threadHandle=TThreadID(-1) then
+          pthread_setname_np(@ThreadName[1]);
+      end;
 {$else}
        {$Warning SetThreadDebugName needs to be implemented}
 {$endif}
@@ -559,6 +572,13 @@ Type  PINTRTLEvent = ^TINTRTLEvent;
       begin
         CSetThreadDebugNameA(threadHandle, AnsiString(ThreadName));
       end;
+{$elseif defined(Darwin) or defined(iphonesim)}
+  {$ifdef dynpthreads}
+      if Assigned(pthread_setname_np) then
+  {$endif dynpthreads}
+      begin
+        CSetThreadDebugNameA(threadHandle, AnsiString(ThreadName));
+      end;
 {$else}
        {$Warning SetThreadDebugName needs to be implemented}
 {$endif}