Skip to content

Commit 3aba2fd

Browse files
pcloudsgitster
authored andcommitted
index-pack: protect deepest_delta in multithread code
deepest_delta is a global variable but is updated without protection in resolve_delta(), a multithreaded function. Add a new mutex for it, but only protect and update when it's actually used (i.e. show_stat is non-zero). Another variable that will not be updated is delta_depth in "struct object_entry" as it's only useful when show_stat is 1. Putting it in "if (show_stat)" makes it clearer. The local variable "stat" is renamed to "show_stat" after moving to global scope because the name "stat" conflicts with stat(2) syscall. Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Reviewed-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7e20105 commit 3aba2fd

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

builtin/index-pack.c

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ static int nr_threads;
7878
static int from_stdin;
7979
static int strict;
8080
static int verbose;
81+
static int show_stat;
8182

8283
static struct progress *progress;
8384

@@ -108,6 +109,10 @@ static pthread_mutex_t work_mutex;
108109
#define work_lock() lock_mutex(&work_mutex)
109110
#define work_unlock() unlock_mutex(&work_mutex)
110111

112+
static pthread_mutex_t deepest_delta_mutex;
113+
#define deepest_delta_lock() lock_mutex(&deepest_delta_mutex)
114+
#define deepest_delta_unlock() unlock_mutex(&deepest_delta_mutex)
115+
111116
static pthread_key_t key;
112117

113118
static inline void lock_mutex(pthread_mutex_t *mutex)
@@ -130,6 +135,8 @@ static void init_thread(void)
130135
init_recursive_mutex(&read_mutex);
131136
pthread_mutex_init(&counter_mutex, NULL);
132137
pthread_mutex_init(&work_mutex, NULL);
138+
if (show_stat)
139+
pthread_mutex_init(&deepest_delta_mutex, NULL);
133140
pthread_key_create(&key, NULL);
134141
thread_data = xcalloc(nr_threads, sizeof(*thread_data));
135142
threads_active = 1;
@@ -143,6 +150,8 @@ static void cleanup_thread(void)
143150
pthread_mutex_destroy(&read_mutex);
144151
pthread_mutex_destroy(&counter_mutex);
145152
pthread_mutex_destroy(&work_mutex);
153+
if (show_stat)
154+
pthread_mutex_destroy(&deepest_delta_mutex);
146155
pthread_key_delete(key);
147156
free(thread_data);
148157
}
@@ -158,6 +167,9 @@ static void cleanup_thread(void)
158167
#define work_lock()
159168
#define work_unlock()
160169

170+
#define deepest_delta_lock()
171+
#define deepest_delta_unlock()
172+
161173
#endif
162174

163175

@@ -833,9 +845,13 @@ static void resolve_delta(struct object_entry *delta_obj,
833845
void *base_data, *delta_data;
834846

835847
delta_obj->real_type = base->obj->real_type;
836-
delta_obj->delta_depth = base->obj->delta_depth + 1;
837-
if (deepest_delta < delta_obj->delta_depth)
838-
deepest_delta = delta_obj->delta_depth;
848+
if (show_stat) {
849+
delta_obj->delta_depth = base->obj->delta_depth + 1;
850+
deepest_delta_lock();
851+
if (deepest_delta < delta_obj->delta_depth)
852+
deepest_delta = delta_obj->delta_depth;
853+
deepest_delta_unlock();
854+
}
839855
delta_obj->base_object_no = base->obj - objects;
840856
delta_data = get_data_from_pack(delta_obj);
841857
base_data = get_base_data(base);
@@ -1461,7 +1477,7 @@ static void show_pack_info(int stat_only)
14611477

14621478
int cmd_index_pack(int argc, const char **argv, const char *prefix)
14631479
{
1464-
int i, fix_thin_pack = 0, verify = 0, stat_only = 0, stat = 0;
1480+
int i, fix_thin_pack = 0, verify = 0, stat_only = 0;
14651481
const char *curr_pack, *curr_index;
14661482
const char *index_name = NULL, *pack_name = NULL;
14671483
const char *keep_name = NULL, *keep_msg = NULL;
@@ -1494,10 +1510,10 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
14941510
verify = 1;
14951511
} else if (!strcmp(arg, "--verify-stat")) {
14961512
verify = 1;
1497-
stat = 1;
1513+
show_stat = 1;
14981514
} else if (!strcmp(arg, "--verify-stat-only")) {
14991515
verify = 1;
1500-
stat = 1;
1516+
show_stat = 1;
15011517
stat_only = 1;
15021518
} else if (!strcmp(arg, "--keep")) {
15031519
keep_msg = "";
@@ -1605,7 +1621,7 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
16051621
if (strict)
16061622
check_objects();
16071623

1608-
if (stat)
1624+
if (show_stat)
16091625
show_pack_info(stat_only);
16101626

16111627
idx_objects = xmalloc((nr_objects) * sizeof(struct pack_idx_entry *));

0 commit comments

Comments
 (0)