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
Junio C Hamano <gitster@pobox.com>
Date
Oct 26, 2011, 20:04 UTC
Message-ID
<7vr51z1pvi.fsf@alter.siamese.dyndns.org>
In-Reply-To
<7v39ef34in.fsf@alter.siamese.dyndns.org>
Junio C Hamano <gitster@pobox.com> writes:
Show 7 quoted lines
> The remainder of this message are hints and random thoughts on potential
> follow-up patches that may want to build on top of this patch for further
> clean-ups (not specifically meant for Dscho but for other people on both
> mailing lists).
> ...
>  - Wouldn't the result be more readable to make these into static inline
>    functions?
That would look like this.
-- >8 --
Subject: [PATCH] builtin/grep: make lock/unlock into static inline
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 builtin/grep.c |   28 +++++++++++++++++++++++-----
 1 files changed, 23 insertions(+), 5 deletions(-)
diff --git a/builtin/grep.c b/builtin/grep.c
index 88b0c80..3ddfae4 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -74,14 +74,32 @@ static int all_work_added;
 /* This lock protects all the variables above. */
 static pthread_mutex_t grep_mutex;
 
+static inline void grep_lock(void)
+{
+	if (use_threads)
+		pthread_mutex_lock(&grep_mutex);
+}
+
+static inline void grep_unlock(void)
+{
+	if (use_threads)
+		pthread_mutex_unlock(&grep_mutex);
+}
+
 /* Used to serialize calls to read_sha1_file. */
 static pthread_mutex_t 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))
+static inline void read_sha1_lock(void)
+{
+	if (use_threads)
+		pthread_mutex_lock(&read_sha1_mutex);
+}
+
+static inline void read_sha1_unlock(void)
+{
+	if (use_threads)
+		pthread_mutex_unlock(&read_sha1_mutex);
+}
 
 /* Signalled when a new work_item is added to todo. */
 static pthread_cond_t cond_add;
-- 
1.7.7.1.504.gcc718
Previous: Junio C HamanoNext: Junio C Hamano
Message 8 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.