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)
@@ -1462,6 +1466,34 @@ static int verify_hdr(struct cache_header *hdr, unsigned long size)
1462
1466
return 0 ;
1463
1467
}
1464
1468
1469
+ #ifndef NO_PTHREADS
1470
+ /*
1471
+ * Require index file to be larger than this threshold before
1472
+ * we bother using a thread to verify the SHA.
1473
+ * This value was arbitrarily chosen.
1474
+ */
1475
+ #define VERIFY_HDR_THRESHOLD 10*1024*1024
1476
+
1477
+ struct verify_hdr_thread_data
1478
+ {
1479
+ pthread_t thread_id ;
1480
+ struct cache_header * hdr ;
1481
+ size_t size ;
1482
+ int result ;
1483
+ };
1484
+
1485
+ /*
1486
+ * A thread proc to run the verify_hdr() computation
1487
+ * in a background thread.
1488
+ */
1489
+ static void * verify_hdr_thread (void * _data )
1490
+ {
1491
+ struct verify_hdr_thread_data * p = _data ;
1492
+ p -> result = verify_hdr (p -> hdr , (unsigned long )p -> size );
1493
+ return NULL ;
1494
+ }
1495
+ #endif
1496
+
1465
1497
static int read_index_extension (struct index_state * istate ,
1466
1498
const char * ext , void * data , unsigned long sz )
1467
1499
{
@@ -1664,6 +1696,9 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
1664
1696
void * mmap ;
1665
1697
size_t mmap_size ;
1666
1698
struct strbuf previous_name_buf = STRBUF_INIT , * previous_name ;
1699
+ #ifndef NO_PTHREADS
1700
+ struct verify_hdr_thread_data verify_hdr_thread_data ;
1701
+ #endif
1667
1702
1668
1703
if (istate -> initialized )
1669
1704
return istate -> cache_nr ;
@@ -1690,8 +1725,23 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
1690
1725
close (fd );
1691
1726
1692
1727
hdr = mmap ;
1728
+ #ifdef NO_PTHREADS
1693
1729
if (verify_hdr (hdr , mmap_size ) < 0 )
1694
1730
goto unmap ;
1731
+ #else
1732
+ if (mmap_size < VERIFY_HDR_THRESHOLD ) {
1733
+ if (verify_hdr (hdr , mmap_size ) < 0 )
1734
+ goto unmap ;
1735
+ } else {
1736
+ verify_hdr_thread_data .hdr = hdr ;
1737
+ verify_hdr_thread_data .size = mmap_size ;
1738
+ verify_hdr_thread_data .result = -1 ;
1739
+ if (pthread_create (
1740
+ & verify_hdr_thread_data .thread_id , NULL ,
1741
+ verify_hdr_thread , & verify_hdr_thread_data ))
1742
+ die_errno ("unable to start verify_hdr_thread" );
1743
+ }
1744
+ #endif
1695
1745
1696
1746
hashcpy (istate -> sha1 , (const unsigned char * )hdr + mmap_size - 20 );
1697
1747
istate -> version = ntohl (hdr -> hdr_version );
@@ -1739,6 +1789,16 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
1739
1789
src_offset += 8 ;
1740
1790
src_offset += extsize ;
1741
1791
}
1792
+
1793
+ #ifndef NO_PTHREADS
1794
+ if (mmap_size >= VERIFY_HDR_THRESHOLD ) {
1795
+ if (pthread_join (verify_hdr_thread_data .thread_id , NULL ))
1796
+ die_errno ("unable to join verify_hdr_thread" );
1797
+ if (verify_hdr_thread_data .result < 0 )
1798
+ goto unmap ;
1799
+ }
1800
+ #endif
1801
+
1742
1802
munmap (mmap , mmap_size );
1743
1803
return istate -> cache_nr ;
1744
1804
0 commit comments