@@ -8,42 +8,41 @@ namespace TedToolkit.CppInteropGen;
88
99public sealed class NativeFunctionLoader : IDisposable
1010{
11- private static readonly ConcurrentDictionary < string , NativeFunctionLoader > Loaders = [ ] ;
12- private readonly ConcurrentDictionary < string , IntPtr > _exportCache = new ( StringComparer . Ordinal ) ;
13- private readonly Lazy < IntPtr > _libHandle ;
14- private bool _disposed ;
11+ private static readonly ConcurrentDictionary < string , NativeFunctionLoader > Loaders = [ ] ;
12+ private readonly ConcurrentDictionary < string , IntPtr > _exportCache = new ( StringComparer . Ordinal ) ;
13+ private readonly IntPtr _libHandle ;
14+ private bool _disposed ;
1515
16- private NativeFunctionLoader ( string libraryPath )
17- {
18- _libHandle = new Lazy < IntPtr > ( ( ) =>
19- {
20- var handle = NativeLibrary . Load ( libraryPath ) ;
21- if ( handle != IntPtr . Zero ) return handle ;
22- throw new InvalidOperationException ( $ "Failed to load native library: { libraryPath } ") ;
23- } ) ;
24- }
16+ private NativeFunctionLoader ( string libraryPath )
17+ {
18+ _libHandle = NativeLibrary . Load ( libraryPath ) ;
19+ if ( _libHandle != IntPtr . Zero ) return ;
20+ throw new InvalidOperationException ( $ "Failed to load native library: { libraryPath } ") ;
21+ }
2522
26- public void Dispose ( )
27- {
28- if ( _disposed ) return ;
29- _disposed = true ;
30- if ( _libHandle . IsValueCreated ) NativeLibrary . Free ( _libHandle . Value ) ;
31- }
23+ public void Dispose ( )
24+ {
25+ if ( _disposed ) return ;
26+ _disposed = true ;
27+ NativeLibrary . Free ( _libHandle ) ;
28+ }
3229
33- internal static NativeFunctionLoader GetLoader ( string libName )
34- {
35- if ( string . IsNullOrEmpty ( libName ) )
36- throw new ArgumentException ( "The libName is null or empty." , nameof ( libName ) ) ;
37- var loader = Loaders . GetOrAdd ( libName , n => new NativeFunctionLoader ( n ) ) ;
38- if ( ! loader . _disposed ) return loader ;
39- return Loaders [ libName ] = new NativeFunctionLoader ( libName ) ;
40- }
30+ internal static NativeFunctionLoader GetLoader ( string libName )
31+ {
32+ if ( string . IsNullOrEmpty ( libName ) )
33+ throw new ArgumentException ( "The libName is null or empty." , nameof ( libName ) ) ;
4134
42- public IntPtr GetFunctionPointer ( string name )
43- {
44- if ( _disposed ) throw new ObjectDisposedException ( nameof ( NativeFunctionLoader ) ) ;
45- return _exportCache . GetOrAdd ( name , n => NativeLibrary . GetExport ( _libHandle . Value , n ) ) ;
46- }
35+ return Loaders . AddOrUpdate (
36+ key : libName ,
37+ addValueFactory : n => new ( n ) ,
38+ updateValueFactory : ( n , existingLoader ) => existingLoader . _disposed ? new ( n ) : existingLoader ) ;
39+ }
40+
41+ public IntPtr GetFunctionPointer ( string name )
42+ {
43+ if ( _disposed ) throw new ObjectDisposedException ( nameof ( NativeFunctionLoader ) ) ;
44+ return _exportCache . GetOrAdd ( name , n => NativeLibrary . GetExport ( _libHandle , n ) ) ;
45+ }
4746}
4847
4948#if NETSTANDARD || NETFRAMEWORK
0 commit comments