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)
@@ -1393,6 +1397,34 @@ static int verify_hdr(struct cache_header *hdr, unsigned long size)
1393
1397
return 0 ;
1394
1398
}
1395
1399
1400
+ #ifndef NO_PTHREADS
1401
+ /*
1402
+ * Require index file to be larger than this threshold before
1403
+ * we bother using a thread to verify the SHA.
1404
+ * This value was arbitrarily chosen.
1405
+ */
1406
+ #define VERIFY_HDR_THRESHOLD 10*1024*1024
1407
+
1408
+ struct verify_hdr_thread_data
1409
+ {
1410
+ pthread_t thread_id ;
1411
+ struct cache_header * hdr ;
1412
+ size_t size ;
1413
+ int result ;
1414
+ };
1415
+
1416
+ /*
1417
+ * A thread proc to run the verify_hdr() computation
1418
+ * in a background thread.
1419
+ */
1420
+ static void * verify_hdr_thread (void * _data )
1421
+ {
1422
+ struct verify_hdr_thread_data * p = _data ;
1423
+ p -> result = verify_hdr (p -> hdr , (unsigned long )p -> size );
1424
+ return NULL ;
1425
+ }
1426
+ #endif
1427
+
1396
1428
static int read_index_extension (struct index_state * istate ,
1397
1429
const char * ext , void * data , unsigned long sz )
1398
1430
{
@@ -1578,6 +1610,9 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
1578
1610
void * mmap ;
1579
1611
size_t mmap_size ;
1580
1612
struct strbuf previous_name_buf = STRBUF_INIT , * previous_name ;
1613
+ #ifndef NO_PTHREADS
1614
+ struct verify_hdr_thread_data verify_hdr_thread_data ;
1615
+ #endif
1581
1616
1582
1617
if (istate -> initialized )
1583
1618
return istate -> cache_nr ;
@@ -1604,8 +1639,23 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
1604
1639
close (fd );
1605
1640
1606
1641
hdr = mmap ;
1642
+ #ifdef NO_PTHREADS
1607
1643
if (verify_hdr (hdr , mmap_size ) < 0 )
1608
1644
goto unmap ;
1645
+ #else
1646
+ if (mmap_size < VERIFY_HDR_THRESHOLD ) {
1647
+ if (verify_hdr (hdr , mmap_size ) < 0 )
1648
+ goto unmap ;
1649
+ } else {
1650
+ verify_hdr_thread_data .hdr = hdr ;
1651
+ verify_hdr_thread_data .size = mmap_size ;
1652
+ verify_hdr_thread_data .result = -1 ;
1653
+ if (pthread_create (
1654
+ & verify_hdr_thread_data .thread_id , NULL ,
1655
+ verify_hdr_thread , & verify_hdr_thread_data ))
1656
+ die_errno ("unable to start verify_hdr_thread" );
1657
+ }
1658
+ #endif
1609
1659
1610
1660
hashcpy (istate -> sha1 , (const unsigned char * )hdr + mmap_size - 20 );
1611
1661
istate -> version = ntohl (hdr -> hdr_version );
@@ -1653,6 +1703,16 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
1653
1703
src_offset += 8 ;
1654
1704
src_offset += extsize ;
1655
1705
}
1706
+
1707
+ #ifndef NO_PTHREADS
1708
+ if (mmap_size >= VERIFY_HDR_THRESHOLD ) {
1709
+ if (pthread_join (verify_hdr_thread_data .thread_id , NULL ))
1710
+ die_errno ("unable to join verify_hdr_thread" );
1711
+ if (verify_hdr_thread_data .result < 0 )
1712
+ goto unmap ;
1713
+ }
1714
+ #endif
1715
+
1656
1716
munmap (mmap , mmap_size );
1657
1717
return istate -> cache_nr ;
1658
1718
0 commit comments