thread_posix.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "thread_posix.h"
  30. #if defined(UNIX_ENABLED) || defined(PTHREAD_ENABLED)
  31. #ifdef PTHREAD_BSD_SET_NAME
  32. #include <pthread_np.h>
  33. #endif
  34. #include "os/memory.h"
  35. Thread::ID ThreadPosix::get_ID() const {
  36. return id;
  37. }
  38. Thread* ThreadPosix::create_thread_posix() {
  39. return memnew( ThreadPosix );
  40. }
  41. void *ThreadPosix::thread_callback(void *userdata) {
  42. ThreadPosix *t=reinterpret_cast<ThreadPosix*>(userdata);
  43. t->id=(ID)pthread_self();
  44. t->callback(t->user);
  45. return NULL;
  46. }
  47. Thread* ThreadPosix::create_func_posix(ThreadCreateCallback p_callback,void *p_user,const Settings&) {
  48. ThreadPosix *tr= memnew(ThreadPosix);
  49. tr->callback=p_callback;
  50. tr->user=p_user;
  51. pthread_attr_init(&tr->pthread_attr);
  52. pthread_attr_setdetachstate(&tr->pthread_attr, PTHREAD_CREATE_JOINABLE);
  53. pthread_attr_setstacksize(&tr->pthread_attr, 256 * 1024);
  54. pthread_create(&tr->pthread, &tr->pthread_attr, thread_callback, tr);
  55. return tr;
  56. }
  57. Thread::ID ThreadPosix::get_thread_ID_func_posix() {
  58. return (ID)pthread_self();
  59. }
  60. void ThreadPosix::wait_to_finish_func_posix(Thread* p_thread) {
  61. ThreadPosix *tp=static_cast<ThreadPosix*>(p_thread);
  62. ERR_FAIL_COND(!tp);
  63. ERR_FAIL_COND(tp->pthread==0);
  64. pthread_join(tp->pthread,NULL);
  65. tp->pthread=0;
  66. }
  67. Error ThreadPosix::set_name_func_posix(const String& p_name) {
  68. pthread_t running_thread = pthread_self();
  69. #ifdef PTHREAD_NO_RENAME
  70. return ERR_UNAVAILABLE;
  71. #else
  72. #ifdef PTHREAD_RENAME_SELF
  73. // check if thread is the same as caller
  74. int err = pthread_setname_np(p_name.utf8().get_data());
  75. #else
  76. #ifdef PTHREAD_BSD_SET_NAME
  77. pthread_set_name_np(running_thread, p_name.utf8().get_data());
  78. int err = 0; // Open/FreeBSD ignore errors in this function
  79. #else
  80. int err = pthread_setname_np(running_thread, p_name.utf8().get_data());
  81. #endif // PTHREAD_BSD_SET_NAME
  82. #endif // PTHREAD_RENAME_SELF
  83. return err == 0 ? OK : ERR_INVALID_PARAMETER;
  84. #endif // PTHREAD_NO_RENAME
  85. };
  86. void ThreadPosix::make_default() {
  87. create_func=create_func_posix;
  88. get_thread_ID_func=get_thread_ID_func_posix;
  89. wait_to_finish_func=wait_to_finish_func_posix;
  90. set_name_func = set_name_func_posix;
  91. }
  92. ThreadPosix::ThreadPosix() {
  93. pthread=0;
  94. }
  95. ThreadPosix::~ThreadPosix() {
  96. }
  97. #endif