Skip to content

Commit

Permalink
move thread checking from ULPLatform to Renderer instance
Browse files Browse the repository at this point in the history
related #60
Add GC.KeepAlive to ULApp
  • Loading branch information
SupinePandora43 committed Jul 7, 2022
1 parent 9c808ba commit 9e2b4ec
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 28 deletions.
50 changes: 43 additions & 7 deletions src/UltralightNet.AppCore/ULApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ public ULApp(IntPtr ptr, bool dispose = false)
public ULApp(ULSettings settings, ULConfig config = default)
{
Ptr = AppCoreMethods.ulCreateApp(settings, config);
ULPlatform.thread = Thread.CurrentThread;
}

public unsafe void SetUpdateCallback(ULUpdateCallback callback, IntPtr userData = default)
Expand All @@ -76,16 +75,53 @@ public unsafe void SetUpdateCallback(ULUpdateCallback callback, IntPtr userData
SetUpdateCallback((delegate* unmanaged[Cdecl]<void*, void>)null, (void*)userData);
}
}
public unsafe void SetUpdateCallback(delegate* unmanaged[Cdecl]<void*, void> callback, void* userData = null) => AppCoreMethods.ulAppSetUpdateCallback(Ptr, callback, userData);
public unsafe void SetUpdateCallback(delegate* unmanaged[Cdecl]<void*, void> callback, void* userData = null)
{
AppCoreMethods.ulAppSetUpdateCallback(Ptr, callback, userData);
GC.KeepAlive(this);
}

public bool IsRunning => AppCoreMethods.ulAppIsRunning(Ptr);
public bool IsRunning
{
get
{
var returnValue = AppCoreMethods.ulAppIsRunning(Ptr);
GC.KeepAlive(this);
return returnValue;
}
}

public ULMonitor MainMonitor => new(AppCoreMethods.ulAppGetMainMonitor(Ptr));
public ULMonitor MainMonitor
{
get
{
ULMonitor returnValue = new(AppCoreMethods.ulAppGetMainMonitor(Ptr));
GC.KeepAlive(this);
return returnValue;
}
}

public Renderer Renderer => Renderer.FromHandle(AppCoreMethods.ulAppGetRenderer(Ptr), false);
public Renderer Renderer
{
get
{
var returnValue = Renderer.FromHandle(AppCoreMethods.ulAppGetRenderer(Ptr), false);
returnValue.ThreadId = Thread.CurrentThread.ManagedThreadId;
GC.KeepAlive(this);
return returnValue;
}
}

public void Run() => AppCoreMethods.ulAppRun(Ptr);
public void Quit() => AppCoreMethods.ulAppQuit(Ptr);
public void Run()
{
AppCoreMethods.ulAppRun(Ptr);
GC.KeepAlive(this);
}
public void Quit()
{
AppCoreMethods.ulAppQuit(Ptr);
GC.KeepAlive(this);
}

~ULApp() => Dispose();

Expand Down
2 changes: 1 addition & 1 deletion src/UltralightNet/INativeContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ private unsafe Handle(void* _value)
}
public unsafe abstract class INativeContainer<TSelf> : IDisposable where TSelf : INativeContainer<TSelf>, INativeContainerInterface<TSelf>, IEquatable<TSelf>
{
private Handle<TSelf> _ptr;
protected Handle<TSelf> _ptr;
internal virtual Handle<TSelf> Handle { get => !IsDisposed ? _ptr : throw new ObjectDisposedException(nameof(TSelf)); init => _ptr = value; }
public bool IsDisposed { get; protected set; }
protected bool Owns { get; init; } = true;
Expand Down
21 changes: 12 additions & 9 deletions src/UltralightNet/Renderer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
using UltralightNet.LowStuff;

namespace UltralightNet;
Expand All @@ -14,9 +15,8 @@ public static unsafe partial class Methods
//[GeneratedDllImport(LibUltralight)]
public static Handle<Renderer> ulCreateRenderer(in ULConfig config)
{
_ULConfig nativeConfig = new(config);
using _ULConfig nativeConfig = new(config);
var ret = ulCreateRenderer(&nativeConfig);
nativeConfig.Dispose();
return ret;
}

Expand All @@ -42,21 +42,24 @@ public static Handle<Renderer> ulCreateRenderer(in ULConfig config)

public class Renderer : INativeContainer<Renderer>, INativeContainerInterface<Renderer>, IEquatable<Renderer>
{
private readonly Handle<Renderer> _handle;
internal override Handle<Renderer> Handle
{
get
{
static void Throw() => throw new ObjectDisposedException(nameof(Renderer));
if (IsDisposed) Throw();
ULPlatform.CheckThread();
return _handle;
AssertNotWrongThread();
return base.Handle;
}
init => _handle = value;
init => base.Handle = value;
}

private Renderer() { }

internal int ThreadId { get; set; } = -1;
internal void AssertNotWrongThread() // hungry
{
if (ThreadId is not -1 or int.MaxValue && ThreadId != Thread.CurrentThread.ManagedThreadId) throw new AggregateException("Wrong thread.");
}

public View CreateView(uint width, uint height) => CreateView(width, height, new ULViewConfig());
public View CreateView(uint width, uint height, ULViewConfig viewConfig) => CreateView(width, height, viewConfig, DefaultSession);
public View CreateView(uint width, uint height, ULViewConfig viewConfig, Session session) => CreateView(width, height, viewConfig, session, true);
Expand Down Expand Up @@ -103,7 +106,7 @@ public override void Dispose()
{
if (IsDisposed || !Owns) return;
Methods.ulDestroyRenderer(Handle);
ULPlatform.thread = null;
GC.KeepAlive(this);
base.Dispose();
}

Expand Down
1 change: 0 additions & 1 deletion src/UltralightNet/ULBitmap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ public IntPtr Ptr
{
static void Throw() => throw new ObjectDisposedException(nameof(ULBitmap));
if (IsDisposed) Throw();
ULPlatform.CheckThread();
return _ptr;
}
init => _ptr = value;
Expand Down
12 changes: 3 additions & 9 deletions src/UltralightNet/ULPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,6 @@ public static void Free()
public static bool ErrorGPUDriverNotSet { get; set; } = true;

public static bool ErrorWrongThread { get; set; } = true;
[Obsolete]
internal static Thread? thread;
internal static void CheckThread()
{
if (thread is null || !ErrorWrongThread) return;
if (Thread.CurrentThread != thread) throw new InvalidOperationException($"{nameof(ULPlatform.ErrorWrongThread)}: Use of ultralight api from a different thread.");
}

private static ULLogger _logger;
internal static ULFileSystem _filesystem;
Expand Down Expand Up @@ -324,7 +317,8 @@ public static Renderer CreateRenderer(ULConfig config, bool dispose = true)
LogMessage = (ULLogLevel level, string message) => { foreach (ReadOnlySpan<char> line in new LineEnumerator(message.AsSpan())) { Console.WriteLine($"(UL) {level}: {line.ToString()}"); } }
};
}
thread = Thread.CurrentThread;
return Renderer.FromHandle((Handle<Renderer>)Methods.ulCreateRenderer(config), true);
var returnValue = Renderer.FromHandle((Handle<Renderer>)Methods.ulCreateRenderer(config), true);
returnValue.ThreadId = Thread.CurrentThread.ManagedThreadId;
return returnValue;
}
}
2 changes: 1 addition & 1 deletion src/UltralightNet/View.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public IntPtr Ptr
{
static void Throw() => throw new ObjectDisposedException(nameof(View));
if (IsDisposed) Throw();
ULPlatform.CheckThread();
Renderer?.AssertNotWrongThread();
return (IntPtr)_ptr;
}
private set => _ptr = value.ToPointer();
Expand Down

0 comments on commit 9e2b4ec

Please sign in to comment.