修复事件等待指定时间BUG

This commit is contained in:
gb 2024-02-18 17:44:02 +08:00
parent d401f76669
commit 3c8cb3d382
1 changed files with 23 additions and 3 deletions

View File

@ -120,9 +120,29 @@ bool platform_event::wait(unsigned timeout)
sem_wait(&sem_);
else
{
struct timespec to;
to.tv_sec = timeout / 1000;
to.tv_nsec = (long)((timeout % 1000) * 1000 * 1000);
struct timespec to = { 0 };
//to.tv_sec = timeout / 1000;
//to.tv_nsec = (long)((timeout % 1000) * 1000 * 1000);
#if !(defined(WIN32) || defined(_WIN64))
if (clock_gettime(CLOCK_REALTIME, &to) == -1)
{
VLOG_MINI_2(LOG_LEVEL_DEBUG_INFO, "clock_gettime failed: %d - %s\n", errno, strerror(errno));
to.tv_sec = time(nullptr);
}
#endif
/*/
to.tv_nsec += (uint64_t)timeout * 1000 * 1000;
to.tv_sec += to.tv_nsec / (uint64_t)1 * 1000 * 1000 * 1000;
to.tv_nsec %= (uint64_t)1 * 1000 * 1000 * 1000;
/*/
to.tv_sec += timeout / 1000;
to.tv_nsec += (uint64_t)(timeout % 1000) * 1000 * 1000;
if(to.tv_nsec >= (uint64_t)1 * 1000 * 1000 * 1000)
{
to.tv_sec++;
to.tv_nsec -= (uint64_t)1 * 1000 * 1000 * 1000;
}
////////*//////////////////
waited = sem_timedwait(&sem_, &to) == 0;
}
VLOG_MINI_3(LOG_LEVEL_DEBUG_INFO, "platform_event(%s - %s) --> %s.\n", hg_log::format_ptr(this).c_str(), dbg_info_.c_str(), waited ? "waited" : "wait timeout");