huangcm
2024-12-18 9d29be7f7249789d6ffd0440067187a9f040c2cd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/******************************************************************************/
#ifdef JEMALLOC_H_TYPES
 
typedef struct malloc_mutex_s malloc_mutex_t;
 
#ifdef _WIN32
#  define MALLOC_MUTEX_INITIALIZER
#elif (defined(JEMALLOC_OS_UNFAIR_LOCK))
#  define MALLOC_MUTEX_INITIALIZER                    \
     {OS_UNFAIR_LOCK_INIT, WITNESS_INITIALIZER(WITNESS_RANK_OMIT)}
#elif (defined(JEMALLOC_OSSPIN))
#  define MALLOC_MUTEX_INITIALIZER {0, WITNESS_INITIALIZER(WITNESS_RANK_OMIT)}
#elif (defined(JEMALLOC_MUTEX_INIT_CB))
#  define MALLOC_MUTEX_INITIALIZER                    \
    {PTHREAD_MUTEX_INITIALIZER, NULL, WITNESS_INITIALIZER(WITNESS_RANK_OMIT)}
#else
#  if (defined(JEMALLOC_HAVE_PTHREAD_MUTEX_ADAPTIVE_NP) &&        \
       defined(PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP))
#    define MALLOC_MUTEX_TYPE PTHREAD_MUTEX_ADAPTIVE_NP
#    define MALLOC_MUTEX_INITIALIZER                    \
       {PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP,                \
        WITNESS_INITIALIZER(WITNESS_RANK_OMIT)}
#  else
#    define MALLOC_MUTEX_TYPE PTHREAD_MUTEX_DEFAULT
#    define MALLOC_MUTEX_INITIALIZER                    \
       {PTHREAD_MUTEX_INITIALIZER, WITNESS_INITIALIZER(WITNESS_RANK_OMIT)}
#  endif
#endif
 
#endif /* JEMALLOC_H_TYPES */
/******************************************************************************/
#ifdef JEMALLOC_H_STRUCTS
 
struct malloc_mutex_s {
#ifdef _WIN32
#  if _WIN32_WINNT >= 0x0600
   SRWLOCK             lock;
#  else
   CRITICAL_SECTION    lock;
#  endif
#elif (defined(JEMALLOC_OS_UNFAIR_LOCK))
   os_unfair_lock        lock;
#elif (defined(JEMALLOC_OSSPIN))
   OSSpinLock        lock;
#elif (defined(JEMALLOC_MUTEX_INIT_CB))
   pthread_mutex_t        lock;
   malloc_mutex_t        *postponed_next;
#else
   pthread_mutex_t        lock;
#endif
   witness_t        witness;
};
 
#endif /* JEMALLOC_H_STRUCTS */
/******************************************************************************/
#ifdef JEMALLOC_H_EXTERNS
 
#ifdef JEMALLOC_LAZY_LOCK
extern bool isthreaded;
#else
#  undef isthreaded /* Undo private_namespace.h definition. */
#  define isthreaded true
#endif
 
bool    malloc_mutex_init(malloc_mutex_t *mutex, const char *name,
    witness_rank_t rank);
void    malloc_mutex_prefork(tsdn_t *tsdn, malloc_mutex_t *mutex);
void    malloc_mutex_postfork_parent(tsdn_t *tsdn, malloc_mutex_t *mutex);
void    malloc_mutex_postfork_child(tsdn_t *tsdn, malloc_mutex_t *mutex);
bool    malloc_mutex_boot(void);
 
#endif /* JEMALLOC_H_EXTERNS */
/******************************************************************************/
#ifdef JEMALLOC_H_INLINES
 
#ifndef JEMALLOC_ENABLE_INLINE
void    malloc_mutex_lock(tsdn_t *tsdn, malloc_mutex_t *mutex);
void    malloc_mutex_unlock(tsdn_t *tsdn, malloc_mutex_t *mutex);
void    malloc_mutex_assert_owner(tsdn_t *tsdn, malloc_mutex_t *mutex);
void    malloc_mutex_assert_not_owner(tsdn_t *tsdn, malloc_mutex_t *mutex);
#endif
 
#if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_MUTEX_C_))
JEMALLOC_INLINE void
malloc_mutex_lock(tsdn_t *tsdn, malloc_mutex_t *mutex)
{
 
   if (isthreaded) {
       witness_assert_not_owner(tsdn, &mutex->witness);
#ifdef _WIN32
#  if _WIN32_WINNT >= 0x0600
       AcquireSRWLockExclusive(&mutex->lock);
#  else
       EnterCriticalSection(&mutex->lock);
#  endif
#elif (defined(JEMALLOC_OS_UNFAIR_LOCK))
       os_unfair_lock_lock(&mutex->lock);
#elif (defined(JEMALLOC_OSSPIN))
       OSSpinLockLock(&mutex->lock);
#else
       pthread_mutex_lock(&mutex->lock);
#endif
       witness_lock(tsdn, &mutex->witness);
   }
}
 
JEMALLOC_INLINE void
malloc_mutex_unlock(tsdn_t *tsdn, malloc_mutex_t *mutex)
{
 
   if (isthreaded) {
       witness_unlock(tsdn, &mutex->witness);
#ifdef _WIN32
#  if _WIN32_WINNT >= 0x0600
       ReleaseSRWLockExclusive(&mutex->lock);
#  else
       LeaveCriticalSection(&mutex->lock);
#  endif
#elif (defined(JEMALLOC_OS_UNFAIR_LOCK))
       os_unfair_lock_unlock(&mutex->lock);
#elif (defined(JEMALLOC_OSSPIN))
       OSSpinLockUnlock(&mutex->lock);
#else
       pthread_mutex_unlock(&mutex->lock);
#endif
   }
}
 
JEMALLOC_INLINE void
malloc_mutex_assert_owner(tsdn_t *tsdn, malloc_mutex_t *mutex)
{
 
   if (isthreaded)
       witness_assert_owner(tsdn, &mutex->witness);
}
 
JEMALLOC_INLINE void
malloc_mutex_assert_not_owner(tsdn_t *tsdn, malloc_mutex_t *mutex)
{
 
   if (isthreaded)
       witness_assert_not_owner(tsdn, &mutex->witness);
}
#endif
 
#endif /* JEMALLOC_H_INLINES */
/******************************************************************************/