Просмотр исходного кода

修复删除Fiber线程竞争的bug

tanghai 2 лет назад
Родитель
Сommit
5aaae03939

+ 1 - 1
Unity/Assets/Resources/GlobalConfig.asset

@@ -12,6 +12,6 @@ MonoBehaviour:
   m_Script: {fileID: 11500000, guid: 36527db572638af47b03c805671cba75, type: 3}
   m_Name: GlobalConfig
   m_EditorClassIdentifier: 
-  CodeMode: 1
+  CodeMode: 3
   BuildType: 1
   AppType: 7

+ 2 - 0
Unity/Assets/Scripts/Core/Fiber/Fiber.cs

@@ -144,6 +144,8 @@ namespace ET
             }
             this.IsDisposed = true;
             
+            FiberManager.Instance.RemoveReal(this.Id);
+            
             this.Root.Dispose();
         }
     }

+ 11 - 4
Unity/Assets/Scripts/Core/World/Module/Fiber/FiberManager.cs

@@ -103,10 +103,17 @@ namespace ET
         
         public void Remove(int id)
         {
-            if (this.fibers.Remove(id, out Fiber fiber))
-            {
-                fiber.Dispose();
-            }
+            Fiber fiber = this.Get(id);
+            fiber.ThreadSynchronizationContext.Post(()=>{fiber.Dispose();});
+            // 这里不能dispose,因为有可能fiber还在运行,会出现线程竞争
+            //fiber.Dispose();
+        }
+        
+        internal void RemoveReal(int id)
+        {
+            this.fibers.Remove(id, out _);
+            // 这里不能dispose,因为有可能fiber还在运行,会出现线程竞争
+            //fiber.Dispose();
         }
 
         // 不允许外部调用,容易出现多线程问题, 只能通过消息通信,不允许直接获取其它Fiber引用

+ 3 - 0
Unity/Assets/Scripts/Core/World/Module/Fiber/MainThreadScheduler.cs

@@ -25,6 +25,7 @@ namespace ET
 
         public void Update()
         {
+            SynchronizationContext.SetSynchronizationContext(this.threadSynchronizationContext);
             this.threadSynchronizationContext.Update();
             
             int count = this.idQueue.Count;
@@ -46,6 +47,7 @@ namespace ET
                     continue;
                 }
                 
+                SynchronizationContext.SetSynchronizationContext(fiber.ThreadSynchronizationContext);
                 fiber.Update();
                 
                 this.idQueue.Enqueue(id);
@@ -75,6 +77,7 @@ namespace ET
 
                 this.idQueue.Enqueue(id);
 
+                SynchronizationContext.SetSynchronizationContext(fiber.ThreadSynchronizationContext);
                 fiber.LateUpdate();
             }
 

+ 1 - 1
Unity/Assets/Scripts/Hotfix/Server/Module/ActorLocation/ActorLocationSenderComponentSystem.cs

@@ -36,7 +36,7 @@ namespace ET.Server
         [EntitySystem]
         private static void Destroy(this ActorLocationSenderOneType self)
         {
-            self.Fiber().TimerComponent.Remove(ref self.CheckTimer);
+            self.Fiber().TimerComponent?.Remove(ref self.CheckTimer);
         }
 
         private static void Check(this ActorLocationSenderOneType self)

+ 1 - 1
Unity/Assets/Scripts/Hotfix/Share/Module/AI/AIComponentSystem.cs

@@ -32,7 +32,7 @@ namespace ET
         [EntitySystem]
         private static void Destroy(this AIComponent self)
         {
-            self.Fiber().TimerComponent.Remove(ref self.Timer);
+            self.Fiber().TimerComponent?.Remove(ref self.Timer);
             self.CancellationToken?.Cancel();
             self.CancellationToken = null;
             self.Current = 0;

+ 1 - 1
Unity/Assets/Scripts/Hotfix/Share/Module/Message/SessionAcceptTimeoutComponentSystem.cs

@@ -30,7 +30,7 @@ namespace ET
         [EntitySystem]
         private static void Destroy(this SessionAcceptTimeoutComponent self)
         {
-            self.Fiber().TimerComponent.Remove(ref self.Timer);
+            self.Fiber().TimerComponent?.Remove(ref self.Timer);
         }
         
     }

+ 1 - 1
Unity/Assets/Scripts/Hotfix/Share/Module/Message/SessionIdleCheckerComponentSystem.cs

@@ -30,7 +30,7 @@ namespace ET
         [EntitySystem]
         private static void Destroy(this SessionIdleCheckerComponent self)
         {
-            self.Fiber().TimerComponent.Remove(ref self.RepeatedTimer);
+            self.Fiber().TimerComponent?.Remove(ref self.RepeatedTimer);
         }
         
         public const int CheckInteral = 2000;

+ 1 - 1
Unity/Assets/Scripts/Hotfix/Share/Module/Move/MoveComponentSystem.cs

@@ -280,7 +280,7 @@ namespace ET
             self.N = 0;
             self.TurnTime = 0;
             self.IsTurnHorizontal = false;
-            self.Fiber().TimerComponent.Remove(ref self.MoveTimer);
+            self.Fiber().TimerComponent?.Remove(ref self.MoveTimer);
 
             if (self.tcs != null)
             {