Commit 6853dfab authored by Mike Hommey's avatar Mike Hommey
Browse files

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

parent 92ee6771
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -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"
        fi
        WRAP_LDFLAGS="${WRAP_LDFLAGS} -Wl,--wrap=getaddrinfo,--wrap=freeaddrinfo,--wrap=gai_strerror"
        WRAP_LDFLAGS="${WRAP_LDFLAGS} -Wl,--wrap=fork,--wrap=pthread_atfork"
    fi
fi

+1 −16
Original line number Diff line number Diff line
@@ -1419,13 +1419,8 @@ static
#endif
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_postfork(void);
#endif

#ifdef MOZ_MEMORY_DARWIN
/*
@@ -5923,10 +5918,8 @@ MALLOC_OUT:
#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. */
	/* 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);
#endif

@@ -6849,11 +6842,7 @@ _msize(const void *ptr)
 * is threaded here.
 */

#ifdef MOZ_MEMORY_ANDROID
void
#else
static void
#endif
_malloc_prefork(void)
{
	unsigned i;
@@ -6871,11 +6860,7 @@ _malloc_prefork(void)
	malloc_mutex_lock(&huge_mtx);
}

#ifdef MOZ_MEMORY_ANDROID
void
#else
static void
#endif
_malloc_postfork(void)
{
	unsigned i;
+0 −4
Original line number Diff line number Diff line
@@ -53,10 +53,6 @@ int posix_memalign(void **memptr, size_t alignment, size_t size);
/* Android doesn't have posix_memalign */
#ifdef MOZ_MEMORY_ANDROID
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

#if defined(MOZ_MEMORY_DARWIN) || defined(MOZ_MEMORY_WINDOWS)
+43 −0
Original line number Diff line number Diff line
@@ -1037,3 +1037,46 @@ ChildProcessInit(int argc, char* argv[])
  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;
}
+4 −0
Original line number Diff line number Diff line
@@ -84,6 +84,10 @@ endif
ifeq (android, $(MOZ_WIDGET_TOOLKIT))
# Add Android specific code
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,../android)
endif