1/******************************************************************************/
2#ifdef JEMALLOC_H_TYPES
3
4typedef 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(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
27struct malloc_mutex_s {
28#ifdef _WIN32
29	CRITICAL_SECTION	lock;
30#elif (defined(JEMALLOC_OSSPIN))
31	OSSpinLock		lock;
32#elif (defined(JEMALLOC_MUTEX_INIT_CB))
33	pthread_mutex_t		lock;
34	malloc_mutex_t		*postponed_next;
35#else
36	pthread_mutex_t		lock;
37#endif
38};
39
40#endif /* JEMALLOC_H_STRUCTS */
41/******************************************************************************/
42#ifdef JEMALLOC_H_EXTERNS
43
44#ifdef JEMALLOC_LAZY_LOCK
45extern bool isthreaded;
46#else
47#  undef isthreaded /* Undo private_namespace.h definition. */
48#  define isthreaded true
49#endif
50
51bool	malloc_mutex_init(malloc_mutex_t *mutex);
52void	malloc_mutex_prefork(malloc_mutex_t *mutex);
53void	malloc_mutex_postfork_parent(malloc_mutex_t *mutex);
54void	malloc_mutex_postfork_child(malloc_mutex_t *mutex);
55bool	mutex_boot(void);
56
57#endif /* JEMALLOC_H_EXTERNS */
58/******************************************************************************/
59#ifdef JEMALLOC_H_INLINES
60
61#ifndef JEMALLOC_ENABLE_INLINE
62void	malloc_mutex_lock(malloc_mutex_t *mutex);
63void	malloc_mutex_unlock(malloc_mutex_t *mutex);
64#endif
65
66#if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_MUTEX_C_))
67JEMALLOC_INLINE void
68malloc_mutex_lock(malloc_mutex_t *mutex)
69{
70
71	if (isthreaded) {
72#ifdef _WIN32
73		EnterCriticalSection(&mutex->lock);
74#elif (defined(JEMALLOC_OSSPIN))
75		OSSpinLockLock(&mutex->lock);
76#else
77		pthread_mutex_lock(&mutex->lock);
78#endif
79	}
80}
81
82JEMALLOC_INLINE void
83malloc_mutex_unlock(malloc_mutex_t *mutex)
84{
85
86	if (isthreaded) {
87#ifdef _WIN32
88		LeaveCriticalSection(&mutex->lock);
89#elif (defined(JEMALLOC_OSSPIN))
90		OSSpinLockUnlock(&mutex->lock);
91#else
92		pthread_mutex_unlock(&mutex->lock);
93#endif
94	}
95}
96#endif
97
98#endif /* JEMALLOC_H_INLINES */
99/******************************************************************************/
100