1 #ifndef JEMALLOC_INTERNAL_MUTEX_INLINES_H
2 #define JEMALLOC_INTERNAL_MUTEX_INLINES_H
3
4 #ifndef JEMALLOC_ENABLE_INLINE
5 void malloc_mutex_lock(tsdn_t *tsdn, malloc_mutex_t *mutex);
6 void malloc_mutex_unlock(tsdn_t *tsdn, malloc_mutex_t *mutex);
7 void malloc_mutex_assert_owner(tsdn_t *tsdn, malloc_mutex_t *mutex);
8 void malloc_mutex_assert_not_owner(tsdn_t *tsdn, malloc_mutex_t *mutex);
9 #endif
10
11 #if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_MUTEX_C_))
12 JEMALLOC_INLINE void
malloc_mutex_lock(tsdn_t * tsdn,malloc_mutex_t * mutex)13 malloc_mutex_lock(tsdn_t *tsdn, malloc_mutex_t *mutex)
14 {
15 if (isthreaded) {
16 witness_assert_not_owner(tsdn, &mutex->witness);
17 #ifdef _WIN32
18 # if _WIN32_WINNT >= 0x0600
19 AcquireSRWLockExclusive(&mutex->lock);
20 # else
21 EnterCriticalSection(&mutex->lock);
22 # endif
23 #elif (defined(JEMALLOC_OS_UNFAIR_LOCK))
24 os_unfair_lock_lock(&mutex->lock);
25 #elif (defined(JEMALLOC_OSSPIN))
26 OSSpinLockLock(&mutex->lock);
27 #else
28 pthread_mutex_lock(&mutex->lock);
29 #endif
30 witness_lock(tsdn, &mutex->witness);
31 }
32 }
33
34 JEMALLOC_INLINE void
malloc_mutex_unlock(tsdn_t * tsdn,malloc_mutex_t * mutex)35 malloc_mutex_unlock(tsdn_t *tsdn, malloc_mutex_t *mutex)
36 {
37 if (isthreaded) {
38 witness_unlock(tsdn, &mutex->witness);
39 #ifdef _WIN32
40 # if _WIN32_WINNT >= 0x0600
41 ReleaseSRWLockExclusive(&mutex->lock);
42 # else
43 LeaveCriticalSection(&mutex->lock);
44 # endif
45 #elif (defined(JEMALLOC_OS_UNFAIR_LOCK))
46 os_unfair_lock_unlock(&mutex->lock);
47 #elif (defined(JEMALLOC_OSSPIN))
48 OSSpinLockUnlock(&mutex->lock);
49 #else
50 pthread_mutex_unlock(&mutex->lock);
51 #endif
52 }
53 }
54
55 JEMALLOC_INLINE void
malloc_mutex_assert_owner(tsdn_t * tsdn,malloc_mutex_t * mutex)56 malloc_mutex_assert_owner(tsdn_t *tsdn, malloc_mutex_t *mutex)
57 {
58 if (isthreaded)
59 witness_assert_owner(tsdn, &mutex->witness);
60 }
61
62 JEMALLOC_INLINE void
malloc_mutex_assert_not_owner(tsdn_t * tsdn,malloc_mutex_t * mutex)63 malloc_mutex_assert_not_owner(tsdn_t *tsdn, malloc_mutex_t *mutex)
64 {
65 if (isthreaded)
66 witness_assert_not_owner(tsdn, &mutex->witness);
67 }
68 #endif
69
70 #endif /* JEMALLOC_INTERNAL_MUTEX_INLINES_H */
71