Commit

654567cf1bc756f4f6db5724689008491f8ba628

reftable/stack: move list lock to `struct reftable_stack`

The struct `reftable_addition` is used to modify a given stack, as such,
it also includes a `struct reftable_flock` used to obtain the lock to
the list file. While the scope of the field lies within this struct, it
doesn't allow for optimizations to be made on `struct reftable_stack`
itself.

Move the field to `struct reftable_stack`, allowing us to make a simple
optimization around avoiding a stack reload when we have already
obtained a lock. While this is currently possible in the write path, the
write path also contains multiple branches to reads which only work
on top of `struct reftable_stack`, and we would miss the optimization in
such paths.

Since the lock is now shared across all additions on the same stack, a
second `reftable_addition` that fails to acquire the already held lock
would still call `reftable_addition_close()`, which will release the
`stack->list_lock` which is still held by the first addition. To avoid
this, add a new bit field `locked` to `reftable_addition` that tracks
whether a particular addition is the one holding the lock, and only
release it in that case. Add a unit test to validate this behavior.

While here, remove an unused header file from 'reftable/stack.h'.

Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

Karthik Nayak <karthik.188@gmail.com> · 2026-08-24 09:31 UTC · committed by Junio C Hamano

Browse files
3 files changed, +53 −8
modified reftable/stack.c
@@ -536,6 +536,8 @@ int reftable_new_stack(struct reftable_stack **dest, const char *dir,
536 536 goto out;
537 537 }
538 538
539 + p->list_lock = REFTABLE_FLOCK_INIT;
540 +
539 541 err = reftable_stack_reload_maybe_reuse(p, 1);
540 542 if (err < 0)
541 543 goto out;
@@ -628,10 +630,16 @@ int reftable_stack_reload(struct reftable_stack *st)
628 630 }
629 631
630 632 struct reftable_addition {
631 - struct reftable_flock tables_list_lock;
632 633 struct reftable_stack *stack;
633 634 struct reftable_write_options opts;
634 635
636 + /*
637 + * While the list lock is acquired on the stack, we need to distinguish
638 + * which 'reftable_addition' is responsible for the lock. This avoids
639 + * clearing the lock of another 'reftable_addition'.
640 + */
641 + unsigned int locked : 1;
642 +
635 643 char **new_tables;
636 644 size_t new_tables_len, new_tables_cap;
637 645 uint64_t next_update_index;
@@ -653,7 +661,9 @@ static void reftable_addition_close(struct reftable_addition *add)
653 661 add->new_tables_len = 0;
654 662 add->new_tables_cap = 0;
655 663
656 - flock_release(&add->tables_list_lock);
664 + if (add->locked)
665 + flock_release(&add->stack->list_lock);
666 + add->locked = 0;
657 667 reftable_buf_release(&nm);
658 668 }
659 669
@@ -669,13 +679,14 @@ static int reftable_stack_init_addition(struct reftable_addition *add,
669 679 if (opts)
670 680 add->opts = *opts;
671 681
672 - err = flock_acquire(&add->tables_list_lock, st->list_file,
682 + err = flock_acquire(&add->stack->list_lock, st->list_file,
673 683 add->opts.lock_timeout_ms);
674 684 if (err < 0)
675 685 goto done;
686 + add->locked = 1;
676 687
677 688 if (add->opts.default_permissions) {
678 - if (chmod(add->tables_list_lock.path,
689 + if (chmod(add->stack->list_lock.path,
679 690 add->opts.default_permissions) < 0) {
680 691 err = REFTABLE_IO_ERROR;
681 692 goto done;
@@ -774,7 +785,7 @@ int reftable_addition_commit(struct reftable_addition *add)
774 785 goto done;
775 786 }
776 787
777 - err = reftable_write_data(add->tables_list_lock.fd,
788 + err = reftable_write_data(add->stack->list_lock.fd,
778 789 table_list.buf, table_list.len);
779 790 reftable_buf_release(&table_list);
780 791 if (err < 0) {
@@ -782,17 +793,18 @@ int reftable_addition_commit(struct reftable_addition *add)
782 793 goto done;
783 794 }
784 795
785 - err = fsync(add->tables_list_lock.fd);
796 + err = fsync(add->stack->list_lock.fd);
786 797 if (err < 0) {
787 798 err = REFTABLE_IO_ERROR;
788 799 goto done;
789 800 }
790 801
791 - err = flock_commit(&add->tables_list_lock);
802 + err = flock_commit(&add->stack->list_lock);
792 803 if (err < 0) {
793 804 err = REFTABLE_IO_ERROR;
794 805 goto done;
795 806 }
807 + add->locked = 0;
796 808
797 809 /* success, no more state to clean up. */
798 810 for (i = 0; i < add->new_tables_len; i++)
modified reftable/stack.h
@@ -10,7 +10,6 @@
10 10 #define STACK_H
11 11
12 12 #include "system.h"
13 - #include "reftable-writer.h"
14 13 #include "reftable-stack.h"
15 14
16 15 struct reftable_stack {
@@ -18,6 +17,12 @@ struct reftable_stack {
18 17 char *list_file;
19 18 int list_fd;
20 19
20 + /*
21 + * Set while an addition holds the stack locked. Used by
22 + * stack_uptodate() to skip reload checks while locked.
23 + */
24 + struct reftable_flock list_lock;
25 +
21 26 char *reftable_dir;
22 27
23 28 struct reftable_stack_options opts;
modified t/unit-tests/u-reftable-stack.c
@@ -1310,3 +1310,31 @@ void test_reftable_stack__invalid_limit_updates(void)
1310 1310 reftable_stack_destroy(st);
1311 1311 clear_dir(dir);
1312 1312 }
1313 +
1314 + void test_reftable_stack__two_additions(void)
1315 + {
1316 + struct reftable_stack *st = NULL;
1317 + char *dir = get_tmp_dir(__LINE__);
1318 + struct reftable_addition *add1 = NULL;
1319 + struct reftable_addition *add2 = NULL;
1320 +
1321 + struct reftable_ref_record ref = {
1322 + .refname = (char *) "HEAD",
1323 + .update_index = 1,
1324 + .value_type = REFTABLE_REF_SYMREF,
1325 + .value.symref = (char *) "master",
1326 + };
1327 +
1328 + cl_assert_equal_i(reftable_new_stack(&st, dir, NULL), 0);
1329 +
1330 + cl_assert_equal_i(reftable_stack_addition_new(&add1, st, NULL), 0);
1331 + cl_assert_equal_i(reftable_stack_addition_new(&add2, st, NULL), REFTABLE_LOCK_ERROR);
1332 +
1333 + cl_assert_equal_i(reftable_addition_add(add1, write_test_ref, &ref), 0);
1334 +
1335 + cl_assert_equal_i(reftable_addition_commit(add1), 0);
1336 +
1337 + reftable_addition_destroy(add1);
1338 + reftable_stack_destroy(st);
1339 + clear_dir(dir);
1340 + }