Skip to content

Commit e285a17

Browse files
committed
Merge pull request #978 from jeffhostetler/jeffhostetler/thread_verify_hdr
read-cache: run verify_hdr() in background thread
2 parents 4a85eae + b90f396 commit e285a17

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)
@@ -1392,6 +1396,34 @@ static int verify_hdr(struct cache_header *hdr, unsigned long size)
13921396
return 0;
13931397
}
13941398

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+
13951427
static int read_index_extension(struct index_state *istate,
13961428
const char *ext, void *data, unsigned long sz)
13971429
{
@@ -1577,6 +1609,9 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
15771609
void *mmap;
15781610
size_t mmap_size;
15791611
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
15801615

15811616
if (istate->initialized)
15821617
return istate->cache_nr;
@@ -1603,8 +1638,23 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
16031638
close(fd);
16041639

16051640
hdr = mmap;
1641+
#ifdef NO_PTHREADS
16061642
if (verify_hdr(hdr, mmap_size) < 0)
16071643
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
16081658

16091659
hashcpy(istate->sha1, (const unsigned char *)hdr + mmap_size - 20);
16101660
istate->version = ntohl(hdr->hdr_version);
@@ -1652,6 +1702,16 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
16521702
src_offset += 8;
16531703
src_offset += extsize;
16541704
}
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+
16551715
munmap(mmap, mmap_size);
16561716
return istate->cache_nr;
16571717

0 commit comments

Comments
 (0)