From 13c44abaacb045d9b5ade829ea2842739ec67b08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michel=20D=C3=A4nzer?= Date: Wed, 18 Sep 2024 09:42:12 +0200 Subject: [PATCH] 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: 32211788d053 ("util/disk_cache: Add new mesa-db cache type") Reviewed-by: Dmitry Osipenko Tested-by: Dmitry Osipenko Acked-by: Timothy Arceri Part-of: --- src/util/mesa_cache_db.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/util/mesa_cache_db.c b/src/util/mesa_cache_db.c index 7cd0ca4e436..384ac556fe5 100644 --- a/src/util/mesa_cache_db.c +++ b/src/util/mesa_cache_db.c @@ -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); }