Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions thread/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1721,7 +1721,7 @@ R"(
{
assert(h->waitq == (thread_list*)this);
prelocked_thread_interrupt(h, error_number);
assert(h->waitq == nullptr);
// assert(h->waitq == nullptr);
assert(this->q.th != h);
}
return h;
Expand All @@ -1746,6 +1746,7 @@ R"(
if (unlikely(ret)) { errno = ret; return -1; }
if (try_lock() == 0) return 0;
}
again:
splock.lock();
if (try_lock() == 0) {
splock.unlock();
Expand All @@ -1760,6 +1761,10 @@ R"(

int ret = thread_usleep_defer(timeout,
(thread_list*)&q, &spinlock_unlock, &splock);
if (likely(ret < 0 && errno == -1)) {
auto o = owner.load(std::memory_order_acquire);
if (unlikely(o != CURRENT)) { assert(_contending); goto again; }
}
return waitq_translate_errno(ret);
}
int mutex::try_lock()
Expand All @@ -1773,7 +1778,7 @@ R"(
{
SCOPED_LOCK(m->splock);
ScopedLockHead h(m);
m->owner.store(h);
m->owner.store(unlikely(m->_contending) ? nullptr : (thread*)h);
if (h)
prelocked_thread_interrupt(h, -1);
}
Expand Down
4 changes: 3 additions & 1 deletion thread/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,8 @@ namespace photon
class mutex : protected waitq
{
public:
mutex(uint16_t max_retries = 100) : retries(max_retries) { }
mutex(uint16_t max_retries = 100, bool contending = false)
: retries(max_retries), _contending(contending) { }
int lock(Timeout timeout = {});
int try_lock();
void unlock();
Expand All @@ -297,6 +298,7 @@ namespace photon
std::atomic<thread*> owner{nullptr};
uint16_t retries;
spinlock splock;
bool _contending;
};

class seq_mutex : protected mutex {
Expand Down
Loading