thread_posix.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*************************************************************************/
  2. /* thread_posix.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.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 "os/memory.h"
  37. Thread::ID ThreadPosix::get_id() const {
  38. return id;
  39. }
  40. Thread *ThreadPosix::create_thread_posix() {
  41. return memnew(ThreadPosix);
  42. }
  43. void *ThreadPosix::thread_callback(void *userdata) {
  44. ThreadPosix *t = reinterpret_cast<ThreadPosix *>(userdata);
  45. t->id = (ID)pthread_self();
  46. ScriptServer::thread_enter(); //scripts may need to attach a stack
  47. t->callback(t->user);
  48. ScriptServer::thread_exit();
  49. return NULL;
  50. }
  51. Thread *ThreadPosix::create_func_posix(ThreadCreateCallback p_callback, void *p_user, const Settings &) {
  52. ThreadPosix *tr = memnew(ThreadPosix);
  53. tr->callback = p_callback;
  54. tr->user = p_user;
  55. pthread_attr_init(&tr->pthread_attr);
  56. pthread_attr_setdetachstate(&tr->pthread_attr, PTHREAD_CREATE_JOINABLE);
  57. pthread_attr_setstacksize(&tr->pthread_attr, 256 * 1024);
  58. pthread_create(&tr->pthread, &tr->pthread_attr, thread_callback, tr);
  59. return tr;
  60. }
  61. Thread::ID ThreadPosix::get_thread_id_func_posix() {
  62. return (ID)pthread_self();
  63. }
  64. void ThreadPosix::wait_to_finish_func_posix(Thread *p_thread) {
  65. ThreadPosix *tp = static_cast<ThreadPosix *>(p_thread);
  66. ERR_FAIL_COND(!tp);
  67. ERR_FAIL_COND(tp->pthread == 0);
  68. pthread_join(tp->pthread, NULL);
  69. tp->pthread = 0;
  70. }
  71. Error ThreadPosix::set_name_func_posix(const String &p_name) {
  72. pthread_t running_thread = pthread_self();
  73. #ifdef PTHREAD_NO_RENAME
  74. return ERR_UNAVAILABLE;
  75. #else
  76. #ifdef PTHREAD_RENAME_SELF
  77. // check if thread is the same as caller
  78. int err = pthread_setname_np(p_name.utf8().get_data());
  79. #else
  80. #ifdef PTHREAD_BSD_SET_NAME
  81. pthread_set_name_np(running_thread, p_name.utf8().get_data());
  82. int err = 0; // Open/FreeBSD ignore errors in this function
  83. #else
  84. int err = pthread_setname_np(running_thread, p_name.utf8().get_data());
  85. #endif // PTHREAD_BSD_SET_NAME
  86. #endif // PTHREAD_RENAME_SELF
  87. return err == 0 ? OK : ERR_INVALID_PARAMETER;
  88. #endif // PTHREAD_NO_RENAME
  89. };
  90. void ThreadPosix::make_default() {
  91. create_func = create_func_posix;
  92. get_thread_id_func = get_thread_id_func_posix;
  93. wait_to_finish_func = wait_to_finish_func_posix;
  94. set_name_func = set_name_func_posix;
  95. }
  96. ThreadPosix::ThreadPosix() {
  97. pthread = 0;
  98. }
  99. ThreadPosix::~ThreadPosix() {
  100. }
  101. #endif