10 #ifndef EIGEN_CXX11_THREADPOOL_RUNQUEUE_H_ 11 #define EIGEN_CXX11_THREADPOOL_RUNQUEUE_H_ 38 template <
typename Work,
unsigned kSize>
46 for (
unsigned i = 0; i < kSize; i++)
47 array_[i].state.store(
kEmpty, std::memory_order_relaxed);
55 unsigned front =
front_.load(std::memory_order_relaxed);
57 uint8_t
s = e->
state.load(std::memory_order_relaxed);
59 !e->
state.compare_exchange_strong(s,
kBusy, std::memory_order_acquire))
61 front_.store(front + 1 + (kSize << 1), std::memory_order_relaxed);
70 unsigned front =
front_.load(std::memory_order_relaxed);
72 uint8_t
s = e->
state.load(std::memory_order_relaxed);
74 !e->
state.compare_exchange_strong(s,
kBusy, std::memory_order_acquire))
76 Work
w = std::move(e->
w);
79 front_.store(front, std::memory_order_relaxed);
86 std::unique_lock<std::mutex> lock(
mutex_);
87 unsigned back =
back_.load(std::memory_order_relaxed);
89 uint8_t
s = e->
state.load(std::memory_order_relaxed);
91 !e->
state.compare_exchange_strong(s,
kBusy, std::memory_order_acquire))
94 back_.store(back, std::memory_order_relaxed);
103 if (
Empty())
return Work();
104 std::unique_lock<std::mutex> lock(
mutex_, std::try_to_lock);
105 if (!lock)
return Work();
106 unsigned back =
back_.load(std::memory_order_relaxed);
108 uint8_t
s = e->
state.load(std::memory_order_relaxed);
110 !e->
state.compare_exchange_strong(s,
kBusy, std::memory_order_acquire))
112 Work
w = std::move(e->
w);
114 back_.store(back + 1 + (kSize << 1), std::memory_order_relaxed);
121 if (
Empty())
return 0;
122 std::unique_lock<std::mutex> lock(
mutex_, std::try_to_lock);
124 unsigned back =
back_.load(std::memory_order_relaxed);
127 if (size > 1) mid = back + (size - 1) / 2;
130 for (;
static_cast<int>(mid - back) >= 0; mid--) {
132 uint8_t
s = e->
state.load(std::memory_order_relaxed);
136 std::memory_order_acquire))
144 result->push_back(std::move(e->
w));
149 back_.store(start + 1 + (kSize << 1), std::memory_order_relaxed);
160 unsigned front =
front_.load(std::memory_order_acquire);
161 unsigned back =
back_.load(std::memory_order_acquire);
162 unsigned front1 =
front_.load(std::memory_order_relaxed);
163 if (front != front1)
continue;
166 if (size < 0) size += 2 * kSize;
171 if (size > static_cast<int>(kSize)) size = kSize;
181 static const unsigned kMask = kSize - 1;
182 static const unsigned kMask2 = (kSize << 1) - 1;
210 #endif // EIGEN_CXX11_THREADPOOL_RUNQUEUE_H_
std::atomic< unsigned > back_
std::atomic< uint8_t > state
static constexpr size_t size(Tuple< Args... > &)
Provides access to the number of elements in a tuple as a compile-time constant expression.
void operator=(const RunQueue &)=delete
static const unsigned kMask2
std::atomic< unsigned > front_
static const unsigned kMask
TFSIMD_FORCE_INLINE const tfScalar & w() const
unsigned PopBackHalf(std::vector< Work > *result)