threads.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 "opthelpers.h"
  22. #include "threads.h"
  23. #include <system_error>
  24. #ifdef _WIN32
  25. #define WIN32_LEAN_AND_MEAN
  26. #include <windows.h>
  27. #include <limits>
  28. void althrd_setname(const char *name)
  29. {
  30. #if defined(_MSC_VER)
  31. #define MS_VC_EXCEPTION 0x406D1388
  32. #pragma pack(push,8)
  33. struct {
  34. DWORD dwType; // Must be 0x1000.
  35. LPCSTR szName; // Pointer to name (in user addr space).
  36. DWORD dwThreadID; // Thread ID (-1=caller thread).
  37. DWORD dwFlags; // Reserved for future use, must be zero.
  38. } info;
  39. #pragma pack(pop)
  40. info.dwType = 0x1000;
  41. info.szName = name;
  42. info.dwThreadID = ~DWORD{0};
  43. info.dwFlags = 0;
  44. __try {
  45. RaiseException(MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info);
  46. }
  47. __except(EXCEPTION_CONTINUE_EXECUTION) {
  48. }
  49. #undef MS_VC_EXCEPTION
  50. #else
  51. (void)name;
  52. #endif
  53. }
  54. namespace al {
  55. semaphore::semaphore(unsigned int initial)
  56. {
  57. if(initial > static_cast<unsigned int>(std::numeric_limits<int>::max()))
  58. throw std::system_error(std::make_error_code(std::errc::value_too_large));
  59. mSem = CreateSemaphore(nullptr, initial, std::numeric_limits<int>::max(), nullptr);
  60. if(mSem == nullptr)
  61. throw std::system_error(std::make_error_code(std::errc::resource_unavailable_try_again));
  62. }
  63. semaphore::~semaphore()
  64. { CloseHandle(mSem); }
  65. void semaphore::post()
  66. {
  67. if UNLIKELY(!ReleaseSemaphore(static_cast<HANDLE>(mSem), 1, nullptr))
  68. throw std::system_error(std::make_error_code(std::errc::value_too_large));
  69. }
  70. void semaphore::wait() noexcept
  71. { WaitForSingleObject(static_cast<HANDLE>(mSem), INFINITE); }
  72. bool semaphore::try_wait() noexcept
  73. { return WaitForSingleObject(static_cast<HANDLE>(mSem), 0) == WAIT_OBJECT_0; }
  74. } // namespace al
  75. #else
  76. #if defined(HAVE_PTHREAD_SETNAME_NP) || defined(HAVE_PTHREAD_SET_NAME_NP)
  77. #include <pthread.h>
  78. #ifdef HAVE_PTHREAD_NP_H
  79. #include <pthread_np.h>
  80. #endif
  81. void althrd_setname(const char *name)
  82. {
  83. #if defined(HAVE_PTHREAD_SET_NAME_NP)
  84. pthread_set_name_np(pthread_self(), name);
  85. #elif defined(PTHREAD_SETNAME_NP_ONE_PARAM)
  86. pthread_setname_np(name);
  87. #elif defined(PTHREAD_SETNAME_NP_THREE_PARAMS)
  88. pthread_setname_np(pthread_self(), "%s", (void*)name);
  89. #else
  90. pthread_setname_np(pthread_self(), name);
  91. #endif
  92. }
  93. #else
  94. void althrd_setname(const char*) { }
  95. #endif
  96. #ifdef __APPLE__
  97. namespace al {
  98. semaphore::semaphore(unsigned int initial)
  99. {
  100. mSem = dispatch_semaphore_create(initial);
  101. if(!mSem)
  102. throw std::system_error(std::make_error_code(std::errc::resource_unavailable_try_again));
  103. }
  104. semaphore::~semaphore()
  105. { dispatch_release(mSem); }
  106. void semaphore::post()
  107. { dispatch_semaphore_signal(mSem); }
  108. void semaphore::wait() noexcept
  109. { dispatch_semaphore_wait(mSem, DISPATCH_TIME_FOREVER); }
  110. bool semaphore::try_wait() noexcept
  111. { return dispatch_semaphore_wait(mSem, DISPATCH_TIME_NOW) == 0; }
  112. } // namespace al
  113. #else /* !__APPLE__ */
  114. #include <cerrno>
  115. namespace al {
  116. semaphore::semaphore(unsigned int initial)
  117. {
  118. if(sem_init(&mSem, 0, initial) != 0)
  119. throw std::system_error(std::make_error_code(std::errc::resource_unavailable_try_again));
  120. }
  121. semaphore::~semaphore()
  122. { sem_destroy(&mSem); }
  123. void semaphore::post()
  124. {
  125. if(sem_post(&mSem) != 0)
  126. throw std::system_error(std::make_error_code(std::errc::value_too_large));
  127. }
  128. void semaphore::wait() noexcept
  129. {
  130. while(sem_wait(&mSem) == -1 && errno == EINTR) {
  131. }
  132. }
  133. bool semaphore::try_wait() noexcept
  134. { return sem_trywait(&mSem) == 0; }
  135. } // namespace al
  136. #endif /* __APPLE__ */
  137. #endif /* _WIN32 */