return false;
simple_mtx_init(&foz_db->mtx, mtx_plain);
+ simple_mtx_init(&foz_db->flock_mtx, mtx_plain);
foz_db->mem_ctx = ralloc_context(NULL);
foz_db->index_db = _mesa_hash_table_u64_create(NULL);
if (foz_db->mem_ctx) {
_mesa_hash_table_u64_destroy(foz_db->index_db);
ralloc_free(foz_db->mem_ctx);
+ simple_mtx_destroy(&foz_db->flock_mtx);
simple_mtx_destroy(&foz_db->mtx);
}
}
if (!foz_db->alive)
return false;
- /* Wait for 1 second. This is done outside of the mutex as I believe there is more potential
+ /* The flock is per-fd, not per thread, we do it outside of the main mutex to avoid having to
+ * wait in the mutex potentially blocking reads. We use the secondary flock_mtx to stop race
+ * conditions between the write threads sharing the same file descriptor. */
+ simple_mtx_lock(&foz_db->flock_mtx);
+
+ /* Wait for 1 second. This is done outside of the main mutex as I believe there is more potential
* for file contention than mtx contention of significant length. */
int err = lock_file_with_timeout(foz_db->file[0], 1000000000);
if (err == -1)
if (entry) {
simple_mtx_unlock(&foz_db->mtx);
flock(fileno(foz_db->file[0]), LOCK_UN);
+ simple_mtx_unlock(&foz_db->flock_mtx);
return NULL;
}
simple_mtx_unlock(&foz_db->mtx);
flock(fileno(foz_db->file[0]), LOCK_UN);
+ simple_mtx_unlock(&foz_db->flock_mtx);
return true;
simple_mtx_unlock(&foz_db->mtx);
fail_file:
flock(fileno(foz_db->file[0]), LOCK_UN);
+ simple_mtx_unlock(&foz_db->flock_mtx);
return false;
}
#else
FILE *file[FOZ_MAX_DBS]; /* An array of all foz dbs */
FILE *db_idx; /* The default writable foz db idx */
simple_mtx_t mtx; /* Mutex for file/hash table read/writes */
+ simple_mtx_t flock_mtx; /* Mutex for flocking the file for writes */
void *mem_ctx;
struct hash_table_u64 *index_db; /* Hash table of all foz db entries */
bool alive;