You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.8 KiB

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