Win32: patch Windows environment on startup
[git/mingw/4msysgit/kblees.git] / preload-index.c
blob49cb08df96faa90101bb25d8f96acaffc066f19b
1 /*
2 * Copyright (C) 2008 Linus Torvalds
3 */
4 #include "cache.h"
6 #ifdef NO_PTHREADS
7 static void preload_index(struct index_state *index, const char **pathspec)
9 ; /* nothing */
11 #else
13 #include <pthread.h>
16 * Mostly randomly chosen maximum thread counts: we
17 * cap the parallelism to 20 threads, and we want
18 * to have at least 500 lstat's per thread for it to
19 * be worth starting a thread.
21 #define MAX_PARALLEL (20)
22 #define THREAD_COST (500)
24 struct thread_data {
25 pthread_t pthread;
26 struct index_state *index;
27 const char **pathspec;
28 int offset, nr;
31 static void *preload_thread(void *_data)
33 int nr;
34 struct thread_data *p = _data;
35 struct index_state *index = p->index;
36 struct cache_entry **cep = index->cache + p->offset;
37 struct cache_def cache;
38 struct pathspec pathspec;
40 init_pathspec(&pathspec, p->pathspec);
41 memset(&cache, 0, sizeof(cache));
42 nr = p->nr;
43 if (nr + p->offset > index->cache_nr)
44 nr = index->cache_nr - p->offset;
46 do {
47 struct cache_entry *ce = *cep++;
48 struct stat st;
50 if (ce_stage(ce))
51 continue;
52 if (S_ISGITLINK(ce->ce_mode))
53 continue;
54 if (ce_uptodate(ce))
55 continue;
56 if (!ce_path_match(ce, &pathspec))
57 continue;
58 if (threaded_has_symlink_leading_path(&cache, ce->name, ce_namelen(ce)))
59 continue;
60 if (lstat(ce->name, &st))
61 continue;
62 if (ie_match_stat(index, ce, &st, CE_MATCH_RACY_IS_DIRTY))
63 continue;
64 ce_mark_uptodate(ce);
65 } while (--nr > 0);
66 free_pathspec(&pathspec);
67 return NULL;
70 static void preload_index(struct index_state *index, const char **pathspec)
72 int threads, i, work, offset;
73 struct thread_data data[MAX_PARALLEL];
75 if (!core_preload_index)
76 return;
78 threads = index->cache_nr / THREAD_COST;
79 if (threads < 2)
80 return;
81 if (threads > MAX_PARALLEL)
82 threads = MAX_PARALLEL;
83 offset = 0;
84 work = DIV_ROUND_UP(index->cache_nr, threads);
85 for (i = 0; i < threads; i++) {
86 struct thread_data *p = data+i;
87 p->index = index;
88 p->pathspec = pathspec;
89 p->offset = offset;
90 p->nr = work;
91 offset += work;
92 if (pthread_create(&p->pthread, NULL, preload_thread, p))
93 die("unable to create threaded lstat");
95 for (i = 0; i < threads; i++) {
96 struct thread_data *p = data+i;
97 if (pthread_join(p->pthread, NULL))
98 die("unable to join threaded lstat");
101 #endif
103 int read_index_preload(struct index_state *index, const char **pathspec)
105 int retval = read_index(index);
107 preload_index(index, pathspec);
108 return retval;