2010-01-17 01:53:50 +08:00
|
|
|
/******************************************************************************/
|
|
|
|
#ifdef JEMALLOC_H_TYPES
|
|
|
|
|
2012-04-03 23:47:07 +08:00
|
|
|
typedef struct malloc_mutex_s malloc_mutex_t;
|
|
|
|
|
2012-04-22 12:27:46 +08:00
|
|
|
#ifdef _WIN32
|
|
|
|
# define MALLOC_MUTEX_INITIALIZER
|
|
|
|
#elif (defined(JEMALLOC_OSSPIN))
|
|
|
|
# define MALLOC_MUTEX_INITIALIZER {0}
|
2012-04-03 23:47:07 +08:00
|
|
|
#elif (defined(JEMALLOC_MUTEX_INIT_CB))
|
2012-04-22 12:27:46 +08:00
|
|
|
# define MALLOC_MUTEX_INITIALIZER {PTHREAD_MUTEX_INITIALIZER, NULL}
|
2011-03-19 10:30:18 +08:00
|
|
|
#else
|
2012-02-03 14:04:57 +08:00
|
|
|
# if (defined(PTHREAD_MUTEX_ADAPTIVE_NP) && \
|
|
|
|
defined(PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP))
|
|
|
|
# define MALLOC_MUTEX_TYPE PTHREAD_MUTEX_ADAPTIVE_NP
|
2012-04-03 23:47:07 +08:00
|
|
|
# define MALLOC_MUTEX_INITIALIZER {PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP}
|
2012-02-11 12:22:09 +08:00
|
|
|
# else
|
2012-02-03 14:04:57 +08:00
|
|
|
# define MALLOC_MUTEX_TYPE PTHREAD_MUTEX_DEFAULT
|
2012-04-03 23:47:07 +08:00
|
|
|
# define MALLOC_MUTEX_INITIALIZER {PTHREAD_MUTEX_INITIALIZER}
|
2012-02-11 12:22:09 +08:00
|
|
|
# endif
|
2010-09-06 01:35:13 +08:00
|
|
|
#endif
|
|
|
|
|
2010-01-17 01:53:50 +08:00
|
|
|
#endif /* JEMALLOC_H_TYPES */
|
|
|
|
/******************************************************************************/
|
|
|
|
#ifdef JEMALLOC_H_STRUCTS
|
|
|
|
|
2012-04-03 23:47:07 +08:00
|
|
|
struct malloc_mutex_s {
|
2012-04-22 12:27:46 +08:00
|
|
|
#ifdef _WIN32
|
|
|
|
CRITICAL_SECTION lock;
|
|
|
|
#elif (defined(JEMALLOC_OSSPIN))
|
2012-04-03 23:47:07 +08:00
|
|
|
OSSpinLock lock;
|
|
|
|
#elif (defined(JEMALLOC_MUTEX_INIT_CB))
|
|
|
|
pthread_mutex_t lock;
|
|
|
|
malloc_mutex_t *postponed_next;
|
|
|
|
#else
|
|
|
|
pthread_mutex_t lock;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2010-01-17 01:53:50 +08:00
|
|
|
#endif /* JEMALLOC_H_STRUCTS */
|
|
|
|
/******************************************************************************/
|
|
|
|
#ifdef JEMALLOC_H_EXTERNS
|
|
|
|
|
|
|
|
#ifdef JEMALLOC_LAZY_LOCK
|
|
|
|
extern bool isthreaded;
|
|
|
|
#else
|
2012-04-21 05:12:30 +08:00
|
|
|
# undef isthreaded /* Undo private_namespace.h definition. */
|
2010-01-17 01:53:50 +08:00
|
|
|
# define isthreaded true
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bool malloc_mutex_init(malloc_mutex_t *mutex);
|
2012-03-14 07:31:41 +08:00
|
|
|
void malloc_mutex_prefork(malloc_mutex_t *mutex);
|
|
|
|
void malloc_mutex_postfork_parent(malloc_mutex_t *mutex);
|
|
|
|
void malloc_mutex_postfork_child(malloc_mutex_t *mutex);
|
2012-04-03 23:47:07 +08:00
|
|
|
bool mutex_boot(void);
|
2010-01-17 01:53:50 +08:00
|
|
|
|
|
|
|
#endif /* JEMALLOC_H_EXTERNS */
|
|
|
|
/******************************************************************************/
|
|
|
|
#ifdef JEMALLOC_H_INLINES
|
|
|
|
|
|
|
|
#ifndef JEMALLOC_ENABLE_INLINE
|
|
|
|
void malloc_mutex_lock(malloc_mutex_t *mutex);
|
|
|
|
void malloc_mutex_unlock(malloc_mutex_t *mutex);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_MUTEX_C_))
|
|
|
|
JEMALLOC_INLINE void
|
|
|
|
malloc_mutex_lock(malloc_mutex_t *mutex)
|
|
|
|
{
|
|
|
|
|
2011-03-19 10:30:18 +08:00
|
|
|
if (isthreaded) {
|
2012-04-22 12:27:46 +08:00
|
|
|
#ifdef _WIN32
|
|
|
|
EnterCriticalSection(&mutex->lock);
|
|
|
|
#elif (defined(JEMALLOC_OSSPIN))
|
2012-04-03 23:47:07 +08:00
|
|
|
OSSpinLockLock(&mutex->lock);
|
2011-03-19 10:30:18 +08:00
|
|
|
#else
|
2012-04-03 23:47:07 +08:00
|
|
|
pthread_mutex_lock(&mutex->lock);
|
2011-03-19 10:30:18 +08:00
|
|
|
#endif
|
|
|
|
}
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_INLINE void
|
|
|
|
malloc_mutex_unlock(malloc_mutex_t *mutex)
|
|
|
|
{
|
|
|
|
|
2011-03-19 10:30:18 +08:00
|
|
|
if (isthreaded) {
|
2012-04-22 12:27:46 +08:00
|
|
|
#ifdef _WIN32
|
|
|
|
LeaveCriticalSection(&mutex->lock);
|
|
|
|
#elif (defined(JEMALLOC_OSSPIN))
|
2012-04-03 23:47:07 +08:00
|
|
|
OSSpinLockUnlock(&mutex->lock);
|
2011-03-19 10:30:18 +08:00
|
|
|
#else
|
2012-04-03 23:47:07 +08:00
|
|
|
pthread_mutex_unlock(&mutex->lock);
|
2011-03-19 10:30:18 +08:00
|
|
|
#endif
|
|
|
|
}
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* JEMALLOC_H_INLINES */
|
|
|
|
/******************************************************************************/
|