Repository: godot
Issue: Multithreaded import deadlocks when importer generates lightmap UV's.
Labels: bug,topic:rendering,needs testing,topic:import,crash

### Tested versions

- Reproducible in 4.2.0

### System information

Godot v4.2.dev (e5f728c07) - Windows 10.0.19045 - Vulkan (Forward+) - dedicated NVIDIA GeForce RTX 3080 (NVIDIA; 31.0.15.3623) - AMD Ryzen 7 3700X 8-Core Processor (16 Threads)

### Issue description

I have an importer for BSP files: https://github.com/jitspoe/godot_bsp_importer / https://godotengine.org/asset-library/asset/2147

I recently added the option to automatically generate UV2's for lightmaps, but this seems to break with multithreaded importing.

Call stack:
```
 	ntdll.dll!NtWaitForAlertByThreadId()	Unknown
 	ntdll.dll!RtlSleepConditionVariableSRW()	Unknown
 	KernelBase.dll!SleepConditionVariableSRW()	Unknown
 	[Inline Frame] godot.windows.editor.x86_64.exe!Concurrency::details::stl_condition_variable_win7::wait_for(Concurrency::details::stl_critical_section_interface *) Line 167	C++
 	godot.windows.editor.x86_64.exe!Concurrency::details::stl_condition_variable_win7::wait(Concurrency::details::stl_critical_section_interface * lock) Line 161	C++
 	godot.windows.editor.x86_64.exe!_Cnd_wait(_Cnd_internal_imp_t * cond, _Mtx_internal_imp_t * mtx) Line 60	C++
 	[Inline Frame] godot.windows.editor.x86_64.exe!std::condition_variable::wait(std::unique_lock<std::mutex> &) Line 608	C++
 	[Inline Frame] godot.windows.editor.x86_64.exe!Semaphore::wait() Line 73	C++
 	godot.windows.editor.x86_64.exe!CommandQueueMT::push_and_ret<RendererMeshStorage,RenderingServer::SurfaceData (__cdecl RendererMeshStorage::*)(RID,int)const ,RID,int,RenderingServer::SurfaceData>(RendererMeshStorage * p_instance, RenderingServer::SurfaceData(const RendererMeshStorage::*)(RID, int) p_method, RID p1, int p2, RenderingServer::SurfaceData * r_ret) Line 396	C++
>	godot.windows.editor.x86_64.exe!RenderingServerDefault::mesh_get_surface(RID p1, int p2) Line 311	C++
 	godot.windows.editor.x86_64.exe!RenderingServer::mesh_surface_get_arrays(RID p_mesh, int p_surface) Line 1705	C++
 	godot.windows.editor.x86_64.exe!ArrayMesh::surface_get_arrays(int p_surface) Line 1820	C++
 	godot.windows.editor.x86_64.exe!ArrayMesh::lightmap_unwrap_cached(const Transform3D & p_base_transform, float p_texel_size, const Vector<unsigned char> & p_src_cache, Vector<unsigned char> & r_dst_cache, bool p_generate_cache) Line 2080	C++
 	godot.windows.editor.x86_64.exe!ArrayMesh::lightmap_unwrap(const Transform3D & p_base_transform, float p_texel_size) Line 2046	C++
 	[Inline Frame] godot.windows.editor.x86_64.exe!call_with_variant_args_ret_helper(ArrayMesh *) Line 755	C++
 	godot.windows.editor.x86_64.exe!call_with_variant_args_ret_dv<ArrayMesh,enum Error,Transform3D const &,float>(ArrayMesh * p_instance, Error(ArrayMesh::*)(const Transform3D &, float) p_method, const Variant * * p_args, int p_argcount, Variant & r_ret, Callable::CallError & r_error, const Vector<Variant> & default_values) Line 534	C++
 	godot.windows.editor.x86_64.exe!MethodBindTR<ArrayMesh,enum Error,Transform3D const &,float>::call(Object * p_object, const Variant * * p_args, int p_arg_count, Callable::CallError & r_error) Line 498	C++
 	godot.windows.editor.x86_64.exe!Object::callp(const StringName & p_method, const Variant * * p_args, int p_argcount, Callable::CallError & r_error) Line 775	C++
 	godot.windows.editor.x86_64.exe!Variant::callp(const StringName & p_method, const Variant * * p_args, int p_argcount, Variant & r_ret, Callable::CallError & r_error) Line 1168	C++
 	godot.windows.editor.x86_64.exe!GDScriptFunction::call(GDScriptInstance * p_instance, const Variant * * p_args, int p_argcount, Callable::CallError & r_err, GDScriptFunction::CallState * p_state) Line 1676	C++
 	godot.windows.editor.x86_64.exe!GDScriptInstance::callp(const StringName & p_method, const Variant * * p_args, int p_argcount, Callable::CallError & r_error) Line 1937	C++
 	godot.windows.editor.x86_64.exe!Object::callp(const StringName & p_method, const Variant * * p_args, int p_argcount, Callable::CallError & r_error) Line 753	C++
 	godot.windows.editor.x86_64.exe!Variant::callp(const StringName & p_method, const Variant * * p_args, int p_argcount, Variant & r_ret, Callable::CallError & r_error) Line 1168	C++
 	godot.windows.editor.x86_64.exe!GDScriptFunction::call(GDScriptInstance * p_instance, const Variant * * p_args, int p_argcount, Callable::CallError & r_err, GDScriptFunction::CallState * p_state) Line 1676	C++
 	godot.windows.editor.x86_64.exe!GDScriptInstance::callp(const StringName & p_method, const Variant * * p_args, int p_argcount, Callable::CallError & r_error) Line 1937	C++
 	[Inline Frame] godot.windows.editor.x86_64.exe!EditorImportPlugin::_gdvirtual__import_call(String) Line 54	C++
 	godot.windows.editor.x86_64.exe!EditorImportPlugin::import(const String & p_source_file, const String & p_save_path, const HashMap<StringName,Variant,HashMapHasherDefault,HashMapComparatorDefault<StringName>,DefaultTypedAllocator<HashMapElement<StringName,Variant>>> & p_options, List<String,DefaultAllocator> * r_platform_variants, List<String,DefaultAllocator> * r_gen_files, Variant * r_metadata) Line 177	C++
 	godot.windows.editor.x86_64.exe!EditorFileSystem::_reimport_file(const String & p_file, const HashMap<StringName,Variant,HashMapHasherDefault,HashMapComparatorDefault<StringName>,DefaultTypedAllocator<HashMapElement<StringName,Variant>>> & p_custom_options, const String & p_custom_importer, Variant * p_generator_parameters) Line 2050	C++
 	godot.windows.editor.x86_64.exe!EditorFileSystem::_reimport_thread(unsigned int p_index, EditorFileSystem::ImportThreadData * p_import_data) Line 2215	C++
 	godot.windows.editor.x86_64.exe!WorkerThreadPool::_process_task(WorkerThreadPool::Task * p_task) Line 98	C++
 	godot.windows.editor.x86_64.exe!Thread::callback(unsigned __int64 p_caller_id, const Thread::Settings & p_settings, void(*)(void *) p_callback, void * p_userdata) Line 63	C++
 	[Inline Frame] godot.windows.editor.x86_64.exe!std::invoke(void(*)(unsigned __int64, const Thread::Settings &, void(*)(void *), void *) &&) Line 1595	C++
 	godot.windows.editor.x86_64.exe!std::thread::_Invoke<std::tuple<void (__cdecl*)(unsigned __int64,Thread::Settings const &,void (__cdecl*)(void *),void *),unsigned __int64,Thread::Settings,void (__cdecl*)(void *),void *>,0,1,2,3,4>(void * _RawVals) Line 56	C++
 	godot.windows.editor.x86_64.exe!thread_start<unsigned int (__cdecl*)(void *),1>(void * const parameter) Line 97	C++
 	kernel32.dll!BaseThreadInitThunk()	Unknown
 	ntdll.dll!RtlUserThreadStart()	Unknown

```

Note that the wait in mesh_get_surface deadlocks the import.  Disabling multithreaded import fixes the issue.

### Steps to reproduce

Install the BSP importer addon.

Add multiple BSP files to your project at once.

Or simply try to load the repro project and it should freeze on load.

### Minimal reproduction project (MRP)

[test_mt_import_bsp.zip](https://github.com/godotengine/godot/files/13556146/test_mt_import_bsp.zip)