combinable.h 2.5 KB

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