thread_posix.cpp 4.8 KB

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