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

增加一个主线程的ThreadSynchronizationContext

tanghai 2 лет назад
Родитель
Сommit
8aa16879fc

+ 9 - 15
Unity/Assets/Scripts/Core/Fiber/Fiber.cs

@@ -33,17 +33,17 @@ namespace ET
         
         public int Process { get; }
         
-        public EntitySystem EntitySystem { get; private set; }
-        public TimeInfo TimeInfo { get; private set; }
+        public EntitySystem EntitySystem { get; }
+        public TimeInfo TimeInfo { get; }
         public IdGenerater IdGenerater { get; private set; }
         public Mailboxes Mailboxes { get; private set; }
-        public ThreadSynchronizationContext ThreadSynchronizationContext { get; private set; }
-        public TimerComponent TimerComponent { get; private set; }
-        public CoroutineLockComponent CoroutineLockComponent { get; private set; }
+        public ThreadSynchronizationContext ThreadSynchronizationContext { get; }
+        public TimerComponent TimerComponent { get; }
+        public CoroutineLockComponent CoroutineLockComponent { get; }
 
         private readonly Queue<ETTask> frameFinishTasks = new();
         
-        public Fiber(int id, int process, int zone, SceneType sceneType, string name)
+        internal Fiber(int id, int process, int zone, SceneType sceneType, string name)
         {
             this.Id = id;
             this.Process = process;
@@ -54,11 +54,11 @@ namespace ET
             this.Mailboxes = new Mailboxes();
             this.TimerComponent = new TimerComponent(this.TimeInfo);
             this.CoroutineLockComponent = new CoroutineLockComponent(this.TimerComponent);
-            this.ThreadSynchronizationContext = new();
+            this.ThreadSynchronizationContext = new ThreadSynchronizationContext();
             this.Root = new Scene(this, id, 1, sceneType, name);
         }
 
-        public void Update()
+        internal void Update()
         {
             try
             {
@@ -74,7 +74,7 @@ namespace ET
             }
         }
         
-        public void LateUpdate()
+        internal void LateUpdate()
         {
             try
             {
@@ -112,12 +112,6 @@ namespace ET
             this.IsDisposed = true;
             
             this.Root.Dispose();
-            //this.ThreadSynchronizationContext = null;
-            //this.CoroutineLockComponent = null;
-            //this.TimerComponent = null;
-            //this.IdGenerater = null;
-            //this.TimeInfo = null;
-            //this.EntitySystem = null;
         }
     }
 }

+ 1 - 0
Unity/Assets/Scripts/Core/World/Module/Fiber/FiberManager.cs

@@ -66,6 +66,7 @@ namespace ET
             try
             {
                 Fiber fiber = new(fiberId, Options.Instance.Process, zone, sceneType, name);
+                
                 this.fibers[fiber.Id] = fiber;
                 this.schedulers[(int) schedulerType].Add(fiberId);
                 

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

@@ -9,9 +9,11 @@ namespace ET
         private readonly ConcurrentQueue<int> idQueue = new();
         private readonly ConcurrentQueue<int> addIds = new();
         private readonly FiberManager fiberManager;
+        private readonly ThreadSynchronizationContext threadSynchronizationContext = new();
 
         public MainThreadScheduler(FiberManager fiberManager)
         {
+            SynchronizationContext.SetSynchronizationContext(this.threadSynchronizationContext);
             this.fiberManager = fiberManager;
         }
 
@@ -23,6 +25,8 @@ namespace ET
 
         public void Update()
         {
+            this.threadSynchronizationContext.Update();
+            
             int count = this.idQueue.Count;
             while (count-- > 0)
             {
@@ -41,8 +45,7 @@ namespace ET
                 {
                     continue;
                 }
-
-                SynchronizationContext.SetSynchronizationContext(fiber.ThreadSynchronizationContext);
+                
                 fiber.Update();
                 
                 this.idQueue.Enqueue(id);
@@ -72,7 +75,6 @@ namespace ET
 
                 this.idQueue.Enqueue(id);
 
-                SynchronizationContext.SetSynchronizationContext(fiber.ThreadSynchronizationContext);
                 fiber.LateUpdate();
             }
 

+ 0 - 2
Unity/Assets/Scripts/Model/Share/Entry.cs

@@ -52,9 +52,7 @@ namespace ET
             
             World.Instance.AddSingleton<FiberManager>();
             
-            // 注意这里创建Main Fiber 服务端没有设置同步上下文,会导致await后面的代码回调到了其它线程,一定不要在这个await后面写代码
             await FiberManager.Instance.Create(SchedulerType.Main, ConstFiberId.Main, 0, SceneType.Main, "");
-            // 注意下面不能加代码!!!!!!!!
         }
     }
 }