Bug 1361258 - Use Thread Local Storage in mozjemalloc on mac. r=erahm

NO_TLS used to be hardcoded on mac because up to 10.6, __thread was not
supported. Until recently, we still supported for 10.6, and it's not the
case anymore, so we could make mac builds use __thread.

Unfortunately, on OSX, __thread circles back calling malloc to allocate
storage on first access, so we have an infinite loop problem here.
Fortunately, pthread_keys don't have this property, so we can use that
instead. It doesn't appear to have significantly more overhead (and TLS
overhead is small anyways compared to the amount of work involved in
allocating memory with mozjemalloc).

At the same time, we uniformize the initialization sequence between
mozjemalloc and mozjemalloc+replace-malloc, such that we have less
occasions for surprises when riding the trains (replace-malloc being
nightly only), ensuring the zone registration happens at the end of
mozjemalloc's initialization.
This commit is contained in:
Mike Hommey 2017-05-11 14:16:43 +09:00
Родитель f5461de678
Коммит fcd9a5550a
2 изменённых файлов: 21 добавлений и 9 удалений

Просмотреть файл

@ -336,7 +336,7 @@ static malloc_zone_t *get_default_zone()
}
#ifdef MOZ_REPLACE_MALLOC
#if defined(MOZ_REPLACE_MALLOC) && defined(MOZ_JEMALLOC4)
__attribute__((constructor))
#endif
void

Просмотреть файл

@ -433,8 +433,9 @@ void *_mmap(void *addr, size_t length, int prot, int flags,
#endif
#endif
#ifdef MOZ_MEMORY_DARWIN
static const bool isthreaded = true;
#ifdef MOZ_MEMORY_DARWIN
static pthread_key_t tlsIndex;
#endif
#if defined(MOZ_MEMORY_SOLARIS) && defined(MAP_ALIGN) && !defined(JEMALLOC_NEVER_USES_MAP_ALIGN)
@ -471,11 +472,6 @@ static const bool isthreaded = true;
# define SIZEOF_PTR_2POW 2
#endif
#define PIC
#ifndef MOZ_MEMORY_DARWIN
static const bool isthreaded = true;
#else
# define NO_TLS
#endif
#if 0
#ifdef __i386__
# define QUANTUM_2POW_MIN 4
@ -3066,6 +3062,8 @@ jemalloc_thread_local_arena_impl(bool enabled)
}
#ifdef MOZ_MEMORY_WINDOWS
TlsSetValue(tlsIndex, arena);
#elif defined(MOZ_MEMORY_DARWIN)
pthread_setspecific(tlsIndex, arena);
#else
arenas_map = arena;
#endif
@ -3095,6 +3093,8 @@ choose_arena(void)
# ifdef MOZ_MEMORY_WINDOWS
ret = (arena_t*)TlsGetValue(tlsIndex);
# elif defined(MOZ_MEMORY_DARWIN)
ret = (arena_t*)pthread_getspecific(tlsIndex);
# else
ret = arenas_map;
# endif
@ -3191,6 +3191,8 @@ choose_arena_hard(void)
#ifdef MOZ_MEMORY_WINDOWS
TlsSetValue(tlsIndex, ret);
#elif defined(MOZ_MEMORY_DARWIN)
pthread_setspecific(tlsIndex, ret);
#else
arenas_map = ret;
#endif
@ -4153,6 +4155,8 @@ arena_lock_balance_hard(arena_t *arena)
if (arenas[ind] != NULL) {
#ifdef MOZ_MEMORY_WINDOWS
TlsSetValue(tlsIndex, arenas[ind]);
#elif defined(MOZ_MEMORY_DARWIN)
pthread_setspecific(tlsIndex, arenas[ind]);
#else
arenas_map = arenas[ind];
#endif
@ -4161,12 +4165,16 @@ arena_lock_balance_hard(arena_t *arena)
if (arenas[ind] != NULL) {
#ifdef MOZ_MEMORY_WINDOWS
TlsSetValue(tlsIndex, arenas[ind]);
#elif defined(MOZ_MEMORY_DARWIN)
pthread_setspecific(tlsIndex, arenas[ind]);
#else
arenas_map = arenas[ind];
#endif
} else {
#ifdef MOZ_MEMORY_WINDOWS
TlsSetValue(tlsIndex, arenas_extend(ind));
#elif defined(MOZ_MEMORY_DARWIN)
pthread_setspecific(tlsIndex, arenas_extend(ind));
#else
arenas_map = arenas_extend(ind);
#endif
@ -5539,7 +5547,7 @@ malloc_init(void)
}
#endif
#if defined(MOZ_MEMORY_DARWIN) && !defined(MOZ_REPLACE_MALLOC)
#if defined(MOZ_MEMORY_DARWIN)
extern void register_zone(void);
#endif
@ -5575,6 +5583,8 @@ malloc_init_hard(void)
#ifdef MOZ_MEMORY_WINDOWS
/* get a thread local storage index */
tlsIndex = TlsAlloc();
#elif defined(MOZ_MEMORY_DARWIN)
pthread_key_create(&tlsIndex, NULL);
#endif
/* Get page size and number of CPUs */
@ -6026,6 +6036,8 @@ MALLOC_OUT:
*/
#ifdef MOZ_MEMORY_WINDOWS
TlsSetValue(tlsIndex, arenas[0]);
#elif defined(MOZ_MEMORY_DARWIN)
pthread_setspecific(tlsIndex, arenas[0]);
#else
arenas_map = arenas[0];
#endif
@ -6060,7 +6072,7 @@ MALLOC_OUT:
}
#endif
#if defined(MOZ_MEMORY_DARWIN) && !defined(MOZ_REPLACE_MALLOC)
#if defined(MOZ_MEMORY_DARWIN)
register_zone();
#endif