Skip to content

Commit

Permalink
try_wait for semaphore
Browse files Browse the repository at this point in the history
Summary:
Adding a new "non waitng" version if the `try_wait` that doesn't require a waiter.
This also makes the `BatchSemaphore` aligned with the `Semaphore`.

Reviewed By: Gownta

Differential Revision: D41634112

fbshipit-source-id: c50656b42922b441f6018c275ece3f071c4c910b
  • Loading branch information
Skory authored and facebook-github-bot committed Dec 2, 2022
1 parent 3aefb06 commit a01d38a
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 2 deletions.
4 changes: 4 additions & 0 deletions folly/fibers/BatchSemaphore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ bool BatchSemaphore::try_wait(Waiter& waiter, int64_t tokens) {
return try_wait_common(waiter, tokens);
}

bool BatchSemaphore::try_wait(int64_t tokens) {
return try_wait_common(tokens);
}

#if FOLLY_HAS_COROUTINES

coro::Task<void> BatchSemaphore::co_wait(int64_t tokens) {
Expand Down
6 changes: 6 additions & 0 deletions folly/fibers/BatchSemaphore.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ class BatchSemaphore : public SemaphoreBase {
*/
bool try_wait(Waiter& waiter, int64_t tokens);

/**
* If the semaphore has capacity, removes a token and returns true. Otherwise
* returns false and leaves the semaphore unchanged.
*/
bool try_wait(int64_t tokens);

#if FOLLY_HAS_COROUTINES

/*
Expand Down
14 changes: 14 additions & 0 deletions folly/fibers/SemaphoreBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,20 @@ bool SemaphoreBase::try_wait_common(Waiter& waiter, int64_t tokens) {
return true;
}

bool SemaphoreBase::try_wait_common(int64_t tokens) {
auto oldVal = tokens_.load(std::memory_order_acquire);
do {
if (oldVal < tokens) {
return false;
}
} while (!tokens_.compare_exchange_weak(
oldVal,
oldVal - tokens,
std::memory_order_release,
std::memory_order_acquire));
return true;
}

#if FOLLY_HAS_COROUTINES

coro::Task<void> SemaphoreBase::co_wait_common(int64_t tokens) {
Expand Down
6 changes: 6 additions & 0 deletions folly/fibers/SemaphoreBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ class SemaphoreBase {
*/
bool try_wait_common(Waiter& waiter, int64_t tokens);

/**
* If the semaphore has capacity, removes a token and returns true. Otherwise
* returns false and leaves the semaphore unchanged.
*/
bool try_wait_common(int64_t tokens);

#if FOLLY_HAS_COROUTINES

/*
Expand Down
10 changes: 8 additions & 2 deletions folly/fibers/test/FibersTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1859,8 +1859,8 @@ TEST(FiberManager, batchSemaphore) {
for (size_t i = 0; i < kTasks; ++i) {
manager.addTask([&, completionCounter]() {
for (size_t j = 0; j < kIterations; ++j) {
int tokens = j % 4 + 1;
switch (j % 4) {
int tokens = j % 5 + 1;
switch (j % 5) {
case 0:
sem.wait(tokens);
break;
Expand All @@ -1878,6 +1878,12 @@ TEST(FiberManager, batchSemaphore) {
case 3:
folly::coro::blockingWait(sem.co_wait(tokens));
break;
case 4: {
if (!sem.try_wait(tokens)) {
sem.wait(tokens);
}
break;
}
}
counter += tokens;
sem.signal(tokens);
Expand Down

0 comments on commit a01d38a

Please sign in to comment.