mirror of
https://github.com/stan220/godot-docs.git
synced 2026-09-08 18:29:11 +00:00
Add exit condition to prevent thread from hanging in resource_queue.gd (#5833)
Co-authored-by: Hugo Locurcio <hugo.locurcio@hugo.pro>
This commit is contained in:
co-authored by
Hugo Locurcio
parent
494d2d5c73
commit
8b4213bbee
@@ -1,6 +1,9 @@
|
|||||||
|
extends Node
|
||||||
|
|
||||||
var thread
|
var thread
|
||||||
var mutex
|
var mutex
|
||||||
var sem
|
var semaphore
|
||||||
|
var exit_thread = false
|
||||||
|
|
||||||
var time_max = 100 # Milliseconds.
|
var time_max = 100 # Milliseconds.
|
||||||
|
|
||||||
@@ -16,11 +19,11 @@ func _unlock(_caller):
|
|||||||
|
|
||||||
|
|
||||||
func _post(_caller):
|
func _post(_caller):
|
||||||
sem.post()
|
semaphore.post()
|
||||||
|
|
||||||
|
|
||||||
func _wait(_caller):
|
func _wait(_caller):
|
||||||
sem.wait()
|
semaphore.wait()
|
||||||
|
|
||||||
|
|
||||||
func queue_resource(path, p_in_front = false):
|
func queue_resource(path, p_in_front = false):
|
||||||
@@ -135,11 +138,29 @@ func thread_process():
|
|||||||
|
|
||||||
func thread_func(_u):
|
func thread_func(_u):
|
||||||
while true:
|
while true:
|
||||||
|
mutex.lock()
|
||||||
|
var should_exit = exit_thread # Protect with Mutex.
|
||||||
|
mutex.unlock()
|
||||||
|
|
||||||
|
if should_exit:
|
||||||
|
break
|
||||||
thread_process()
|
thread_process()
|
||||||
|
|
||||||
|
|
||||||
func start():
|
func start():
|
||||||
mutex = Mutex.new()
|
mutex = Mutex.new()
|
||||||
sem = Semaphore.new()
|
semaphore = Semaphore.new()
|
||||||
thread = Thread.new()
|
thread = Thread.new()
|
||||||
thread.start(self, "thread_func", 0)
|
thread.start(self, "thread_func", 0)
|
||||||
|
|
||||||
|
# Triggered by calling "get_tree().quit()".
|
||||||
|
func _exit_tree():
|
||||||
|
mutex.lock()
|
||||||
|
exit_thread = true # Protect with Mutex.
|
||||||
|
mutex.unlock()
|
||||||
|
|
||||||
|
# Unblock by posting.
|
||||||
|
semaphore.post()
|
||||||
|
|
||||||
|
# Wait until it exits.
|
||||||
|
thread.wait_to_finish()
|
||||||
|
|||||||
Reference in New Issue
Block a user