git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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.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
Previous: René Scharfe
Message 11 of 11 in “git grep: be careful to use mutices only when they are initialized”
  1. git grep: be careful to use mutices only when they are initializedJohannes Schindelin, Oct 25, 2011
  2. Tay Ray ChuanOct 26, 2011
  3. Johannes SchindelinOct 26, 2011
  4. Pat ThoytsOct 26, 2011
  5. Junio C HamanoOct 26, 2011
  6. Johannes SchindelinOct 26, 2011
  7. Junio C HamanoOct 26, 2011
  8. Junio C HamanoOct 26, 2011
  9. Junio C HamanoOct 26, 2011
  10. René ScharfeOct 27, 2011
  11. Jeff KingOct 27, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.