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