| 1 | /**************************************************************************/ |
| 2 | /* thread_apple.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "thread_apple.h" |
| 32 | |
| 33 | #include "core/error/error_macros.h" |
| 34 | #include "core/object/script_language.h" |
| 35 | #include "core/string/ustring.h" |
| 36 | |
| 37 | SafeNumeric<uint64_t> Thread::id_counter(1); // The first value after .increment() is 2, hence by default the main thread ID should be 1. |
| 38 | thread_local Thread::ID Thread::caller_id = Thread::id_counter.increment(); |
| 39 | |
| 40 | struct ThreadData { |
| 41 | Thread::Callback callback; |
| 42 | void *userdata; |
| 43 | Thread::ID caller_id; |
| 44 | }; |
| 45 | |
| 46 | void *Thread::thread_callback(void *p_data) { |
| 47 | ThreadData *thread_data = static_cast<ThreadData *>(p_data); |
| 48 | |
| 49 | // Set the caller ID for this thread |
| 50 | caller_id = thread_data->caller_id; |
| 51 | |
| 52 | ScriptServer::thread_enter(); // Scripts may need to attach a stack. |
| 53 | |
| 54 | // Call the actual callback |
| 55 | thread_data->callback(thread_data->userdata); |
| 56 | |
| 57 | ScriptServer::thread_exit(); |
| 58 | |
| 59 | // Clean up |
| 60 | memdelete(thread_data); |
| 61 | |
| 62 | return nullptr; |
| 63 | } |
| 64 | |
| 65 | Error Thread::set_name(const String &p_name) { |
| 66 | int err = pthread_setname_np(p_name.utf8().get_data()); |
| 67 | return err == 0 ? OK : ERR_INVALID_PARAMETER; |
| 68 | } |
| 69 | |
| 70 | Thread::ID Thread::start(Thread::Callback p_callback, void *p_user, const Settings &p_settings) { |
| 71 | ERR_FAIL_COND_V_MSG(id != UNASSIGNED_ID, UNASSIGNED_ID, "A Thread object has been re-started without wait_to_finish() having been called on it."); |
| 72 | id = id_counter.increment(); |
| 73 | |
| 74 | ThreadData *thread_data = memnew(ThreadData); |
| 75 | thread_data->callback = p_callback; |
| 76 | thread_data->userdata = p_user; |
| 77 | thread_data->caller_id = id; |
| 78 | |
| 79 | // Create the thread |
| 80 | pthread_attr_t attr; |
| 81 | pthread_attr_init(&attr); |
| 82 | |
| 83 | switch (p_settings.priority) { |
| 84 | case PRIORITY_LOW: |
| 85 | pthread_attr_set_qos_class_np(&attr, QOS_CLASS_UTILITY, 0); |
| 86 | break; |
| 87 | case PRIORITY_NORMAL: |
| 88 | pthread_attr_set_qos_class_np(&attr, QOS_CLASS_USER_INITIATED, 0); |
| 89 | break; |
| 90 | case PRIORITY_HIGH: |
| 91 | pthread_attr_set_qos_class_np(&attr, QOS_CLASS_USER_INTERACTIVE, 0); |
| 92 | break; |
| 93 | } |
| 94 | |
| 95 | // The default stack size for secondary threads on Apple platforms is 512KiB. |
| 96 | // This is insufficient when using a library like SPIRV-Cross, which can generate deep stacks and result in a stack overflow. |
| 97 | // It also creates a problematic discrepancy with other platforms, where secondary threads are often at least 1 MiB. |
| 98 | pthread_attr_setstacksize(&attr, |
| 99 | #if __has_feature(address_sanitizer) || __has_feature(thread_sanitizer) |
| 100 | // ASan (and to some degree TSan) needs a lot of extra stack size. |
| 101 | 4 * 1024 * 1024 // 4 MiB |
| 102 | #elif !defined(__OPTIMIZE__) |
| 103 | // Unoptimized builds also need a larger stack size. |
| 104 | 2 * 1024 * 1024 // 2 MiB |
| 105 | #else |
| 106 | 1 * 1024 * 1024 // 1 MiB |
| 107 | #endif |
| 108 | ); |
| 109 | |
| 110 | // Create the thread |
| 111 | pthread_create(&pthread, &attr, thread_callback, thread_data); |
| 112 | |
| 113 | // Clean up attributes |
| 114 | pthread_attr_destroy(&attr); |
| 115 | |
| 116 | return id; |
| 117 | } |
| 118 | |
| 119 | void Thread::wait_to_finish() { |
| 120 | ERR_FAIL_COND_MSG(id == UNASSIGNED_ID, "Attempt of waiting to finish on a thread that was never started."); |
| 121 | ERR_FAIL_COND_MSG(id == get_caller_id(), "Threads can't wait to finish on themselves, another thread must wait."); |
| 122 | |
| 123 | int err = pthread_join(pthread, nullptr); |
| 124 | if (err != 0) { |
| 125 | ERR_FAIL_MSG("Thread::wait_to_finish() failed to join thread."); |
| 126 | } |
| 127 | pthread = pthread_t(); |
| 128 | id = UNASSIGNED_ID; |
| 129 | } |
| 130 | |
| 131 | Thread::~Thread() { |
| 132 | if (id != UNASSIGNED_ID) { |
| 133 | #ifdef DEBUG_ENABLED |
| 134 | WARN_PRINT( |
| 135 | "A Thread object is being destroyed without its completion having been realized.\n" |
| 136 | "Please call wait_to_finish() on it to ensure correct cleanup."); |
| 137 | #endif |
| 138 | pthread_detach(pthread); |
| 139 | } |
| 140 | } |