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-03-01 02:37:27 +08:00
|
|
|
#ifdef JEMALLOC_LAZY_LOCK
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#endif
|
|
|
|
|
2010-01-17 01:53:50 +08:00
|
|
|
/******************************************************************************/
|
|
|
|
/* Data. */
|
|
|
|
|
|
|
|
#ifdef JEMALLOC_LAZY_LOCK
|
|
|
|
bool isthreaded = false;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef JEMALLOC_LAZY_LOCK
|
|
|
|
static void pthread_create_once(void);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/*
|
|
|
|
* We intercept pthread_create() calls in order to toggle isthreaded if the
|
|
|
|
* process goes multi-threaded.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef JEMALLOC_LAZY_LOCK
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_ATTR(visibility("default"))
|
|
|
|
int
|
|
|
|
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
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
bool
|
|
|
|
malloc_mutex_init(malloc_mutex_t *mutex)
|
|
|
|
{
|
2011-03-19 10:30:18 +08:00
|
|
|
#ifdef JEMALLOC_OSSPIN
|
|
|
|
*mutex = 0;
|
|
|
|
#else
|
2010-01-17 01:53:50 +08:00
|
|
|
pthread_mutexattr_t attr;
|
|
|
|
|
|
|
|
if (pthread_mutexattr_init(&attr) != 0)
|
|
|
|
return (true);
|
2010-09-06 01:35:13 +08:00
|
|
|
#ifdef PTHREAD_MUTEX_ADAPTIVE_NP
|
2010-01-17 01:53:50 +08:00
|
|
|
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ADAPTIVE_NP);
|
2010-09-06 01:35:13 +08:00
|
|
|
#else
|
|
|
|
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_DEFAULT);
|
|
|
|
#endif
|
2010-01-17 01:53:50 +08:00
|
|
|
if (pthread_mutex_init(mutex, &attr) != 0) {
|
|
|
|
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
|
|
|
|
|
|
|
void
|
|
|
|
malloc_mutex_destroy(malloc_mutex_t *mutex)
|
|
|
|
{
|
|
|
|
|
2011-03-19 10:30:18 +08:00
|
|
|
#ifndef JEMALLOC_OSSPIN
|
2010-10-03 06:18:50 +08:00
|
|
|
if (pthread_mutex_destroy(mutex) != 0) {
|
|
|
|
malloc_write("<jemalloc>: Error in pthread_mutex_destroy()\n");
|
|
|
|
abort();
|
|
|
|
}
|
2011-03-19 10:30:18 +08:00
|
|
|
#endif
|
2010-10-03 06:18:50 +08:00
|
|
|
}
|