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)
@@ -1397,6 +1401,34 @@ static int verify_hdr(struct cache_header *hdr, unsigned long size)
1397
1401
return 0 ;
1398
1402
}
1399
1403
1404
+ #ifndef NO_PTHREADS
1405
+ /*
1406
+ * Require index file to be larger than this threshold before
1407
+ * we bother using a thread to verify the SHA.
1408
+ * This value was arbitrarily chosen.
1409
+ */
1410
+ #define VERIFY_HDR_THRESHOLD 10*1024*1024
1411
+
1412
+ struct verify_hdr_thread_data
1413
+ {
1414
+ pthread_t thread_id ;
1415
+ struct cache_header * hdr ;
1416
+ size_t size ;
1417
+ int result ;
1418
+ };
1419
+
1420
+ /*
1421
+ * A thread proc to run the verify_hdr() computation
1422
+ * in a background thread.
1423
+ */
1424
+ static void * verify_hdr_thread (void * _data )
1425
+ {
1426
+ struct verify_hdr_thread_data * p = _data ;
1427
+ p -> result = verify_hdr (p -> hdr , (unsigned long )p -> size );
1428
+ return NULL ;
1429
+ }
1430
+ #endif
1431
+
1400
1432
static int read_index_extension (struct index_state * istate ,
1401
1433
const char * ext , void * data , unsigned long sz )
1402
1434
{
@@ -1584,6 +1616,9 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
1584
1616
void * mmap ;
1585
1617
size_t mmap_size ;
1586
1618
struct strbuf previous_name_buf = STRBUF_INIT , * previous_name ;
1619
+ #ifndef NO_PTHREADS
1620
+ struct verify_hdr_thread_data verify_hdr_thread_data ;
1621
+ #endif
1587
1622
1588
1623
if (istate -> initialized )
1589
1624
return istate -> cache_nr ;
@@ -1610,8 +1645,23 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
1610
1645
close (fd );
1611
1646
1612
1647
hdr = mmap ;
1648
+ #ifdef NO_PTHREADS
1613
1649
if (verify_hdr (hdr , mmap_size ) < 0 )
1614
1650
goto unmap ;
1651
+ #else
1652
+ if (mmap_size < VERIFY_HDR_THRESHOLD ) {
1653
+ if (verify_hdr (hdr , mmap_size ) < 0 )
1654
+ goto unmap ;
1655
+ } else {
1656
+ verify_hdr_thread_data .hdr = hdr ;
1657
+ verify_hdr_thread_data .size = mmap_size ;
1658
+ verify_hdr_thread_data .result = -1 ;
1659
+ if (pthread_create (
1660
+ & verify_hdr_thread_data .thread_id , NULL ,
1661
+ verify_hdr_thread , & verify_hdr_thread_data ))
1662
+ die_errno ("unable to start verify_hdr_thread" );
1663
+ }
1664
+ #endif
1615
1665
1616
1666
hashcpy (istate -> sha1 , (const unsigned char * )hdr + mmap_size - 20 );
1617
1667
istate -> version = ntohl (hdr -> hdr_version );
@@ -1659,6 +1709,16 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
1659
1709
src_offset += 8 ;
1660
1710
src_offset += extsize ;
1661
1711
}
1712
+
1713
+ #ifndef NO_PTHREADS
1714
+ if (mmap_size >= VERIFY_HDR_THRESHOLD ) {
1715
+ if (pthread_join (verify_hdr_thread_data .thread_id , NULL ))
1716
+ die_errno ("unable to join verify_hdr_thread" );
1717
+ if (verify_hdr_thread_data .result < 0 )
1718
+ goto unmap ;
1719
+ }
1720
+ #endif
1721
+
1662
1722
munmap (mmap , mmap_size );
1663
1723
return istate -> cache_nr ;
1664
1724
0 commit comments