preload-index.con commit send-email: Remove superfluous `my $editor = ...' (bec99cf)
   1/*
   2 * Copyright (C) 2008 Linus Torvalds
   3 */
   4#include "cache.h"
   5
   6#ifdef NO_PTHREADS
   7static void preload_index(struct index_state *index, const char **pathspec)
   8{
   9        ; /* nothing */
  10}
  11#else
  12
  13#include <pthread.h>
  14
  15/*
  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.
  20 */
  21#define MAX_PARALLEL (20)
  22#define THREAD_COST (500)
  23
  24struct thread_data {
  25        pthread_t pthread;
  26        struct index_state *index;
  27        const char **pathspec;
  28        int offset, nr;
  29};
  30
  31static void *preload_thread(void *_data)
  32{
  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
  38        nr = p->nr;
  39        if (nr + p->offset > index->cache_nr)
  40                nr = index->cache_nr - p->offset;
  41
  42        do {
  43                struct cache_entry *ce = *cep++;
  44                struct stat st;
  45
  46                if (ce_stage(ce))
  47                        continue;
  48                if (ce_uptodate(ce))
  49                        continue;
  50                if (!ce_path_match(ce, p->pathspec))
  51                        continue;
  52                if (lstat(ce->name, &st))
  53                        continue;
  54                if (ie_match_stat(index, ce, &st, CE_MATCH_RACY_IS_DIRTY))
  55                        continue;
  56                ce_mark_uptodate(ce);
  57        } while (--nr > 0);
  58        return NULL;
  59}
  60
  61static void preload_index(struct index_state *index, const char **pathspec)
  62{
  63        int threads, i, work, offset;
  64        struct thread_data data[MAX_PARALLEL];
  65
  66        if (!core_preload_index)
  67                return;
  68
  69        threads = index->cache_nr / THREAD_COST;
  70        if (threads < 2)
  71                return;
  72        if (threads > MAX_PARALLEL)
  73                threads = MAX_PARALLEL;
  74        offset = 0;
  75        work = (index->cache_nr + threads - 1) / threads;
  76        for (i = 0; i < threads; i++) {
  77                struct thread_data *p = data+i;
  78                p->index = index;
  79                p->pathspec = pathspec;
  80                p->offset = offset;
  81                p->nr = work;
  82                offset += work;
  83                if (pthread_create(&p->pthread, NULL, preload_thread, p))
  84                        die("unable to create threaded lstat");
  85        }
  86        for (i = 0; i < threads; i++) {
  87                struct thread_data *p = data+i;
  88                if (pthread_join(p->pthread, NULL))
  89                        die("unable to join threaded lstat");
  90        }
  91}
  92#endif
  93
  94int read_index_preload(struct index_state *index, const char **pathspec)
  95{
  96        int retval = read_index(index);
  97
  98        preload_index(index, pathspec);
  99        return retval;
 100}