🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
4.9 KiB

  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. #include <pthread.h>
  77. #ifdef HAVE_PTHREAD_NP_H
  78. #include <pthread_np.h>
  79. #endif
  80. #include <tuple>
  81. namespace {
  82. using setname_t1 = int(*)(const char*);
  83. using setname_t2 = int(*)(pthread_t, const char*);
  84. using setname_t3 = int(*)(pthread_t, const char*, void*);
  85. void setname_caller(setname_t1 func, const char *name)
  86. { func(name); }
  87. void setname_caller(setname_t2 func, const char *name)
  88. { func(pthread_self(), name); }
  89. void setname_caller(setname_t3 func, const char *name)
  90. { func(pthread_self(), "%s", static_cast<void*>(const_cast<char*>(name))); }
  91. } // namespace
  92. void althrd_setname(const char *name)
  93. {
  94. #if defined(HAVE_PTHREAD_SET_NAME_NP)
  95. setname_caller(pthread_set_name_np, name);
  96. #elif defined(HAVE_PTHREAD_SETNAME_NP)
  97. setname_caller(pthread_setname_np, name);
  98. #endif
  99. /* Avoid unused function/parameter warnings. */
  100. std::ignore = name;
  101. std::ignore = static_cast<void(*)(setname_t1,const char*)>(&setname_caller);
  102. std::ignore = static_cast<void(*)(setname_t2,const char*)>(&setname_caller);
  103. std::ignore = static_cast<void(*)(setname_t3,const char*)>(&setname_caller);
  104. }
  105. #ifdef __APPLE__
  106. namespace al {
  107. semaphore::semaphore(unsigned int initial)
  108. {
  109. mSem = dispatch_semaphore_create(initial);
  110. if(!mSem)
  111. throw std::system_error(std::make_error_code(std::errc::resource_unavailable_try_again));
  112. }
  113. semaphore::~semaphore()
  114. { dispatch_release(mSem); }
  115. void semaphore::post()
  116. { dispatch_semaphore_signal(mSem); }
  117. void semaphore::wait() noexcept
  118. { dispatch_semaphore_wait(mSem, DISPATCH_TIME_FOREVER); }
  119. bool semaphore::try_wait() noexcept
  120. { return dispatch_semaphore_wait(mSem, DISPATCH_TIME_NOW) == 0; }
  121. } // namespace al
  122. #else /* !__APPLE__ */
  123. #include <cerrno>
  124. namespace al {
  125. semaphore::semaphore(unsigned int initial)
  126. {
  127. if(sem_init(&mSem, 0, initial) != 0)
  128. throw std::system_error(std::make_error_code(std::errc::resource_unavailable_try_again));
  129. }
  130. semaphore::~semaphore()
  131. { sem_destroy(&mSem); }
  132. void semaphore::post()
  133. {
  134. if(sem_post(&mSem) != 0)
  135. throw std::system_error(std::make_error_code(std::errc::value_too_large));
  136. }
  137. void semaphore::wait() noexcept
  138. {
  139. while(sem_wait(&mSem) == -1 && errno == EINTR) {
  140. }
  141. }
  142. bool semaphore::try_wait() noexcept
  143. { return sem_trywait(&mSem) == 0; }
  144. } // namespace al
  145. #endif /* __APPLE__ */
  146. #endif /* _WIN32 */