threads.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /**
  2. * OpenAL cross platform audio library
  3. * Copyright (C) 1999-2007 by authors.
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public
  15. * License along with this library; if not, write to the
  16. * Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. * Or go to http://www.gnu.org/copyleft/lgpl.html
  19. */
  20. #include "config.h"
  21. #include "threads.h"
  22. #include <system_error>
  23. #ifdef _WIN32
  24. #include <limits>
  25. void althrd_setname(const char *name)
  26. {
  27. #if defined(_MSC_VER)
  28. #define MS_VC_EXCEPTION 0x406D1388
  29. #pragma pack(push,8)
  30. struct {
  31. DWORD dwType; // Must be 0x1000.
  32. LPCSTR szName; // Pointer to name (in user addr space).
  33. DWORD dwThreadID; // Thread ID (-1=caller thread).
  34. DWORD dwFlags; // Reserved for future use, must be zero.
  35. } info;
  36. #pragma pack(pop)
  37. info.dwType = 0x1000;
  38. info.szName = name;
  39. info.dwThreadID = ~DWORD{0};
  40. info.dwFlags = 0;
  41. __try {
  42. RaiseException(MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info);
  43. }
  44. __except(EXCEPTION_CONTINUE_EXECUTION) {
  45. }
  46. #undef MS_VC_EXCEPTION
  47. #else
  48. (void)name;
  49. #endif
  50. }
  51. namespace al {
  52. semaphore::semaphore(unsigned int initial)
  53. {
  54. if(initial > static_cast<unsigned int>(std::numeric_limits<int>::max()))
  55. throw std::system_error(std::make_error_code(std::errc::value_too_large));
  56. mSem = CreateSemaphore(nullptr, initial, std::numeric_limits<int>::max(), nullptr);
  57. if(mSem == nullptr)
  58. throw std::system_error(std::make_error_code(std::errc::resource_unavailable_try_again));
  59. }
  60. semaphore::~semaphore()
  61. { CloseHandle(mSem); }
  62. void semaphore::post()
  63. {
  64. if(!ReleaseSemaphore(mSem, 1, nullptr))
  65. throw std::system_error(std::make_error_code(std::errc::value_too_large));
  66. }
  67. void semaphore::wait() noexcept
  68. { WaitForSingleObject(mSem, INFINITE); }
  69. bool semaphore::try_wait() noexcept
  70. { return WaitForSingleObject(mSem, 0) == WAIT_OBJECT_0; }
  71. } // namespace al
  72. #else
  73. #if defined(HAVE_PTHREAD_SETNAME_NP) || defined(HAVE_PTHREAD_SET_NAME_NP)
  74. #include <pthread.h>
  75. #ifdef HAVE_PTHREAD_NP_H
  76. #include <pthread_np.h>
  77. #endif
  78. void althrd_setname(const char *name)
  79. {
  80. #if defined(HAVE_PTHREAD_SET_NAME_NP)
  81. pthread_set_name_np(pthread_self(), name);
  82. #elif defined(PTHREAD_SETNAME_NP_ONE_PARAM)
  83. pthread_setname_np(name);
  84. #elif defined(PTHREAD_SETNAME_NP_THREE_PARAMS)
  85. pthread_setname_np(pthread_self(), "%s", (void*)name);
  86. #else
  87. pthread_setname_np(pthread_self(), name);
  88. #endif
  89. }
  90. #else
  91. void althrd_setname(const char*) { }
  92. #endif
  93. namespace al {
  94. #ifdef __APPLE__
  95. semaphore::semaphore(unsigned int initial)
  96. {
  97. mSem = dispatch_semaphore_create(initial);
  98. if(!mSem)
  99. throw std::system_error(std::make_error_code(std::errc::resource_unavailable_try_again));
  100. }
  101. semaphore::~semaphore()
  102. { dispatch_release(mSem); }
  103. void semaphore::post()
  104. { dispatch_semaphore_signal(mSem); }
  105. void semaphore::wait() noexcept
  106. { dispatch_semaphore_wait(mSem, DISPATCH_TIME_FOREVER); }
  107. bool semaphore::try_wait() noexcept
  108. { return dispatch_semaphore_wait(mSem, DISPATCH_TIME_NOW) == 0; }
  109. #else /* !__APPLE__ */
  110. #include <cerrno>
  111. semaphore::semaphore(unsigned int initial)
  112. {
  113. if(sem_init(&mSem, 0, initial) != 0)
  114. throw std::system_error(std::make_error_code(std::errc::resource_unavailable_try_again));
  115. }
  116. semaphore::~semaphore()
  117. { sem_destroy(&mSem); }
  118. void semaphore::post()
  119. {
  120. if(sem_post(&mSem) != 0)
  121. throw std::system_error(std::make_error_code(std::errc::value_too_large));
  122. }
  123. void semaphore::wait() noexcept
  124. {
  125. while(sem_wait(&mSem) == -1 && errno == EINTR) {
  126. }
  127. }
  128. bool semaphore::try_wait() noexcept
  129. { return sem_trywait(&mSem) == 0; }
  130. #endif /* __APPLE__ */
  131. } // namespace al
  132. #endif /* _WIN32 */