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 8ecce684a3 (basic threaded delta search - 2007-09-06) 384b32c09b (pack-objects: fix threaded load balancing - 2007-12-08) 50f22ada52 (threaded pack-objects: Use condition... - 2007-12-16) Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Jul 29, 2018 at 17:36 UTC ffbd51cc609eebfd3ce9dd360ef145cfafc2e088
1 file changed +19
builtin/pack-objects.c
+19
@@ -1852,18 +1852,30 @@ static int delta_cacheable(unsigned long src_size, unsigned long trg_size,
1852
1853 #ifndef NO_PTHREADS
1854
1855 +/* Protect access to object database */
1856 static pthread_mutex_t read_mutex;
1857 #define read_lock() pthread_mutex_lock(&read_mutex)
1858 #define read_unlock() pthread_mutex_unlock(&read_mutex)
1859
1860 +/* Protect delta_cache_size */
1861 static pthread_mutex_t cache_mutex;
1862 #define cache_lock() pthread_mutex_lock(&cache_mutex)
1863 #define cache_unlock() pthread_mutex_unlock(&cache_mutex)
1864
1865 +/*
1866 + * Protect object list partitioning (e.g. struct thread_param) and
1867 + * progress_state
1868 + */
1869 static pthread_mutex_t progress_mutex;
1870 #define progress_lock() pthread_mutex_lock(&progress_mutex)
1871 #define progress_unlock() pthread_mutex_unlock(&progress_mutex)
1872
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 + */
1879 #else
1880
1881 #define read_lock() (void)0
@@ -2245,12 +2257,19 @@ static void try_to_free_from_threads(size_t size)
2257 static try_to_free_t old_try_to_free_routine;
2258
2259 /*
2260 + * The main object list is split into smaller lists, each is handed to
2261 + * one worker.
2262 + *
2263 * The main thread waits on the condition that (at least) one of the workers
2264 * has stopped working (which is indicated in the .working member of
2265 * struct thread_params).
2266 + *
2267 * When a work thread has completed its work, it sets .working to 0 and
2268 * signals the main thread and waits on the condition that .data_ready
2269 * 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.
2273 */
2274
2275 struct thread_params {