Skip to content

Commit

Permalink
mutex lock add retries (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
beef9999 committed Jul 25, 2023
1 parent ec4f49c commit e8d082e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
7 changes: 5 additions & 2 deletions thread/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1453,8 +1453,11 @@ R"(
}
int mutex::lock(uint64_t timeout)
{
if (try_lock() == 0)
return 0;
for (int tries = 0; tries < MaxTries; ++tries) {
if (try_lock() == 0)
return 0;
thread_yield();
}
splock.lock();
if (try_lock() == 0) {
splock.unlock();
Expand Down
1 change: 1 addition & 0 deletions thread/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ namespace photon
}

protected:
static constexpr const int MaxTries = 100;
std::atomic<thread*> owner{nullptr};
spinlock splock;
};
Expand Down

0 comments on commit e8d082e

Please sign in to comment.