File tree Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -139,6 +139,15 @@ class ThrottledLifoSem {
139
139
return static_cast <uint32_t >(state_.load () & kValueMask );
140
140
}
141
141
142
+ // Returns the number of posts that are not assigned yet to existing
143
+ // waiters. This can be negative if there are more waiters than posts.
144
+ int64_t excessValueGuess () const {
145
+ uint64_t state = state_.load (std::memory_order_relaxed);
146
+ uint64_t value = state & kValueMask ;
147
+ uint64_t numWaiters = state >> kNumWaitersShift ;
148
+ return static_cast <int64_t >(value) - static_cast <int64_t >(numWaiters);
149
+ }
150
+
142
151
private:
143
152
friend class ThrottledLifoSemTestHelper ;
144
153
Original file line number Diff line number Diff line change @@ -183,6 +183,30 @@ TEST(ThrottledLifoSem, TryPost) {
183
183
EXPECT_EQ (sem.valueGuess (), 0 );
184
184
}
185
185
186
+ TEST (ThrottledLifoSem, ExcessValueGuess) {
187
+ constexpr size_t kNumWaiters = 64 ;
188
+ // Use a large wakeUpInterval so that waiters do not wake up immediately.
189
+ folly::ThrottledLifoSem sem ({.wakeUpInterval = std::chrono::milliseconds (1 )});
190
+
191
+ std::vector<std::thread> threads;
192
+ for (size_t i = 0 ; i < kNumWaiters ; ++i) {
193
+ threads.emplace_back ([&] { sem.wait (); });
194
+ }
195
+
196
+ folly::ThrottledLifoSemTestHelper::spinUntilWaiters (
197
+ sem, kNumWaiters , /* assertExact */ true );
198
+
199
+ EXPECT_EQ (sem.excessValueGuess (), -static_cast <int64_t >(kNumWaiters ));
200
+ sem.post (kNumWaiters + 1 );
201
+ EXPECT_EQ (sem.excessValueGuess (), 1 );
202
+ // This should be > 1 but it is time-dependent so we cannot assert it.
203
+ LOG (INFO) << " Value: " << sem.valueGuess ();
204
+
205
+ for (auto & t : threads) {
206
+ t.join ();
207
+ }
208
+ }
209
+
186
210
TEST (ThrottledLifoSem, MPMCStress) {
187
211
// Same number of producers and consumers.
188
212
constexpr size_t kNumThreads = 16 ;
You can’t perform that action at this time.
0 commit comments