| 1 | /**************************************************************************/ |
| 2 | /* thread_apple.h */ |
| 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 | #pragma once |
| 32 | |
| 33 | #include "core/templates/safe_refcount.h" |
| 34 | #include "core/typedefs.h" |
| 35 | |
| 36 | #include <pthread.h> |
| 37 | #include <new> // For hardware interference size |
| 38 | |
| 39 | class String; |
| 40 | |
| 41 | class Thread { |
| 42 | public: |
| 43 | typedef void (*Callback)(void *p_userdata); |
| 44 | |
| 45 | typedef uint64_t ID; |
| 46 | |
| 47 | enum : ID { |
| 48 | UNASSIGNED_ID = 0, |
| 49 | MAIN_ID = 1 |
| 50 | }; |
| 51 | |
| 52 | enum Priority { |
| 53 | PRIORITY_LOW, |
| 54 | PRIORITY_NORMAL, |
| 55 | PRIORITY_HIGH |
| 56 | }; |
| 57 | |
| 58 | struct Settings { |
| 59 | Priority priority; |
| 60 | Settings() { priority = PRIORITY_NORMAL; } |
| 61 | }; |
| 62 | |
| 63 | #if defined(__cpp_lib_hardware_interference_size) |
| 64 | GODOT_GCC_WARNING_PUSH_AND_IGNORE("-Winterference-size") |
| 65 | static constexpr size_t CACHE_LINE_BYTES = std::hardware_destructive_interference_size; |
| 66 | GODOT_GCC_WARNING_POP |
| 67 | #else |
| 68 | // At a negligible memory cost, we use a conservatively high value. |
| 69 | static constexpr size_t CACHE_LINE_BYTES = 128; |
| 70 | #endif |
| 71 | |
| 72 | private: |
| 73 | friend class Main; |
| 74 | |
| 75 | ID id = UNASSIGNED_ID; |
| 76 | pthread_t pthread; |
| 77 | |
| 78 | static SafeNumeric<uint64_t> id_counter; |
| 79 | static thread_local ID caller_id; |
| 80 | |
| 81 | static void *thread_callback(void *p_data); |
| 82 | |
| 83 | static void make_main_thread() { caller_id = MAIN_ID; } |
| 84 | static void release_main_thread() { caller_id = id_counter.increment(); } |
| 85 | |
| 86 | public: |
| 87 | _FORCE_INLINE_ static void yield() { pthread_yield_np(); } |
| 88 | |
| 89 | _FORCE_INLINE_ ID get_id() const { return id; } |
| 90 | // get the ID of the caller thread |
| 91 | _FORCE_INLINE_ static ID get_caller_id() { |
| 92 | return caller_id; |
| 93 | } |
| 94 | // get the ID of the main thread |
| 95 | _FORCE_INLINE_ static ID get_main_id() { return MAIN_ID; } |
| 96 | |
| 97 | _FORCE_INLINE_ static bool is_main_thread() { return caller_id == MAIN_ID; } |
| 98 | |
| 99 | static Error set_name(const String &p_name); |
| 100 | |
| 101 | ID start(Thread::Callback p_callback, void *p_user, const Settings &p_settings = Settings()); |
| 102 | bool is_started() const { return id != UNASSIGNED_ID; } |
| 103 | /// Waits until thread is finished, and deallocates it. |
| 104 | void wait_to_finish(); |
| 105 | |
| 106 | Thread() = default; |
| 107 | ~Thread(); |
| 108 | }; |