Skip to content

Commit cff52f1

Browse files
author
Git for Windows Build Agent
committed
Merge pull request #978 from jeffhostetler/jeffhostetler/thread_verify_hdr
read-cache: run verify_hdr() in background thread
2 parents 72e705c + 6fb46c7 commit cff52f1

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

read-cache.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
#include "split-index.h"
2020
#include "utf8.h"
2121

22+
#ifndef NO_PTHREADS
23+
#include <pthread.h>
24+
#endif
25+
2226
/* Mask for the name length in ce_flags in the on-disk index */
2327

2428
#define CE_NAMEMASK (0x0fff)
@@ -1399,6 +1403,34 @@ static int verify_hdr(struct cache_header *hdr, unsigned long size)
13991403
return 0;
14001404
}
14011405

1406+
#ifndef NO_PTHREADS
1407+
/*
1408+
* Require index file to be larger than this threshold before
1409+
* we bother using a thread to verify the SHA.
1410+
* This value was arbitrarily chosen.
1411+
*/
1412+
#define VERIFY_HDR_THRESHOLD 10*1024*1024
1413+
1414+
struct verify_hdr_thread_data
1415+
{
1416+
pthread_t thread_id;
1417+
struct cache_header *hdr;
1418+
size_t size;
1419+
int result;
1420+
};
1421+
1422+
/*
1423+
* A thread proc to run the verify_hdr() computation
1424+
* in a background thread.
1425+
*/
1426+
static void *verify_hdr_thread(void *_data)
1427+
{
1428+
struct verify_hdr_thread_data *p = _data;
1429+
p->result = verify_hdr(p->hdr, (unsigned long)p->size);
1430+
return NULL;
1431+
}
1432+
#endif
1433+
14021434
static int read_index_extension(struct index_state *istate,
14031435
const char *ext, void *data, unsigned long sz)
14041436
{
@@ -1587,6 +1619,9 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
15871619
void *mmap;
15881620
size_t mmap_size;
15891621
struct strbuf previous_name_buf = STRBUF_INIT, *previous_name;
1622+
#ifndef NO_PTHREADS
1623+
struct verify_hdr_thread_data verify_hdr_thread_data;
1624+
#endif
15901625

15911626
if (istate->initialized)
15921627
return istate->cache_nr;
@@ -1613,8 +1648,23 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
16131648
close(fd);
16141649

16151650
hdr = mmap;
1651+
#ifdef NO_PTHREADS
16161652
if (verify_hdr(hdr, mmap_size) < 0)
16171653
goto unmap;
1654+
#else
1655+
if (mmap_size < VERIFY_HDR_THRESHOLD) {
1656+
if (verify_hdr(hdr, mmap_size) < 0)
1657+
goto unmap;
1658+
} else {
1659+
verify_hdr_thread_data.hdr = hdr;
1660+
verify_hdr_thread_data.size = mmap_size;
1661+
verify_hdr_thread_data.result = -1;
1662+
if (pthread_create(
1663+
&verify_hdr_thread_data.thread_id, NULL,
1664+
verify_hdr_thread, &verify_hdr_thread_data))
1665+
die_errno("unable to start verify_hdr_thread");
1666+
}
1667+
#endif
16181668

16191669
hashcpy(istate->sha1, (const unsigned char *)hdr + mmap_size - 20);
16201670
istate->version = ntohl(hdr->hdr_version);
@@ -1662,6 +1712,16 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
16621712
src_offset += 8;
16631713
src_offset += extsize;
16641714
}
1715+
1716+
#ifndef NO_PTHREADS
1717+
if (mmap_size >= VERIFY_HDR_THRESHOLD) {
1718+
if (pthread_join(verify_hdr_thread_data.thread_id, NULL))
1719+
die_errno("unable to join verify_hdr_thread");
1720+
if (verify_hdr_thread_data.result < 0)
1721+
goto unmap;
1722+
}
1723+
#endif
1724+
16651725
munmap(mmap, mmap_size);
16661726
return istate->cache_nr;
16671727

0 commit comments

Comments
 (0)