aboutsummaryrefslogtreecommitdiff
path: root/lib/Utils/src/Resources
diff options
context:
space:
mode:
authorLibravatar vnugent <public@vaughnnugent.com>2024-03-30 21:36:18 -0400
committerLibravatar vnugent <public@vaughnnugent.com>2024-03-30 21:36:18 -0400
commit3ff90da4f02af47ea6d233fdd4445337ebe36452 (patch)
tree1df23f215922e8f11679f01ca847c13a15e77478 /lib/Utils/src/Resources
parent8d6b79b5ae309b36f265ba81529bcef8bfcd7414 (diff)
refactor: Updates, advanced tracing, http optimizations
Diffstat (limited to 'lib/Utils/src/Resources')
-rw-r--r--lib/Utils/src/Resources/CallbackOpenHandle.cs44
-rw-r--r--lib/Utils/src/Resources/ManagedLibrary.cs22
2 files changed, 16 insertions, 50 deletions
diff --git a/lib/Utils/src/Resources/CallbackOpenHandle.cs b/lib/Utils/src/Resources/CallbackOpenHandle.cs
deleted file mode 100644
index 625bd45..0000000
--- a/lib/Utils/src/Resources/CallbackOpenHandle.cs
+++ /dev/null
@@ -1,44 +0,0 @@
-/*
-* Copyright (c) 2022 Vaughn Nugent
-*
-* Library: VNLib
-* Package: VNLib.Utils
-* File: CallbackOpenHandle.cs
-*
-* CallbackOpenHandle.cs is part of VNLib.Utils which is part of the larger
-* VNLib collection of libraries and utilities.
-*
-* VNLib.Utils is free software: you can redistribute it and/or modify
-* it under the terms of the GNU General Public License as published
-* by the Free Software Foundation, either version 2 of the License,
-* or (at your option) any later version.
-*
-* VNLib.Utils is distributed in the hope that it will be useful,
-* but WITHOUT ANY WARRANTY; without even the implied warranty of
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-* General Public License for more details.
-*
-* You should have received a copy of the GNU General Public License
-* along with VNLib.Utils. If not, see http://www.gnu.org/licenses/.
-*/
-
-using System;
-
-namespace VNLib.Utils.Resources
-{
- /// <summary>
- /// A concrete <see cref="OpenHandle"/> for a defered operation or a resource that should be released or unwound
- /// when the instance lifetime has ended.
- /// </summary>
- public sealed class CallbackOpenHandle : OpenHandle
- {
- private readonly Action ReleaseFunc;
- /// <summary>
- /// Creates a new generic <see cref="OpenHandle"/> with the specified release callback method
- /// </summary>
- /// <param name="release">The callback function to invoke when the <see cref="OpenHandle"/> is disposed</param>
- public CallbackOpenHandle(Action release) => ReleaseFunc = release;
- ///<inheritdoc/>
- protected override void Free() => ReleaseFunc();
- }
-} \ No newline at end of file
diff --git a/lib/Utils/src/Resources/ManagedLibrary.cs b/lib/Utils/src/Resources/ManagedLibrary.cs
index 786a22d..c899156 100644
--- a/lib/Utils/src/Resources/ManagedLibrary.cs
+++ b/lib/Utils/src/Resources/ManagedLibrary.cs
@@ -1,5 +1,5 @@
/*
-* Copyright (c) 2023 Vaughn Nugent
+* Copyright (c) 2024 Vaughn Nugent
*
* Library: VNLib
* Package: VNLib.Utils
@@ -25,7 +25,6 @@
using System;
using System.IO;
using System.Linq;
-using System.Threading;
using System.Reflection;
using System.Runtime.Loader;
using System.Collections.Generic;
@@ -43,7 +42,7 @@ namespace VNLib.Utils.Resources
{
private readonly AssemblyLoadContext _loadContext;
private readonly AssemblyDependencyResolver _resolver;
- private readonly Lazy<Assembly> _lazyAssembly;
+ private readonly LazyInitializer<Assembly> _lazyAssembly;
/// <summary>
/// The absolute path to the assembly file
@@ -53,7 +52,7 @@ namespace VNLib.Utils.Resources
/// <summary>
/// The assembly that is maintained by this loader
/// </summary>
- public Assembly Assembly => _lazyAssembly.Value;
+ public Assembly Assembly => _lazyAssembly.Instance;
/// <summary>
/// Initializes a new <see cref="ManagedLibrary"/> and skips
@@ -74,11 +73,15 @@ namespace VNLib.Utils.Resources
context.ResolvingUnmanagedDll += OnNativeLibraryResolving;
//Lazy load the assembly
- _lazyAssembly = new(LoadAssembly, LazyThreadSafetyMode.PublicationOnly);
+ _lazyAssembly = new(LoadAssembly);
}
//Load the assembly into the parent context
- private Assembly LoadAssembly() => _loadContext.LoadFromAssemblyPath(AssemblyPath);
+ private Assembly LoadAssembly()
+ {
+ AdvancedTrace.WriteLine($"Loading managed library {AssemblyPath} into context {_loadContext.Name}");
+ return _loadContext.LoadFromAssemblyPath(AssemblyPath);
+ }
/// <summary>
/// Raised when the load context that owns this assembly
@@ -91,6 +94,7 @@ namespace VNLib.Utils.Resources
/// </remarks>
protected virtual void OnUnload(AssemblyLoadContext? ctx = null)
{
+ AdvancedTrace.WriteLine($"Unloading managed library {AssemblyPath}");
//Remove resolving event handlers
_loadContext.Unloading -= OnUnload;
_loadContext.Resolving -= OnDependencyResolving;
@@ -111,6 +115,8 @@ namespace VNLib.Utils.Resources
//Resolve the desired asm dependency for the current context
string? requestedDll = _resolver.ResolveUnmanagedDllToPath(libname);
+ AdvancedTrace.WriteLineIf(requestedDll != null,$"Resolving native library {libname} to path {requestedDll} for library {AssemblyPath}");
+
//if the dep is resolved, seach in the assembly directory for the manageed dll only
return requestedDll == null ?
IntPtr.Zero :
@@ -122,6 +128,8 @@ namespace VNLib.Utils.Resources
//Resolve the desired asm dependency for the current context
string? desiredAsm = _resolver.ResolveAssemblyToPath(asmName);
+ AdvancedTrace.WriteLineIf(desiredAsm != null, $"Resolving managed assembly {asmName.Name} to path {desiredAsm} for library {AssemblyPath}");
+
//If the asm exists in the dir, load it
return desiredAsm == null ? null : _loadContext.LoadFromAssemblyPath(desiredAsm);
}
@@ -137,6 +145,8 @@ namespace VNLib.Utils.Resources
//See if the type is exported
Type exp = TryGetExportedType<T>() ?? throw new EntryPointNotFoundException($"Imported assembly does not export desired type {typeof(T).FullName}");
+ AdvancedTrace.WriteLine($"Creating instance of type {exp.FullName} from assembly {AssemblyPath}");
+
//Create instance
return (T)Activator.CreateInstance(exp)!;
}