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)
@@ -1391,6 +1395,34 @@ static int verify_hdr(struct cache_header *hdr, unsigned long size)
1391
1395
return 0 ;
1392
1396
}
1393
1397
1398
+ #ifndef NO_PTHREADS
1399
+ /*
1400
+ * Require index file to be larger than this threshold before
1401
+ * we bother using a thread to verify the SHA.
1402
+ * This value was arbitrarily chosen.
1403
+ */
1404
+ #define VERIFY_HDR_THRESHOLD 10*1024*1024
1405
+
1406
+ struct verify_hdr_thread_data
1407
+ {
1408
+ pthread_t thread_id ;
1409
+ struct cache_header * hdr ;
1410
+ size_t size ;
1411
+ int result ;
1412
+ };
1413
+
1414
+ /*
1415
+ * A thread proc to run the verify_hdr() computation
1416
+ * in a background thread.
1417
+ */
1418
+ static void * verify_hdr_thread (void * _data )
1419
+ {
1420
+ struct verify_hdr_thread_data * p = _data ;
1421
+ p -> result = verify_hdr (p -> hdr , (unsigned long )p -> size );
1422
+ return NULL ;
1423
+ }
1424
+ #endif
1425
+
1394
1426
static int read_index_extension (struct index_state * istate ,
1395
1427
const char * ext , void * data , unsigned long sz )
1396
1428
{
@@ -1592,6 +1624,9 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
1592
1624
void * mmap ;
1593
1625
size_t mmap_size ;
1594
1626
struct strbuf previous_name_buf = STRBUF_INIT , * previous_name ;
1627
+ #ifndef NO_PTHREADS
1628
+ struct verify_hdr_thread_data verify_hdr_thread_data ;
1629
+ #endif
1595
1630
1596
1631
if (istate -> initialized )
1597
1632
return istate -> cache_nr ;
@@ -1618,8 +1653,23 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
1618
1653
close (fd );
1619
1654
1620
1655
hdr = mmap ;
1656
+ #ifdef NO_PTHREADS
1621
1657
if (verify_hdr (hdr , mmap_size ) < 0 )
1622
1658
goto unmap ;
1659
+ #else
1660
+ if (mmap_size < VERIFY_HDR_THRESHOLD ) {
1661
+ if (verify_hdr (hdr , mmap_size ) < 0 )
1662
+ goto unmap ;
1663
+ } else {
1664
+ verify_hdr_thread_data .hdr = hdr ;
1665
+ verify_hdr_thread_data .size = mmap_size ;
1666
+ verify_hdr_thread_data .result = -1 ;
1667
+ if (pthread_create (
1668
+ & verify_hdr_thread_data .thread_id , NULL ,
1669
+ verify_hdr_thread , & verify_hdr_thread_data ))
1670
+ die_errno ("unable to start verify_hdr_thread" );
1671
+ }
1672
+ #endif
1623
1673
1624
1674
hashcpy (istate -> sha1 , (const unsigned char * )hdr + mmap_size - 20 );
1625
1675
istate -> version = ntohl (hdr -> hdr_version );
@@ -1667,6 +1717,16 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
1667
1717
src_offset += 8 ;
1668
1718
src_offset += extsize ;
1669
1719
}
1720
+
1721
+ #ifndef NO_PTHREADS
1722
+ if (mmap_size >= VERIFY_HDR_THRESHOLD ) {
1723
+ if (pthread_join (verify_hdr_thread_data .thread_id , NULL ))
1724
+ die_errno ("unable to join verify_hdr_thread" );
1725
+ if (verify_hdr_thread_data .result < 0 )
1726
+ goto unmap ;
1727
+ }
1728
+ #endif
1729
+
1670
1730
munmap (mmap , mmap_size );
1671
1731
return istate -> cache_nr ;
1672
1732
0 commit comments