Skip to content

Commit 00d1744

Browse files
mhaggergitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 55c6bc3 commit 00d1744

File tree

1 file changed

+11
-18
lines changed

1 file changed

+11
-18
lines changed

refs/files-backend.c

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,12 @@ struct files_ref_store {
6363
struct packed_ref_cache *packed;
6464

6565
/*
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.
6968
*/
70-
struct lock_file *packed_refs_lock;
69+
struct lock_file packed_refs_lock;
7170
};
7271

73-
/* Lock used for the main packed-refs file: */
74-
static struct lock_file packlock;
75-
7672
/*
7773
* Increment the reference count of *packed_refs.
7874
*/
@@ -102,7 +98,7 @@ static void clear_packed_ref_cache(struct files_ref_store *refs)
10298
if (refs->packed) {
10399
struct packed_ref_cache *packed_refs = refs->packed;
104100

105-
if (refs->packed_refs_lock)
101+
if (is_lock_file_locked(&refs->packed_refs_lock))
106102
die("BUG: packed-ref cache cleared while locked");
107103
refs->packed = NULL;
108104
release_packed_ref_cache(packed_refs);
@@ -394,7 +390,7 @@ static void add_packed_ref(struct files_ref_store *refs,
394390
{
395391
struct packed_ref_cache *packed_ref_cache = get_packed_ref_cache(refs);
396392

397-
if (!refs->packed_refs_lock)
393+
if (!is_lock_file_locked(&refs->packed_refs_lock))
398394
die("BUG: packed refs not locked");
399395
add_ref_entry(get_packed_ref_dir(packed_ref_cache),
400396
create_ref_entry(refname, oid, REF_ISPACKED, 1));
@@ -1288,7 +1284,7 @@ static int lock_packed_refs(struct files_ref_store *refs, int flags)
12881284
}
12891285

12901286
if (hold_lock_file_for_update_timeout(
1291-
&packlock, files_packed_refs_path(refs),
1287+
&refs->packed_refs_lock, files_packed_refs_path(refs),
12921288
flags, timeout_value) < 0)
12931289
return -1;
12941290
/*
@@ -1298,7 +1294,6 @@ static int lock_packed_refs(struct files_ref_store *refs, int flags)
12981294
* the packed-refs file.
12991295
*/
13001296
packed_ref_cache = get_packed_ref_cache(refs);
1301-
refs->packed_refs_lock = &packlock;
13021297
/* Increment the reference count to prevent it from being freed: */
13031298
acquire_packed_ref_cache(packed_ref_cache);
13041299
return 0;
@@ -1321,10 +1316,10 @@ static int commit_packed_refs(struct files_ref_store *refs)
13211316

13221317
files_assert_main_repository(refs, "commit_packed_refs");
13231318

1324-
if (!refs->packed_refs_lock)
1319+
if (!is_lock_file_locked(&refs->packed_refs_lock))
13251320
die("BUG: packed-refs not locked");
13261321

1327-
out = fdopen_lock_file(refs->packed_refs_lock, "w");
1322+
out = fdopen_lock_file(&refs->packed_refs_lock, "w");
13281323
if (!out)
13291324
die_errno("unable to fdopen packed-refs descriptor");
13301325

@@ -1342,11 +1337,10 @@ static int commit_packed_refs(struct files_ref_store *refs)
13421337
if (ok != ITER_DONE)
13431338
die("error while iterating over references");
13441339

1345-
if (commit_lock_file(refs->packed_refs_lock)) {
1340+
if (commit_lock_file(&refs->packed_refs_lock)) {
13461341
save_errno = errno;
13471342
error = -1;
13481343
}
1349-
refs->packed_refs_lock = NULL;
13501344
release_packed_ref_cache(packed_ref_cache);
13511345
errno = save_errno;
13521346
return error;
@@ -1364,10 +1358,9 @@ static void rollback_packed_refs(struct files_ref_store *refs)
13641358

13651359
files_assert_main_repository(refs, "rollback_packed_refs");
13661360

1367-
if (!refs->packed_refs_lock)
1361+
if (!is_lock_file_locked(&refs->packed_refs_lock))
13681362
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);
13711364
release_packed_ref_cache(packed_ref_cache);
13721365
clear_packed_ref_cache(refs);
13731366
}

0 commit comments

Comments
 (0)