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