Home | History | Annotate | Line # | Download | only in experimental
      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 #pragma GCC system_header
     34 
     35 #include <bits/requires_hosted.h> // for std::mutex
     36 
     37 #if __cplusplus >= 201703L
     38 #include <mutex>
     39 #include <bits/invoke.h>
     40 
     41 namespace std _GLIBCXX_VISIBILITY(default)
     42 {
     43 _GLIBCXX_BEGIN_NAMESPACE_VERSION
     44 namespace experimental::inline concurrency_v2
     45 {
     46 #define __cpp_lib_concurrency_v2_synchronized_value 202302
     47 
     48   template<typename _Tp>
     49     class synchronized_value
     50     {
     51       // TODO: Use partial specialization after PR c++/71954 is fixed.
     52       template<typename... _Args>
     53 	static inline constexpr bool __is_self
     54 	  = sizeof...(_Args) == 1
     55 	      && (is_same_v<__remove_cvref_t<_Args>, synchronized_value> && ...);
     56 
     57 #if ! __cpp_concepts
     58       template<typename... _Args>
     59 	using __not_self = bool_constant<!__is_self<_Args...>>;
     60 #endif
     61 
     62     public:
     63       synchronized_value(const synchronized_value&) = delete;
     64       synchronized_value& operator=(const synchronized_value&) = delete;
     65 
     66 #if __cpp_concepts
     67       template<typename... _Args>
     68 	requires (!__is_self<_Args...>) && is_constructible_v<_Tp, _Args...>
     69 #else
     70       template<typename... _Args, typename = _Require<__not_self<_Args...>>,
     71 	       typename = _Require<is_constructible<_Tp, _Args...>>>
     72 #endif
     73 	synchronized_value(_Args&&... __args)
     74 	noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
     75 	: _M_val(std::forward<_Args>(__args)...)
     76 	{ }
     77 
     78       template<typename _Fn, typename _Up, typename ... _Types>
     79 	friend invoke_result_t<_Fn, _Up&, _Types&...>
     80 	apply(_Fn&&, synchronized_value<_Up>&, synchronized_value<_Types>&...);
     81 
     82     private:
     83       mutex _M_mut;
     84       _Tp _M_val;
     85     };
     86 
     87   template<typename _Fn, typename _Tp, typename... _Types>
     88     inline invoke_result_t<_Fn, _Tp&, _Types&...>
     89     apply(_Fn&& __f, synchronized_value<_Tp>& __val,
     90 	  synchronized_value<_Types>&... __vals)
     91     {
     92       scoped_lock __l(__val._M_mut, __vals._M_mut...);
     93       return std::__invoke(std::forward<_Fn>(__f), __val._M_val,
     94 			   __vals._M_val...);
     95     }
     96 } // namespace experimental::concurrency_v2
     97 _GLIBCXX_END_NAMESPACE_VERSION
     98 } // namespace std
     99 #endif // C++20
    100 #endif // _GLIBCXX_EXPERIMENTAL_SYNCVAL
    101