protected override void Main() { GpuImpl.InitSynchronizedOnce(); GpuProcessor.ProcessInit(); Console.WriteLine("GpuComponentThread.Start()"); try { while (true) { WaitHandle.WaitAny( new WaitHandle[] { GpuProcessor.DisplayListQueueUpdated, ThreadTaskQueue.EnqueuedEvent, RunningUpdatedEvent }, 200.Milliseconds()); // TODO: Should wait until the Form has created its context. ThreadTaskQueue.HandleEnqueued(); if (!Running) { break; } GpuProcessor.SetCurrent(); GpuProcessor.ProcessStep(); GpuProcessor.UnsetCurrent(); } } finally { Console.WriteLine("GpuComponentThread.End()"); } }
protected override void Main() { Thread.CurrentThread.Priority = ThreadPriority.AboveNormal; //Thread.CurrentThread.Priority = ThreadPriority.Normal; Console.WriteLine("AudioComponentThread.Start()"); try { while (true) { ThreadTaskQueue.HandleEnqueued(); if (!Running) { break; } PspAudio.Update(); Thread.Sleep(1); } PspAudio.StopSynchronized(); } finally { Console.WriteLine("AudioComponentThread.End()"); } }
public void InterlockedTest() { int threads = 4; var tasks = new ThreadTaskQueue(threads); tasks.UseSystemThreadPool = false; for (int thread = 0; thread < threads; thread++) { tasks.Add(() => { int count = 100 * 1000 * 1000; for (int i = 0; i < count; i++) { Interlocked.Increment(ref Value); } for (int i = 0; i < count; i++) { Interlocked.Decrement(ref Value); } }); } tasks.WaitForAllTasksCompleted(); Assert.Equal(0, Value); }
protected void SetupWorkQueues(int size) { _workQueues = new ThreadTaskQueue[size]; for (var i = 0; i < size; i++) { _workQueues[i] = new ThreadTaskQueue(); } }
protected virtual void SetupWorkQueues(int size) { workQueues = new ThreadTaskQueue[size]; for (int i = 0; i < size; i++) { workQueues[i] = new ThreadTaskQueue(); } }
protected virtual void StartThreadPoolWorker(ThreadTaskQueue input) { input.WorkerStartTime = Time.CurrentTimeMillis; System.Threading.ThreadPool.QueueUserWorkItem( (queue) => { #pragma warning disable S2696 // Instance members should not write to "static" fields isHystrixThreadPoolThread = true; workQueue = queue as ThreadTaskQueue; #pragma warning restore S2696 // Instance members should not write to "static" fields workQueue.ThreadStartTime = Time.CurrentTimeMillis; try { while (!shutdown) { Task item = null; workQueue.Signal.Wait(250); item = workQueue.Task; if (item != null) { try { Interlocked.Increment(ref runningTasks); TryExecuteTask(item); } catch (Exception) { // log } finally { Interlocked.Decrement(ref runningTasks); Interlocked.Increment(ref completedTasks); } workQueue.Signal.Reset(); workQueue.Task = null; } } } finally { isHystrixThreadPoolThread = false; workQueue.Signal.Reset(); workQueue.ThreadAssigned = false; Interlocked.Decrement(ref runningThreads); workQueue = null; } }, input); }
public void MultiThreadingTest() { var account = TestHelper.GetTestAccount(); var component = account.CreateRandomComponentControl(); var unitTest = component.GetOrCreateUnitTestControl("UnitTest." + Guid.NewGuid()); var tasks = new ThreadTaskQueue(10); var random = new Random(); Response firstError = null; for (int i = 0; i < 20; i++) { tasks.Add(() => { int value = 0; lock (tasks) { value = random.Next(100); } Response response = null; if (value > 50) { response = unitTest.SendResult(UnitTestResult.Success, TimeSpan.FromDays(1)); } else { response = unitTest.SendResult(UnitTestResult.Alarm, TimeSpan.FromDays(1)); } if (response.Success == false && firstError == null) { firstError = response; } }); } tasks.WaitForAllTasksCompleted(); account.GetClient().Flush(); account.SaveAllCaches(); if (firstError != null) { throw new Exception(firstError.ErrorMessage); } var actualDate = DateTime.Now.AddMinutes(30); var events = account.GetClient().ApiService.GetEvents(new GetEventsData() { OwnerId = unitTest.Info.Id, Category = EventCategory.UnitTestStatus, From = actualDate }).Data; Assert.Equal(1, events.Count);// должно быть только 1 актуальное событие }
protected virtual void StartThreadPoolWorker(ThreadTaskQueue input) { input.WorkerStartTime = Time.CurrentTimeMillis; System.Threading.ThreadPool.QueueUserWorkItem( (queue) => { isHystrixThreadPoolThread = true; workQueue = queue as ThreadTaskQueue; workQueue.ThreadStartTime = Time.CurrentTimeMillis; try { while (!this.shutdown) { Task item = null; workQueue.Signal.Wait(250); item = workQueue.Task; if (item != null) { try { Interlocked.Increment(ref this.runningTasks); TryExecuteTask(item); } catch (Exception) { // log } finally { Interlocked.Decrement(ref this.runningTasks); Interlocked.Increment(ref completedTasks); } workQueue.Signal.Reset(); workQueue.Task = null; } } } finally { isHystrixThreadPoolThread = false; workQueue.Signal.Reset(); workQueue.ThreadAssigned = false; Interlocked.Decrement(ref runningThreads); workQueue = null; } }, input); }
private void Main_Ended() { StoppedEndedEvent.Set(); // Completed execution. Wait for stopping. while (true) { ThreadTaskQueue.HandleEnqueued(); if (!Running) { return; } Thread.Sleep(1); } }
protected override void Main() { Thread.CurrentThread.Priority = ThreadPriority.AboveNormal; while (true) { ThreadTaskQueue.HandleEnqueued(); if (!Running) { return; } PspAudio.Update(); Thread.Sleep(1); } }
protected override void Main() { while (true) { ThreadTaskQueue.HandleEnqueued(); if (!Running) { return; } //Console.Error.WriteLine("Triggered!"); HleInterruptManager.GetInterruptHandler(PspInterrupts.PSP_VBLANK_INT).Trigger(); //PspDisplay.Update(); Thread.Sleep(TimeSpan.FromSeconds(1.0 / 59.94)); } }
protected override void Main() { PspEmulatorContext.GetInstance <GpuImpl>().InitSynchronizedOnce(); GpuProcessor.ProcessInit(); while (true) { ThreadTaskQueue.HandleEnqueued(); if (!Running) { return; } GpuProcessor.ProcessStep(); } }
protected override void Main() { Console.WriteLine("DisplayComponentThread.Start()"); try { var vSyncTimeIncrement = TimeSpan.FromSeconds(1.0 / (PspDisplay.HorizontalSyncHertz / (double)(PspDisplay.VsyncRow))); //var VSyncTimeIncrement = TimeSpan.FromSeconds(1.0 / (PspDisplay.HorizontalSyncHertz / (double)(PspDisplay.VsyncRow / 2))); // HACK to give more time to render! var endTimeIncrement = TimeSpan.FromSeconds(1.0 / (PspDisplay.HorizontalSyncHertz / (double)(PspDisplay.NumberOfRows))); var vBlankInterruptHandler = _hleInterruptManager.GetInterruptHandler(PspInterrupts.PspVblankInt); while (true) { //Console.WriteLine("[1]"); var startTime = DateTime.UtcNow; var vSyncTime = startTime + vSyncTimeIncrement; var endTime = startTime + endTimeIncrement; ThreadTaskQueue.HandleEnqueued(); if (!Running) { return; } // Draw time _pspDisplay.TriggerDrawStart(); ThreadUtils.SleepUntilUtc(vSyncTime); // VBlank time _pspDisplay.TriggerVBlankStart(); vBlankInterruptHandler.Trigger(); ThreadUtils.SleepUntilUtc(endTime); _pspDisplay.TriggerVBlankEnd(); } } finally { Console.WriteLine("DisplayComponentThread.End()"); } }
public void Step(Action DrawStart, Action VBlankStart, Action VBlankEnd) { //Console.WriteLine("[1]"); var startTime = DateTime.UtcNow; var vSyncTime = startTime + vSyncTimeIncrement; var endTime = startTime + endTimeIncrement; ThreadTaskQueue.HandleEnqueued(); if (!Running) { return; } // Draw time DrawStart(); ThreadUtils.SleepUntilUtc(vSyncTime); // VBlank time VBlankStart(); vBlankInterruptHandler.Trigger(); ThreadUtils.SleepUntilUtc(endTime); VBlankEnd(); }
protected override void Main() { while (Running) { #if !DO_NOT_PROPAGATE_EXCEPTIONS try #endif { // HACK! TODO: Update PspRtc every 2 thread switchings. // Note: It should update the RTC after selecting the next thread to run. // But currently is is not possible since updating the RTC and waking up // threads has secondary effects that I have to consideer first. var tickAlternate = false; //PspRtc.Update(); while (true) { ThreadTaskQueue.HandleEnqueued(); if (!Running) { return; } if (!tickAlternate) { PspRtc.Update(); } tickAlternate = !tickAlternate; HleThreadManager.StepNext(DoBeforeSelectingNext: () => { //PspRtc.Update(); }); } } #if !DO_NOT_PROPAGATE_EXCEPTIONS catch (Exception e2) { if (e2 is SceKernelSelfStopUnloadModuleException || e2.InnerException is SceKernelSelfStopUnloadModuleException) { Console.WriteLine("SceKernelSelfStopUnloadModuleException"); Main_Ended(); return; } var errorOut = Console.Error; ConsoleUtils.SaveRestoreConsoleState(() => { Console.ForegroundColor = ConsoleColor.Red; try { errorOut.WriteLine("Error on thread {0}", HleThreadManager.Current); try { errorOut.WriteLine(e2); } catch (Exception e) { Console.WriteLine(e); } HleThreadManager.Current.CpuThreadState.DumpRegisters(errorOut); errorOut.WriteLine( "Last registered PC = 0x{0:X}, RA = 0x{1:X}, RelocatedBaseAddress=0x{2:X}, UnrelocatedPC=0x{3:X}", HleThreadManager.Current.CpuThreadState.Pc, HleThreadManager.Current.CpuThreadState.Ra, ElfConfig.RelocatedBaseAddress, HleThreadManager.Current.CpuThreadState.Pc - ElfConfig.RelocatedBaseAddress ); errorOut.WriteLine("Last called syscalls: "); foreach (var calledCallback in ModuleManager.LastCalledCallbacks.Reverse()) { errorOut.WriteLine(" {0}", calledCallback); } foreach (var thread in HleThreadManager.Threads) { errorOut.WriteLine("{0}", thread.ToExtendedString()); errorOut.WriteLine( "Last valid PC: 0x{0:X} :, 0x{1:X}", thread.CpuThreadState.LastValidPc, thread.CpuThreadState.LastValidPc - ElfConfig.RelocatedBaseAddress ); thread.DumpStack(errorOut); } errorOut.WriteLine( "Executable had relocation: {0}. RelocationAddress: 0x{1:X}", ElfConfig.InfoExeHasRelocation, ElfConfig.RelocatedBaseAddress ); errorOut.WriteLine(""); errorOut.WriteLine("Error on thread {0}", HleThreadManager.Current); errorOut.WriteLine(e2); //ErrorOut.WriteLine("Saved a memory dump to 'error_memorydump.bin'", HleThreadManager.Current); //MemoryManager.Memory.Dump("error_memorydump.bin"); } catch (Exception e3) { Console.WriteLine("{0}", e3); } }); Main_Ended(); } #endif } }
protected override void Main() { while (Running) { try { while (true) { ThreadTaskQueue.HandleEnqueued(); if (!Running) { return; } PspRtc.Update(); ThreadManager.StepNext(); } } catch (Exception Exception) { if (Exception is SceKernelSelfStopUnloadModuleException || Exception.InnerException is SceKernelSelfStopUnloadModuleException) { Console.WriteLine("SceKernelSelfStopUnloadModuleException"); Main_Ended(); return; } var ErrorOut = Console.Error; ConsoleUtils.SaveRestoreConsoleState(() => { Console.ForegroundColor = ConsoleColor.Red; try { ErrorOut.WriteLine("Error on thread {0}", ThreadManager.Current); ErrorOut.WriteLine(Exception); ThreadManager.Current.CpuThreadState.DumpRegisters(ErrorOut); ErrorOut.WriteLine( "Last registered PC = 0x{0:X}, RA = 0x{1:X}", ThreadManager.Current.CpuThreadState.PC, ThreadManager.Current.CpuThreadState.RA ); foreach (var Thread in ThreadManager.Threads) { ErrorOut.WriteLine("{0}", Thread); } ErrorOut.WriteLine("Executable had relocation: {0}", PspEmulatorContext.PspConfig.InfoExeHasRelocation); } catch (Exception Exception2) { Console.WriteLine("{0}", Exception2); } }); Main_Ended(); } } }