2010-01-17 01:53:50 +08:00
|
|
|
/******************************************************************************/
|
|
|
|
#ifdef JEMALLOC_H_TYPES
|
|
|
|
|
2011-03-19 10:30:18 +08:00
|
|
|
#ifdef JEMALLOC_OSSPIN
|
|
|
|
typedef OSSpinLock malloc_mutex_t;
|
2012-02-11 12:22:09 +08:00
|
|
|
#define MALLOC_MUTEX_INITIALIZER 0
|
2011-03-19 10:30:18 +08:00
|
|
|
#else
|
2010-01-17 01:53:50 +08:00
|
|
|
typedef pthread_mutex_t malloc_mutex_t;
|
2012-02-11 12:22:09 +08:00
|
|
|
# ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
|
|
|
|
# define MALLOC_MUTEX_INITIALIZER PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
|
|
|
|
# else
|
|
|
|
# define MALLOC_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
|
|
|
|
# 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
|
|
|
|
|
|
|
|
#endif /* JEMALLOC_H_STRUCTS */
|
|
|
|
/******************************************************************************/
|
|
|
|
#ifdef JEMALLOC_H_EXTERNS
|
|
|
|
|
|
|
|
#ifdef JEMALLOC_LAZY_LOCK
|
|
|
|
extern bool isthreaded;
|
|
|
|
#else
|
|
|
|
# define isthreaded true
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bool malloc_mutex_init(malloc_mutex_t *mutex);
|
2010-10-03 06:18:50 +08:00
|
|
|
void malloc_mutex_destroy(malloc_mutex_t *mutex);
|
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);
|
2010-03-05 13:35:07 +08:00
|
|
|
bool malloc_mutex_trylock(malloc_mutex_t *mutex);
|
2010-01-17 01:53:50 +08:00
|
|
|
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) {
|
|
|
|
#ifdef JEMALLOC_OSSPIN
|
|
|
|
OSSpinLockLock(mutex);
|
|
|
|
#else
|
2010-01-17 01:53:50 +08:00
|
|
|
pthread_mutex_lock(mutex);
|
2011-03-19 10:30:18 +08:00
|
|
|
#endif
|
|
|
|
}
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
|
|
|
|
2010-03-05 13:35:07 +08:00
|
|
|
JEMALLOC_INLINE bool
|
|
|
|
malloc_mutex_trylock(malloc_mutex_t *mutex)
|
|
|
|
{
|
|
|
|
|
2011-03-19 10:30:18 +08:00
|
|
|
if (isthreaded) {
|
|
|
|
#ifdef JEMALLOC_OSSPIN
|
|
|
|
return (OSSpinLockTry(mutex) == false);
|
|
|
|
#else
|
2010-03-05 13:35:07 +08:00
|
|
|
return (pthread_mutex_trylock(mutex) != 0);
|
2011-03-19 10:30:18 +08:00
|
|
|
#endif
|
|
|
|
} else
|
2010-03-05 13:35:07 +08:00
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
#ifdef JEMALLOC_OSSPIN
|
|
|
|
OSSpinLockUnlock(mutex);
|
|
|
|
#else
|
2010-01-17 01:53:50 +08:00
|
|
|
pthread_mutex_unlock(mutex);
|
2011-03-19 10:30:18 +08:00
|
|
|
#endif
|
|
|
|
}
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* JEMALLOC_H_INLINES */
|
|
|
|
/******************************************************************************/
|