pack-objects: merge read_lock and lock in packing_data struct
Rename the packing_data lock to obd_lock and upgrade it to a recursive mutex to make it suitable for current read_lock usages. Additionally remove the superfluous #ifndef NO_PTHREADS guard around mutex initialization in prepare_packing_data as the mutex functions themselves are already protected. Signed-off-by: Patrick Hogg <phogg@novamoon.net> Helped-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Patrick Hogg committed
Jan 24, 2019 at 19:22 UTC
edb673cf1001eeff140370c41139aaa06e67cea0
3 files changed
+19
-31
builtin/pack-objects.c
+10
-14
@@ -1953,10 +1953,6 @@ static int delta_cacheable(unsigned long src_size, unsigned long trg_size,
1953
return 0;
1954
}
1955
1956
-/* Protect access to object database */
1957
-#define read_lock() packing_data_read_lock(&to_pack)
1958
-#define read_unlock() packing_data_read_unlock(&to_pack)
1959
-
1956
/* Protect delta_cache_size */
1957
static pthread_mutex_t cache_mutex;
1958
#define cache_lock() pthread_mutex_lock(&cache_mutex)
@@ -1992,11 +1988,11 @@ unsigned long oe_get_size_slow(struct packing_data *pack,
1988
unsigned long used, avail, size;
1989
1990
if (e->type_ != OBJ_OFS_DELTA && e->type_ != OBJ_REF_DELTA) {
1995
- read_lock();
1991
+ packing_data_lock(&to_pack);
1992
if (oid_object_info(the_repository, &e->idx.oid, &size) < 0)
1993
die(_("unable to get size of %s"),
1994
oid_to_hex(&e->idx.oid));
1999
- read_unlock();
1995
+ packing_data_unlock(&to_pack);
1996
return size;
1997
}
1998
@@ -2004,7 +2000,7 @@ unsigned long oe_get_size_slow(struct packing_data *pack,
2000
if (!p)
2001
BUG("when e->type is a delta, it must belong to a pack");
2002
2007
- read_lock();
2003
+ packing_data_lock(&to_pack);
2004
w_curs = NULL;
2005
buf = use_pack(p, &w_curs, e->in_pack_offset, &avail);
2006
used = unpack_object_header_buffer(buf, avail, &type, &size);
@@ -2013,7 +2009,7 @@ unsigned long oe_get_size_slow(struct packing_data *pack,
2009
oid_to_hex(&e->idx.oid));
2010
2011
unuse_pack(&w_curs);
2016
- read_unlock();
2012
+ packing_data_unlock(&to_pack);
2013
return size;
2014
}
2015
@@ -2075,9 +2071,9 @@ static int try_delta(struct unpacked *trg, struct unpacked *src,
2071
2072
/* Load data if not already done */
2073
if (!trg->data) {
2078
- read_lock();
2074
+ packing_data_lock(&to_pack);
2075
trg->data = read_object_file(&trg_entry->idx.oid, &type, &sz);
2080
- read_unlock();
2076
+ packing_data_unlock(&to_pack);
2077
if (!trg->data)
2078
die(_("object %s cannot be read"),
2079
oid_to_hex(&trg_entry->idx.oid));
@@ -2088,9 +2084,9 @@ static int try_delta(struct unpacked *trg, struct unpacked *src,
2084
*mem_usage += sz;
2085
}
2086
if (!src->data) {
2091
- read_lock();
2087
+ packing_data_lock(&to_pack);
2088
src->data = read_object_file(&src_entry->idx.oid, &type, &sz);
2093
- read_unlock();
2089
+ packing_data_unlock(&to_pack);
2090
if (!src->data) {
2091
if (src_entry->preferred_base) {
2092
static int warned = 0;
@@ -2336,9 +2332,9 @@ static void find_deltas(struct object_entry **list, unsigned *list_size,
2332
2333
static void try_to_free_from_threads(size_t size)
2334
{
2339
- read_lock();
2335
+ packing_data_lock(&to_pack);
2336
release_pack_memory(size);
2341
- read_unlock();
2337
+ packing_data_unlock(&to_pack);
2338
}
2339
2340
static try_to_free_t old_try_to_free_routine;
pack-objects.c
+1
-4
@@ -148,10 +148,7 @@ void prepare_packing_data(struct packing_data *pdata)
148
1U << OE_SIZE_BITS);
149
pdata->oe_delta_size_limit = git_env_ulong("GIT_TEST_OE_DELTA_SIZE",
150
1UL << OE_DELTA_SIZE_BITS);
151
-#ifndef NO_PTHREADS
152
- pthread_mutex_init(&pdata->lock, NULL);
153
- init_recursive_mutex(&pdata->read_lock);
154
-#endif
151
+ init_recursive_mutex(&pdata->odb_lock);
152
}
153
154
struct object_entry *packlist_alloc(struct packing_data *pdata,
pack-objects.h
+8
-13
@@ -145,8 +145,11 @@ struct packing_data {
145
struct packed_git **in_pack_by_idx;
146
struct packed_git **in_pack;
147
148
- pthread_mutex_t lock;
149
- pthread_mutex_t read_lock;
148
+ /*
149
+ * During packing with multiple threads, protect the in-core
150
+ * object database from concurrent accesses.
151
+ */
152
+ pthread_mutex_t odb_lock;
153
154
/*
155
* This list contains entries for bases which we know the other side
@@ -166,22 +169,14 @@ struct packing_data {
169
170
void prepare_packing_data(struct packing_data *pdata);
171
172
+/* Protect access to object database */
173
static inline void packing_data_lock(struct packing_data *pdata)
174
{
171
- pthread_mutex_lock(&pdata->lock);
175
+ pthread_mutex_lock(&pdata->odb_lock);
176
}
177
static inline void packing_data_unlock(struct packing_data *pdata)
178
{
175
- pthread_mutex_unlock(&pdata->lock);
176
-}
177
-
178
-static inline void packing_data_read_lock(struct packing_data *pdata)
179
-{
180
- pthread_mutex_lock(&pdata->read_lock);
181
-}
182
-static inline void packing_data_read_unlock(struct packing_data *pdata)
183
-{
184
- pthread_mutex_unlock(&pdata->read_lock);
179
+ pthread_mutex_unlock(&pdata->odb_lock);
180
}
181
182
struct object_entry *packlist_alloc(struct packing_data *pdata,