Debian Codegraph @

libstdc++-v3/include/experimental/synchronized_value

raw
1 // <experimental/synchronized_value> -*- C++ -*-
2
3 // Copyright The GNU Toolchain Authors.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24
25 /** @file include/experimental/synchronized_value
26 * This is a TS C++ Library header.
27 * @ingroup libfund-ts
28 */
29
30 #ifndef _GLIBCXX_EXPERIMENTAL_SYNCVAL
31 #define _GLIBCXX_EXPERIMENTAL_SYNCVAL 1
32
33 #ifdef _GLIBCXX_SYSHDR
34 #pragma GCC system_header
35 #endif
36
37 #include <bits/requires_hosted.h> // for std::mutex
38
39 #if __cplusplus >= 201703L
40 #include <mutex>
41 #include <bits/invoke.h>
42
43 namespace std _GLIBCXX_VISIBILITY(default)
44 {
45 _GLIBCXX_BEGIN_NAMESPACE_VERSION
46 namespace experimental::inline concurrency_v2
47 {
48 #define __cpp_lib_concurrency_v2_synchronized_value 202302
49
50 template<typename _Tp>
51 class synchronized_value
52 {
53 // TODO: Use partial specialization after PR c++/71954 is fixed.
54 template<typename... _Args>
55 static inline constexpr bool __is_self
56 = sizeof...(_Args) == 1
57 && (is_same_v<__remove_cvref_t<_Args>, synchronized_value> && ...);
58
59 #if ! __cpp_concepts
60 template<typename... _Args>
61 using __not_self = bool_constant<!__is_self<_Args...>>;
62 #endif
63
64 public:
65 synchronized_value(const synchronized_value&) = delete;
66 synchronized_value& operator=(const synchronized_value&) = delete;
67
68 #if __cpp_concepts
69 template<typename... _Args>
70 requires (!__is_self<_Args...>) && is_constructible_v<_Tp, _Args...>
71 #else
72 template<typename... _Args, typename = _Require<__not_self<_Args...>>,
73 typename = _Require<is_constructible<_Tp, _Args...>>>
74 #endif
75 synchronized_value(_Args&&... __args)
76 noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
77 : _M_val(std::forward<_Args>(__args)...)
78 { }
79
80 template<typename _Fn, typename _Up, typename ... _Types>
81 friend invoke_result_t<_Fn, _Up&, _Types&...>
82 apply(_Fn&&, synchronized_value<_Up>&, synchronized_value<_Types>&...);
83
84 private:
85 mutex _M_mut;
86 _Tp _M_val;
87 };
88
89 template<typename _Fn, typename _Tp, typename... _Types>
90 inline invoke_result_t<_Fn, _Tp&, _Types&...>
91 apply(_Fn&& __f, synchronized_value<_Tp>& __val,
92 synchronized_value<_Types>&... __vals)
93 {
94 scoped_lock __l(__val._M_mut, __vals._M_mut...);
95 return std::__invoke(std::forward<_Fn>(__f), __val._M_val,
96 __vals._M_val...);
97 }
98 } // namespace experimental::concurrency_v2
99 _GLIBCXX_END_NAMESPACE_VERSION
100 } // namespace std
101 #endif // C++20
102 #endif // _GLIBCXX_EXPERIMENTAL_SYNCVAL