21
21
#include "utf8.h"
22
22
#include "fsmonitor.h"
23
23
24
+ #ifndef NO_PTHREADS
25
+ #include <pthread.h>
26
+ #endif
27
+
24
28
/* Mask for the name length in ce_flags in the on-disk index */
25
29
26
30
#define CE_NAMEMASK (0x0fff)
@@ -1555,6 +1559,34 @@ static int verify_hdr(struct cache_header *hdr, unsigned long size)
1555
1559
return 0 ;
1556
1560
}
1557
1561
1562
+ #ifndef NO_PTHREADS
1563
+ /*
1564
+ * Require index file to be larger than this threshold before
1565
+ * we bother using a thread to verify the SHA.
1566
+ * This value was arbitrarily chosen.
1567
+ */
1568
+ #define VERIFY_HDR_THRESHOLD 10*1024*1024
1569
+
1570
+ struct verify_hdr_thread_data
1571
+ {
1572
+ pthread_t thread_id ;
1573
+ struct cache_header * hdr ;
1574
+ size_t size ;
1575
+ int result ;
1576
+ };
1577
+
1578
+ /*
1579
+ * A thread proc to run the verify_hdr() computation
1580
+ * in a background thread.
1581
+ */
1582
+ static void * verify_hdr_thread (void * _data )
1583
+ {
1584
+ struct verify_hdr_thread_data * p = _data ;
1585
+ p -> result = verify_hdr (p -> hdr , (unsigned long )p -> size );
1586
+ return NULL ;
1587
+ }
1588
+ #endif
1589
+
1558
1590
static int read_index_extension (struct index_state * istate ,
1559
1591
const char * ext , void * data , unsigned long sz )
1560
1592
{
@@ -1760,6 +1792,9 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
1760
1792
void * mmap ;
1761
1793
size_t mmap_size ;
1762
1794
struct strbuf previous_name_buf = STRBUF_INIT , * previous_name ;
1795
+ #ifndef NO_PTHREADS
1796
+ struct verify_hdr_thread_data verify_hdr_thread_data ;
1797
+ #endif
1763
1798
1764
1799
if (istate -> initialized )
1765
1800
return istate -> cache_nr ;
@@ -1786,8 +1821,23 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
1786
1821
close (fd );
1787
1822
1788
1823
hdr = mmap ;
1824
+ #ifdef NO_PTHREADS
1789
1825
if (verify_hdr (hdr , mmap_size ) < 0 )
1790
1826
goto unmap ;
1827
+ #else
1828
+ if (mmap_size < VERIFY_HDR_THRESHOLD ) {
1829
+ if (verify_hdr (hdr , mmap_size ) < 0 )
1830
+ goto unmap ;
1831
+ } else {
1832
+ verify_hdr_thread_data .hdr = hdr ;
1833
+ verify_hdr_thread_data .size = mmap_size ;
1834
+ verify_hdr_thread_data .result = -1 ;
1835
+ if (pthread_create (
1836
+ & verify_hdr_thread_data .thread_id , NULL ,
1837
+ verify_hdr_thread , & verify_hdr_thread_data ))
1838
+ die_errno ("unable to start verify_hdr_thread" );
1839
+ }
1840
+ #endif
1791
1841
1792
1842
hashcpy (istate -> sha1 , (const unsigned char * )hdr + mmap_size - 20 );
1793
1843
istate -> version = ntohl (hdr -> hdr_version );
@@ -1835,6 +1885,16 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
1835
1885
src_offset += 8 ;
1836
1886
src_offset += extsize ;
1837
1887
}
1888
+
1889
+ #ifndef NO_PTHREADS
1890
+ if (mmap_size >= VERIFY_HDR_THRESHOLD ) {
1891
+ if (pthread_join (verify_hdr_thread_data .thread_id , NULL ))
1892
+ die_errno ("unable to join verify_hdr_thread" );
1893
+ if (verify_hdr_thread_data .result < 0 )
1894
+ goto unmap ;
1895
+ }
1896
+ #endif
1897
+
1838
1898
munmap (mmap , mmap_size );
1839
1899
return istate -> cache_nr ;
1840
1900
0 commit comments