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
{
@@ -1594,6 +1626,9 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
1594
1626
void * mmap ;
1595
1627
size_t mmap_size ;
1596
1628
struct strbuf previous_name_buf = STRBUF_INIT , * previous_name ;
1629
+ #ifndef NO_PTHREADS
1630
+ struct verify_hdr_thread_data verify_hdr_thread_data ;
1631
+ #endif
1597
1632
1598
1633
if (istate -> initialized )
1599
1634
return istate -> cache_nr ;
@@ -1620,8 +1655,23 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
1620
1655
close (fd );
1621
1656
1622
1657
hdr = mmap ;
1658
+ #ifdef NO_PTHREADS
1623
1659
if (verify_hdr (hdr , mmap_size ) < 0 )
1624
1660
goto unmap ;
1661
+ #else
1662
+ if (mmap_size < VERIFY_HDR_THRESHOLD ) {
1663
+ if (verify_hdr (hdr , mmap_size ) < 0 )
1664
+ goto unmap ;
1665
+ } else {
1666
+ verify_hdr_thread_data .hdr = hdr ;
1667
+ verify_hdr_thread_data .size = mmap_size ;
1668
+ verify_hdr_thread_data .result = -1 ;
1669
+ if (pthread_create (
1670
+ & verify_hdr_thread_data .thread_id , NULL ,
1671
+ verify_hdr_thread , & verify_hdr_thread_data ))
1672
+ die_errno ("unable to start verify_hdr_thread" );
1673
+ }
1674
+ #endif
1625
1675
1626
1676
hashcpy (istate -> sha1 , (const unsigned char * )hdr + mmap_size - 20 );
1627
1677
istate -> version = ntohl (hdr -> hdr_version );
@@ -1669,6 +1719,16 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
1669
1719
src_offset += 8 ;
1670
1720
src_offset += extsize ;
1671
1721
}
1722
+
1723
+ #ifndef NO_PTHREADS
1724
+ if (mmap_size >= VERIFY_HDR_THRESHOLD ) {
1725
+ if (pthread_join (verify_hdr_thread_data .thread_id , NULL ))
1726
+ die_errno ("unable to join verify_hdr_thread" );
1727
+ if (verify_hdr_thread_data .result < 0 )
1728
+ goto unmap ;
1729
+ }
1730
+ #endif
1731
+
1672
1732
munmap (mmap , mmap_size );
1673
1733
return istate -> cache_nr ;
1674
1734
0 commit comments