godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
91.48k stars 21.26k forks source link

Multithreading in ImportPlugins basically impossible #54178

Open 0x0ACB opened 3 years ago

0x0ACB commented 3 years ago

Godot version

3.3.3, 3.3.4

System information

Windows 10, Ubuntu 20.4 LTS

Issue description

I have written am Import plugin that I am using to import larger scenes into Godot. However I am facing some issues trying to optimize the plugin. Especially trying to use multiple threads during the import.

Since there are 4 independent steps during the import process, creating different parts of the scene, the logical Idea to speed up the import process was to use multiple threads to parallelize the workload. The problem is that at least SurfaceTool::commit, ArrayMesh::_new and ResourceLoader::load all deadlock when the editor thread is blocked by the import plugin. Note that when launching the whole import process in a separate thread and directly returning from the import method in the Plugin the deadlock does not occur however the import itself also does not work correctly in that case.

Steps to reproduce

  1. Write an import plugin.
  2. Create a separate thread.
  3. Call SurfaceTool::commit, ArrayMesh::_new or ResourceLoader::load in the new thread while the import thread is still locked in your import method.
  4. Deadlock

Minimal reproduction project

No response

Calinou commented 3 years ago

Multi-threaded import is already implemented in master, but I'm not sure if it can be done in 3.x without breaking compatibility.