xref: /oneTBB/include/oneapi/tbb/combinable.h (revision b15aabb3)
1 /*
2     Copyright (c) 2005-2021 Intel Corporation
3 
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7 
8         http://www.apache.org/licenses/LICENSE-2.0
9 
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15 */
16 
17 #ifndef __TBB_combinable_H
18 #define __TBB_combinable_H
19 
20 #include "detail/_namespace_injection.h"
21 
22 #include "enumerable_thread_specific.h"
23 #include "cache_aligned_allocator.h"
24 
25 namespace tbb {
26 namespace detail {
27 namespace d1 {
28 /** \name combinable **/
29 //@{
30 //! Thread-local storage with optional reduction
31 /** @ingroup containers */
32 template <typename T>
33 class combinable {
34     using my_alloc = typename tbb::cache_aligned_allocator<T>;
35     using my_ets_type = typename tbb::enumerable_thread_specific<T, my_alloc, ets_no_key>;
36     my_ets_type my_ets;
37 
38 public:
39     combinable() = default;
40 
41     template <typename Finit>
combinable(Finit _finit)42     explicit combinable(Finit _finit) : my_ets(_finit) { }
43 
clear()44     void clear() { my_ets.clear(); }
45 
local()46     T& local() { return my_ets.local(); }
47 
local(bool & exists)48     T& local(bool& exists) { return my_ets.local(exists); }
49 
50     // combine_func_t has signature T(T,T) or T(const T&, const T&)
51     template <typename CombineFunc>
combine(CombineFunc f_combine)52     T combine(CombineFunc f_combine) { return my_ets.combine(f_combine); }
53 
54     // combine_func_t has signature void(T) or void(const T&)
55     template <typename CombineFunc>
combine_each(CombineFunc f_combine)56     void combine_each(CombineFunc f_combine) { my_ets.combine_each(f_combine); }
57 };
58 
59 } // namespace d1
60 } // namespace detail
61 
62 inline namespace v1 {
63 using detail::d1::combinable;
64 } // inline namespace v1
65 
66 } // namespace tbb
67 
68 #endif /* __TBB_combinable_H */
69 
70