Re: [PATCH] git grep: be careful to use mutices only when they are initialized
From: Jeff King <hidden>
Date: 2016-06-15 22:52:20
On Wed, Oct 26, 2011 at 01:02:40PM -0700, Junio C Hamano wrote:
- Could we lose "#ifndef NO_PTHREADS" inside grep_sha1(), grep_file(), and possibly cmd_grep() functions and let the compiler optimize things away under NO_PTHREADS compilation?
I don't think so. If NO_PTHREADS is set, we might not have pthread
functions at all. Sure, many compilers will optimize:
if (0)
pthread_mutex_lock(...);
to remove the call completely. But would a compiler be wrong to complain
that pthread_mutex_lock is not defined, or to include reference to it
for the linker? gcc, both with and without optimizations, will complain
about:
echo 'int main() { if (0) does_not_exist(); return 0; }' >foo.c
gcc -Wall -c foo.c
though it does actually remove the dead code and link properly. I
wouldn't be surprised if some other compilers don't work, though (and of
course the warning is ugly).
I think you would have to do something like this in thread-utils.h:
#ifndef NO_PTHREADS
#include <pthread.h>
#else
#define pthread_mutex_t int
#define pthread_mutex_init(m, a) do {} while(0)
#define pthread_mutex_lock(m) do {} while(0)
#define pthread_mutex_unlock(m) do {} while (0)
/* and so forth for every pthread function */
#endif
-Peff