2010-01-17 01:53:50 +08:00
|
|
|
#define JEMALLOC_MUTEX_C_
|
2010-02-12 06:45:59 +08:00
|
|
|
#include "jemalloc/internal/jemalloc_internal.h"
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2012-04-22 12:27:46 +08:00
|
|
|
#if defined(JEMALLOC_LAZY_LOCK) && !defined(_WIN32)
|
2012-03-01 02:37:27 +08:00
|
|
|
#include <dlfcn.h>
|
|
|
|
#endif
|
|
|
|
|
2012-04-22 12:27:46 +08:00
|
|
|
#ifndef _CRT_SPINCOUNT
|
2013-12-09 14:28:27 +08:00
|
|
|
#define _CRT_SPINCOUNT 4000
|
2012-04-22 12:27:46 +08:00
|
|
|
#endif
|
|
|
|
|
2010-01-17 01:53:50 +08:00
|
|
|
/******************************************************************************/
|
|
|
|
/* Data. */
|
|
|
|
|
|
|
|
#ifdef JEMALLOC_LAZY_LOCK
|
|
|
|
bool isthreaded = false;
|
|
|
|
#endif
|
2012-04-03 23:47:07 +08:00
|
|
|
#ifdef JEMALLOC_MUTEX_INIT_CB
|
|
|
|
static bool postpone_init = true;
|
|
|
|
static malloc_mutex_t *postponed_mutexes = NULL;
|
|
|
|
#endif
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2012-04-22 12:27:46 +08:00
|
|
|
#if defined(JEMALLOC_LAZY_LOCK) && !defined(_WIN32)
|
2010-01-17 01:53:50 +08:00
|
|
|
static void pthread_create_once(void);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/*
|
|
|
|
* We intercept pthread_create() calls in order to toggle isthreaded if the
|
|
|
|
* process goes multi-threaded.
|
|
|
|
*/
|
|
|
|
|
2012-04-22 12:27:46 +08:00
|
|
|
#if defined(JEMALLOC_LAZY_LOCK) && !defined(_WIN32)
|
2010-01-17 01:53:50 +08:00
|
|
|
static int (*pthread_create_fptr)(pthread_t *__restrict, const pthread_attr_t *,
|
|
|
|
void *(*)(void *), void *__restrict);
|
|
|
|
|
|
|
|
static void
|
|
|
|
pthread_create_once(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
pthread_create_fptr = dlsym(RTLD_NEXT, "pthread_create");
|
|
|
|
if (pthread_create_fptr == NULL) {
|
2010-03-04 09:45:38 +08:00
|
|
|
malloc_write("<jemalloc>: Error in dlsym(RTLD_NEXT, "
|
|
|
|
"\"pthread_create\")\n");
|
2010-01-17 01:53:50 +08:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
isthreaded = true;
|
|
|
|
}
|
|
|
|
|
2012-04-30 18:38:29 +08:00
|
|
|
JEMALLOC_EXPORT int
|
2010-01-17 01:53:50 +08:00
|
|
|
pthread_create(pthread_t *__restrict thread,
|
|
|
|
const pthread_attr_t *__restrict attr, void *(*start_routine)(void *),
|
|
|
|
void *__restrict arg)
|
|
|
|
{
|
|
|
|
static pthread_once_t once_control = PTHREAD_ONCE_INIT;
|
|
|
|
|
|
|
|
pthread_once(&once_control, pthread_create_once);
|
|
|
|
|
|
|
|
return (pthread_create_fptr(thread, attr, start_routine, arg));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2012-02-03 14:04:57 +08:00
|
|
|
#ifdef JEMALLOC_MUTEX_INIT_CB
|
2012-09-18 20:40:31 +08:00
|
|
|
JEMALLOC_EXPORT int _pthread_mutex_init_calloc_cb(pthread_mutex_t *mutex,
|
2012-02-03 14:04:57 +08:00
|
|
|
void *(calloc_cb)(size_t, size_t));
|
|
|
|
#endif
|
|
|
|
|
2010-01-17 01:53:50 +08:00
|
|
|
bool
|
|
|
|
malloc_mutex_init(malloc_mutex_t *mutex)
|
|
|
|
{
|
2012-04-30 18:38:29 +08:00
|
|
|
|
2012-04-22 12:27:46 +08:00
|
|
|
#ifdef _WIN32
|
|
|
|
if (!InitializeCriticalSectionAndSpinCount(&mutex->lock,
|
|
|
|
_CRT_SPINCOUNT))
|
|
|
|
return (true);
|
|
|
|
#elif (defined(JEMALLOC_OSSPIN))
|
2012-04-03 23:47:07 +08:00
|
|
|
mutex->lock = 0;
|
2012-02-03 14:04:57 +08:00
|
|
|
#elif (defined(JEMALLOC_MUTEX_INIT_CB))
|
2012-04-03 23:47:07 +08:00
|
|
|
if (postpone_init) {
|
|
|
|
mutex->postponed_next = postponed_mutexes;
|
|
|
|
postponed_mutexes = mutex;
|
|
|
|
} else {
|
2015-01-31 13:49:19 +08:00
|
|
|
if (_pthread_mutex_init_calloc_cb(&mutex->lock,
|
|
|
|
bootstrap_calloc) != 0)
|
2012-04-03 23:47:07 +08:00
|
|
|
return (true);
|
|
|
|
}
|
2011-03-19 10:30:18 +08:00
|
|
|
#else
|
2010-01-17 01:53:50 +08:00
|
|
|
pthread_mutexattr_t attr;
|
|
|
|
|
|
|
|
if (pthread_mutexattr_init(&attr) != 0)
|
|
|
|
return (true);
|
2012-02-03 14:04:57 +08:00
|
|
|
pthread_mutexattr_settype(&attr, MALLOC_MUTEX_TYPE);
|
2012-04-03 23:47:07 +08:00
|
|
|
if (pthread_mutex_init(&mutex->lock, &attr) != 0) {
|
2010-01-17 01:53:50 +08:00
|
|
|
pthread_mutexattr_destroy(&attr);
|
|
|
|
return (true);
|
|
|
|
}
|
|
|
|
pthread_mutexattr_destroy(&attr);
|
2011-03-19 10:30:18 +08:00
|
|
|
#endif
|
2010-01-17 01:53:50 +08:00
|
|
|
return (false);
|
|
|
|
}
|
2010-10-03 06:18:50 +08:00
|
|
|
|
2012-03-14 07:31:41 +08:00
|
|
|
void
|
|
|
|
malloc_mutex_prefork(malloc_mutex_t *mutex)
|
|
|
|
{
|
|
|
|
|
|
|
|
malloc_mutex_lock(mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
malloc_mutex_postfork_parent(malloc_mutex_t *mutex)
|
|
|
|
{
|
|
|
|
|
|
|
|
malloc_mutex_unlock(mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
malloc_mutex_postfork_child(malloc_mutex_t *mutex)
|
|
|
|
{
|
|
|
|
|
2012-02-03 14:04:57 +08:00
|
|
|
#ifdef JEMALLOC_MUTEX_INIT_CB
|
|
|
|
malloc_mutex_unlock(mutex);
|
|
|
|
#else
|
2012-03-14 07:31:41 +08:00
|
|
|
if (malloc_mutex_init(mutex)) {
|
|
|
|
malloc_printf("<jemalloc>: Error re-initializing mutex in "
|
|
|
|
"child\n");
|
|
|
|
if (opt_abort)
|
|
|
|
abort();
|
|
|
|
}
|
2012-02-03 14:04:57 +08:00
|
|
|
#endif
|
2012-03-14 07:31:41 +08:00
|
|
|
}
|
2012-04-03 23:47:07 +08:00
|
|
|
|
|
|
|
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,
|
2015-01-31 13:49:19 +08:00
|
|
|
bootstrap_calloc) != 0)
|
2012-04-03 23:47:07 +08:00
|
|
|
return (true);
|
|
|
|
postponed_mutexes = postponed_mutexes->postponed_next;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return (false);
|
|
|
|
}
|