util/mesa-db: Make mesa_db_lock robust against signals

flock may be interrupted by a signal, in which case it returns with
EINTR error. In this case we need to retry until it returns success
or another error.

Fixes: 32211788d0 ("util/disk_cache: Add new mesa-db cache type")
Reviewed-by: Dmitry Osipenko <dmitry.osipenko@collabora.com>
Tested-by: Dmitry Osipenko <dmitry.osipenko@collabora.com>
Acked-by: Timothy Arceri <tarceri@itsqueeze.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/30988>
This commit is contained in:
Michel Dänzer 2024-09-18 09:42:12 +02:00 committed by Marge Bot
parent d01c1ba939
commit 13c44abaac

View file

@ -90,21 +90,33 @@ static inline bool mesa_db_truncate(FILE *file, long pos)
return !ftruncate(fileno(file), pos);
}
static int
mesa_db_flock(FILE *file, int op)
{
int ret;
do {
ret = flock(fileno(file), op);
} while (ret < 0 && errno == EINTR);
return ret;
}
static bool
mesa_db_lock(struct mesa_cache_db *db)
{
simple_mtx_lock(&db->flock_mtx);
if (flock(fileno(db->cache.file), LOCK_EX) == -1)
if (mesa_db_flock(db->cache.file, LOCK_EX) < 0)
goto unlock_mtx;
if (flock(fileno(db->index.file), LOCK_EX) == -1)
if (mesa_db_flock(db->index.file, LOCK_EX) < 0)
goto unlock_cache;
return true;
unlock_cache:
flock(fileno(db->cache.file), LOCK_UN);
mesa_db_flock(db->cache.file, LOCK_UN);
unlock_mtx:
simple_mtx_unlock(&db->flock_mtx);
@ -114,8 +126,8 @@ unlock_mtx:
static void
mesa_db_unlock(struct mesa_cache_db *db)
{
flock(fileno(db->index.file), LOCK_UN);
flock(fileno(db->cache.file), LOCK_UN);
mesa_db_flock(db->index.file, LOCK_UN);
mesa_db_flock(db->cache.file, LOCK_UN);
simple_mtx_unlock(&db->flock_mtx);
}