Skip to content

Commit 220c045

Browse files
committed
Merge branch 'js/grep-mutex'
* js/grep-mutex: builtin/grep: simplify lock_and_read_sha1_file() builtin/grep: make lock/unlock into static inline functions git grep: be careful to use mutexes only when they are initialized
2 parents 82bc9f5 + 7641613 commit 220c045

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

builtin/grep.c

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,32 @@ static int all_work_added;
7474
/* This lock protects all the variables above. */
7575
static pthread_mutex_t grep_mutex;
7676

77+
static inline void grep_lock(void)
78+
{
79+
if (use_threads)
80+
pthread_mutex_lock(&grep_mutex);
81+
}
82+
83+
static inline void grep_unlock(void)
84+
{
85+
if (use_threads)
86+
pthread_mutex_unlock(&grep_mutex);
87+
}
88+
7789
/* Used to serialize calls to read_sha1_file. */
7890
static pthread_mutex_t read_sha1_mutex;
7991

80-
#define grep_lock() pthread_mutex_lock(&grep_mutex)
81-
#define grep_unlock() pthread_mutex_unlock(&grep_mutex)
82-
#define read_sha1_lock() pthread_mutex_lock(&read_sha1_mutex)
83-
#define read_sha1_unlock() pthread_mutex_unlock(&read_sha1_mutex)
92+
static inline void read_sha1_lock(void)
93+
{
94+
if (use_threads)
95+
pthread_mutex_lock(&read_sha1_mutex);
96+
}
97+
98+
static inline void read_sha1_unlock(void)
99+
{
100+
if (use_threads)
101+
pthread_mutex_unlock(&read_sha1_mutex);
102+
}
84103

85104
/* Signalled when a new work_item is added to todo. */
86105
static pthread_cond_t cond_add;
@@ -354,13 +373,9 @@ static void *lock_and_read_sha1_file(const unsigned char *sha1, enum object_type
354373
{
355374
void *data;
356375

357-
if (use_threads) {
358-
read_sha1_lock();
359-
data = read_sha1_file(sha1, type, size);
360-
read_sha1_unlock();
361-
} else {
362-
data = read_sha1_file(sha1, type, size);
363-
}
376+
read_sha1_lock();
377+
data = read_sha1_file(sha1, type, size);
378+
read_sha1_unlock();
364379
return data;
365380
}
366381

0 commit comments

Comments
 (0)