| 1 | /**************************************************************************/ |
| 2 | /* thread.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 | #include "platform_config.h" |
| 32 | // Define PLATFORM_THREAD_OVERRIDE in your platform's `platform_config.h` |
| 33 | // to use a custom Thread implementation defined in `platform/[your_platform]/platform_thread.h` |
| 34 | // Overriding the platform implementation is required in some proprietary platforms |
| 35 | #ifdef PLATFORM_THREAD_OVERRIDE |
| 36 | #include "platform_thread.h" |
| 37 | #else |
| 38 | |
| 39 | #ifndef THREAD_H |
| 40 | #define THREAD_H |
| 41 | |
| 42 | #include "core/templates/safe_refcount.h" |
| 43 | #include "core/typedefs.h" |
| 44 | |
| 45 | #include <thread> |
| 46 | |
| 47 | class String; |
| 48 | |
| 49 | class Thread { |
| 50 | public: |
| 51 | typedef void (*Callback)(void *p_userdata); |
| 52 | |
| 53 | typedef uint64_t ID; |
| 54 | |
| 55 | enum : ID { |
| 56 | UNASSIGNED_ID = 0, |
| 57 | MAIN_ID = 1 |
| 58 | }; |
| 59 | |
| 60 | enum Priority { |
| 61 | PRIORITY_LOW, |
| 62 | PRIORITY_NORMAL, |
| 63 | PRIORITY_HIGH |
| 64 | }; |
| 65 | |
| 66 | struct Settings { |
| 67 | Priority priority; |
| 68 | Settings() { priority = PRIORITY_NORMAL; } |
| 69 | }; |
| 70 | |
| 71 | struct PlatformFunctions { |
| 72 | Error (*set_name)(const String &) = nullptr; |
| 73 | void (*set_priority)(Thread::Priority) = nullptr; |
| 74 | void (*init)() = nullptr; |
| 75 | void (*wrapper)(Thread::Callback, void *) = nullptr; |
| 76 | void (*term)() = nullptr; |
| 77 | }; |
| 78 | |
| 79 | private: |
| 80 | friend class Main; |
| 81 | |
| 82 | ID id = UNASSIGNED_ID; |
| 83 | static SafeNumeric<uint64_t> id_counter; |
| 84 | static thread_local ID caller_id; |
| 85 | std::thread thread; |
| 86 | |
| 87 | static void callback(ID p_caller_id, const Settings &p_settings, Thread::Callback p_callback, void *p_userdata); |
| 88 | |
| 89 | static PlatformFunctions platform_functions; |
| 90 | |
| 91 | static void make_main_thread() { caller_id = MAIN_ID; } |
| 92 | static void release_main_thread() { caller_id = UNASSIGNED_ID; } |
| 93 | |
| 94 | public: |
| 95 | static void _set_platform_functions(const PlatformFunctions &p_functions); |
| 96 | |
| 97 | _FORCE_INLINE_ ID get_id() const { return id; } |
| 98 | // get the ID of the caller thread |
| 99 | _FORCE_INLINE_ static ID get_caller_id() { |
| 100 | if (unlikely(caller_id == UNASSIGNED_ID)) { |
| 101 | caller_id = id_counter.increment(); |
| 102 | } |
| 103 | return caller_id; |
| 104 | } |
| 105 | // get the ID of the main thread |
| 106 | _FORCE_INLINE_ static ID get_main_id() { return MAIN_ID; } |
| 107 | |
| 108 | _FORCE_INLINE_ static bool is_main_thread() { return caller_id == MAIN_ID; } // Gain a tiny bit of perf here because there is no need to validate caller_id here, because only main thread will be set as 1. |
| 109 | |
| 110 | static Error set_name(const String &p_name); |
| 111 | |
| 112 | ID start(Thread::Callback p_callback, void *p_user, const Settings &p_settings = Settings()); |
| 113 | bool is_started() const; |
| 114 | ///< waits until thread is finished, and deallocates it. |
| 115 | void wait_to_finish(); |
| 116 | |
| 117 | Thread(); |
| 118 | ~Thread(); |
| 119 | }; |
| 120 | |
| 121 | #endif // THREAD_H |
| 122 | |
| 123 | #endif // PLATFORM_THREAD_OVERRIDE |
| 124 | |