@@ -584,6 +584,40 @@ netcore_probe_for_module_nofail (MonoImage *image, const char *file_name, int fl
584584 return result ;
585585}
586586
587+ static MonoDl *
588+ netcore_lookup_self_native_handle (void )
589+ {
590+ ERROR_DECL (load_error );
591+ if (!internal_module )
592+ internal_module = mono_dl_open_self (load_error );
593+
594+ if (!internal_module )
595+ mono_trace (G_LOG_LEVEL_INFO , MONO_TRACE_DLLIMPORT , "DllImport error loading library '__Internal': '%s'." , mono_error_get_message_without_fields (load_error ));
596+
597+ mono_trace (G_LOG_LEVEL_DEBUG , MONO_TRACE_DLLIMPORT , "Native library found via __Internal." );
598+ mono_error_cleanup (load_error );
599+
600+ return internal_module ;
601+ }
602+
603+ static MonoDl * native_handle_lookup_wrapper (gpointer handle )
604+ {
605+ MonoDl * result = NULL ;
606+
607+ if (!internal_module )
608+ netcore_lookup_self_native_handle ();
609+
610+ if (internal_module -> handle == handle ) {
611+ result = internal_module ;
612+ } else {
613+ native_library_lock ();
614+ result = netcore_handle_lookup (handle );
615+ native_library_unlock ();
616+ }
617+
618+ return result ;
619+ }
620+
587621static MonoDl *
588622netcore_resolve_with_dll_import_resolver (MonoAssemblyLoadContext * alc , MonoAssembly * assembly , const char * scope , guint32 flags , MonoError * error )
589623{
@@ -631,9 +665,7 @@ netcore_resolve_with_dll_import_resolver (MonoAssemblyLoadContext *alc, MonoAsse
631665 mono_runtime_invoke_checked (resolve , NULL , args , error );
632666 goto_if_nok (error , leave );
633667
634- native_library_lock ();
635- result = netcore_handle_lookup (lib );
636- native_library_unlock ();
668+ result = native_handle_lookup_wrapper (lib );
637669
638670leave :
639671 HANDLE_FUNCTION_RETURN_VAL (result );
@@ -688,9 +720,7 @@ netcore_resolve_with_load (MonoAssemblyLoadContext *alc, const char *scope, Mono
688720 mono_runtime_invoke_checked (resolve , NULL , args , error );
689721 goto_if_nok (error , leave );
690722
691- native_library_lock ();
692- result = netcore_handle_lookup (lib );
693- native_library_unlock ();
723+ result = native_handle_lookup_wrapper (lib );
694724
695725leave :
696726 HANDLE_FUNCTION_RETURN_VAL (result );
@@ -755,9 +785,7 @@ netcore_resolve_with_resolving_event (MonoAssemblyLoadContext *alc, MonoAssembly
755785 mono_runtime_invoke_checked (resolve , NULL , args , error );
756786 goto_if_nok (error , leave );
757787
758- native_library_lock ();
759- result = netcore_handle_lookup (lib );
760- native_library_unlock ();
788+ result = native_handle_lookup_wrapper (lib );
761789
762790leave :
763791 HANDLE_FUNCTION_RETURN_VAL (result );
@@ -802,22 +830,6 @@ netcore_check_alc_cache (MonoAssemblyLoadContext *alc, const char *scope)
802830 return result ;
803831}
804832
805- static MonoDl *
806- netcore_lookup_self_native_handle (void )
807- {
808- ERROR_DECL (load_error );
809- if (!internal_module )
810- internal_module = mono_dl_open_self (load_error );
811-
812- if (!internal_module )
813- mono_trace (G_LOG_LEVEL_INFO , MONO_TRACE_DLLIMPORT , "DllImport error loading library '__Internal': '%s'." , mono_error_get_message_without_fields (load_error ));
814-
815- mono_trace (G_LOG_LEVEL_DEBUG , MONO_TRACE_DLLIMPORT , "Native library found via __Internal." );
816- mono_error_cleanup (load_error );
817-
818- return internal_module ;
819- }
820-
821833static MonoDl *
822834netcore_lookup_native_library (MonoAssemblyLoadContext * alc , MonoImage * image , const char * scope , guint32 flags )
823835{
0 commit comments