1 /*
2     Copyright 2005-2014 Intel Corporation.  All Rights Reserved.
3 
4     This file is part of Threading Building Blocks. Threading Building Blocks is free software;
5     you can redistribute it and/or modify it under the terms of the GNU General Public License
6     version 2  as  published  by  the  Free Software Foundation.  Threading Building Blocks is
7     distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
8     implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9     See  the GNU General Public License for more details.   You should have received a copy of
10     the  GNU General Public License along with Threading Building Blocks; if not, write to the
11     Free Software Foundation, Inc.,  51 Franklin St,  Fifth Floor,  Boston,  MA 02110-1301 USA
12 
13     As a special exception,  you may use this file  as part of a free software library without
14     restriction.  Specifically,  if other files instantiate templates  or use macros or inline
15     functions from this file, or you compile this file and link it with other files to produce
16     an executable,  this file does not by itself cause the resulting executable to be covered
17     by the GNU General Public License. This exception does not however invalidate any other
18     reasons why the executable file might be covered by the GNU General Public License.
19 */
20 
21 #ifndef __TBB_combinable_H
22 #define __TBB_combinable_H
23 
24 #include "enumerable_thread_specific.h"
25 #include "cache_aligned_allocator.h"
26 
27 namespace tbb {
28 /** \name combinable
29     **/
30 //@{
31 //! Thread-local storage with optional reduction
32 /** @ingroup containers */
33     template <typename T>
34         class combinable {
35     private:
36         typedef typename tbb::cache_aligned_allocator<T> my_alloc;
37 
38         typedef typename tbb::enumerable_thread_specific<T, my_alloc, ets_no_key> my_ets_type;
39         my_ets_type my_ets;
40 
41     public:
42 
combinable()43         combinable() { }
44 
45         template <typename finit>
combinable(finit _finit)46         combinable( finit _finit) : my_ets(_finit) { }
47 
48         //! destructor
~combinable()49         ~combinable() {
50         }
51 
combinable(const combinable & other)52         combinable(const combinable& other) : my_ets(other.my_ets) { }
53 
54         combinable & operator=( const combinable & other) { my_ets = other.my_ets; return *this; }
55 
clear()56         void clear() { my_ets.clear(); }
57 
local()58         T& local() { return my_ets.local(); }
59 
local(bool & exists)60         T& local(bool & exists) { return my_ets.local(exists); }
61 
62         // combine_func_t has signature T(T,T) or T(const T&, const T&)
63         template <typename combine_func_t>
combine(combine_func_t f_combine)64         T combine(combine_func_t f_combine) { return my_ets.combine(f_combine); }
65 
66         // combine_func_t has signature void(T) or void(const T&)
67         template <typename combine_func_t>
combine_each(combine_func_t f_combine)68         void combine_each(combine_func_t f_combine) { my_ets.combine_each(f_combine); }
69 
70     };
71 } // namespace tbb
72 #endif /* __TBB_combinable_H */
73