let's try to kill the thread right away and protect the thread variable with a mutex
authorkoda
Sat, 13 Apr 2013 20:34:30 +0200
changeset 8908 d3033e3a4113
parent 8907 7e9c6efe90fd
child 8909 95542e198bc8
let's try to kill the thread right away and protect the thread variable with a mutex
hedgewars/uAI.pas
--- a/hedgewars/uAI.pas	Sat Apr 13 20:13:52 2013 +0200
+++ b/hedgewars/uAI.pas	Sat Apr 13 20:34:30 2013 +0200
@@ -36,15 +36,18 @@
 var BestActions: TActions;
     CanUseAmmo: array [TAmmoType] of boolean;
     StopThinking: boolean;
-    ThinkThread: PSDL_Thread = nil;
     StartTicks: Longword;
+    ThinkThread: PSDL_Thread;
+    ThreadLock: PSDL_Mutex;
 
 procedure FreeActionsList;
 begin
     AddFileLog('FreeActionsList called');
+    SDL_LockMutex(ThreadLock);
     if (ThinkThread <> nil) then
-        SDL_WaitThread(ThinkThread, nil);
-    ThinkThread:=nil;
+        SDL_KillThread(ThinkThread);
+    ThinkThread:= nil;
+    SDL_UnlockMutex(ThreadLock);
 
     with CurrentHedgehog^ do
         if Gear <> nil then
@@ -360,7 +363,7 @@
     switchImmediatelyAvailable: boolean;
     Actions: TActions;
 begin
-AddFileLog('Thread started');
+AddFileLog('Think thread started');
 dmgMod:= 0.01 * hwFloat2Float(cDamageModifier) * cDamagePercent;
 StartTicks:= GameTicks;
 currHedgehogIndex:= CurrentTeam^.CurrHedgehog;
@@ -440,7 +443,9 @@
     end;
 
 Me^.State:= Me^.State and (not gstHHThinking);
+SDL_LockMutex(ThreadLock);
 ThinkThread:= nil;
+SDL_UnlockMutex(ThreadLock);
 Think:= 0;
 end;
 
@@ -471,8 +476,10 @@
     end;
 
 FillBonuses((Me^.State and gstAttacked) <> 0);
-AddFileLog('Enter Think Thread');
+
+SDL_LockMutex(ThreadLock);
 ThinkThread:= SDL_CreateThread(@Think{$IFDEF SDL13}, 'think'{$ENDIF}, Me);
+SDL_UnlockMutex(ThreadLock);
 end;
 
 //var scoreShown: boolean = false;
@@ -519,14 +526,13 @@
 begin
     StartTicks:= 0;
     ThinkThread:= nil;
+    ThreadLock:= SDL_CreateMutex();
 end;
 
 procedure freeModule;
 begin
-    if (ThinkThread <> nil) then
-        SDL_KillThread(ThinkThread);
-    ThinkThread:= nil;
     FreeActionsList();
+    SDL_DestroyMutex(ThreadLock);
 end;
 
 end.