@@ -63,17 +63,14 @@ void WorkerThreadPool::_process_task(Task *p_task) {
63
63
// Tasks must start with these at default values. They are free to set-and-forget otherwise.
64
64
set_current_thread_safe_for_nodes (false );
65
65
MessageQueue::set_thread_singleton_override (nullptr );
66
+
66
67
// Since the WorkerThreadPool is started before the script server,
67
68
// its pre-created threads can't have ScriptServer::thread_enter() called on them early.
68
69
// Therefore, we do it late at the first opportunity, so in case the task
69
70
// about to be run uses scripting, guarantees are held.
71
+ ScriptServer::thread_enter ();
72
+
70
73
task_mutex.lock ();
71
- if (!curr_thread.ready_for_scripting && ScriptServer::are_languages_initialized ()) {
72
- task_mutex.unlock ();
73
- ScriptServer::thread_enter ();
74
- task_mutex.lock ();
75
- curr_thread.ready_for_scripting = true ;
76
- }
77
74
p_task->pool_thread_index = pool_thread_index;
78
75
prev_task = curr_thread.current_task ;
79
76
curr_thread.current_task = p_task;
@@ -326,6 +323,8 @@ WorkerThreadPool::TaskID WorkerThreadPool::add_native_task(void (*p_func)(void *
326
323
}
327
324
328
325
WorkerThreadPool::TaskID WorkerThreadPool::_add_task (const Callable &p_callable, void (*p_func)(void *), void *p_userdata, BaseTemplateUserdata *p_template_userdata, bool p_high_priority, const String &p_description) {
326
+ ERR_FAIL_COND_V_MSG (threads.is_empty (), INVALID_TASK_ID, " Can't add a task because the WorkerThreadPool is either not initialized yet or already terminated." );
327
+
329
328
task_mutex.lock ();
330
329
// Get a free task
331
330
Task *task = task_allocator.alloc ();
@@ -514,6 +513,12 @@ void WorkerThreadPool::yield() {
514
513
int th_index = get_thread_index ();
515
514
ERR_FAIL_COND_MSG (th_index == -1 , " This function can only be called from a worker thread." );
516
515
_wait_collaboratively (&threads[th_index], ThreadData::YIELDING);
516
+
517
+ // If this long-lived task started before the scripting server was initialized,
518
+ // now is a good time to have scripting languages ready for the current thread.
519
+ // Otherwise, such a piece of setup won't happen unless another task has been
520
+ // run during the collaborative wait.
521
+ ScriptServer::thread_enter ();
517
522
}
518
523
519
524
void WorkerThreadPool::notify_yield_over (TaskID p_task_id) {
@@ -538,6 +543,7 @@ void WorkerThreadPool::notify_yield_over(TaskID p_task_id) {
538
543
}
539
544
540
545
WorkerThreadPool::GroupID WorkerThreadPool::_add_group_task (const Callable &p_callable, void (*p_func)(void *, uint32_t ), void *p_userdata, BaseTemplateUserdata *p_template_userdata, int p_elements, int p_tasks, bool p_high_priority, const String &p_description) {
546
+ ERR_FAIL_COND_V_MSG (threads.is_empty (), INVALID_TASK_ID, " Can't add a group task because the WorkerThreadPool is either not initialized yet or already terminated." );
541
547
ERR_FAIL_COND_V (p_elements < 0 , INVALID_TASK_ID);
542
548
if (p_tasks < 0 ) {
543
549
p_tasks = MAX (1u , threads.size ());
@@ -749,5 +755,5 @@ WorkerThreadPool::WorkerThreadPool() {
749
755
}
750
756
751
757
WorkerThreadPool::~WorkerThreadPool () {
752
- finish ();
758
+ DEV_ASSERT (threads. size () == 0 && " finish() hasn't been called! " );
753
759
}
0 commit comments