From cee712105850ac3385cd0091a923438167433f9f Mon Sep 17 00:00:00 2001 From: TSR Berry <20988865+TSRBerry@users.noreply.github.com> Date: Sat, 8 Apr 2023 01:22:00 +0200 Subject: Move solution and projects to src --- .../BackgroundContextWorker.cs | 91 ++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 src/Ryujinx.Graphics.OpenGL/BackgroundContextWorker.cs (limited to 'src/Ryujinx.Graphics.OpenGL/BackgroundContextWorker.cs') diff --git a/src/Ryujinx.Graphics.OpenGL/BackgroundContextWorker.cs b/src/Ryujinx.Graphics.OpenGL/BackgroundContextWorker.cs new file mode 100644 index 00000000..764ea715 --- /dev/null +++ b/src/Ryujinx.Graphics.OpenGL/BackgroundContextWorker.cs @@ -0,0 +1,91 @@ +using Ryujinx.Common; +using System; +using System.Collections.Generic; +using System.Threading; + +namespace Ryujinx.Graphics.OpenGL +{ + unsafe class BackgroundContextWorker : IDisposable + { + [ThreadStatic] + public static bool InBackground; + private Thread _thread; + private bool _running; + + private AutoResetEvent _signal; + private Queue _work; + private ObjectPool _invokePool; + private readonly IOpenGLContext _backgroundContext; + + public BackgroundContextWorker(IOpenGLContext backgroundContext) + { + _backgroundContext = backgroundContext; + _running = true; + + _signal = new AutoResetEvent(false); + _work = new Queue(); + _invokePool = new ObjectPool(() => new ManualResetEventSlim(), 10); + + _thread = new Thread(Run); + _thread.Start(); + } + + private void Run() + { + InBackground = true; + + _backgroundContext.MakeCurrent(); + + while (_running) + { + Action action; + + lock (_work) + { + _work.TryDequeue(out action); + } + + if (action != null) + { + action(); + } + else + { + _signal.WaitOne(); + } + } + + _backgroundContext.Dispose(); + } + + public void Invoke(Action action) + { + ManualResetEventSlim actionComplete = _invokePool.Allocate(); + + lock (_work) + { + _work.Enqueue(() => + { + action(); + actionComplete.Set(); + }); + } + + _signal.Set(); + + actionComplete.Wait(); + actionComplete.Reset(); + + _invokePool.Release(actionComplete); + } + + public void Dispose() + { + _running = false; + _signal.Set(); + + _thread.Join(); + _signal.Dispose(); + } + } +} \ No newline at end of file -- cgit v1.2.3