🛠️🐜 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.

165 lines
4.2 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 "threads.h"
  22. #include <limits>
  23. #include <system_error>
  24. #ifdef _WIN32
  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 = -1;
  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. #include <cerrno>
  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_SETNAME_NP)
  81. #if defined(PTHREAD_SETNAME_NP_ONE_PARAM)
  82. pthread_setname_np(name);
  83. #elif defined(PTHREAD_SETNAME_NP_THREE_PARAMS)
  84. pthread_setname_np(pthread_self(), "%s", (void*)name);
  85. #else
  86. pthread_setname_np(pthread_self(), name);
  87. #endif
  88. #elif defined(HAVE_PTHREAD_SET_NAME_NP)
  89. pthread_set_name_np(pthread_self(), name);
  90. #else
  91. (void)name;
  92. #endif
  93. }
  94. namespace al {
  95. #ifdef __APPLE__
  96. semaphore::semaphore(unsigned int initial)
  97. {
  98. mSem = dispatch_semaphore_create(initial);
  99. if(!mSem)
  100. throw std::system_error(std::make_error_code(std::errc::resource_unavailable_try_again));
  101. }
  102. semaphore::~semaphore()
  103. { dispatch_release(mSem); }
  104. void semaphore::post()
  105. { dispatch_semaphore_signal(mSem); }
  106. void semaphore::wait() noexcept
  107. { dispatch_semaphore_wait(mSem, DISPATCH_TIME_FOREVER); }
  108. bool semaphore::try_wait() noexcept
  109. { return dispatch_semaphore_wait(mSem, DISPATCH_TIME_NOW) == 0; }
  110. #else /* !__APPLE__ */
  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 */