Clean up pthread_create() error handling

Normally pthread_create() rarely fails. But with new pthreads wrapper, pthread_create() will return ENOSYS on a system without thread support. Threaded code _is_ protected by HAVE_THREADS and pthread_create() should never run in the first place. But the situation could change in the future and bugs may sneak in. Make sure that all pthread_create() reports the error cause. While at there, mark these strings for translation if they aren't. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Nov 3, 2018 at 09:48 UTC 2179045fd02acca83127f8d15ccd0eeb70b17400
3 files changed +17 -9
name-hash.c
+10 -6
@@ -494,6 +494,7 @@ static inline void lazy_update_dir_ref_counts(
494 static void threaded_lazy_init_name_hash(
495 struct index_state *istate)
496 {
497 + int err;
498 int nr_each;
499 int k_start;
500 int t;
@@ -526,8 +527,9 @@ static void threaded_lazy_init_name_hash(
527 if (k_start > istate->cache_nr)
528 k_start = istate->cache_nr;
529 td_dir_t->k_end = k_start;
529 - if (pthread_create(&td_dir_t->pthread, NULL, lazy_dir_thread_proc, td_dir_t))
530 - die("unable to create lazy_dir_thread");
530 + err = pthread_create(&td_dir_t->pthread, NULL, lazy_dir_thread_proc, td_dir_t);
531 + if (err)
532 + die(_("unable to create lazy_dir thread: %s"), strerror(err));
533 }
534 for (t = 0; t < lazy_nr_dir_threads; t++) {
535 struct lazy_dir_thread_data *td_dir_t = td_dir + t;
@@ -547,13 +549,15 @@ static void threaded_lazy_init_name_hash(
549 */
550 td_name->istate = istate;
551 td_name->lazy_entries = lazy_entries;
550 - if (pthread_create(&td_name->pthread, NULL, lazy_name_thread_proc, td_name))
551 - die("unable to create lazy_name_thread");
552 + err = pthread_create(&td_name->pthread, NULL, lazy_name_thread_proc, td_name);
553 + if (err)
554 + die(_("unable to create lazy_name thread: %s"), strerror(err));
555
556 lazy_update_dir_ref_counts(istate, lazy_entries);
557
555 - if (pthread_join(td_name->pthread, NULL))
556 - die("unable to join lazy_name_thread");
558 + err = pthread_join(td_name->pthread, NULL);
559 + if (err)
560 + die(_("unable to join lazy_name thread: %s"), strerror(err));
561
562 cleanup_dir_mutex();
563
preload-index.c
+6 -2
@@ -121,6 +121,8 @@ static void preload_index(struct index_state *index,
121
122 for (i = 0; i < threads; i++) {
123 struct thread_data *p = data+i;
124 + int err;
125 +
126 p->index = index;
127 if (pathspec)
128 copy_pathspec(&p->pathspec, pathspec);
@@ -129,8 +131,10 @@ static void preload_index(struct index_state *index,
131 if (pd.progress)
132 p->progress = &pd;
133 offset += work;
132 - if (pthread_create(&p->pthread, NULL, preload_thread, p))
133 - die("unable to create threaded lstat");
134 + err = pthread_create(&p->pthread, NULL, preload_thread, p);
135 +
136 + if (err)
137 + die(_("unable to create threaded lstat: %s"), strerror(err));
138 }
139 for (i = 0; i < threads; i++) {
140 struct thread_data *p = data+i;
run-command.c
+1 -1
@@ -1213,7 +1213,7 @@ int start_async(struct async *async)
1213 {
1214 int err = pthread_create(&async->tid, NULL, run_thread, async);
1215 if (err) {
1216 - error_errno("cannot create thread");
1216 + error(_("cannot create async thread: %s"), strerror(err));
1217 goto error;
1218 }
1219 }