Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BLI_enumerable_thread_specific.hh « blenlib « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b5981028893c135cd1015c818dafa976501d8be6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#pragma once

#ifdef WITH_TBB
#  include <tbb/enumerable_thread_specific.h>
#endif

#include <atomic>
#include <mutex>

#include "BLI_map.hh"
#include "BLI_utility_mixins.hh"

namespace blender::threading {

namespace enumerable_thread_specific_utils {
inline std::atomic<int> next_id = 0;
inline thread_local int thread_id = next_id.fetch_add(1, std::memory_order_relaxed);
}  // namespace enumerable_thread_specific_utils

/**
 * This is mainly a wrapper for `tbb::enumerable_thread_specific`. The wrapper is needed because we
 * want to be able to build without tbb.
 *
 * More features of the tbb version can be wrapped when they are used.
 */
template<typename T> class EnumerableThreadSpecific : NonCopyable, NonMovable {
#ifdef WITH_TBB

 private:
  tbb::enumerable_thread_specific<T> values_;

 public:
  using iterator = typename tbb::enumerable_thread_specific<T>::iterator;

  EnumerableThreadSpecific() = default;

  template<typename F> EnumerableThreadSpecific(F initializer) : values_(std::move(initializer))
  {
  }

  T &local()
  {
    return values_.local();
  }

  iterator begin()
  {
    return values_.begin();
  }

  iterator end()
  {
    return values_.end();
  }

#else /* WITH_TBB */

 private:
  std::mutex mutex_;
  /* Maps thread ids to their corresponding values. The values are not embedded in the map, so that
   * their addresses do not change when the map grows. */
  Map<int, std::reference_wrapper<T>> values_;
  Vector<std::unique_ptr<T>> owned_values_;
  std::function<void(void *)> initializer_;

 public:
  using iterator = typename Map<int, std::reference_wrapper<T>>::MutableValueIterator;

  EnumerableThreadSpecific() : initializer_([](void *buffer) { new (buffer) T(); })
  {
  }

  template<typename F>
  EnumerableThreadSpecific(F initializer)
      : initializer_([=](void *buffer) { new (buffer) T(initializer()); })
  {
  }

  T &local()
  {
    const int thread_id = enumerable_thread_specific_utils::thread_id;
    std::lock_guard lock{mutex_};
    return values_.lookup_or_add_cb(thread_id, [&]() {
      T *value = (T *)::operator new(sizeof(T));
      initializer_(value);
      owned_values_.append(std::unique_ptr<T>{value});
      return std::reference_wrapper<T>{*value};
    });
  }

  iterator begin()
  {
    return values_.values().begin();
  }

  iterator end()
  {
    return values_.values().end();
  }

#endif /* WITH_TBB */
};

}  // namespace blender::threading