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

Re: [msysGit] [PATCH] git grep: be careful to use mutices only when they are initialized

From
Pat Thoyts <patthoyts@gmail.com>
Date
Oct 26, 2011, 09:19 UTC
Message-ID
<CABNJ2GL7khag3uD=kX+Ui9aqn5A-bkM5wfK=of0-=3fRrjmJ4w@mail.gmail.com>
In-Reply-To
<alpine.DEB.1.00.1110251223500.32316@s15462909.onlinehome-server.info>

On 25 October 2011 18:25, Johannes Schindelin <Johannes.Schindelin@gmx.de> wrote:

Show 10 quoted lines
>
> Rather nasty things happen when a mutex is not initialized but locked
> nevertheless. Now, when we're not running in a threaded manner, the mutex
> is not initialized, which is correct. But then we went and used the mutex
> anyway, which -- at least on Windows -- leads to a hard crash (ordinarily
> it would be called a segmentation fault, but in Windows speak it is an
> access violation).
>
> This problem was identified by our faithful tests when run in the msysGit
> environment.

I did not see this failure when running the tests on my machine. But then threaded issues are often intermittent depending on load, number of cores, phase of the moon, etc. You never said _which_ test either although there are only 3 to try - most likey t7810-grep.sh

I was going to point out that it should be "mutexes" but I see it is committed already :)

> To avoid having to wrap the line due to the 80 column limit, we use
So last century!
Show 34 quoted lines
> the name "WHEN_THREADED" instead of "IF_USE_THREADS" because it is one
> character shorter. Which is all we need in this case.
>
> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
> ---
>
>        I looked around a bit but ran out of time to identify the reason why
>        this was not caught earlier.
>
>  builtin/grep.c |    9 +++++----
>  1 files changed, 5 insertions(+), 4 deletions(-)
>
> diff --git a/builtin/grep.c b/builtin/grep.c
> index 92eeada..e94c5fe 100644
> --- a/builtin/grep.c
> +++ b/builtin/grep.c
> @@ -78,10 +78,11 @@ static pthread_mutex_t grep_mutex;
>  /* Used to serialize calls to read_sha1_file. */
>  static pthread_mutex_t read_sha1_mutex;
>
> -#define grep_lock() pthread_mutex_lock(&grep_mutex)
> -#define grep_unlock() pthread_mutex_unlock(&grep_mutex)
> -#define read_sha1_lock() pthread_mutex_lock(&read_sha1_mutex)
> -#define read_sha1_unlock() pthread_mutex_unlock(&read_sha1_mutex)
> +#define WHEN_THREADED(x) do { if (use_threads) (x); } while (0)
> +#define grep_lock() WHEN_THREADED(pthread_mutex_lock(&grep_mutex))
> +#define grep_unlock() WHEN_THREADED(pthread_mutex_unlock(&grep_mutex))
> +#define read_sha1_lock() WHEN_THREADED(pthread_mutex_lock(&read_sha1_mutex))
> +#define read_sha1_unlock() WHEN_THREADED(pthread_mutex_unlock(&read_sha1_mutex))
>
>  /* Signalled when a new work_item is added to todo. */
>  static pthread_cond_t cond_add;
> --
> 1.7.5.3.4540.g15f89
Works for me.
Pat.
Previous: Johannes SchindelinNext: Junio C Hamano
Message 4 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.