semaphore 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // <semaphore> -*- C++ -*-
  2. // Copyright (C) 2020-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  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
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file include/semaphore
  21. * This is a Standard C++ Library header.
  22. */
  23. #ifndef _GLIBCXX_SEMAPHORE
  24. #define _GLIBCXX_SEMAPHORE 1
  25. #pragma GCC system_header
  26. #if __cplusplus > 201703L
  27. #include <bits/semaphore_base.h>
  28. #if __cpp_lib_atomic_wait || _GLIBCXX_HAVE_POSIX_SEMAPHORE
  29. namespace std _GLIBCXX_VISIBILITY(default)
  30. {
  31. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  32. #define __cpp_lib_semaphore 201907L
  33. template<ptrdiff_t __least_max_value = __semaphore_impl::_S_max>
  34. class counting_semaphore
  35. {
  36. static_assert(__least_max_value >= 0);
  37. static_assert(__least_max_value <= __semaphore_impl::_S_max);
  38. __semaphore_impl _M_sem;
  39. public:
  40. explicit counting_semaphore(ptrdiff_t __desired) noexcept
  41. : _M_sem(__desired)
  42. { }
  43. ~counting_semaphore() = default;
  44. counting_semaphore(const counting_semaphore&) = delete;
  45. counting_semaphore& operator=(const counting_semaphore&) = delete;
  46. static constexpr ptrdiff_t
  47. max() noexcept
  48. { return __least_max_value; }
  49. void
  50. release(ptrdiff_t __update = 1) noexcept(noexcept(_M_sem._M_release(1)))
  51. { _M_sem._M_release(__update); }
  52. void
  53. acquire() noexcept(noexcept(_M_sem._M_acquire()))
  54. { _M_sem._M_acquire(); }
  55. bool
  56. try_acquire() noexcept(noexcept(_M_sem._M_try_acquire()))
  57. { return _M_sem._M_try_acquire(); }
  58. template<typename _Rep, typename _Period>
  59. bool
  60. try_acquire_for(const std::chrono::duration<_Rep, _Period>& __rtime)
  61. { return _M_sem._M_try_acquire_for(__rtime); }
  62. template<typename _Clock, typename _Dur>
  63. bool
  64. try_acquire_until(const std::chrono::time_point<_Clock, _Dur>& __atime)
  65. { return _M_sem._M_try_acquire_until(__atime); }
  66. };
  67. using binary_semaphore = std::counting_semaphore<1>;
  68. _GLIBCXX_END_NAMESPACE_VERSION
  69. } // namespace
  70. #endif // cpp_lib_atomic_wait || _GLIBCXX_HAVE_POSIX_SEMAPHORE
  71. #endif // C++20
  72. #endif // _GLIBCXX_SEMAPHORE