Home | History | Annotate | Download | only in internal
      1 /******************************************************************************/
      2 #ifdef JEMALLOC_H_TYPES
      3 
      4 typedef struct malloc_mutex_s malloc_mutex_t;
      5 
      6 #ifdef _WIN32
      7 #  define MALLOC_MUTEX_INITIALIZER
      8 #elif (defined(JEMALLOC_OSSPIN))
      9 #  define MALLOC_MUTEX_INITIALIZER {0}
     10 #elif (defined(JEMALLOC_MUTEX_INIT_CB))
     11 #  define MALLOC_MUTEX_INITIALIZER {PTHREAD_MUTEX_INITIALIZER, NULL}
     12 #else
     13 #  if (defined(JEMALLOC_HAVE_PTHREAD_MUTEX_ADAPTIVE_NP) &&		\
     14        defined(PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP))
     15 #    define MALLOC_MUTEX_TYPE PTHREAD_MUTEX_ADAPTIVE_NP
     16 #    define MALLOC_MUTEX_INITIALIZER {PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP}
     17 #  else
     18 #    define MALLOC_MUTEX_TYPE PTHREAD_MUTEX_DEFAULT
     19 #    define MALLOC_MUTEX_INITIALIZER {PTHREAD_MUTEX_INITIALIZER}
     20 #  endif
     21 #endif
     22 
     23 #endif /* JEMALLOC_H_TYPES */
     24 /******************************************************************************/
     25 #ifdef JEMALLOC_H_STRUCTS
     26 
     27 struct malloc_mutex_s {
     28 #ifdef _WIN32
     29 #  if _WIN32_WINNT >= 0x0600
     30 	SRWLOCK         	lock;
     31 #  else
     32 	CRITICAL_SECTION	lock;
     33 #  endif
     34 #elif (defined(JEMALLOC_OSSPIN))
     35 	OSSpinLock		lock;
     36 #elif (defined(JEMALLOC_MUTEX_INIT_CB))
     37 	pthread_mutex_t		lock;
     38 	malloc_mutex_t		*postponed_next;
     39 #else
     40 	pthread_mutex_t		lock;
     41 #endif
     42 };
     43 
     44 #endif /* JEMALLOC_H_STRUCTS */
     45 /******************************************************************************/
     46 #ifdef JEMALLOC_H_EXTERNS
     47 
     48 #ifdef JEMALLOC_LAZY_LOCK
     49 extern bool isthreaded;
     50 #else
     51 #  undef isthreaded /* Undo private_namespace.h definition. */
     52 #  define isthreaded true
     53 #endif
     54 
     55 bool	malloc_mutex_init(malloc_mutex_t *mutex);
     56 void	malloc_mutex_prefork(malloc_mutex_t *mutex);
     57 void	malloc_mutex_postfork_parent(malloc_mutex_t *mutex);
     58 void	malloc_mutex_postfork_child(malloc_mutex_t *mutex);
     59 bool	mutex_boot(void);
     60 
     61 #endif /* JEMALLOC_H_EXTERNS */
     62 /******************************************************************************/
     63 #ifdef JEMALLOC_H_INLINES
     64 
     65 #ifndef JEMALLOC_ENABLE_INLINE
     66 void	malloc_mutex_lock(malloc_mutex_t *mutex);
     67 void	malloc_mutex_unlock(malloc_mutex_t *mutex);
     68 #endif
     69 
     70 #if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_MUTEX_C_))
     71 JEMALLOC_INLINE void
     72 malloc_mutex_lock(malloc_mutex_t *mutex)
     73 {
     74 
     75 	if (isthreaded) {
     76 #ifdef _WIN32
     77 #  if _WIN32_WINNT >= 0x0600
     78 		AcquireSRWLockExclusive(&mutex->lock);
     79 #  else
     80 		EnterCriticalSection(&mutex->lock);
     81 #  endif
     82 #elif (defined(JEMALLOC_OSSPIN))
     83 		OSSpinLockLock(&mutex->lock);
     84 #else
     85 		pthread_mutex_lock(&mutex->lock);
     86 #endif
     87 	}
     88 }
     89 
     90 JEMALLOC_INLINE void
     91 malloc_mutex_unlock(malloc_mutex_t *mutex)
     92 {
     93 
     94 	if (isthreaded) {
     95 #ifdef _WIN32
     96 #  if _WIN32_WINNT >= 0x0600
     97 		ReleaseSRWLockExclusive(&mutex->lock);
     98 #  else
     99 		LeaveCriticalSection(&mutex->lock);
    100 #  endif
    101 #elif (defined(JEMALLOC_OSSPIN))
    102 		OSSpinLockUnlock(&mutex->lock);
    103 #else
    104 		pthread_mutex_unlock(&mutex->lock);
    105 #endif
    106 	}
    107 }
    108 #endif
    109 
    110 #endif /* JEMALLOC_H_INLINES */
    111 /******************************************************************************/
    112