libstdc++: semaphore Source File (original) (raw)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29#ifndef _GLIBCXX_SEMAPHORE

30#define _GLIBCXX_SEMAPHORE 1

31

32#pragma GCC system_header

33

35

36#if __cplusplus > 201703L

38

39#define __glibcxx_want_semaphore

41

42#ifdef __cpp_lib_semaphore

43namespace std _GLIBCXX_VISIBILITY(default)

44{

45_GLIBCXX_BEGIN_NAMESPACE_VERSION

46

47 template<ptrdiff_t __least_max_value = __semaphore_impl::_S_max>

48 class counting_semaphore

49 {

50 static_assert(__least_max_value >= 0);

51 static_assert(__least_max_value <= __semaphore_impl::_S_max);

52

53 __semaphore_impl _M_sem;

54

55 public:

56 explicit counting_semaphore(ptrdiff_t __desired) noexcept

57 : _M_sem(__desired)

58 { }

59

60 ~counting_semaphore() = default;

61

62 counting_semaphore(const counting_semaphore&) = delete;

63 counting_semaphore& operator=(const counting_semaphore&) = delete;

64

65 static constexpr ptrdiff_t

66 max() noexcept

67 { return __least_max_value; }

68

69 void

70 release(ptrdiff_t __update = 1) noexcept(noexcept(_M_sem._M_release(1)))

71 { _M_sem._M_release(__update); }

72

73 void

74 acquire() noexcept(noexcept(_M_sem._M_acquire()))

75 { _M_sem._M_acquire(); }

76

77 bool

78 try_acquire() noexcept(noexcept(_M_sem._M_try_acquire()))

79 { return _M_sem._M_try_acquire(); }

80

81 template<typename _Rep, typename _Period>

82 bool

84 { return _M_sem._M_try_acquire_for(__rtime); }

85

86 template<typename _Clock, typename _Dur>

87 bool

89 { return _M_sem._M_try_acquire_until(__atime); }

90 };

91

92 using binary_semaphore = std::counting_semaphore<1>;

93

94_GLIBCXX_END_NAMESPACE_VERSION

95}

96#endif

97#endif

98#endif

ISO C++ entities toplevel namespace is std.

chrono::duration represents a distance between two points in time

chrono::time_point represents a point in time as measured by a clock