-
Notifications
You must be signed in to change notification settings - Fork 31
/
thread_worker.gd
96 lines (71 loc) · 2.31 KB
/
thread_worker.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# This file is part of Unidot Importer. See LICENSE.txt for full MIT license.
# Copyright (c) 2021-present Lyuma <[email protected]> and contributors
# SPDX-License-Identifier: MIT
@tool
extends RefCounted
const queue_lib := preload("./queue_lib.gd")
const asset_adapter_class := preload("./asset_adapter.gd")
var asset_adapter = asset_adapter_class.new()
var thread_queue: queue_lib.BlockingQueue
var thread_count: int
var threads: Array
var disable_threads: bool = false
var asset_database: RefCounted
class ShutdownSentinel:
pass
signal asset_processing_started(tw: Object)
signal asset_processing_finished(tw: Object)
func _init():
thread_queue = queue_lib.BlockingQueue.new()
thread_count = 0
threads = [].duplicate()
func start_thread():
thread_count += 1
print("Starting thread")
var thread: Thread = Thread.new()
# Third argument is optional userdata, it can be any variable.
thread.start(self._thread_function.bind("THR" + str(thread_count)))
threads.push_back(thread)
return thread
func start_threads(count: int):
disable_threads = (count == 0)
for i in range(count):
start_thread()
func tell_all_threads_to_stop():
for i in range(thread_count):
thread_queue.push(ShutdownSentinel)
thread_count = 0
func stop_all_threads_and_wait():
tell_all_threads_to_stop()
#### FIXME: CAUSES GODOT TO CRASH??
for thread in threads:
thread.wait_to_finish()
thread_queue = queue_lib.BlockingQueue.new()
threads = [].duplicate()
# asset: package_file.PkgAsset object
func push_work_obj(tw: Object):
if disable_threads:
self.call_deferred("_run_single_item_delayed", tw)
else:
self.thread_queue.push(tw)
func _run_single_item_delayed(tw: Object):
self.call_deferred("_run_single_item", tw, "NOTHR")
# OVERRIDE This!
func _run_single_item(tw: Object, thread_subdir: String):
asset_processing_started.emit(tw)
pass
asset_processing_finished.emit(tw)
# Run here and exit.
# The argument is the userdata passed from start().
# If no argument was passed, this one still needs to
# be here and it will be null.
func _thread_function(thread_subdir: String):
# Print the userdata ("Wafflecopter")
print("I'm a thread! Userdata is: ", thread_subdir)
while true:
var tw = thread_queue.pop()
#print(tw)
if tw == ShutdownSentinel:
print("I was told to shutdown")
break
_run_single_item(tw, thread_subdir)