20
20
#include "split-index.h"
21
21
#include "utf8.h"
22
22
23
+ #ifndef NO_PTHREADS
24
+ #include <pthread.h>
25
+ #endif
26
+
23
27
/* Mask for the name length in ce_flags in the on-disk index */
24
28
25
29
#define CE_NAMEMASK (0x0fff)
@@ -1532,6 +1536,34 @@ static int verify_hdr(struct cache_header *hdr, unsigned long size)
1532
1536
return 0 ;
1533
1537
}
1534
1538
1539
+ #ifndef NO_PTHREADS
1540
+ /*
1541
+ * Require index file to be larger than this threshold before
1542
+ * we bother using a thread to verify the SHA.
1543
+ * This value was arbitrarily chosen.
1544
+ */
1545
+ #define VERIFY_HDR_THRESHOLD 10*1024*1024
1546
+
1547
+ struct verify_hdr_thread_data
1548
+ {
1549
+ pthread_t thread_id ;
1550
+ struct cache_header * hdr ;
1551
+ size_t size ;
1552
+ int result ;
1553
+ };
1554
+
1555
+ /*
1556
+ * A thread proc to run the verify_hdr() computation
1557
+ * in a background thread.
1558
+ */
1559
+ static void * verify_hdr_thread (void * _data )
1560
+ {
1561
+ struct verify_hdr_thread_data * p = _data ;
1562
+ p -> result = verify_hdr (p -> hdr , (unsigned long )p -> size );
1563
+ return NULL ;
1564
+ }
1565
+ #endif
1566
+
1535
1567
static int read_index_extension (struct index_state * istate ,
1536
1568
const char * ext , void * data , unsigned long sz )
1537
1569
{
@@ -1733,6 +1765,9 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
1733
1765
void * mmap ;
1734
1766
size_t mmap_size ;
1735
1767
struct strbuf previous_name_buf = STRBUF_INIT , * previous_name ;
1768
+ #ifndef NO_PTHREADS
1769
+ struct verify_hdr_thread_data verify_hdr_thread_data ;
1770
+ #endif
1736
1771
1737
1772
if (istate -> initialized )
1738
1773
return istate -> cache_nr ;
@@ -1759,8 +1794,23 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
1759
1794
close (fd );
1760
1795
1761
1796
hdr = mmap ;
1797
+ #ifdef NO_PTHREADS
1762
1798
if (verify_hdr (hdr , mmap_size ) < 0 )
1763
1799
goto unmap ;
1800
+ #else
1801
+ if (mmap_size < VERIFY_HDR_THRESHOLD ) {
1802
+ if (verify_hdr (hdr , mmap_size ) < 0 )
1803
+ goto unmap ;
1804
+ } else {
1805
+ verify_hdr_thread_data .hdr = hdr ;
1806
+ verify_hdr_thread_data .size = mmap_size ;
1807
+ verify_hdr_thread_data .result = -1 ;
1808
+ if (pthread_create (
1809
+ & verify_hdr_thread_data .thread_id , NULL ,
1810
+ verify_hdr_thread , & verify_hdr_thread_data ))
1811
+ die_errno ("unable to start verify_hdr_thread" );
1812
+ }
1813
+ #endif
1764
1814
1765
1815
hashcpy (istate -> sha1 , (const unsigned char * )hdr + mmap_size - 20 );
1766
1816
istate -> version = ntohl (hdr -> hdr_version );
@@ -1808,6 +1858,16 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
1808
1858
src_offset += 8 ;
1809
1859
src_offset += extsize ;
1810
1860
}
1861
+
1862
+ #ifndef NO_PTHREADS
1863
+ if (mmap_size >= VERIFY_HDR_THRESHOLD ) {
1864
+ if (pthread_join (verify_hdr_thread_data .thread_id , NULL ))
1865
+ die_errno ("unable to join verify_hdr_thread" );
1866
+ if (verify_hdr_thread_data .result < 0 )
1867
+ goto unmap ;
1868
+ }
1869
+ #endif
1870
+
1811
1871
munmap (mmap , mmap_size );
1812
1872
return istate -> cache_nr ;
1813
1873
0 commit comments