files_ref_store: put the packed files lock directly in this struct
Instead of using a global `lock_file` instance for the main "packed-refs" file and using a pointer in `files_ref_store` to keep track of whether it is locked, embed the `lock_file` instance directly in the `files_ref_store` struct and use the new `is_lock_file_locked()` function to keep track of whether it is locked. This keeps related data together and makes the main reference store less of a special case. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Michael Haggerty committed
May 22, 2017 at 16:17 UTC
00d174489e9905411dbae5f895758f2ca489bd9f
1 file changed
+11
-18
refs/files-backend.c
+11
-18
@@ -63,16 +63,12 @@ struct files_ref_store {
63
struct packed_ref_cache *packed;
64
65
/*
66
- * Iff the packed-refs file associated with this instance is
67
- * currently locked for writing, this points at the associated
68
- * lock (which is owned by somebody else).
66
+ * Lock used for the "packed-refs" file. Note that this (and
67
+ * thus the enclosing `files_ref_store`) must not be freed.
68
*/
70
- struct lock_file *packed_refs_lock;
69
+ struct lock_file packed_refs_lock;
70
};
71
73
-/* Lock used for the main packed-refs file: */
74
-static struct lock_file packlock;
75
-
72
/*
73
* Increment the reference count of *packed_refs.
74
*/
@@ -102,7 +98,7 @@ static void clear_packed_ref_cache(struct files_ref_store *refs)
98
if (refs->packed) {
99
struct packed_ref_cache *packed_refs = refs->packed;
100
105
- if (refs->packed_refs_lock)
101
+ if (is_lock_file_locked(&refs->packed_refs_lock))
102
die("BUG: packed-ref cache cleared while locked");
103
refs->packed = NULL;
104
release_packed_ref_cache(packed_refs);
@@ -394,7 +390,7 @@ static void add_packed_ref(struct files_ref_store *refs,
390
{
391
struct packed_ref_cache *packed_ref_cache = get_packed_ref_cache(refs);
392
397
- if (!refs->packed_refs_lock)
393
+ if (!is_lock_file_locked(&refs->packed_refs_lock))
394
die("BUG: packed refs not locked");
395
add_ref_entry(get_packed_ref_dir(packed_ref_cache),
396
create_ref_entry(refname, oid, REF_ISPACKED, 1));
@@ -1288,7 +1284,7 @@ static int lock_packed_refs(struct files_ref_store *refs, int flags)
1284
}
1285
1286
if (hold_lock_file_for_update_timeout(
1291
- &packlock, files_packed_refs_path(refs),
1287
+ &refs->packed_refs_lock, files_packed_refs_path(refs),
1288
flags, timeout_value) < 0)
1289
return -1;
1290
/*
@@ -1298,7 +1294,6 @@ static int lock_packed_refs(struct files_ref_store *refs, int flags)
1294
* the packed-refs file.
1295
*/
1296
packed_ref_cache = get_packed_ref_cache(refs);
1301
- refs->packed_refs_lock = &packlock;
1297
/* Increment the reference count to prevent it from being freed: */
1298
acquire_packed_ref_cache(packed_ref_cache);
1299
return 0;
@@ -1321,10 +1316,10 @@ static int commit_packed_refs(struct files_ref_store *refs)
1316
1317
files_assert_main_repository(refs, "commit_packed_refs");
1318
1324
- if (!refs->packed_refs_lock)
1319
+ if (!is_lock_file_locked(&refs->packed_refs_lock))
1320
die("BUG: packed-refs not locked");
1321
1327
- out = fdopen_lock_file(refs->packed_refs_lock, "w");
1322
+ out = fdopen_lock_file(&refs->packed_refs_lock, "w");
1323
if (!out)
1324
die_errno("unable to fdopen packed-refs descriptor");
1325
@@ -1342,11 +1337,10 @@ static int commit_packed_refs(struct files_ref_store *refs)
1337
if (ok != ITER_DONE)
1338
die("error while iterating over references");
1339
1345
- if (commit_lock_file(refs->packed_refs_lock)) {
1340
+ if (commit_lock_file(&refs->packed_refs_lock)) {
1341
save_errno = errno;
1342
error = -1;
1343
}
1349
- refs->packed_refs_lock = NULL;
1344
release_packed_ref_cache(packed_ref_cache);
1345
errno = save_errno;
1346
return error;
@@ -1364,10 +1358,9 @@ static void rollback_packed_refs(struct files_ref_store *refs)
1358
1359
files_assert_main_repository(refs, "rollback_packed_refs");
1360
1367
- if (!refs->packed_refs_lock)
1361
+ if (!is_lock_file_locked(&refs->packed_refs_lock))
1362
die("BUG: packed-refs not locked");
1369
- rollback_lock_file(refs->packed_refs_lock);
1370
- refs->packed_refs_lock = NULL;
1363
+ rollback_lock_file(&refs->packed_refs_lock);
1364
release_packed_ref_cache(packed_ref_cache);
1365
clear_packed_ref_cache(refs);
1366
}