xref: /netbsd-src/external/gpl3/gcc/dist/libstdc++-v3/include/std/semaphore (revision b1e838363e3c6fc78a55519254d99869742dd33c)
1*b1e83836Smrg// <semaphore> -*- C++ -*-
2*b1e83836Smrg
3*b1e83836Smrg// Copyright (C) 2020-2022 Free Software Foundation, Inc.
4*b1e83836Smrg//
5*b1e83836Smrg// This file is part of the GNU ISO C++ Library.  This library is free
6*b1e83836Smrg// software; you can redistribute it and/or modify it under the
7*b1e83836Smrg// terms of the GNU General Public License as published by the
8*b1e83836Smrg// Free Software Foundation; either version 3, or (at your option)
9*b1e83836Smrg// any later version.
10*b1e83836Smrg
11*b1e83836Smrg// This library is distributed in the hope that it will be useful,
12*b1e83836Smrg// but WITHOUT ANY WARRANTY; without even the implied warranty of
13*b1e83836Smrg// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*b1e83836Smrg// GNU General Public License for more details.
15*b1e83836Smrg
16*b1e83836Smrg// Under Section 7 of GPL version 3, you are granted additional
17*b1e83836Smrg// permissions described in the GCC Runtime Library Exception, version
18*b1e83836Smrg// 3.1, as published by the Free Software Foundation.
19*b1e83836Smrg
20*b1e83836Smrg// You should have received a copy of the GNU General Public License and
21*b1e83836Smrg// a copy of the GCC Runtime Library Exception along with this program;
22*b1e83836Smrg// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23*b1e83836Smrg// <http://www.gnu.org/licenses/>.
24*b1e83836Smrg
25*b1e83836Smrg/** @file include/semaphore
26*b1e83836Smrg *  This is a Standard C++ Library header.
27*b1e83836Smrg */
28*b1e83836Smrg
29*b1e83836Smrg#ifndef _GLIBCXX_SEMAPHORE
30*b1e83836Smrg#define _GLIBCXX_SEMAPHORE 1
31*b1e83836Smrg
32*b1e83836Smrg#pragma GCC system_header
33*b1e83836Smrg
34*b1e83836Smrg#if __cplusplus > 201703L
35*b1e83836Smrg#include <bits/semaphore_base.h>
36*b1e83836Smrg
37*b1e83836Smrg#if __cpp_lib_atomic_wait || _GLIBCXX_HAVE_POSIX_SEMAPHORE
38*b1e83836Smrgnamespace std _GLIBCXX_VISIBILITY(default)
39*b1e83836Smrg{
40*b1e83836Smrg_GLIBCXX_BEGIN_NAMESPACE_VERSION
41*b1e83836Smrg
42*b1e83836Smrg#define __cpp_lib_semaphore 201907L
43*b1e83836Smrg
44*b1e83836Smrg  template<ptrdiff_t __least_max_value = __semaphore_impl::_S_max>
45*b1e83836Smrg    class counting_semaphore
46*b1e83836Smrg    {
47*b1e83836Smrg      static_assert(__least_max_value >= 0);
48*b1e83836Smrg      static_assert(__least_max_value <= __semaphore_impl::_S_max);
49*b1e83836Smrg
50*b1e83836Smrg      __semaphore_impl _M_sem;
51*b1e83836Smrg
52*b1e83836Smrg    public:
53*b1e83836Smrg      explicit counting_semaphore(ptrdiff_t __desired) noexcept
54*b1e83836Smrg	: _M_sem(__desired)
55*b1e83836Smrg      { }
56*b1e83836Smrg
57*b1e83836Smrg      ~counting_semaphore() = default;
58*b1e83836Smrg
59*b1e83836Smrg      counting_semaphore(const counting_semaphore&) = delete;
60*b1e83836Smrg      counting_semaphore& operator=(const counting_semaphore&) = delete;
61*b1e83836Smrg
62*b1e83836Smrg      static constexpr ptrdiff_t
63*b1e83836Smrg      max() noexcept
64*b1e83836Smrg      { return __least_max_value; }
65*b1e83836Smrg
66*b1e83836Smrg      void
67*b1e83836Smrg      release(ptrdiff_t __update = 1) noexcept(noexcept(_M_sem._M_release(1)))
68*b1e83836Smrg      { _M_sem._M_release(__update); }
69*b1e83836Smrg
70*b1e83836Smrg      void
71*b1e83836Smrg      acquire() noexcept(noexcept(_M_sem._M_acquire()))
72*b1e83836Smrg      { _M_sem._M_acquire(); }
73*b1e83836Smrg
74*b1e83836Smrg      bool
75*b1e83836Smrg      try_acquire() noexcept(noexcept(_M_sem._M_try_acquire()))
76*b1e83836Smrg      { return _M_sem._M_try_acquire(); }
77*b1e83836Smrg
78*b1e83836Smrg      template<typename _Rep, typename _Period>
79*b1e83836Smrg	bool
80*b1e83836Smrg	try_acquire_for(const std::chrono::duration<_Rep, _Period>& __rtime)
81*b1e83836Smrg	{ return _M_sem._M_try_acquire_for(__rtime); }
82*b1e83836Smrg
83*b1e83836Smrg      template<typename _Clock, typename _Dur>
84*b1e83836Smrg	bool
85*b1e83836Smrg	try_acquire_until(const std::chrono::time_point<_Clock, _Dur>& __atime)
86*b1e83836Smrg	{ return _M_sem._M_try_acquire_until(__atime); }
87*b1e83836Smrg    };
88*b1e83836Smrg
89*b1e83836Smrg  using binary_semaphore = std::counting_semaphore<1>;
90*b1e83836Smrg
91*b1e83836Smrg_GLIBCXX_END_NAMESPACE_VERSION
92*b1e83836Smrg} // namespace
93*b1e83836Smrg#endif // cpp_lib_atomic_wait || _GLIBCXX_HAVE_POSIX_SEMAPHORE
94*b1e83836Smrg#endif // C++20
95*b1e83836Smrg#endif // _GLIBCXX_SEMAPHORE
96