git/thread-utils.h
Jeff King 7d0037b59a thread-utils: introduce optional barrier type
One thread primitive we don't yet support is a barrier: it waits for all
threads to reach a synchronization point before letting any of them
continue. This would be useful for avoiding the LSan race we see in
index-pack (and other places) by having all threads complete their
initialization before any of them start to do real work.

POSIX introduced a pthread_barrier_t in 2004, which does what we want.
But if we want to rely on it:

  1. Our Windows pthread emulation would need a new set of wrapper
     functions. There's a Synchronization Barrier primitive there, which
     was introduced in Windows 8 (which is old enough for us to depend
     on).

  2. macOS (and possibly other systems) has pthreads but not
     pthread_barrier_t. So there we'd have to implement our own barrier
     based on the mutex and cond primitives.

Those are do-able, but since we only care about avoiding races in our
LSan builds, there's an easier way: make it a noop on systems without a
native pthread barrier.

This patch introduces a "maybe_thread_barrier" API. The clunky name
(rather than just using pthread_barrier directly) should hopefully clue
people in that on some systems it will do nothing. It's wired to a
Makefile knob which has to be triggered manually, and we enable it for
the linux-leaks CI jobs (since we know we'll have it there).

There are some other possible options:

  - we could turn it on all the time for Linux systems based on uname.
    But we really only care about it for LSan builds, and there is no
    need to add extra code to regular builds.

  - we could turn it on only for LSan builds. But that would break
    builds on non-Linux platforms (like macOS) that otherwise should
    support sanitizers.

  - we could trigger only on the combination of Linux and LSan together.
    This isn't too hard to do, but the uname check isn't completely
    accurate. It is really about what your libc supports, and non-glibc
    systems might not have it (though at least musl seems to).

    So we'd risk breaking builds on those systems, which would need to
    add a new knob. Though the upside would be that running local "make
    SANITIZE=leak test" would be protected automatically.

And of course none of this protects LSan runs from races on systems
without pthread barriers. It's probably OK in practice to protect only
our CI jobs, though. The race is rare-ish and most leak-checking happens
through CI.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2024-12-30 06:18:57 -08:00

75 lines
2.0 KiB
C

#ifndef THREAD_COMPAT_H
#define THREAD_COMPAT_H
#ifndef NO_PTHREADS
#include <pthread.h>
#define HAVE_THREADS 1
#else
#define HAVE_THREADS 0
/*
* macros instead of typedefs because pthread definitions may have
* been pulled in by some system dependencies even though the user
* wants to disable pthread.
*/
#define pthread_t int
#define pthread_mutex_t int
#define pthread_cond_t int
#define pthread_key_t int
#define pthread_mutex_init(mutex, attr) dummy_pthread_init(mutex)
#define pthread_mutex_lock(mutex)
#define pthread_mutex_unlock(mutex)
#define pthread_mutex_destroy(mutex)
#define pthread_cond_init(cond, attr) dummy_pthread_init(cond)
#define pthread_cond_wait(cond, mutex)
#define pthread_cond_signal(cond)
#define pthread_cond_broadcast(cond)
#define pthread_cond_destroy(cond)
#define pthread_key_create(key, attr) dummy_pthread_init(key)
#define pthread_key_delete(key)
#define pthread_create(thread, attr, fn, data) \
dummy_pthread_create(thread, attr, fn, data)
#define pthread_join(thread, retval) \
dummy_pthread_join(thread, retval)
#define pthread_setspecific(key, data)
#define pthread_getspecific(key) NULL
int dummy_pthread_create(pthread_t *pthread, const void *attr,
void *(*fn)(void *), void *data);
int dummy_pthread_join(pthread_t pthread, void **retval);
int dummy_pthread_init(void *);
#endif
int online_cpus(void);
int init_recursive_mutex(pthread_mutex_t*);
#ifdef THREAD_BARRIER_PTHREAD
#define maybe_thread_barrier_t pthread_barrier_t
#define maybe_thread_barrier_init pthread_barrier_init
#define maybe_thread_barrier_wait pthread_barrier_wait
#define maybe_thread_barrier_destroy pthread_barrier_destroy
#else
#define maybe_thread_barrier_t int
static inline int maybe_thread_barrier_init(maybe_thread_barrier_t *b UNUSED,
void *attr UNUSED,
unsigned nr UNUSED)
{
errno = ENOSYS;
return -1;
}
#define maybe_thread_barrier_wait(barrier)
#define maybe_thread_barrier_destroy(barrier)
#endif
#endif /* THREAD_COMPAT_H */