thread_posix.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*************************************************************************/
  2. /* thread_posix.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. #include "thread_posix.h"
  31. #include "core/script_language.h"
  32. #if (defined(UNIX_ENABLED) || defined(PTHREAD_ENABLED)) && !defined(NO_THREADS)
  33. #ifdef PTHREAD_BSD_SET_NAME
  34. #include <pthread_np.h>
  35. #endif
  36. #include "core/os/memory.h"
  37. #include "core/safe_refcount.h"
  38. static void _thread_id_key_destr_callback(void *p_value) {
  39. memdelete(static_cast<Thread::ID *>(p_value));
  40. }
  41. static pthread_key_t _create_thread_id_key() {
  42. pthread_key_t key;
  43. pthread_key_create(&key, &_thread_id_key_destr_callback);
  44. return key;
  45. }
  46. pthread_key_t ThreadPosix::thread_id_key = _create_thread_id_key();
  47. Thread::ID ThreadPosix::next_thread_id = 0;
  48. Thread::ID ThreadPosix::get_id() const {
  49. return id;
  50. }
  51. Thread *ThreadPosix::create_thread_posix() {
  52. return memnew(ThreadPosix);
  53. }
  54. void *ThreadPosix::thread_callback(void *userdata) {
  55. ThreadPosix *t = reinterpret_cast<ThreadPosix *>(userdata);
  56. t->id = atomic_increment(&next_thread_id);
  57. pthread_setspecific(thread_id_key, (void *)memnew(ID(t->id)));
  58. ScriptServer::thread_enter(); //scripts may need to attach a stack
  59. t->callback(t->user);
  60. ScriptServer::thread_exit();
  61. return NULL;
  62. }
  63. Thread *ThreadPosix::create_func_posix(ThreadCreateCallback p_callback, void *p_user, const Settings &) {
  64. ThreadPosix *tr = memnew(ThreadPosix);
  65. tr->callback = p_callback;
  66. tr->user = p_user;
  67. pthread_attr_init(&tr->pthread_attr);
  68. pthread_attr_setdetachstate(&tr->pthread_attr, PTHREAD_CREATE_JOINABLE);
  69. pthread_attr_setstacksize(&tr->pthread_attr, 256 * 1024);
  70. pthread_create(&tr->pthread, &tr->pthread_attr, thread_callback, tr);
  71. return tr;
  72. }
  73. Thread::ID ThreadPosix::get_thread_id_func_posix() {
  74. void *value = pthread_getspecific(thread_id_key);
  75. if (value)
  76. return *static_cast<ID *>(value);
  77. ID new_id = atomic_increment(&next_thread_id);
  78. pthread_setspecific(thread_id_key, (void *)memnew(ID(new_id)));
  79. return new_id;
  80. }
  81. void ThreadPosix::wait_to_finish_func_posix(Thread *p_thread) {
  82. ThreadPosix *tp = static_cast<ThreadPosix *>(p_thread);
  83. ERR_FAIL_COND(!tp);
  84. ERR_FAIL_COND(tp->pthread == 0);
  85. pthread_join(tp->pthread, NULL);
  86. tp->pthread = 0;
  87. }
  88. Error ThreadPosix::set_name_func_posix(const String &p_name) {
  89. #ifdef PTHREAD_NO_RENAME
  90. return ERR_UNAVAILABLE;
  91. #else
  92. #ifdef PTHREAD_RENAME_SELF
  93. // check if thread is the same as caller
  94. int err = pthread_setname_np(p_name.utf8().get_data());
  95. #else
  96. pthread_t running_thread = pthread_self();
  97. #ifdef PTHREAD_BSD_SET_NAME
  98. pthread_set_name_np(running_thread, p_name.utf8().get_data());
  99. int err = 0; // Open/FreeBSD ignore errors in this function
  100. #else
  101. int err = pthread_setname_np(running_thread, p_name.utf8().get_data());
  102. #endif // PTHREAD_BSD_SET_NAME
  103. #endif // PTHREAD_RENAME_SELF
  104. return err == 0 ? OK : ERR_INVALID_PARAMETER;
  105. #endif // PTHREAD_NO_RENAME
  106. };
  107. void ThreadPosix::make_default() {
  108. create_func = create_func_posix;
  109. get_thread_id_func = get_thread_id_func_posix;
  110. wait_to_finish_func = wait_to_finish_func_posix;
  111. set_name_func = set_name_func_posix;
  112. }
  113. ThreadPosix::ThreadPosix() {
  114. pthread = 0;
  115. }
  116. ThreadPosix::~ThreadPosix() {
  117. }
  118. #endif