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