Postpone mutex initialization on FreeBSD.

Postpone mutex initialization on FreeBSD until after base allocation is
safe.
This commit is contained in:
Jason Evans 2012-04-03 08:47:07 -07:00
parent 48db6167e7
commit 633aaff967
3 changed files with 58 additions and 13 deletions

View File

@ -1,18 +1,20 @@
/******************************************************************************/ /******************************************************************************/
#ifdef JEMALLOC_H_TYPES #ifdef JEMALLOC_H_TYPES
typedef struct malloc_mutex_s malloc_mutex_t;
#ifdef JEMALLOC_OSSPIN #ifdef JEMALLOC_OSSPIN
typedef OSSpinLock malloc_mutex_t; #define MALLOC_MUTEX_INITIALIZER {0}
#define MALLOC_MUTEX_INITIALIZER 0 #elif (defined(JEMALLOC_MUTEX_INIT_CB))
#define MALLOC_MUTEX_INITIALIZER {PTHREAD_MUTEX_INITIALIZER, NULL}
#else #else
typedef pthread_mutex_t malloc_mutex_t;
# if (defined(PTHREAD_MUTEX_ADAPTIVE_NP) && \ # if (defined(PTHREAD_MUTEX_ADAPTIVE_NP) && \
defined(PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP)) defined(PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP))
# define MALLOC_MUTEX_TYPE PTHREAD_MUTEX_ADAPTIVE_NP # define MALLOC_MUTEX_TYPE PTHREAD_MUTEX_ADAPTIVE_NP
# define MALLOC_MUTEX_INITIALIZER PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP # define MALLOC_MUTEX_INITIALIZER {PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP}
# else # else
# define MALLOC_MUTEX_TYPE PTHREAD_MUTEX_DEFAULT # define MALLOC_MUTEX_TYPE PTHREAD_MUTEX_DEFAULT
# define MALLOC_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER # define MALLOC_MUTEX_INITIALIZER {PTHREAD_MUTEX_INITIALIZER}
# endif # endif
#endif #endif
@ -20,6 +22,17 @@ typedef pthread_mutex_t malloc_mutex_t;
/******************************************************************************/ /******************************************************************************/
#ifdef JEMALLOC_H_STRUCTS #ifdef JEMALLOC_H_STRUCTS
struct malloc_mutex_s {
#ifdef JEMALLOC_OSSPIN
OSSpinLock lock;
#elif (defined(JEMALLOC_MUTEX_INIT_CB))
pthread_mutex_t lock;
malloc_mutex_t *postponed_next;
#else
pthread_mutex_t lock;
#endif
};
#endif /* JEMALLOC_H_STRUCTS */ #endif /* JEMALLOC_H_STRUCTS */
/******************************************************************************/ /******************************************************************************/
#ifdef JEMALLOC_H_EXTERNS #ifdef JEMALLOC_H_EXTERNS
@ -34,6 +47,7 @@ bool malloc_mutex_init(malloc_mutex_t *mutex);
void malloc_mutex_prefork(malloc_mutex_t *mutex); void malloc_mutex_prefork(malloc_mutex_t *mutex);
void malloc_mutex_postfork_parent(malloc_mutex_t *mutex); void malloc_mutex_postfork_parent(malloc_mutex_t *mutex);
void malloc_mutex_postfork_child(malloc_mutex_t *mutex); void malloc_mutex_postfork_child(malloc_mutex_t *mutex);
bool mutex_boot(void);
#endif /* JEMALLOC_H_EXTERNS */ #endif /* JEMALLOC_H_EXTERNS */
/******************************************************************************/ /******************************************************************************/
@ -51,9 +65,9 @@ malloc_mutex_lock(malloc_mutex_t *mutex)
if (isthreaded) { if (isthreaded) {
#ifdef JEMALLOC_OSSPIN #ifdef JEMALLOC_OSSPIN
OSSpinLockLock(mutex); OSSpinLockLock(&mutex->lock);
#else #else
pthread_mutex_lock(mutex); pthread_mutex_lock(&mutex->lock);
#endif #endif
} }
} }
@ -64,9 +78,9 @@ malloc_mutex_unlock(malloc_mutex_t *mutex)
if (isthreaded) { if (isthreaded) {
#ifdef JEMALLOC_OSSPIN #ifdef JEMALLOC_OSSPIN
OSSpinLockUnlock(mutex); OSSpinLockUnlock(&mutex->lock);
#else #else
pthread_mutex_unlock(mutex); pthread_mutex_unlock(&mutex->lock);
#endif #endif
} }
} }

View File

@ -651,6 +651,11 @@ malloc_init_hard(void)
return (true); return (true);
} }
if (mutex_boot()) {
malloc_mutex_unlock(&init_lock);
return (true);
}
if (opt_narenas == 0) { if (opt_narenas == 0) {
/* /*
* For SMP systems, create more than one arena per CPU by * For SMP systems, create more than one arena per CPU by

View File

@ -11,6 +11,10 @@
#ifdef JEMALLOC_LAZY_LOCK #ifdef JEMALLOC_LAZY_LOCK
bool isthreaded = false; bool isthreaded = false;
#endif #endif
#ifdef JEMALLOC_MUTEX_INIT_CB
static bool postpone_init = true;
static malloc_mutex_t *postponed_mutexes = NULL;
#endif
#ifdef JEMALLOC_LAZY_LOCK #ifdef JEMALLOC_LAZY_LOCK
static void pthread_create_once(void); static void pthread_create_once(void);
@ -65,17 +69,23 @@ bool
malloc_mutex_init(malloc_mutex_t *mutex) malloc_mutex_init(malloc_mutex_t *mutex)
{ {
#ifdef JEMALLOC_OSSPIN #ifdef JEMALLOC_OSSPIN
*mutex = 0; mutex->lock = 0;
#elif (defined(JEMALLOC_MUTEX_INIT_CB)) #elif (defined(JEMALLOC_MUTEX_INIT_CB))
if (_pthread_mutex_init_calloc_cb(mutex, base_calloc) != 0) if (postpone_init) {
mutex->postponed_next = postponed_mutexes;
postponed_mutexes = mutex;
} else {
if (_pthread_mutex_init_calloc_cb(&mutex->lock, base_calloc) !=
0)
return (true); return (true);
}
#else #else
pthread_mutexattr_t attr; pthread_mutexattr_t attr;
if (pthread_mutexattr_init(&attr) != 0) if (pthread_mutexattr_init(&attr) != 0)
return (true); return (true);
pthread_mutexattr_settype(&attr, MALLOC_MUTEX_TYPE); pthread_mutexattr_settype(&attr, MALLOC_MUTEX_TYPE);
if (pthread_mutex_init(mutex, &attr) != 0) { if (pthread_mutex_init(&mutex->lock, &attr) != 0) {
pthread_mutexattr_destroy(&attr); pthread_mutexattr_destroy(&attr);
return (true); return (true);
} }
@ -114,3 +124,19 @@ malloc_mutex_postfork_child(malloc_mutex_t *mutex)
} }
#endif #endif
} }
bool
mutex_boot(void)
{
#ifdef JEMALLOC_MUTEX_INIT_CB
postpone_init = false;
while (postponed_mutexes != NULL) {
if (_pthread_mutex_init_calloc_cb(&postponed_mutexes->lock,
base_calloc) != 0)
return (true);
postponed_mutexes = postponed_mutexes->postponed_next;
}
#endif
return (false);
}