alsem.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 "alsem.h"
  22. #include <system_error>
  23. #ifdef _WIN32
  24. #define WIN32_LEAN_AND_MEAN
  25. #include <windows.h>
  26. #include <limits>
  27. namespace al {
  28. semaphore::semaphore(unsigned int initial)
  29. {
  30. if(initial > static_cast<unsigned int>(std::numeric_limits<int>::max()))
  31. throw std::system_error(std::make_error_code(std::errc::value_too_large));
  32. mSem = CreateSemaphoreW(nullptr, static_cast<LONG>(initial), std::numeric_limits<int>::max(),
  33. nullptr);
  34. if(mSem == nullptr)
  35. throw std::system_error(std::make_error_code(std::errc::resource_unavailable_try_again));
  36. }
  37. semaphore::~semaphore()
  38. { CloseHandle(mSem); }
  39. void semaphore::post()
  40. {
  41. if(!ReleaseSemaphore(static_cast<HANDLE>(mSem), 1, nullptr))
  42. throw std::system_error(std::make_error_code(std::errc::value_too_large));
  43. }
  44. void semaphore::wait() noexcept
  45. { WaitForSingleObject(static_cast<HANDLE>(mSem), INFINITE); }
  46. bool semaphore::try_wait() noexcept
  47. { return WaitForSingleObject(static_cast<HANDLE>(mSem), 0) == WAIT_OBJECT_0; }
  48. } // namespace al
  49. #else
  50. /* Do not try using libdispatch on systems where it is absent. */
  51. #if defined(AL_APPLE_HAVE_DISPATCH)
  52. namespace al {
  53. semaphore::semaphore(unsigned int initial)
  54. {
  55. mSem = dispatch_semaphore_create(initial);
  56. if(!mSem)
  57. throw std::system_error(std::make_error_code(std::errc::resource_unavailable_try_again));
  58. }
  59. semaphore::~semaphore()
  60. { dispatch_release(mSem); }
  61. void semaphore::post()
  62. { dispatch_semaphore_signal(mSem); }
  63. void semaphore::wait() noexcept
  64. { dispatch_semaphore_wait(mSem, DISPATCH_TIME_FOREVER); }
  65. bool semaphore::try_wait() noexcept
  66. { return dispatch_semaphore_wait(mSem, DISPATCH_TIME_NOW) == 0; }
  67. } // namespace al
  68. #else /* !__APPLE__ */
  69. #include <cerrno>
  70. namespace al {
  71. semaphore::semaphore(unsigned int initial)
  72. {
  73. if(sem_init(&mSem, 0, initial) != 0)
  74. throw std::system_error(std::make_error_code(std::errc::resource_unavailable_try_again));
  75. }
  76. semaphore::~semaphore()
  77. { sem_destroy(&mSem); }
  78. void semaphore::post()
  79. {
  80. if(sem_post(&mSem) != 0)
  81. throw std::system_error(std::make_error_code(std::errc::value_too_large));
  82. }
  83. void semaphore::wait() noexcept
  84. {
  85. while(sem_wait(&mSem) == -1 && errno == EINTR) {
  86. }
  87. }
  88. bool semaphore::try_wait() noexcept
  89. { return sem_trywait(&mSem) == 0; }
  90. } // namespace al
  91. #endif /* __APPLE__ */
  92. #endif /* _WIN32 */