]> git.proxmox.com Git - rustc.git/blame - src/jemalloc/include/jemalloc/internal/mutex.h
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / jemalloc / include / jemalloc / internal / mutex.h
CommitLineData
970d7e83
LB
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
1a4d82fc 13# if (defined(JEMALLOC_HAVE_PTHREAD_MUTEX_ADAPTIVE_NP) && \
970d7e83
LB
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
54a0048b
SL
29# if _WIN32_WINNT >= 0x0600
30 SRWLOCK lock;
31# else
970d7e83 32 CRITICAL_SECTION lock;
54a0048b 33# endif
970d7e83
LB
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
49extern bool isthreaded;
50#else
51# undef isthreaded /* Undo private_namespace.h definition. */
52# define isthreaded true
53#endif
54
55bool malloc_mutex_init(malloc_mutex_t *mutex);
56void malloc_mutex_prefork(malloc_mutex_t *mutex);
57void malloc_mutex_postfork_parent(malloc_mutex_t *mutex);
58void malloc_mutex_postfork_child(malloc_mutex_t *mutex);
59bool mutex_boot(void);
60
61#endif /* JEMALLOC_H_EXTERNS */
62/******************************************************************************/
63#ifdef JEMALLOC_H_INLINES
64
65#ifndef JEMALLOC_ENABLE_INLINE
66void malloc_mutex_lock(malloc_mutex_t *mutex);
67void malloc_mutex_unlock(malloc_mutex_t *mutex);
68#endif
69
70#if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_MUTEX_C_))
71JEMALLOC_INLINE void
72malloc_mutex_lock(malloc_mutex_t *mutex)
73{
74
75 if (isthreaded) {
76#ifdef _WIN32
54a0048b
SL
77# if _WIN32_WINNT >= 0x0600
78 AcquireSRWLockExclusive(&mutex->lock);
79# else
970d7e83 80 EnterCriticalSection(&mutex->lock);
54a0048b 81# endif
970d7e83
LB
82#elif (defined(JEMALLOC_OSSPIN))
83 OSSpinLockLock(&mutex->lock);
84#else
85 pthread_mutex_lock(&mutex->lock);
86#endif
87 }
88}
89
90JEMALLOC_INLINE void
91malloc_mutex_unlock(malloc_mutex_t *mutex)
92{
93
94 if (isthreaded) {
95#ifdef _WIN32
54a0048b
SL
96# if _WIN32_WINNT >= 0x0600
97 ReleaseSRWLockExclusive(&mutex->lock);
98# else
970d7e83 99 LeaveCriticalSection(&mutex->lock);
54a0048b 100# endif
970d7e83
LB
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/******************************************************************************/