Re: [PATCH] git grep: be careful to use mutices only when they are initialized
- From
Jeff King <peff@peff.net>
- Date
- Oct 27, 2011, 18:02 UTC
- Message-ID
- <20111027180251.GE1967@sigill.intra.peff.net>
- In-Reply-To
- <7v39ef34in.fsf@alter.siamese.dyndns.org>
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.cthough 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