thread.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. #include "platform_config.h"
  31. // Define PLATFORM_THREAD_OVERRIDE in your platform's `platform_config.h`
  32. // to use a custom Thread implementation defined in `platform/[your_platform]/platform_thread.h`
  33. // Overriding the platform implementation is required in some proprietary platforms
  34. #ifdef PLATFORM_THREAD_OVERRIDE
  35. #include "platform_thread.h"
  36. #else
  37. #ifndef THREAD_H
  38. #define THREAD_H
  39. #include "core/templates/safe_refcount.h"
  40. #include "core/typedefs.h"
  41. #include <thread>
  42. class String;
  43. class Thread {
  44. public:
  45. typedef void (*Callback)(void *p_userdata);
  46. typedef uint64_t ID;
  47. enum Priority {
  48. PRIORITY_LOW,
  49. PRIORITY_NORMAL,
  50. PRIORITY_HIGH
  51. };
  52. struct Settings {
  53. Priority priority;
  54. Settings() { priority = PRIORITY_NORMAL; }
  55. };
  56. struct PlatformFunctions {
  57. Error (*set_name)(const String &) = nullptr;
  58. void (*set_priority)(Thread::Priority) = nullptr;
  59. void (*init)() = nullptr;
  60. void (*wrapper)(Thread::Callback, void *) = nullptr;
  61. void (*term)() = nullptr;
  62. };
  63. private:
  64. friend class Main;
  65. static ID main_thread_id;
  66. static uint64_t _thread_id_hash(const std::thread::id &p_t);
  67. ID id = _thread_id_hash(std::thread::id());
  68. static thread_local ID caller_id;
  69. std::thread thread;
  70. static void callback(Thread *p_self, const Settings &p_settings, Thread::Callback p_callback, void *p_userdata);
  71. static PlatformFunctions platform_functions;
  72. public:
  73. static void _set_platform_functions(const PlatformFunctions &p_functions);
  74. _FORCE_INLINE_ ID get_id() const { return id; }
  75. // get the ID of the caller thread
  76. _FORCE_INLINE_ static ID get_caller_id() { return caller_id; }
  77. // get the ID of the main thread
  78. _FORCE_INLINE_ static ID get_main_id() { return main_thread_id; }
  79. static Error set_name(const String &p_name);
  80. void start(Thread::Callback p_callback, void *p_user, const Settings &p_settings = Settings());
  81. bool is_started() const;
  82. ///< waits until thread is finished, and deallocates it.
  83. void wait_to_finish();
  84. Thread();
  85. ~Thread();
  86. };
  87. #endif // THREAD_H
  88. #endif // PLATFORM_THREAD_OVERRIDE