Bug 737084 - Do pthread_atfork in jemalloc on mac and android. r=blassey,r=khuey

This commit is contained in:
Mike Hommey 2012-03-22 08:01:51 +01:00
parent bd6a8730b3
commit c862bf7314
5 changed files with 49 additions and 20 deletions

View File

@ -7233,6 +7233,7 @@ if test "$OS_TARGET" = Android; then
WRAP_LDFLAGS="${WRAP_LDFLAGS} -Wl,--wrap=dlopen,--wrap=dlclose,--wrap=dlerror,--wrap=dlsym,--wrap=dladdr" WRAP_LDFLAGS="${WRAP_LDFLAGS} -Wl,--wrap=dlopen,--wrap=dlclose,--wrap=dlerror,--wrap=dlsym,--wrap=dladdr"
fi fi
WRAP_LDFLAGS="${WRAP_LDFLAGS} -Wl,--wrap=getaddrinfo,--wrap=freeaddrinfo,--wrap=gai_strerror" WRAP_LDFLAGS="${WRAP_LDFLAGS} -Wl,--wrap=getaddrinfo,--wrap=freeaddrinfo,--wrap=gai_strerror"
WRAP_LDFLAGS="${WRAP_LDFLAGS} -Wl,--wrap=fork,--wrap=pthread_atfork"
fi fi
fi fi

View File

@ -1419,13 +1419,8 @@ static
#endif #endif
bool malloc_init_hard(void); bool malloc_init_hard(void);
#ifdef MOZ_MEMORY_ANDROID
void _malloc_prefork(void);
void _malloc_postfork(void);
#else
static void _malloc_prefork(void); static void _malloc_prefork(void);
static void _malloc_postfork(void); static void _malloc_postfork(void);
#endif
#ifdef MOZ_MEMORY_DARWIN #ifdef MOZ_MEMORY_DARWIN
/* /*
@ -5923,10 +5918,8 @@ MALLOC_OUT:
#endif #endif
} }
#if (!defined(MOZ_MEMORY_WINDOWS) && !defined(MOZ_MEMORY_DARWIN) && !defined(MOZ_MEMORY_ANDROID)) #if !defined(MOZ_MEMORY_WINDOWS)
/* Prevent potential deadlock on malloc locks after fork. */ /* Prevent potential deadlock on malloc locks after fork. */
/* XXX on Android there is no pthread_atfork, so we specifically
call _malloc_prefork and _malloc_postfork in process_util_linux.cc */
pthread_atfork(_malloc_prefork, _malloc_postfork, _malloc_postfork); pthread_atfork(_malloc_prefork, _malloc_postfork, _malloc_postfork);
#endif #endif
@ -6849,11 +6842,7 @@ _msize(const void *ptr)
* is threaded here. * is threaded here.
*/ */
#ifdef MOZ_MEMORY_ANDROID
void
#else
static void static void
#endif
_malloc_prefork(void) _malloc_prefork(void)
{ {
unsigned i; unsigned i;
@ -6871,11 +6860,7 @@ _malloc_prefork(void)
malloc_mutex_lock(&huge_mtx); malloc_mutex_lock(&huge_mtx);
} }
#ifdef MOZ_MEMORY_ANDROID
void
#else
static void static void
#endif
_malloc_postfork(void) _malloc_postfork(void)
{ {
unsigned i; unsigned i;

View File

@ -53,10 +53,6 @@ int posix_memalign(void **memptr, size_t alignment, size_t size);
/* Android doesn't have posix_memalign */ /* Android doesn't have posix_memalign */
#ifdef MOZ_MEMORY_ANDROID #ifdef MOZ_MEMORY_ANDROID
int posix_memalign(void **memptr, size_t alignment, size_t size); int posix_memalign(void **memptr, size_t alignment, size_t size);
/* Android < 2.3 doesn't have pthread_atfork, so we need to call these
* when forking the child process. See bug 680190 */
void _malloc_prefork(void);
void _malloc_postfork(void);
#endif #endif
#if defined(MOZ_MEMORY_DARWIN) || defined(MOZ_MEMORY_WINDOWS) #if defined(MOZ_MEMORY_DARWIN) || defined(MOZ_MEMORY_WINDOWS)

View File

@ -1037,3 +1037,46 @@ ChildProcessInit(int argc, char* argv[])
return fXRE_InitChildProcess(argc, argv, proctype); return fXRE_InitChildProcess(argc, argv, proctype);
} }
/* Android doesn't have pthread_atfork(), so we need to use our own. */
struct AtForkFuncs {
void (*prepare)(void);
void (*parent)(void);
void (*child)(void);
};
static std::vector<AtForkFuncs> atfork;
extern "C" NS_EXPORT int
__wrap_pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void))
{
AtForkFuncs funcs;
funcs.prepare = prepare;
funcs.parent = parent;
funcs.child = child;
atfork.push_back(funcs);
return 0;
}
extern "C" NS_EXPORT pid_t
__wrap_fork(void)
{
pid_t pid;
for (std::vector<AtForkFuncs>::reverse_iterator it = atfork.rbegin();
it < atfork.rend(); ++it)
if (it->prepare)
it->prepare();
switch ((pid = fork())) {
case 0:
for (std::vector<AtForkFuncs>::iterator it = atfork.begin();
it < atfork.end(); ++it)
if (it->child)
it->child();
break;
default:
for (std::vector<AtForkFuncs>::iterator it = atfork.begin();
it < atfork.end(); ++it)
if (it->parent)
it->parent();
}
return pid;
}

View File

@ -84,6 +84,10 @@ endif
ifeq (android, $(MOZ_WIDGET_TOOLKIT)) ifeq (android, $(MOZ_WIDGET_TOOLKIT))
# Add Android specific code # Add Android specific code
EXTRA_DSO_LDOPTS += $(ZLIB_LIBS) EXTRA_DSO_LDOPTS += $(ZLIB_LIBS)
ifdef MOZ_MEMORY
# To properly wrap jemalloc's pthread_atfork call.
EXTRA_DSO_LDOPTS += -Wl,--wrap=pthread_atfork
endif
SHARED_LIBRARY_LIBS += $(call EXPAND_LIBNAME_PATH,android,$(DEPTH)/other-licenses/android) SHARED_LIBRARY_LIBS += $(call EXPAND_LIBNAME_PATH,android,$(DEPTH)/other-licenses/android)
SHARED_LIBRARY_LIBS += $(call EXPAND_LIBNAME_PATH,android,../android) SHARED_LIBRARY_LIBS += $(call EXPAND_LIBNAME_PATH,android,../android)
endif endif