@matheusbsbs / matheus-bagliano / commits / 3c70183918
Fork 0

threaded delta search: proper locking for cache accounting

Signed-off-by: Nicolas Pitre <nico@cam.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nicolas Pitre committed Sep 10, 2007 at 11:10 UTC 3c70183918d97eda06ae847cd7432fd23257caea
1 file changed +20 -4
builtin-pack-objects.c
+20 -4
@@ -1301,6 +1301,10 @@ static pthread_mutex_t read_mutex = PTHREAD_MUTEX_INITIALIZER;
1301 #define read_lock() pthread_mutex_lock(&read_mutex)
1302 #define read_unlock() pthread_mutex_unlock(&read_mutex)
1303
1304 +static pthread_mutex_t cache_mutex = PTHREAD_MUTEX_INITIALIZER;
1305 +#define cache_lock() pthread_mutex_lock(&cache_mutex)
1306 +#define cache_unlock() pthread_mutex_unlock(&cache_mutex)
1307 +
1308 static pthread_mutex_t progress_mutex = PTHREAD_MUTEX_INITIALIZER;
1309 #define progress_lock() pthread_mutex_lock(&progress_mutex)
1310 #define progress_unlock() pthread_mutex_unlock(&progress_mutex)
@@ -1309,6 +1313,8 @@ static pthread_mutex_t progress_mutex = PTHREAD_MUTEX_INITIALIZER;
1313
1314 #define read_lock() 0
1315 #define read_unlock() 0
1316 +#define cache_lock() 0
1317 +#define cache_unlock() 0
1318 #define progress_lock() 0
1319 #define progress_unlock() 0
1320
@@ -1423,17 +1429,27 @@ static int try_delta(struct unpacked *trg, struct unpacked *src,
1429 trg_entry->delta_size = delta_size;
1430 trg->depth = src->depth + 1;
1431
1432 + /*
1433 + * Handle memory allocation outside of the cache
1434 + * accounting lock. Compiler will optimize the strangeness
1435 + * away when THREADED_DELTA_SEARCH is not defined.
1436 + */
1437 + if (trg_entry->delta_data)
1438 + free(trg_entry->delta_data);
1439 + cache_lock();
1440 if (trg_entry->delta_data) {
1441 delta_cache_size -= trg_entry->delta_size;
1428 - free(trg_entry->delta_data);
1442 trg_entry->delta_data = NULL;
1443 }
1431 -
1444 if (delta_cacheable(src_size, trg_size, delta_size)) {
1433 - trg_entry->delta_data = xrealloc(delta_buf, delta_size);
1445 delta_cache_size += trg_entry->delta_size;
1435 - } else
1446 + cache_unlock();
1447 + trg_entry->delta_data = xrealloc(delta_buf, delta_size);
1448 + } else {
1449 + cache_unlock();
1450 free(delta_buf);
1451 + }
1452 +
1453 return 1;
1454 }
1455