hedgewars/uAI.pas
changeset 15365 fcdb6e3a9d36
parent 14669 fd22b6d474e7
child 15442 6031c0cfec89
--- a/hedgewars/uAI.pas	Tue Aug 27 06:05:03 2019 +0200
+++ b/hedgewars/uAI.pas	Tue Aug 27 08:41:48 2019 +0200
@@ -38,20 +38,15 @@
     CanUseAmmo: array [TAmmoType] of boolean;
     StopThinking: boolean;
     StartTicks: Longword;
-    ThinkThread: PSDL_Thread;
-    ThreadLock: PSDL_Mutex;
+    ThreadSem: PSDL_Sem;
 
 procedure FreeActionsList;
 begin
     AddFileLog('FreeActionsList called');
-    if (ThinkThread <> nil) then
-        begin
-        StopThinking:= true;
-        SDL_WaitThread(ThinkThread, nil);
-        end;
-    SDL_LockMutex(ThreadLock);
-    ThinkThread:= nil;
-    SDL_UnlockMutex(ThreadLock);
+
+    StopThinking:= true;
+    SDL_SemWait(ThreadSem);
+    SDL_SemPost(ThreadSem);
 
     if CurrentHedgehog <> nil then
         with CurrentHedgehog^ do
@@ -522,18 +517,18 @@
     end;
 
 Me^.State:= Me^.State and (not gstHHThinking);
-SDL_LockMutex(ThreadLock);
-ThinkThread:= nil;
-SDL_UnlockMutex(ThreadLock);
 Think:= 0;
+SDL_SemPost(ThreadSem);
 end;
 
 procedure StartThink(Me: PGear);
+var ThinkThread: PSDL_Thread;
 begin
 if ((Me^.State and (gstAttacking or gstHHJumping or gstMoving)) <> 0)
 or isInMultiShoot then
     exit;
 
+SDL_SemWait(ThreadSem);
 //DeleteCI(Me); // this will break demo/netplay
 
 Me^.State:= Me^.State or gstHHThinking;
@@ -556,9 +551,8 @@
 
 FillBonuses(((Me^.State and gstAttacked) <> 0) and (not isInMultiShoot) and ((GameFlags and gfInfAttack) = 0));
 
-SDL_LockMutex(ThreadLock);
 ThinkThread:= SDL_CreateThread(@Think, PChar('think'), Me);
-SDL_UnlockMutex(ThreadLock);
+SDL_DetachThread(ThinkThread);
 end;
 
 {$IFDEF DEBUGAI}
@@ -610,14 +604,13 @@
 procedure initModule;
 begin
     StartTicks:= 0;
-    ThinkThread:= nil;
-    ThreadLock:= SDL_CreateMutex();
+    ThreadSem:= SDL_CreateSemaphore(1);
 end;
 
 procedure freeModule;
 begin
     FreeActionsList();
-    SDL_DestroyMutex(ThreadLock);
+    SDL_DestroySemaphore(ThreadSem);
 end;
 
 end.