Skip to content

Commit

Permalink
Thread: internal cleanup
Browse files Browse the repository at this point in the history
Use different, more simple algorithm in wait_for.
Although the very idea of such notifications was rotten.
  • Loading branch information
Nekotekina committed Mar 3, 2020
1 parent d594490 commit 1588180
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 51 deletions.
52 changes: 8 additions & 44 deletions Utilities/Thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1921,48 +1921,12 @@ void thread_ctrl::_wait_for(u64 usec, bool alert /* true */)
}
#endif

std::unique_lock lock(_this->m_mutex, std::defer_lock);

while (true)
if (_this->m_signal.exchange(0))
{
// Mutex is unlocked at the start and after the waiting
if (u32 sig = _this->m_signal.load())
{
if (sig & 1)
{
_this->m_signal &= ~1;
return;
}
}

if (usec == 0)
{
// No timeout: return immediately
return;
}

if (!lock)
{
lock.lock();
}

// Double-check the value
if (u32 sig = _this->m_signal.load())
{
if (sig & 1)
{
_this->m_signal &= ~1;
return;
}
}

_this->m_cond.wait_unlock(usec, lock);

if (usec < cond_variable::max_timeout)
{
usec = 0;
}
return;
}

_this->m_signal.wait(0, atomic_wait_timeout{usec <= 0xffff'ffff'ffff'ffff / 1000 ? usec * 1000 : 0xffff'ffff'ffff'ffff});
}

std::string thread_ctrl::get_name_cached()
Expand Down Expand Up @@ -2012,11 +1976,11 @@ void thread_base::join() const

void thread_base::notify()
{
if (!(m_signal & 1))
// Increment with saturation
if (m_signal.try_inc())
{
m_signal |= 1;
m_mutex.lock_unlock();
m_cond.notify_one();
// Considered impossible to have a situation when not notified
m_signal.notify_all();
}
}

Expand Down
8 changes: 1 addition & 7 deletions Utilities/Thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,7 @@ class thread_base
// Thread handle (platform-specific)
atomic_t<std::uintptr_t> m_thread{0};

// Thread mutex
mutable shared_mutex m_mutex;

// Thread condition variable
cond_variable m_cond;

// Thread flags
// Thread playtoy, that shouldn't be used
atomic_t<u32> m_signal{0};

// Thread state
Expand Down

0 comments on commit 1588180

Please sign in to comment.