1
0
mirror of https://github.com/RPCS3/rpcs3.git synced 2024-11-25 04:02:42 +01:00

Threads: move linux m_timer to static thread_local variable

Allows lazy allocation of the timer handle.
This commit is contained in:
Nekotekina 2020-03-03 16:36:59 +03:00
parent 5b0476e772
commit 6c66153372
2 changed files with 30 additions and 23 deletions

View File

@ -1800,14 +1800,6 @@ void thread_base::initialize(bool(*wait_cb)(const void*))
name.resize(std::min<std::size_t>(15, name.size()));
pthread_setname_np(pthread_self(), name.c_str());
#endif
#ifdef __linux__
m_timer = timerfd_create(CLOCK_MONOTONIC, 0);
if (m_timer == -1)
{
sig_log.error("Linux timer allocation failed, use wait_unlock() only");
}
#endif
}
void thread_base::notify_abort() noexcept
@ -1823,13 +1815,6 @@ bool thread_base::finalize(int) noexcept
// Report pending errors
error_code::error_report(0, 0, 0, 0);
#ifdef __linux__
if (m_timer != -1)
{
close(m_timer);
}
#endif
#ifdef _WIN32
ULONG64 cycles{};
QueryThreadCycleTime(GetCurrentThread(), &cycles);
@ -1892,7 +1877,34 @@ void thread_ctrl::_wait_for(u64 usec, bool alert /* true */)
auto _this = g_tls_this_thread;
#ifdef __linux__
if (!alert && _this->m_timer != -1 && usec > 0 && usec <= 1000)
static thread_local struct linux_timer_handle_t
{
// Allocate timer only if needed (i.e. someone calls _wait_for with alert and short period)
const int m_timer = timerfd_create(CLOCK_MONOTONIC, 0);
linux_timer_handle_t() noexcept
{
if (m_timer == -1)
{
sig_log.error("Linux timer allocation failed, using the fallback instead.");
}
}
operator int() const
{
return m_timer;
}
~linux_timer_handle_t()
{
if (m_timer != -1)
{
close(m_timer);
}
}
} fd_timer;
if (!alert && usec > 0 && usec <= 1000 && fd_timer != -1)
{
struct itimerspec timeout;
u64 missed;
@ -1902,8 +1914,8 @@ void thread_ctrl::_wait_for(u64 usec, bool alert /* true */)
timeout.it_value.tv_sec = nsec / 1000000000ull;
timeout.it_interval.tv_sec = 0;
timeout.it_interval.tv_nsec = 0;
timerfd_settime(_this->m_timer, 0, &timeout, NULL);
if (read(_this->m_timer, &missed, sizeof(missed)) != sizeof(missed))
timerfd_settime(fd_timer, 0, &timeout, NULL);
if (read(fd_timer, &missed, sizeof(missed)) != sizeof(missed))
sig_log.error("timerfd: read() failed");
return;
}

View File

@ -97,11 +97,6 @@ class thread_base
using native_entry = void*(*)(void* arg);
#endif
#ifdef __linux__
// Linux thread timer
int m_timer = -1;
#endif
// Thread handle (platform-specific)
atomic_t<std::uintptr_t> m_thread{0};