blob: 6333e73d609d0c3f3c49ce295bbbcef5252d26b1 [file] [log] [blame]
Jason Evanse476f8a2010-01-16 09:53:50 -08001#define JEMALLOC_MUTEX_C_
Jason Evans376b1522010-02-11 14:45:59 -08002#include "jemalloc/internal/jemalloc_internal.h"
Jason Evanse476f8a2010-01-16 09:53:50 -08003
Mike Hommeya19e87f2012-04-21 21:27:46 -07004#if defined(JEMALLOC_LAZY_LOCK) && !defined(_WIN32)
Jason Evans4bb09832012-02-29 10:37:27 -08005#include <dlfcn.h>
6#endif
7
Mike Hommeya19e87f2012-04-21 21:27:46 -07008#ifndef _CRT_SPINCOUNT
Jason Evansa4f124f2013-12-08 22:28:27 -08009#define _CRT_SPINCOUNT 4000
Mike Hommeya19e87f2012-04-21 21:27:46 -070010#endif
11
Jason Evanse476f8a2010-01-16 09:53:50 -080012/******************************************************************************/
13/* Data. */
14
15#ifdef JEMALLOC_LAZY_LOCK
16bool isthreaded = false;
17#endif
Jason Evans633aaff2012-04-03 08:47:07 -070018#ifdef JEMALLOC_MUTEX_INIT_CB
19static bool postpone_init = true;
20static malloc_mutex_t *postponed_mutexes = NULL;
21#endif
Jason Evanse476f8a2010-01-16 09:53:50 -080022
Mike Hommeya19e87f2012-04-21 21:27:46 -070023#if defined(JEMALLOC_LAZY_LOCK) && !defined(_WIN32)
Jason Evanse476f8a2010-01-16 09:53:50 -080024static void pthread_create_once(void);
25#endif
26
27/******************************************************************************/
28/*
29 * We intercept pthread_create() calls in order to toggle isthreaded if the
30 * process goes multi-threaded.
31 */
32
Mike Hommeya19e87f2012-04-21 21:27:46 -070033#if defined(JEMALLOC_LAZY_LOCK) && !defined(_WIN32)
Jason Evanse476f8a2010-01-16 09:53:50 -080034static int (*pthread_create_fptr)(pthread_t *__restrict, const pthread_attr_t *,
35 void *(*)(void *), void *__restrict);
36
37static void
38pthread_create_once(void)
39{
40
41 pthread_create_fptr = dlsym(RTLD_NEXT, "pthread_create");
42 if (pthread_create_fptr == NULL) {
Jason Evans698805c2010-03-03 17:45:38 -080043 malloc_write("<jemalloc>: Error in dlsym(RTLD_NEXT, "
44 "\"pthread_create\")\n");
Jason Evanse476f8a2010-01-16 09:53:50 -080045 abort();
46 }
47
48 isthreaded = true;
49}
50
Mike Hommeyda99e312012-04-30 12:38:29 +020051JEMALLOC_EXPORT int
Jason Evanse476f8a2010-01-16 09:53:50 -080052pthread_create(pthread_t *__restrict thread,
53 const pthread_attr_t *__restrict attr, void *(*start_routine)(void *),
54 void *__restrict arg)
55{
56 static pthread_once_t once_control = PTHREAD_ONCE_INIT;
57
58 pthread_once(&once_control, pthread_create_once);
59
60 return (pthread_create_fptr(thread, attr, start_routine, arg));
61}
62#endif
63
64/******************************************************************************/
65
Jason Evans41b6afb2012-02-02 22:04:57 -080066#ifdef JEMALLOC_MUTEX_INIT_CB
Jan Beichd0ffd8e2012-09-18 07:40:31 -050067JEMALLOC_EXPORT int _pthread_mutex_init_calloc_cb(pthread_mutex_t *mutex,
Jason Evans41b6afb2012-02-02 22:04:57 -080068 void *(calloc_cb)(size_t, size_t));
69#endif
70
Jason Evanse476f8a2010-01-16 09:53:50 -080071bool
Jason Evansb2c0d632016-04-13 23:36:15 -070072malloc_mutex_init(malloc_mutex_t *mutex, const char *name, witness_rank_t rank)
Jason Evanse476f8a2010-01-16 09:53:50 -080073{
Mike Hommeyda99e312012-04-30 12:38:29 +020074
Mike Hommeya19e87f2012-04-21 21:27:46 -070075#ifdef _WIN32
Matthijsa1aaf942015-06-25 22:53:58 +020076# if _WIN32_WINNT >= 0x0600
77 InitializeSRWLock(&mutex->lock);
78# else
Mike Hommeya19e87f2012-04-21 21:27:46 -070079 if (!InitializeCriticalSectionAndSpinCount(&mutex->lock,
80 _CRT_SPINCOUNT))
81 return (true);
Matthijsa1aaf942015-06-25 22:53:58 +020082# endif
Jason Evans3f2b8d92016-11-02 18:09:45 -070083#elif (defined(JEMALLOC_OS_UNFAIR_LOCK))
84 mutex->lock = OS_UNFAIR_LOCK_INIT;
Mike Hommeya19e87f2012-04-21 21:27:46 -070085#elif (defined(JEMALLOC_OSSPIN))
Jason Evans633aaff2012-04-03 08:47:07 -070086 mutex->lock = 0;
Jason Evans41b6afb2012-02-02 22:04:57 -080087#elif (defined(JEMALLOC_MUTEX_INIT_CB))
Jason Evans633aaff2012-04-03 08:47:07 -070088 if (postpone_init) {
89 mutex->postponed_next = postponed_mutexes;
90 postponed_mutexes = mutex;
91 } else {
Jason Evansf500a102015-01-30 21:49:19 -080092 if (_pthread_mutex_init_calloc_cb(&mutex->lock,
93 bootstrap_calloc) != 0)
Jason Evans633aaff2012-04-03 08:47:07 -070094 return (true);
95 }
Jason Evans893a0ed2011-03-18 19:30:18 -070096#else
Jason Evanse476f8a2010-01-16 09:53:50 -080097 pthread_mutexattr_t attr;
98
99 if (pthread_mutexattr_init(&attr) != 0)
100 return (true);
Jason Evans41b6afb2012-02-02 22:04:57 -0800101 pthread_mutexattr_settype(&attr, MALLOC_MUTEX_TYPE);
Jason Evans633aaff2012-04-03 08:47:07 -0700102 if (pthread_mutex_init(&mutex->lock, &attr) != 0) {
Jason Evanse476f8a2010-01-16 09:53:50 -0800103 pthread_mutexattr_destroy(&attr);
104 return (true);
105 }
106 pthread_mutexattr_destroy(&attr);
Jason Evans893a0ed2011-03-18 19:30:18 -0700107#endif
Jason Evansb2c0d632016-04-13 23:36:15 -0700108 if (config_debug)
109 witness_init(&mutex->witness, name, rank, NULL);
Jason Evanse476f8a2010-01-16 09:53:50 -0800110 return (false);
111}
Jason Evansa881cd22010-10-02 15:18:50 -0700112
113void
Jason Evansc1e00ef2016-05-10 22:21:10 -0700114malloc_mutex_prefork(tsdn_t *tsdn, malloc_mutex_t *mutex)
Jason Evans4e2e3dd2012-03-13 16:31:41 -0700115{
116
Jason Evansc1e00ef2016-05-10 22:21:10 -0700117 malloc_mutex_lock(tsdn, mutex);
Jason Evans4e2e3dd2012-03-13 16:31:41 -0700118}
119
120void
Jason Evansc1e00ef2016-05-10 22:21:10 -0700121malloc_mutex_postfork_parent(tsdn_t *tsdn, malloc_mutex_t *mutex)
Jason Evans4e2e3dd2012-03-13 16:31:41 -0700122{
123
Jason Evansc1e00ef2016-05-10 22:21:10 -0700124 malloc_mutex_unlock(tsdn, mutex);
Jason Evans4e2e3dd2012-03-13 16:31:41 -0700125}
126
127void
Jason Evansc1e00ef2016-05-10 22:21:10 -0700128malloc_mutex_postfork_child(tsdn_t *tsdn, malloc_mutex_t *mutex)
Jason Evans4e2e3dd2012-03-13 16:31:41 -0700129{
130
Jason Evans41b6afb2012-02-02 22:04:57 -0800131#ifdef JEMALLOC_MUTEX_INIT_CB
Jason Evansc1e00ef2016-05-10 22:21:10 -0700132 malloc_mutex_unlock(tsdn, mutex);
Jason Evans41b6afb2012-02-02 22:04:57 -0800133#else
Jason Evansb2c0d632016-04-13 23:36:15 -0700134 if (malloc_mutex_init(mutex, mutex->witness.name,
135 mutex->witness.rank)) {
Jason Evans4e2e3dd2012-03-13 16:31:41 -0700136 malloc_printf("<jemalloc>: Error re-initializing mutex in "
137 "child\n");
138 if (opt_abort)
139 abort();
140 }
Jason Evans41b6afb2012-02-02 22:04:57 -0800141#endif
Jason Evans4e2e3dd2012-03-13 16:31:41 -0700142}
Jason Evans633aaff2012-04-03 08:47:07 -0700143
144bool
Jason Evansb2c0d632016-04-13 23:36:15 -0700145malloc_mutex_boot(void)
Jason Evans633aaff2012-04-03 08:47:07 -0700146{
147
148#ifdef JEMALLOC_MUTEX_INIT_CB
149 postpone_init = false;
150 while (postponed_mutexes != NULL) {
151 if (_pthread_mutex_init_calloc_cb(&postponed_mutexes->lock,
Jason Evansf500a102015-01-30 21:49:19 -0800152 bootstrap_calloc) != 0)
Jason Evans633aaff2012-04-03 08:47:07 -0700153 return (true);
154 postponed_mutexes = postponed_mutexes->postponed_next;
155 }
156#endif
157 return (false);
158}