mutex.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. #ifndef JEMALLOC_INTERNAL_MUTEX_H
  2. #define JEMALLOC_INTERNAL_MUTEX_H
  3. #include "jemalloc/internal/atomic.h"
  4. #include "jemalloc/internal/mutex_prof.h"
  5. #include "jemalloc/internal/tsd.h"
  6. #include "jemalloc/internal/witness.h"
  7. extern int64_t opt_mutex_max_spin;
  8. typedef enum {
  9. /* Can only acquire one mutex of a given witness rank at a time. */
  10. malloc_mutex_rank_exclusive,
  11. /*
  12. * Can acquire multiple mutexes of the same witness rank, but in
  13. * address-ascending order only.
  14. */
  15. malloc_mutex_address_ordered
  16. } malloc_mutex_lock_order_t;
  17. typedef struct malloc_mutex_s malloc_mutex_t;
  18. struct malloc_mutex_s {
  19. union {
  20. struct {
  21. /*
  22. * prof_data is defined first to reduce cacheline
  23. * bouncing: the data is not touched by the mutex holder
  24. * during unlocking, while might be modified by
  25. * contenders. Having it before the mutex itself could
  26. * avoid prefetching a modified cacheline (for the
  27. * unlocking thread).
  28. */
  29. mutex_prof_data_t prof_data;
  30. #ifdef _WIN32
  31. # if _WIN32_WINNT >= 0x0600
  32. SRWLOCK lock;
  33. # else
  34. CRITICAL_SECTION lock;
  35. # endif
  36. #elif (defined(JEMALLOC_OS_UNFAIR_LOCK))
  37. os_unfair_lock lock;
  38. #elif (defined(JEMALLOC_MUTEX_INIT_CB))
  39. pthread_mutex_t lock;
  40. malloc_mutex_t *postponed_next;
  41. #else
  42. pthread_mutex_t lock;
  43. #endif
  44. /*
  45. * Hint flag to avoid exclusive cache line contention
  46. * during spin waiting
  47. */
  48. atomic_b_t locked;
  49. };
  50. /*
  51. * We only touch witness when configured w/ debug. However we
  52. * keep the field in a union when !debug so that we don't have
  53. * to pollute the code base with #ifdefs, while avoid paying the
  54. * memory cost.
  55. */
  56. #if !defined(JEMALLOC_DEBUG)
  57. witness_t witness;
  58. malloc_mutex_lock_order_t lock_order;
  59. #endif
  60. };
  61. #if defined(JEMALLOC_DEBUG)
  62. witness_t witness;
  63. malloc_mutex_lock_order_t lock_order;
  64. #endif
  65. };
  66. #ifdef _WIN32
  67. # if _WIN32_WINNT >= 0x0600
  68. # define MALLOC_MUTEX_LOCK(m) AcquireSRWLockExclusive(&(m)->lock)
  69. # define MALLOC_MUTEX_UNLOCK(m) ReleaseSRWLockExclusive(&(m)->lock)
  70. # define MALLOC_MUTEX_TRYLOCK(m) (!TryAcquireSRWLockExclusive(&(m)->lock))
  71. # else
  72. # define MALLOC_MUTEX_LOCK(m) EnterCriticalSection(&(m)->lock)
  73. # define MALLOC_MUTEX_UNLOCK(m) LeaveCriticalSection(&(m)->lock)
  74. # define MALLOC_MUTEX_TRYLOCK(m) (!TryEnterCriticalSection(&(m)->lock))
  75. # endif
  76. #elif (defined(JEMALLOC_OS_UNFAIR_LOCK))
  77. # define MALLOC_MUTEX_LOCK(m) os_unfair_lock_lock(&(m)->lock)
  78. # define MALLOC_MUTEX_UNLOCK(m) os_unfair_lock_unlock(&(m)->lock)
  79. # define MALLOC_MUTEX_TRYLOCK(m) (!os_unfair_lock_trylock(&(m)->lock))
  80. #else
  81. # define MALLOC_MUTEX_LOCK(m) pthread_mutex_lock(&(m)->lock)
  82. # define MALLOC_MUTEX_UNLOCK(m) pthread_mutex_unlock(&(m)->lock)
  83. # define MALLOC_MUTEX_TRYLOCK(m) (pthread_mutex_trylock(&(m)->lock) != 0)
  84. #endif
  85. #define LOCK_PROF_DATA_INITIALIZER \
  86. {NSTIME_ZERO_INITIALIZER, NSTIME_ZERO_INITIALIZER, 0, 0, 0, \
  87. ATOMIC_INIT(0), 0, NULL, 0}
  88. #ifdef _WIN32
  89. # define MALLOC_MUTEX_INITIALIZER
  90. #elif (defined(JEMALLOC_OS_UNFAIR_LOCK))
  91. # if defined(JEMALLOC_DEBUG)
  92. # define MALLOC_MUTEX_INITIALIZER \
  93. {{{LOCK_PROF_DATA_INITIALIZER, OS_UNFAIR_LOCK_INIT, ATOMIC_INIT(false)}}, \
  94. WITNESS_INITIALIZER("mutex", WITNESS_RANK_OMIT), 0}
  95. # else
  96. # define MALLOC_MUTEX_INITIALIZER \
  97. {{{LOCK_PROF_DATA_INITIALIZER, OS_UNFAIR_LOCK_INIT, ATOMIC_INIT(false)}}, \
  98. WITNESS_INITIALIZER("mutex", WITNESS_RANK_OMIT)}
  99. # endif
  100. #elif (defined(JEMALLOC_MUTEX_INIT_CB))
  101. # if (defined(JEMALLOC_DEBUG))
  102. # define MALLOC_MUTEX_INITIALIZER \
  103. {{{LOCK_PROF_DATA_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, NULL, ATOMIC_INIT(false)}}, \
  104. WITNESS_INITIALIZER("mutex", WITNESS_RANK_OMIT), 0}
  105. # else
  106. # define MALLOC_MUTEX_INITIALIZER \
  107. {{{LOCK_PROF_DATA_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, NULL, ATOMIC_INIT(false)}}, \
  108. WITNESS_INITIALIZER("mutex", WITNESS_RANK_OMIT)}
  109. # endif
  110. #else
  111. # define MALLOC_MUTEX_TYPE PTHREAD_MUTEX_DEFAULT
  112. # if defined(JEMALLOC_DEBUG)
  113. # define MALLOC_MUTEX_INITIALIZER \
  114. {{{LOCK_PROF_DATA_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, ATOMIC_INIT(false)}}, \
  115. WITNESS_INITIALIZER("mutex", WITNESS_RANK_OMIT), 0}
  116. # else
  117. # define MALLOC_MUTEX_INITIALIZER \
  118. {{{LOCK_PROF_DATA_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, ATOMIC_INIT(false)}}, \
  119. WITNESS_INITIALIZER("mutex", WITNESS_RANK_OMIT)}
  120. # endif
  121. #endif
  122. #ifdef JEMALLOC_LAZY_LOCK
  123. extern bool isthreaded;
  124. #else
  125. # undef isthreaded /* Undo private_namespace.h definition. */
  126. # define isthreaded true
  127. #endif
  128. bool malloc_mutex_init(malloc_mutex_t *mutex, const char *name,
  129. witness_rank_t rank, malloc_mutex_lock_order_t lock_order);
  130. void malloc_mutex_prefork(tsdn_t *tsdn, malloc_mutex_t *mutex);
  131. void malloc_mutex_postfork_parent(tsdn_t *tsdn, malloc_mutex_t *mutex);
  132. void malloc_mutex_postfork_child(tsdn_t *tsdn, malloc_mutex_t *mutex);
  133. bool malloc_mutex_boot(void);
  134. void malloc_mutex_prof_data_reset(tsdn_t *tsdn, malloc_mutex_t *mutex);
  135. void malloc_mutex_lock_slow(malloc_mutex_t *mutex);
  136. static inline void
  137. malloc_mutex_lock_final(malloc_mutex_t *mutex) {
  138. MALLOC_MUTEX_LOCK(mutex);
  139. atomic_store_b(&mutex->locked, true, ATOMIC_RELAXED);
  140. }
  141. static inline bool
  142. malloc_mutex_trylock_final(malloc_mutex_t *mutex) {
  143. return MALLOC_MUTEX_TRYLOCK(mutex);
  144. }
  145. static inline void
  146. mutex_owner_stats_update(tsdn_t *tsdn, malloc_mutex_t *mutex) {
  147. if (config_stats) {
  148. mutex_prof_data_t *data = &mutex->prof_data;
  149. data->n_lock_ops++;
  150. if (data->prev_owner != tsdn) {
  151. data->prev_owner = tsdn;
  152. data->n_owner_switches++;
  153. }
  154. }
  155. }
  156. /* Trylock: return false if the lock is successfully acquired. */
  157. static inline bool
  158. malloc_mutex_trylock(tsdn_t *tsdn, malloc_mutex_t *mutex) {
  159. witness_assert_not_owner(tsdn_witness_tsdp_get(tsdn), &mutex->witness);
  160. if (isthreaded) {
  161. if (malloc_mutex_trylock_final(mutex)) {
  162. atomic_store_b(&mutex->locked, true, ATOMIC_RELAXED);
  163. return true;
  164. }
  165. mutex_owner_stats_update(tsdn, mutex);
  166. }
  167. witness_lock(tsdn_witness_tsdp_get(tsdn), &mutex->witness);
  168. return false;
  169. }
  170. /* Aggregate lock prof data. */
  171. static inline void
  172. malloc_mutex_prof_merge(mutex_prof_data_t *sum, mutex_prof_data_t *data) {
  173. nstime_add(&sum->tot_wait_time, &data->tot_wait_time);
  174. if (nstime_compare(&sum->max_wait_time, &data->max_wait_time) < 0) {
  175. nstime_copy(&sum->max_wait_time, &data->max_wait_time);
  176. }
  177. sum->n_wait_times += data->n_wait_times;
  178. sum->n_spin_acquired += data->n_spin_acquired;
  179. if (sum->max_n_thds < data->max_n_thds) {
  180. sum->max_n_thds = data->max_n_thds;
  181. }
  182. uint32_t cur_n_waiting_thds = atomic_load_u32(&sum->n_waiting_thds,
  183. ATOMIC_RELAXED);
  184. uint32_t new_n_waiting_thds = cur_n_waiting_thds + atomic_load_u32(
  185. &data->n_waiting_thds, ATOMIC_RELAXED);
  186. atomic_store_u32(&sum->n_waiting_thds, new_n_waiting_thds,
  187. ATOMIC_RELAXED);
  188. sum->n_owner_switches += data->n_owner_switches;
  189. sum->n_lock_ops += data->n_lock_ops;
  190. }
  191. static inline void
  192. malloc_mutex_lock(tsdn_t *tsdn, malloc_mutex_t *mutex) {
  193. witness_assert_not_owner(tsdn_witness_tsdp_get(tsdn), &mutex->witness);
  194. if (isthreaded) {
  195. if (malloc_mutex_trylock_final(mutex)) {
  196. malloc_mutex_lock_slow(mutex);
  197. atomic_store_b(&mutex->locked, true, ATOMIC_RELAXED);
  198. }
  199. mutex_owner_stats_update(tsdn, mutex);
  200. }
  201. witness_lock(tsdn_witness_tsdp_get(tsdn), &mutex->witness);
  202. }
  203. static inline void
  204. malloc_mutex_unlock(tsdn_t *tsdn, malloc_mutex_t *mutex) {
  205. atomic_store_b(&mutex->locked, false, ATOMIC_RELAXED);
  206. witness_unlock(tsdn_witness_tsdp_get(tsdn), &mutex->witness);
  207. if (isthreaded) {
  208. MALLOC_MUTEX_UNLOCK(mutex);
  209. }
  210. }
  211. static inline void
  212. malloc_mutex_assert_owner(tsdn_t *tsdn, malloc_mutex_t *mutex) {
  213. witness_assert_owner(tsdn_witness_tsdp_get(tsdn), &mutex->witness);
  214. }
  215. static inline void
  216. malloc_mutex_assert_not_owner(tsdn_t *tsdn, malloc_mutex_t *mutex) {
  217. witness_assert_not_owner(tsdn_witness_tsdp_get(tsdn), &mutex->witness);
  218. }
  219. static inline void
  220. malloc_mutex_prof_copy(mutex_prof_data_t *dst, mutex_prof_data_t *source) {
  221. /*
  222. * Not *really* allowed (we shouldn't be doing non-atomic loads of
  223. * atomic data), but the mutex protection makes this safe, and writing
  224. * a member-for-member copy is tedious for this situation.
  225. */
  226. *dst = *source;
  227. /* n_wait_thds is not reported (modified w/o locking). */
  228. atomic_store_u32(&dst->n_waiting_thds, 0, ATOMIC_RELAXED);
  229. }
  230. /* Copy the prof data from mutex for processing. */
  231. static inline void
  232. malloc_mutex_prof_read(tsdn_t *tsdn, mutex_prof_data_t *data,
  233. malloc_mutex_t *mutex) {
  234. /* Can only read holding the mutex. */
  235. malloc_mutex_assert_owner(tsdn, mutex);
  236. malloc_mutex_prof_copy(data, &mutex->prof_data);
  237. }
  238. static inline void
  239. malloc_mutex_prof_accum(tsdn_t *tsdn, mutex_prof_data_t *data,
  240. malloc_mutex_t *mutex) {
  241. mutex_prof_data_t *source = &mutex->prof_data;
  242. /* Can only read holding the mutex. */
  243. malloc_mutex_assert_owner(tsdn, mutex);
  244. nstime_add(&data->tot_wait_time, &source->tot_wait_time);
  245. if (nstime_compare(&source->max_wait_time, &data->max_wait_time) > 0) {
  246. nstime_copy(&data->max_wait_time, &source->max_wait_time);
  247. }
  248. data->n_wait_times += source->n_wait_times;
  249. data->n_spin_acquired += source->n_spin_acquired;
  250. if (data->max_n_thds < source->max_n_thds) {
  251. data->max_n_thds = source->max_n_thds;
  252. }
  253. /* n_wait_thds is not reported. */
  254. atomic_store_u32(&data->n_waiting_thds, 0, ATOMIC_RELAXED);
  255. data->n_owner_switches += source->n_owner_switches;
  256. data->n_lock_ops += source->n_lock_ops;
  257. }
  258. /* Compare the prof data and update to the maximum. */
  259. static inline void
  260. malloc_mutex_prof_max_update(tsdn_t *tsdn, mutex_prof_data_t *data,
  261. malloc_mutex_t *mutex) {
  262. mutex_prof_data_t *source = &mutex->prof_data;
  263. /* Can only read holding the mutex. */
  264. malloc_mutex_assert_owner(tsdn, mutex);
  265. if (nstime_compare(&source->tot_wait_time, &data->tot_wait_time) > 0) {
  266. nstime_copy(&data->tot_wait_time, &source->tot_wait_time);
  267. }
  268. if (nstime_compare(&source->max_wait_time, &data->max_wait_time) > 0) {
  269. nstime_copy(&data->max_wait_time, &source->max_wait_time);
  270. }
  271. if (source->n_wait_times > data->n_wait_times) {
  272. data->n_wait_times = source->n_wait_times;
  273. }
  274. if (source->n_spin_acquired > data->n_spin_acquired) {
  275. data->n_spin_acquired = source->n_spin_acquired;
  276. }
  277. if (source->max_n_thds > data->max_n_thds) {
  278. data->max_n_thds = source->max_n_thds;
  279. }
  280. if (source->n_owner_switches > data->n_owner_switches) {
  281. data->n_owner_switches = source->n_owner_switches;
  282. }
  283. if (source->n_lock_ops > data->n_lock_ops) {
  284. data->n_lock_ops = source->n_lock_ops;
  285. }
  286. /* n_wait_thds is not reported. */
  287. }
  288. #endif /* JEMALLOC_INTERNAL_MUTEX_H */