Skip to content

Commit ffbd51c

Browse files
pcloudsgitster
authored andcommitted
pack-objects: document about thread synchronization
These extra comments should be make it easier to understand how to use locks in pack-objects delta search code. For reference, see 8ecce68 (basic threaded delta search - 2007-09-06) 384b32c (pack-objects: fix threaded load balancing - 2007-12-08) 50f22ad (threaded pack-objects: Use condition... - 2007-12-16) Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 53f9a3e commit ffbd51c

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

builtin/pack-objects.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1852,18 +1852,30 @@ static int delta_cacheable(unsigned long src_size, unsigned long trg_size,
18521852

18531853
#ifndef NO_PTHREADS
18541854

1855+
/* Protect access to object database */
18551856
static pthread_mutex_t read_mutex;
18561857
#define read_lock() pthread_mutex_lock(&read_mutex)
18571858
#define read_unlock() pthread_mutex_unlock(&read_mutex)
18581859

1860+
/* Protect delta_cache_size */
18591861
static pthread_mutex_t cache_mutex;
18601862
#define cache_lock() pthread_mutex_lock(&cache_mutex)
18611863
#define cache_unlock() pthread_mutex_unlock(&cache_mutex)
18621864

1865+
/*
1866+
* Protect object list partitioning (e.g. struct thread_param) and
1867+
* progress_state
1868+
*/
18631869
static pthread_mutex_t progress_mutex;
18641870
#define progress_lock() pthread_mutex_lock(&progress_mutex)
18651871
#define progress_unlock() pthread_mutex_unlock(&progress_mutex)
18661872

1873+
/*
1874+
* Access to struct object_entry is unprotected since each thread owns
1875+
* a portion of the main object list. Just don't access object entries
1876+
* ahead in the list because they can be stolen and would need
1877+
* progress_mutex for protection.
1878+
*/
18671879
#else
18681880

18691881
#define read_lock() (void)0
@@ -2245,12 +2257,19 @@ static void try_to_free_from_threads(size_t size)
22452257
static try_to_free_t old_try_to_free_routine;
22462258

22472259
/*
2260+
* The main object list is split into smaller lists, each is handed to
2261+
* one worker.
2262+
*
22482263
* The main thread waits on the condition that (at least) one of the workers
22492264
* has stopped working (which is indicated in the .working member of
22502265
* struct thread_params).
2266+
*
22512267
* When a work thread has completed its work, it sets .working to 0 and
22522268
* signals the main thread and waits on the condition that .data_ready
22532269
* becomes 1.
2270+
*
2271+
* The main thread steals half of the work from the worker that has
2272+
* most work left to hand it to the idle worker.
22542273
*/
22552274

22562275
struct thread_params {

0 commit comments

Comments
 (0)