1 | /* |
2 | Copyright (c) 2005-2019 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 "enumerable_thread_specific.h" |
21 | #include "cache_aligned_allocator.h" |
22 | |
23 | namespace tbb { |
24 | /** \name combinable |
25 | **/ |
26 | //@{ |
27 | //! Thread-local storage with optional reduction |
28 | /** @ingroup containers */ |
29 | template <typename T> |
30 | class combinable { |
31 | |
32 | private: |
33 | typedef typename tbb::cache_aligned_allocator<T> my_alloc; |
34 | typedef typename tbb::enumerable_thread_specific<T, my_alloc, ets_no_key> my_ets_type; |
35 | my_ets_type my_ets; |
36 | |
37 | public: |
38 | |
39 | combinable() { } |
40 | |
41 | template <typename finit> |
42 | explicit combinable( finit _finit) : my_ets(_finit) { } |
43 | |
44 | //! destructor |
45 | ~combinable() { } |
46 | |
47 | combinable( const combinable& other) : my_ets(other.my_ets) { } |
48 | |
49 | #if __TBB_ETS_USE_CPP11 |
50 | combinable( combinable&& other) : my_ets( std::move(other.my_ets)) { } |
51 | #endif |
52 | |
53 | combinable & operator=( const combinable & other) { |
54 | my_ets = other.my_ets; |
55 | return *this; |
56 | } |
57 | |
58 | #if __TBB_ETS_USE_CPP11 |
59 | combinable & operator=( combinable && other) { |
60 | my_ets=std::move(other.my_ets); |
61 | return *this; |
62 | } |
63 | #endif |
64 | |
65 | void clear() { my_ets.clear(); } |
66 | |
67 | T& local() { return my_ets.local(); } |
68 | |
69 | T& local(bool & exists) { return my_ets.local(exists); } |
70 | |
71 | // combine_func_t has signature T(T,T) or T(const T&, const T&) |
72 | template <typename combine_func_t> |
73 | T combine(combine_func_t f_combine) { return my_ets.combine(f_combine); } |
74 | |
75 | // combine_func_t has signature void(T) or void(const T&) |
76 | template <typename combine_func_t> |
77 | void combine_each(combine_func_t f_combine) { my_ets.combine_each(f_combine); } |
78 | |
79 | }; |
80 | } // namespace tbb |
81 | #endif /* __TBB_combinable_H */ |
82 | |