aboutsummaryrefslogtreecommitdiffstats
path: root/main/libc0.9.32/0001-malloc-standard-synchronize-on-fork.patch
blob: eb3bfbe04f19aa9fb4d844e1061ddf2388b918ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
From 168215f9c3ec4ec9a2fad9387038ced8b386ebaa Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timo=20Ter=C3=A4s?= <timo.teras@iki.fi>
Date: Sat, 26 Mar 2011 20:23:09 +0200
Subject: [PATCH] malloc-standard: synchronize on fork
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Otherwise other threads can leave malloc state locked, and the child
will hang indefinitely if it tries to malloc something.

Signed-off-by: Timo Teräs <timo.teras@iki.fi>
---
 libc/stdlib/malloc-standard/free.c |   17 +++++++++++++++++
 1 files changed, 17 insertions(+), 0 deletions(-)

diff --git a/libc/stdlib/malloc-standard/free.c b/libc/stdlib/malloc-standard/free.c
index 39e54d6..df512cc 100644
--- a/libc/stdlib/malloc-standard/free.c
+++ b/libc/stdlib/malloc-standard/free.c
@@ -118,6 +118,21 @@ int malloc_trim(size_t pad)
   to inline it at all call points, which turns out not to be an
   optimization at all. (Inlining it in __malloc_consolidate is fine though.)
 */
+static void _malloc_lock(void)
+{
+    __UCLIBC_MUTEX_LOCK_CANCEL_UNSAFE(__malloc_lock);
+}
+
+static void _malloc_unlock(void)
+{
+    __UCLIBC_MUTEX_UNLOCK_CANCEL_UNSAFE(__malloc_lock);
+}
+
+static void _malloc_reset(void)
+{
+    __UCLIBC_MUTEX_INIT_VAR(__malloc_lock);
+}
+
 static void malloc_init_state(mstate av)
 {
     int     i;
@@ -145,6 +160,8 @@ static void malloc_init_state(mstate av)
 
     av->top            = initial_top(av);
     av->pagesize       = malloc_getpagesize;
+
+    __libc_atfork(_malloc_lock, _malloc_unlock, _malloc_reset);
 }
 
 
-- 
1.7.1