thread.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*************************************************************************/
  2. /* thread.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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. #ifndef THREAD_H
  31. #define THREAD_H
  32. #include "core/typedefs.h"
  33. #if !defined(NO_THREADS)
  34. #include "core/templates/safe_refcount.h"
  35. #include <thread>
  36. #endif
  37. class String;
  38. class Thread {
  39. public:
  40. typedef void (*Callback)(void *p_userdata);
  41. typedef uint64_t ID;
  42. enum Priority {
  43. PRIORITY_LOW,
  44. PRIORITY_NORMAL,
  45. PRIORITY_HIGH
  46. };
  47. struct Settings {
  48. Priority priority;
  49. Settings() { priority = PRIORITY_NORMAL; }
  50. };
  51. private:
  52. #if !defined(NO_THREADS)
  53. friend class Main;
  54. static ID main_thread_id;
  55. static SafeNumeric<Thread::ID> last_thread_id;
  56. ID id = 0;
  57. static thread_local ID caller_id;
  58. std::thread thread;
  59. static void callback(Thread *p_self, const Settings &p_settings, Thread::Callback p_callback, void *p_userdata);
  60. static Error (*set_name_func)(const String &);
  61. static void (*set_priority_func)(Thread::Priority);
  62. static void (*init_func)();
  63. static void (*term_func)();
  64. #endif
  65. public:
  66. static void _set_platform_funcs(
  67. Error (*p_set_name_func)(const String &),
  68. void (*p_set_priority_func)(Thread::Priority),
  69. void (*p_init_func)() = nullptr,
  70. void (*p_term_func)() = nullptr);
  71. #if !defined(NO_THREADS)
  72. _FORCE_INLINE_ ID get_id() const { return id; }
  73. // get the ID of the caller thread
  74. _FORCE_INLINE_ static ID get_caller_id() { return caller_id; }
  75. // get the ID of the main thread
  76. _FORCE_INLINE_ static ID get_main_id() { return main_thread_id; }
  77. static Error set_name(const String &p_name);
  78. void start(Thread::Callback p_callback, void *p_user, const Settings &p_settings = Settings());
  79. bool is_started() const;
  80. ///< waits until thread is finished, and deallocates it.
  81. void wait_to_finish();
  82. ~Thread();
  83. #else
  84. _FORCE_INLINE_ ID get_id() const { return 0; }
  85. // get the ID of the caller thread
  86. _FORCE_INLINE_ static ID get_caller_id() { return 0; }
  87. // get the ID of the main thread
  88. _FORCE_INLINE_ static ID get_main_id() { return 0; }
  89. static Error set_name(const String &p_name) { return ERR_UNAVAILABLE; }
  90. void start(Thread::Callback p_callback, void *p_user, const Settings &p_settings = Settings()) {}
  91. bool is_started() const { return false; }
  92. void wait_to_finish() {}
  93. #endif
  94. };
  95. #endif // THREAD_H