浏览代码

* Added plain calls for semaphore-related functionality, this removes need to maintain a second copy of thread manager in unis/tthread.inc.

git-svn-id: trunk@27665 -
sergei 11 年之前
父节点
当前提交
d7c863185f
共有 3 个文件被更改,包括 33 次插入33 次删除
  1. 20 0
      rtl/inc/thread.inc
  2. 5 0
      rtl/inc/threadh.inc
  3. 8 33
      rtl/unix/tthread.inc

+ 20 - 0
rtl/inc/thread.inc

@@ -325,6 +325,26 @@ begin
   currenttm.rtleventWaitForTimeout(state,timeout);
   currenttm.rtleventWaitForTimeout(state,timeout);
 end;
 end;
 
 
+function SemaphoreInit: pointer;
+begin
+  result:=currenttm.SemaphoreInit();
+end;
+
+procedure SemaphoreDestroy(const sem: pointer);
+begin
+  currenttm.SemaphoreDestroy(sem);
+end;
+
+procedure SemaphoreWait(const sem: pointer);
+begin
+  currenttm.SemaphoreWait(sem);
+end;
+
+procedure SemaphorePost(const sem: pointer);
+begin
+  currenttm.SemaphorePost(sem);
+end;
+
 { ---------------------------------------------------------------------
 { ---------------------------------------------------------------------
     ThreadManager which gives run-time error. Use if no thread support.
     ThreadManager which gives run-time error. Use if no thread support.
   ---------------------------------------------------------------------}
   ---------------------------------------------------------------------}

+ 5 - 0
rtl/inc/threadh.inc

@@ -175,3 +175,8 @@ procedure RTLeventResetEvent(state:pRTLEvent);
 procedure RTLeventWaitFor(state:pRTLEvent);
 procedure RTLeventWaitFor(state:pRTLEvent);
 procedure RTLeventWaitFor(state:pRTLEvent;timeout : longint);
 procedure RTLeventWaitFor(state:pRTLEvent;timeout : longint);
 
 
+function SemaphoreInit: Pointer;
+procedure SemaphoreDestroy(const sem: Pointer);
+procedure SemaphoreWait(const sem: Pointer);
+procedure SemaphorePost(const sem: Pointer);
+

+ 8 - 33
rtl/unix/tthread.inc

@@ -51,33 +51,11 @@
 
 
 var
 var
   ThreadsInited: boolean = false;
   ThreadsInited: boolean = false;
-  CurrentTM: TThreadManager;
 const
 const
   // stupid, considering its not even implemented...
   // stupid, considering its not even implemented...
   Priorities: array [TThreadPriority] of Integer =
   Priorities: array [TThreadPriority] of Integer =
    (-20,-19,-10,0,9,18,19);
    (-20,-19,-10,0,9,18,19);
 
 
-procedure InitThreads;
-begin
-  { This is not thread safe, but it doesn't matter if this is executed }
-  { multiple times. Conversely, if one thread goes by this without the }
-  { operation having been finished by another thread already, it will  }
-  { use an uninitialised thread manager -> leave as it is              }
-  if not ThreadsInited then
-    begin
-      GetThreadManager(CurrentTM);
-{$ifdef FPC_HAS_MEMBAR}
-      { however, we have to ensure that a thread never sees ThreadsInited }
-      { as true while CurrentTM hasn't been initialised yet               }
-      WriteBarrier;
-      ThreadsInited := True;
-{$endif}
-    end
-  else
-    { See double checked lock example at                         }
-    { http://ridiculousfish.com/blog/archives/2007/02/17/barrier }
-    ReadDependencyBarrier;
-end;
 
 
 procedure DoneThreads;
 procedure DoneThreads;
 begin
 begin
@@ -105,7 +83,7 @@ begin
     if LThread.FInitialSuspended then
     if LThread.FInitialSuspended then
       begin
       begin
         WRITE_DEBUG('thread ', ptruint(LThread), ' waiting for semaphore ', ptruint(LThread.FSem));
         WRITE_DEBUG('thread ', ptruint(LThread), ' waiting for semaphore ', ptruint(LThread.FSem));
-        CurrentTM.SemaphoreWait(LThread.FSem);
+        SemaphoreWait(LThread.FSem);
         if not(LThread.FTerminated) then
         if not(LThread.FTerminated) then
           begin
           begin
             if not LThread.FSuspended then
             if not LThread.FSuspended then
@@ -125,7 +103,7 @@ begin
        begin
        begin
          LThread.FSuspendedInternal := true;
          LThread.FSuspendedInternal := true;
          WRITE_DEBUG('waiting for SuspendedInternal - ', LThread.ClassName);
          WRITE_DEBUG('waiting for SuspendedInternal - ', LThread.ClassName);
-        CurrentTM.SemaphoreWait(LThread.FSem);
+         SemaphoreWait(LThread.FSem);
          CurrentThreadVar := LThread;
          CurrentThreadVar := LThread;
          WRITE_DEBUG('going into LThread.Execute - ', LThread.ClassName);
          WRITE_DEBUG('going into LThread.Execute - ', LThread.ClassName);
          LThread.Execute;
          LThread.Execute;
@@ -170,10 +148,7 @@ end;
 procedure TThread.SysCreate(CreateSuspended: Boolean;
 procedure TThread.SysCreate(CreateSuspended: Boolean;
                             const StackSize: SizeUInt);
                             const StackSize: SizeUInt);
 begin
 begin
-  // lets just hope that the user doesn't create a thread
-  // via BeginThread and creates the first TThread Object in there!
-  InitThreads;
-  FSem := CurrentTM.SemaphoreInit();
+  FSem := SemaphoreInit();
   if FSem = pointer(-1) then
   if FSem = pointer(-1) then
     raise EThread.create('Semaphore init failed (possibly too many concurrent threads)');
     raise EThread.create('Semaphore init failed (possibly too many concurrent threads)');
   WRITE_DEBUG('thread ', ptruint(self), ' created semaphore ', ptruint(FSem));
   WRITE_DEBUG('thread ', ptruint(self), ' created semaphore ', ptruint(FSem));
@@ -199,7 +174,7 @@ begin
   if (FHandle = TThreadID(0)) then
   if (FHandle = TThreadID(0)) then
   { another exception in constructor }
   { another exception in constructor }
     begin
     begin
-      CurrentTM.SemaphoreDestroy(FSem);
+      SemaphoreDestroy(FSem);
       exit;
       exit;
     end;
     end;
   if (FThreadID = GetCurrentThreadID) then
   if (FThreadID = GetCurrentThreadID) then
@@ -225,7 +200,7 @@ begin
           WaitFor;
           WaitFor;
         end;
         end;
     end;
     end;
-  CurrentTM.SemaphoreDestroy(FSem);
+  SemaphoreDestroy(FSem);
   FFatalException.Free;
   FFatalException.Free;
   FFatalException := nil;
   FFatalException := nil;
   { threadvars have been released by cthreads.ThreadMain -> DoneThread, or  }
   { threadvars have been released by cthreads.ThreadMain -> DoneThread, or  }
@@ -248,7 +223,7 @@ begin
     begin
     begin
       if not FSuspended and
       if not FSuspended and
          (InterLockedExchange(longint(FSuspended),longint(longbool(true))) = longint(longbool(false))) then
          (InterLockedExchange(longint(FSuspended),longint(longbool(true))) = longint(longbool(false))) then
-        CurrentTM.SemaphoreWait(FSem)
+        SemaphoreWait(FSem)
     end
     end
   else
   else
     begin
     begin
@@ -264,7 +239,7 @@ begin
   if FSuspendedInternal and (InterLockedExchange(longint(FSuspendedInternal),ord(false)) = longint(longbool(true))) then
   if FSuspendedInternal and (InterLockedExchange(longint(FSuspendedInternal),ord(false)) = longint(longbool(true))) then
     begin
     begin
       WRITE_DEBUG('resuming thread after TThread construction',ptruint(self));
       WRITE_DEBUG('resuming thread after TThread construction',ptruint(self));
-      CurrentTM.SemaphorePost(FSem);
+      SemaphorePost(FSem);
     end
     end
   else if (not FSuspendedExternal) then
   else if (not FSuspendedExternal) then
     begin
     begin
@@ -274,7 +249,7 @@ begin
          (InterLockedExchange(longint(FSuspended),longint(false)) <> longint(longbool(false))) then
          (InterLockedExchange(longint(FSuspended),longint(false)) <> longint(longbool(false))) then
         begin
         begin
           WRITE_DEBUG('resuming ',ptruint(self));
           WRITE_DEBUG('resuming ',ptruint(self));
-          CurrentTM.SemaphorePost(FSem);
+          SemaphorePost(FSem);
         end
         end
     end
     end
   else
   else