19
19
#include "split-index.h"
20
20
#include "utf8.h"
21
21
22
+ #ifndef NO_PTHREADS
23
+ #include <pthread.h>
24
+ #endif
25
+
22
26
/* Mask for the name length in ce_flags in the on-disk index */
23
27
24
28
#define CE_NAMEMASK (0x0fff)
@@ -1399,6 +1403,34 @@ static int verify_hdr(struct cache_header *hdr, unsigned long size)
1399
1403
return 0 ;
1400
1404
}
1401
1405
1406
+ #ifndef NO_PTHREADS
1407
+ /*
1408
+ * Require index file to be larger than this threshold before
1409
+ * we bother using a thread to verify the SHA.
1410
+ * This value was arbitrarily chosen.
1411
+ */
1412
+ #define VERIFY_HDR_THRESHOLD 10*1024*1024
1413
+
1414
+ struct verify_hdr_thread_data
1415
+ {
1416
+ pthread_t thread_id ;
1417
+ struct cache_header * hdr ;
1418
+ size_t size ;
1419
+ int result ;
1420
+ };
1421
+
1422
+ /*
1423
+ * A thread proc to run the verify_hdr() computation
1424
+ * in a background thread.
1425
+ */
1426
+ static void * verify_hdr_thread (void * _data )
1427
+ {
1428
+ struct verify_hdr_thread_data * p = _data ;
1429
+ p -> result = verify_hdr (p -> hdr , (unsigned long )p -> size );
1430
+ return NULL ;
1431
+ }
1432
+ #endif
1433
+
1402
1434
static int read_index_extension (struct index_state * istate ,
1403
1435
const char * ext , void * data , unsigned long sz )
1404
1436
{
@@ -1587,6 +1619,9 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
1587
1619
void * mmap ;
1588
1620
size_t mmap_size ;
1589
1621
struct strbuf previous_name_buf = STRBUF_INIT , * previous_name ;
1622
+ #ifndef NO_PTHREADS
1623
+ struct verify_hdr_thread_data verify_hdr_thread_data ;
1624
+ #endif
1590
1625
1591
1626
if (istate -> initialized )
1592
1627
return istate -> cache_nr ;
@@ -1613,8 +1648,23 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
1613
1648
close (fd );
1614
1649
1615
1650
hdr = mmap ;
1651
+ #ifdef NO_PTHREADS
1616
1652
if (verify_hdr (hdr , mmap_size ) < 0 )
1617
1653
goto unmap ;
1654
+ #else
1655
+ if (mmap_size < VERIFY_HDR_THRESHOLD ) {
1656
+ if (verify_hdr (hdr , mmap_size ) < 0 )
1657
+ goto unmap ;
1658
+ } else {
1659
+ verify_hdr_thread_data .hdr = hdr ;
1660
+ verify_hdr_thread_data .size = mmap_size ;
1661
+ verify_hdr_thread_data .result = -1 ;
1662
+ if (pthread_create (
1663
+ & verify_hdr_thread_data .thread_id , NULL ,
1664
+ verify_hdr_thread , & verify_hdr_thread_data ))
1665
+ die_errno ("unable to start verify_hdr_thread" );
1666
+ }
1667
+ #endif
1618
1668
1619
1669
hashcpy (istate -> sha1 , (const unsigned char * )hdr + mmap_size - 20 );
1620
1670
istate -> version = ntohl (hdr -> hdr_version );
@@ -1662,6 +1712,16 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
1662
1712
src_offset += 8 ;
1663
1713
src_offset += extsize ;
1664
1714
}
1715
+
1716
+ #ifndef NO_PTHREADS
1717
+ if (mmap_size >= VERIFY_HDR_THRESHOLD ) {
1718
+ if (pthread_join (verify_hdr_thread_data .thread_id , NULL ))
1719
+ die_errno ("unable to join verify_hdr_thread" );
1720
+ if (verify_hdr_thread_data .result < 0 )
1721
+ goto unmap ;
1722
+ }
1723
+ #endif
1724
+
1665
1725
munmap (mmap , mmap_size );
1666
1726
return istate -> cache_nr ;
1667
1727
0 commit comments