vulkan: Use timespec_add_nsec in vk_sync_timeline

Reviewed-By: Mike Blumenkrantz <michael.blumenkrantz@gmail.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/15651>
This commit is contained in:
Jason Ekstrand 2022-03-29 13:14:27 -05:00 committed by Marge Bot
parent 0ba22b203d
commit 2f6bca6a74
1 changed files with 6 additions and 32 deletions

View File

@ -26,6 +26,7 @@
#include <inttypes.h>
#include "util/os_time.h"
#include "util/timespec.h"
#include "vk_alloc.h"
#include "vk_device.h"
@ -420,34 +421,6 @@ vk_sync_timeline_get_value(struct vk_device *device,
return VK_SUCCESS;
}
#define NSEC_PER_SEC 1000000000ull
static bool
timespec_add_ns_overflow(struct timespec ts, uint64_t ns,
struct timespec *ts_out)
{
STATIC_ASSERT(sizeof(ts.tv_sec) <= sizeof(uint64_t));
/* We don't know so assume it's signed */
const uint64_t max_tv_sec = u_intN_max(sizeof(ts.tv_sec) * 8);
if (ns / NSEC_PER_SEC > max_tv_sec)
return true;
if (ts.tv_sec > max_tv_sec - ns / NSEC_PER_SEC)
return true;
ts.tv_sec += ns / NSEC_PER_SEC,
ts.tv_nsec += ns % NSEC_PER_SEC,
ts.tv_sec += ts.tv_nsec / NSEC_PER_SEC;
ts.tv_nsec = ts.tv_nsec % NSEC_PER_SEC;
*ts_out = ts;
return false;
}
static VkResult
vk_sync_timeline_wait_locked(struct vk_device *device,
struct vk_sync_timeline *timeline,
@ -475,13 +448,14 @@ vk_sync_timeline_wait_locked(struct vk_device *device,
*/
uint64_t rel_timeout_ns = abs_timeout_ns - now_ns;
struct timespec abstime;
timespec_get(&abstime, TIME_UTC);
if (timespec_add_ns_overflow(abstime, rel_timeout_ns, &abstime)) {
struct timespec now_ts, abs_timeout_ts;
timespec_get(&now_ts, TIME_UTC);
if (timespec_add_nsec(&abs_timeout_ts, &now_ts, rel_timeout_ns)) {
/* Overflowed; may as well be infinite */
ret = cnd_wait(&timeline->cond, &timeline->mutex);
} else {
ret = cnd_timedwait(&timeline->cond, &timeline->mutex, &abstime);
ret = cnd_timedwait(&timeline->cond, &timeline->mutex,
&abs_timeout_ts);
}
}
if (ret == thrd_error)