Skip to content

Commit 26b80a8

Browse files
committed
Merge branch 'nd/pthreads'
The codebase has been cleaned up to reduce "#ifndef NO_PTHREADS". * nd/pthreads: Clean up pthread_create() error handling read-cache.c: initialize copy_len to shut up gcc 8 read-cache.c: reduce branching based on HAVE_THREADS read-cache.c: remove #ifdef NO_PTHREADS pack-objects: remove #ifdef NO_PTHREADS preload-index.c: remove #ifdef NO_PTHREADS grep: clean up num_threads handling grep: remove #ifdef NO_PTHREADS attr.c: remove #ifdef NO_PTHREADS name-hash.c: remove #ifdef NO_PTHREADS index-pack: remove #ifdef NO_PTHREADS send-pack.c: move async's #ifdef NO_PTHREADS back to run-command.c run-command.h: include thread-utils.h instead of pthread.h thread-utils: macros to unconditionally compile pthreads API
2 parents 62ca33e + 2179045 commit 26b80a8

File tree

16 files changed

+186
-231
lines changed

16 files changed

+186
-231
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,7 @@ LIB_OBJS += sub-process.o
993993
LIB_OBJS += symlinks.o
994994
LIB_OBJS += tag.o
995995
LIB_OBJS += tempfile.o
996+
LIB_OBJS += thread-utils.o
996997
LIB_OBJS += tmp-objdir.o
997998
LIB_OBJS += trace.o
998999
LIB_OBJS += trailer.o
@@ -1677,7 +1678,6 @@ ifdef NO_PTHREADS
16771678
else
16781679
BASIC_CFLAGS += $(PTHREAD_CFLAGS)
16791680
EXTLIBS += $(PTHREAD_LIBS)
1680-
LIB_OBJS += thread-utils.o
16811681
endif
16821682

16831683
ifdef HAVE_PATHS_H

attr.c

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,17 @@ const char *git_attr_name(const struct git_attr *attr)
4141

4242
struct attr_hashmap {
4343
struct hashmap map;
44-
#ifndef NO_PTHREADS
4544
pthread_mutex_t mutex;
46-
#endif
4745
};
4846

4947
static inline void hashmap_lock(struct attr_hashmap *map)
5048
{
51-
#ifndef NO_PTHREADS
5249
pthread_mutex_lock(&map->mutex);
53-
#endif
5450
}
5551

5652
static inline void hashmap_unlock(struct attr_hashmap *map)
5753
{
58-
#ifndef NO_PTHREADS
5954
pthread_mutex_unlock(&map->mutex);
60-
#endif
6155
}
6256

6357
/*
@@ -498,23 +492,17 @@ static struct check_vector {
498492
size_t nr;
499493
size_t alloc;
500494
struct attr_check **checks;
501-
#ifndef NO_PTHREADS
502495
pthread_mutex_t mutex;
503-
#endif
504496
} check_vector;
505497

506498
static inline void vector_lock(void)
507499
{
508-
#ifndef NO_PTHREADS
509500
pthread_mutex_lock(&check_vector.mutex);
510-
#endif
511501
}
512502

513503
static inline void vector_unlock(void)
514504
{
515-
#ifndef NO_PTHREADS
516505
pthread_mutex_unlock(&check_vector.mutex);
517-
#endif
518506
}
519507

520508
static void check_vector_add(struct attr_check *c)
@@ -1181,8 +1169,6 @@ void git_all_attrs(const struct index_state *istate,
11811169

11821170
void attr_start(void)
11831171
{
1184-
#ifndef NO_PTHREADS
11851172
pthread_mutex_init(&g_attr_hashmap.mutex, NULL);
11861173
pthread_mutex_init(&check_vector.mutex, NULL);
1187-
#endif
11881174
}

builtin/grep.c

Lines changed: 30 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ static int recurse_submodules;
3434
#define GREP_NUM_THREADS_DEFAULT 8
3535
static int num_threads;
3636

37-
#ifndef NO_PTHREADS
3837
static pthread_t *threads;
3938

4039
/* We use one producer thread and THREADS consumer
@@ -70,13 +69,11 @@ static pthread_mutex_t grep_mutex;
7069

7170
static inline void grep_lock(void)
7271
{
73-
assert(num_threads);
7472
pthread_mutex_lock(&grep_mutex);
7573
}
7674

7775
static inline void grep_unlock(void)
7876
{
79-
assert(num_threads);
8077
pthread_mutex_unlock(&grep_mutex);
8178
}
8279

@@ -234,6 +231,9 @@ static int wait_all(void)
234231
int hit = 0;
235232
int i;
236233

234+
if (!HAVE_THREADS)
235+
BUG("Never call this function unless you have started threads");
236+
237237
grep_lock();
238238
all_work_added = 1;
239239

@@ -265,13 +265,6 @@ static int wait_all(void)
265265

266266
return hit;
267267
}
268-
#else /* !NO_PTHREADS */
269-
270-
static int wait_all(void)
271-
{
272-
return 0;
273-
}
274-
#endif
275268

276269
static int grep_cmd_config(const char *var, const char *value, void *cb)
277270
{
@@ -284,17 +277,15 @@ static int grep_cmd_config(const char *var, const char *value, void *cb)
284277
if (num_threads < 0)
285278
die(_("invalid number of threads specified (%d) for %s"),
286279
num_threads, var);
287-
#ifdef NO_PTHREADS
288-
else if (num_threads && num_threads != 1) {
280+
else if (!HAVE_THREADS && num_threads > 1) {
289281
/*
290282
* TRANSLATORS: %s is the configuration
291283
* variable for tweaking threads, currently
292284
* grep.threads
293285
*/
294286
warning(_("no threads support, ignoring %s"), var);
295-
num_threads = 0;
287+
num_threads = 1;
296288
}
297-
#endif
298289
}
299290

300291
if (!strcmp(var, "submodule.recurse"))
@@ -330,17 +321,14 @@ static int grep_oid(struct grep_opt *opt, const struct object_id *oid,
330321
grep_source_init(&gs, GREP_SOURCE_OID, pathbuf.buf, path, oid);
331322
strbuf_release(&pathbuf);
332323

333-
#ifndef NO_PTHREADS
334-
if (num_threads) {
324+
if (num_threads > 1) {
335325
/*
336326
* add_work() copies gs and thus assumes ownership of
337327
* its fields, so do not call grep_source_clear()
338328
*/
339329
add_work(opt, &gs);
340330
return 0;
341-
} else
342-
#endif
343-
{
331+
} else {
344332
int hit;
345333

346334
hit = grep_source(opt, &gs);
@@ -363,17 +351,14 @@ static int grep_file(struct grep_opt *opt, const char *filename)
363351
grep_source_init(&gs, GREP_SOURCE_FILE, buf.buf, filename, filename);
364352
strbuf_release(&buf);
365353

366-
#ifndef NO_PTHREADS
367-
if (num_threads) {
354+
if (num_threads > 1) {
368355
/*
369356
* add_work() copies gs and thus assumes ownership of
370357
* its fields, so do not call grep_source_clear()
371358
*/
372359
add_work(opt, &gs);
373360
return 0;
374-
} else
375-
#endif
376-
{
361+
} else {
377362
int hit;
378363

379364
hit = grep_source(opt, &gs);
@@ -1049,39 +1034,35 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
10491034
pathspec.recursive = 1;
10501035
pathspec.recurse_submodules = !!recurse_submodules;
10511036

1052-
#ifndef NO_PTHREADS
1053-
if (list.nr || cached || show_in_pager)
1054-
num_threads = 0;
1055-
else if (num_threads == 0)
1056-
num_threads = GREP_NUM_THREADS_DEFAULT;
1057-
else if (num_threads < 0)
1058-
die(_("invalid number of threads specified (%d)"), num_threads);
1059-
if (num_threads == 1)
1060-
num_threads = 0;
1061-
#else
1062-
if (num_threads)
1037+
if (list.nr || cached || show_in_pager) {
1038+
if (num_threads > 1)
1039+
warning(_("invalid option combination, ignoring --threads"));
1040+
num_threads = 1;
1041+
} else if (!HAVE_THREADS && num_threads > 1) {
10631042
warning(_("no threads support, ignoring --threads"));
1064-
num_threads = 0;
1065-
#endif
1043+
num_threads = 1;
1044+
} else if (num_threads < 0)
1045+
die(_("invalid number of threads specified (%d)"), num_threads);
1046+
else if (num_threads == 0)
1047+
num_threads = HAVE_THREADS ? GREP_NUM_THREADS_DEFAULT : 1;
10661048

1067-
if (!num_threads)
1049+
if (num_threads > 1) {
1050+
if (!HAVE_THREADS)
1051+
BUG("Somebody got num_threads calculation wrong!");
1052+
if (!(opt.name_only || opt.unmatch_name_only || opt.count)
1053+
&& (opt.pre_context || opt.post_context ||
1054+
opt.file_break || opt.funcbody))
1055+
skip_first_line = 1;
1056+
start_threads(&opt);
1057+
} else {
10681058
/*
10691059
* The compiled patterns on the main path are only
10701060
* used when not using threading. Otherwise
1071-
* start_threads() below calls compile_grep_patterns()
1061+
* start_threads() above calls compile_grep_patterns()
10721062
* for each thread.
10731063
*/
10741064
compile_grep_patterns(&opt);
1075-
1076-
#ifndef NO_PTHREADS
1077-
if (num_threads) {
1078-
if (!(opt.name_only || opt.unmatch_name_only || opt.count)
1079-
&& (opt.pre_context || opt.post_context ||
1080-
opt.file_break || opt.funcbody))
1081-
skip_first_line = 1;
1082-
start_threads(&opt);
10831065
}
1084-
#endif
10851066

10861067
if (show_in_pager && (cached || list.nr))
10871068
die(_("--open-files-in-pager only works on the worktree"));
@@ -1132,7 +1113,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
11321113
hit = grep_objects(&opt, &pathspec, &list);
11331114
}
11341115

1135-
if (num_threads)
1116+
if (num_threads > 1)
11361117
hit |= wait_all();
11371118
if (hit && show_in_pager)
11381119
run_pager(&opt, prefix);

0 commit comments

Comments
 (0)