public void ContextSwitch() { ContextSwitchNeeded = false; if (CurrentThread != null) { CoreManager.GetThread(CurrentThread.Context.Work).Reset(); } CurrentThread = SelectedThread; if (CurrentThread != null) { CurrentThread.ClearExclusive(); CoreManager.GetThread(CurrentThread.Context.Work).Set(); CurrentThread.Context.Execute(); } }
public void ContextSwitch() { lock (CoreContexts) { if (MultiCoreScheduling) { int SelectedCount = 0; for (int Core = 0; Core < KScheduler.CpuCoresCount; Core++) { KCoreContext CoreContext = CoreContexts[Core]; if (CoreContext.ContextSwitchNeeded && (CoreContext.CurrentThread?.Thread.IsCurrentThread() ?? false)) { CoreContext.ContextSwitch(); } if (CoreContext.CurrentThread?.Thread.IsCurrentThread() ?? false) { SelectedCount++; } } if (SelectedCount == 0) { CoreManager.GetThread(Thread.CurrentThread).Pause(); } else if (SelectedCount == 1) { CoreManager.GetThread(Thread.CurrentThread).Unpause(); } else { throw new InvalidOperationException("Thread scheduled in more than one core!"); } } else { KThread CurrentThread = CoreContexts[CurrentCore].CurrentThread; bool HasThreadExecuting = CurrentThread != null; if (HasThreadExecuting) { //This is not the thread that is currently executing, we need //to request an interrupt to allow safely starting another thread. if (!CurrentThread.Thread.IsCurrentThread()) { CurrentThread.Thread.RequestInterrupt(); return; } CoreManager.GetThread(CurrentThread.Thread.Work).Pause(); } //Advance current core and try picking a thread, //keep advancing if it is null. for (int Core = 0; Core < 4; Core++) { CurrentCore = (CurrentCore + 1) % CpuCoresCount; KCoreContext CoreContext = CoreContexts[CurrentCore]; CoreContext.UpdateCurrentThread(); if (CoreContext.CurrentThread != null) { CoreContext.CurrentThread.ClearExclusive(); CoreManager.GetThread(CoreContext.CurrentThread.Thread.Work).Unpause(); CoreContext.CurrentThread.Thread.Execute(); break; } } //If nothing was running before, then we are on a "external" //HLE thread, we don't need to wait. if (!HasThreadExecuting) { return; } } } CoreManager.GetThread(Thread.CurrentThread).Wait(); }