mirror of
https://gitlab.freedesktop.org/mesa/mesa.git
synced 2026-05-04 20:38:06 +02:00
c11: Improve mtx_timedlock to use timespec_get instead of time(NULL)
Signed-off-by: Yonggang Luo <luoyonggang@gmail.com> Reviewed-by: Jesse Natalie <jenatali@microsoft.com> Acked-by: David Heidelberg <david.heidelberg@collabora.com> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/23733>
This commit is contained in:
parent
66a99f619f
commit
45bd24708a
1 changed files with 20 additions and 4 deletions
|
|
@ -202,6 +202,21 @@ mtx_lock(mtx_t *mtx)
|
||||||
return (pthread_mutex_lock(mtx) == 0) ? thrd_success : thrd_error;
|
return (pthread_mutex_lock(mtx) == 0) ? thrd_success : thrd_error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
threads_timespec_compare(struct timespec *a, struct timespec *b)
|
||||||
|
{
|
||||||
|
if (a->tv_sec < b->tv_sec) {
|
||||||
|
return -1;
|
||||||
|
} else if (a->tv_sec > b->tv_sec) {
|
||||||
|
return 1;
|
||||||
|
} else if (a->tv_nsec < b->tv_nsec) {
|
||||||
|
return -1;
|
||||||
|
} else if (a->tv_nsec > b->tv_nsec) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
// 7.25.4.4
|
// 7.25.4.4
|
||||||
int
|
int
|
||||||
mtx_timedlock(mtx_t *mtx, const struct timespec *ts)
|
mtx_timedlock(mtx_t *mtx, const struct timespec *ts)
|
||||||
|
|
@ -217,11 +232,12 @@ mtx_timedlock(mtx_t *mtx, const struct timespec *ts)
|
||||||
return thrd_success;
|
return thrd_success;
|
||||||
return (rt == ETIMEDOUT) ? thrd_timedout : thrd_error;
|
return (rt == ETIMEDOUT) ? thrd_timedout : thrd_error;
|
||||||
#else
|
#else
|
||||||
time_t expire = time(NULL);
|
|
||||||
expire += ts->tv_sec;
|
|
||||||
while (mtx_trylock(mtx) != thrd_success) {
|
while (mtx_trylock(mtx) != thrd_success) {
|
||||||
time_t now = time(NULL);
|
struct timespec now;
|
||||||
if (expire < now)
|
if (timespec_get(&now, TIME_UTC) != TIME_UTC) {
|
||||||
|
return thrd_error;
|
||||||
|
}
|
||||||
|
if (threads_timespec_compare(ts, &now) < 0)
|
||||||
return thrd_timedout;
|
return thrd_timedout;
|
||||||
// busy loop!
|
// busy loop!
|
||||||
thrd_yield();
|
thrd_yield();
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue