git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Nicolas Pitre <nico@fluxnic.net>
To: Fredrik Kuivinen <frekui@gmail.com>
Cc: Shawn Pearce <spearce@spearce.org>,
	Junio C Hamano <gitster@pobox.com>,
	git@vger.kernel.org, Johannes Sixt <j6t@kdbg.org>
Subject: [PATCH v2] Make xmalloc and xrealloc thread-safe
Date: Tue, 06 Apr 2010 22:57:58 -0400 (EDT)	[thread overview]
Message-ID: <alpine.LFD.2.00.1004062152260.7232@xanadu.home> (raw)
In-Reply-To: <4c8ef71003270626y45685e69j28ccb8a8738b9083@mail.gmail.com>

[-- Attachment #1: Type: TEXT/PLAIN, Size: 4820 bytes --]

By providing a hook for the routine responsible for trying to free some
memory on malloc failure, we can ensure that the  called routine is
protected by the appropriate locks when threads are in play.

The obvious offender here was pack-objects which was calling xmalloc()
within threads while release_pack_memory() is not thread safe.

To avoid a deadlock if try_to_free_from_threads() is called while
read_lock is already locked within the same thread (may happen through
the read_sha1_file() path), a simple mutex ownership is added. This 
could have been handled automatically with the PTHREAD_MUTEX_RECURSIVE 
type but the Windows pthread emulation would get much more complex.

Signed-off-by: Nicolas Pitre <nico@fluxnic.net>
---

On Sat, 27 Mar 2010, Fredrik Kuivinen wrote:

> > +static void try_to_free_from_threads(size_t size)
> > +{
> > +       read_lock();
> > +       release_pack_memory(size, -1);
> > +       read_unlock();
> > +}
> > +
> 
> Will this really work in all cases? In the find_deltas -> try_delta ->
> read_sha1_file -> ... -> xmalloc call path, the mutex is already
> locked when we get to xmalloc.

So here's a fixed version.

diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c
index 9780258..d3ac41f 100644
--- a/builtin/pack-objects.c
+++ b/builtin/pack-objects.c
@@ -1211,8 +1211,17 @@ static int delta_cacheable(unsigned long src_size, unsigned long trg_size,
 #ifndef NO_PTHREADS
 
 static pthread_mutex_t read_mutex;
-#define read_lock()		pthread_mutex_lock(&read_mutex)
-#define read_unlock()		pthread_mutex_unlock(&read_mutex)
+static pthread_t read_mutex_owner;
+#define read_lock() \
+	do { \
+		pthread_mutex_lock(&read_mutex); \
+		read_mutex_owner = pthread_self(); \
+	} while (0)
+#define read_unlock() \
+	do { \
+		memset(&read_mutex_owner, 0, sizeof(read_mutex_owner)); \
+		pthread_mutex_unlock(&read_mutex); \
+	} while (0)
 
 static pthread_mutex_t cache_mutex;
 #define cache_lock()		pthread_mutex_lock(&cache_mutex)
@@ -1522,6 +1531,16 @@ static void find_deltas(struct object_entry **list, unsigned *list_size,
 
 #ifndef NO_PTHREADS
 
+static void try_to_free_from_threads(size_t size)
+{
+	int self = pthread_equal(read_mutex_owner, pthread_self());
+	if (!self)
+		read_lock();
+	release_pack_memory(size, -1);
+	if (!self)
+		read_unlock();
+}
+
 /*
  * The main thread waits on the condition that (at least) one of the workers
  * has stopped working (which is indicated in the .working member of
@@ -1556,10 +1575,12 @@ static void init_threaded_search(void)
 	pthread_mutex_init(&cache_mutex, NULL);
 	pthread_mutex_init(&progress_mutex, NULL);
 	pthread_cond_init(&progress_cond, NULL);
+	set_try_to_free_routine(try_to_free_from_threads);
 }
 
 static void cleanup_threaded_search(void)
 {
+	set_try_to_free_routine(NULL);
 	pthread_cond_destroy(&progress_cond);
 	pthread_mutex_destroy(&read_mutex);
 	pthread_mutex_destroy(&cache_mutex);
diff --git a/git-compat-util.h b/git-compat-util.h
index b56c297..4ee8f86 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -357,6 +357,8 @@ static inline void *gitmempcpy(void *dest, const void *src, size_t n)
 
 extern void release_pack_memory(size_t, int);
 
+extern void set_try_to_free_routine(void (*routine)(size_t));
+
 extern char *xstrdup(const char *str);
 extern void *xmalloc(size_t size);
 extern void *xmallocz(size_t size);
diff --git a/wrapper.c b/wrapper.c
index 10a6750..58201b6 100644
--- a/wrapper.c
+++ b/wrapper.c
@@ -3,11 +3,23 @@
  */
 #include "cache.h"
 
+static void try_to_free_builtin(size_t size)
+{
+	release_pack_memory(size, -1);
+}
+
+static void (*try_to_free_routine)(size_t size) = try_to_free_builtin;
+
+void set_try_to_free_routine(void (*routine)(size_t))
+{
+	try_to_free_routine = (routine) ? routine : try_to_free_builtin;
+}
+
 char *xstrdup(const char *str)
 {
 	char *ret = strdup(str);
 	if (!ret) {
-		release_pack_memory(strlen(str) + 1, -1);
+		try_to_free_routine(strlen(str) + 1);
 		ret = strdup(str);
 		if (!ret)
 			die("Out of memory, strdup failed");
@@ -21,7 +33,7 @@ void *xmalloc(size_t size)
 	if (!ret && !size)
 		ret = malloc(1);
 	if (!ret) {
-		release_pack_memory(size, -1);
+		try_to_free_routine(size);
 		ret = malloc(size);
 		if (!ret && !size)
 			ret = malloc(1);
@@ -67,7 +79,7 @@ void *xrealloc(void *ptr, size_t size)
 	if (!ret && !size)
 		ret = realloc(ptr, 1);
 	if (!ret) {
-		release_pack_memory(size, -1);
+		try_to_free_routine(size);
 		ret = realloc(ptr, size);
 		if (!ret && !size)
 			ret = realloc(ptr, 1);
@@ -83,7 +95,7 @@ void *xcalloc(size_t nmemb, size_t size)
 	if (!ret && (!nmemb || !size))
 		ret = calloc(1, 1);
 	if (!ret) {
-		release_pack_memory(nmemb * size, -1);
+		try_to_free_routine(nmemb * size);
 		ret = calloc(nmemb, size);
 		if (!ret && (!nmemb || !size))
 			ret = calloc(1, 1);

  parent reply	other threads:[~2010-04-07  2:58 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20100323161713.3183.57927.stgit@fredrik-laptop>
2010-03-23 17:31 ` [PATCH 1/2] Make xmalloc and xrealloc thread-safe Fredrik Kuivinen
2010-03-23 18:43   ` Shawn O. Pearce
2010-03-23 21:21     ` Fredrik Kuivinen
2010-03-23 23:50       ` Nicolas Pitre
2010-03-24 15:23         ` Fredrik Kuivinen
2010-03-24 17:53           ` Nicolas Pitre
2010-03-24 18:22             ` Shawn Pearce
2010-03-24 18:44               ` Junio C Hamano
2010-03-24 18:54               ` Nicolas Pitre
2010-03-24 19:57                 ` Shawn Pearce
2010-03-24 20:22                   ` [PATCH] " Nicolas Pitre
2010-03-24 20:28                     ` Shawn O. Pearce
2010-03-24 21:02                       ` Nicolas Pitre
2010-03-24 21:11                         ` Junio C Hamano
2010-03-24 21:28                       ` Junio C Hamano
2010-03-27 13:26                     ` Fredrik Kuivinen
2010-03-27 18:59                       ` Nicolas Pitre
2010-03-31  6:57                         ` Fredrik Kuivinen
2010-04-07  2:57                       ` Nicolas Pitre [this message]
2010-04-07  3:16                         ` [PATCH v2] " Shawn O. Pearce
2010-04-07  4:51                           ` Nicolas Pitre
2010-04-07 12:29                             ` Shawn Pearce
2010-04-07 13:17                               ` Nicolas Pitre
2010-04-07 14:30                                 ` Shawn Pearce
2010-04-07 14:47                                   ` Nicolas Pitre
2010-04-07 14:45                                 ` Fredrik Kuivinen
2010-04-07 15:08                                   ` Nicolas Pitre
2010-04-07 16:13                                     ` Fredrik Kuivinen
2010-04-07 16:44                                       ` Erik Faye-Lund
2010-04-07 18:37                                       ` Nicolas Pitre
2010-04-07 15:27                                   ` Sverre Rabbelier
2010-04-07 16:15                                     ` Fredrik Kuivinen
2010-04-07 16:17                                   ` Junio C Hamano
2010-04-07 18:49                                   ` Johannes Sixt
2010-04-08  7:15                                   ` [PATCH] Thread-safe xmalloc and xrealloc needs a recursive mutex Johannes Sixt
2010-04-08  8:42                                     ` Fredrik Kuivinen
2010-04-07  5:21                           ` [PATCH v2] Make xmalloc and xrealloc thread-safe Junio C Hamano
2010-03-23 17:31 ` [PATCH 2/2] Make sha1_to_hex thread-safe Fredrik Kuivinen
2010-03-23 20:23   ` Johannes Sixt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=alpine.LFD.2.00.1004062152260.7232@xanadu.home \
    --to=nico@fluxnic.net \
    --cc=frekui@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=j6t@kdbg.org \
    --cc=spearce@spearce.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).