Jason Evans | cd9a134 | 2012-03-21 18:33:03 -0700 | [diff] [blame] | 1 | #define JEMALLOC_TSD_C_ |
| 2 | #include "jemalloc/internal/jemalloc_internal.h" |
| 3 | |
| 4 | /******************************************************************************/ |
| 5 | /* Data. */ |
| 6 | |
| 7 | static unsigned ncleanups; |
| 8 | static malloc_tsd_cleanup_t cleanups[MALLOC_TSD_CLEANUPS_MAX]; |
| 9 | |
Jason Evans | 5460aa6 | 2014-09-22 21:09:23 -0700 | [diff] [blame] | 10 | malloc_tsd_data(, , tsd_t, TSD_INITIALIZER) |
| 11 | |
Jason Evans | cd9a134 | 2012-03-21 18:33:03 -0700 | [diff] [blame] | 12 | /******************************************************************************/ |
| 13 | |
| 14 | void * |
| 15 | malloc_tsd_malloc(size_t size) |
| 16 | { |
| 17 | |
Jason Evans | 4581b97 | 2014-11-27 17:22:36 -0200 | [diff] [blame] | 18 | return (a0malloc(CACHELINE_CEILING(size))); |
Jason Evans | cd9a134 | 2012-03-21 18:33:03 -0700 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | void |
| 22 | malloc_tsd_dalloc(void *wrapper) |
| 23 | { |
| 24 | |
Jason Evans | 10aff3f | 2015-01-20 15:37:51 -0800 | [diff] [blame] | 25 | a0dalloc(wrapper); |
Jason Evans | cd9a134 | 2012-03-21 18:33:03 -0700 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | void |
| 29 | malloc_tsd_no_cleanup(void *arg) |
| 30 | { |
| 31 | |
| 32 | not_reached(); |
| 33 | } |
| 34 | |
Mike Hommey | a19e87f | 2012-04-21 21:27:46 -0700 | [diff] [blame] | 35 | #if defined(JEMALLOC_MALLOC_THREAD_CLEANUP) || defined(_WIN32) |
Mike Hommey | da99e31 | 2012-04-30 12:38:29 +0200 | [diff] [blame] | 36 | #ifndef _WIN32 |
| 37 | JEMALLOC_EXPORT |
| 38 | #endif |
Jason Evans | cd9a134 | 2012-03-21 18:33:03 -0700 | [diff] [blame] | 39 | void |
| 40 | _malloc_thread_cleanup(void) |
| 41 | { |
Mike Hommey | 8b49971 | 2012-04-24 23:22:02 +0200 | [diff] [blame] | 42 | bool pending[MALLOC_TSD_CLEANUPS_MAX], again; |
Jason Evans | cd9a134 | 2012-03-21 18:33:03 -0700 | [diff] [blame] | 43 | unsigned i; |
| 44 | |
| 45 | for (i = 0; i < ncleanups; i++) |
| 46 | pending[i] = true; |
| 47 | |
| 48 | do { |
| 49 | again = false; |
| 50 | for (i = 0; i < ncleanups; i++) { |
| 51 | if (pending[i]) { |
Mike Hommey | 13067ec | 2012-04-18 18:29:49 +0200 | [diff] [blame] | 52 | pending[i] = cleanups[i](); |
Jason Evans | cd9a134 | 2012-03-21 18:33:03 -0700 | [diff] [blame] | 53 | if (pending[i]) |
| 54 | again = true; |
| 55 | } |
| 56 | } |
| 57 | } while (again); |
| 58 | } |
| 59 | #endif |
| 60 | |
| 61 | void |
Mike Hommey | 13067ec | 2012-04-18 18:29:49 +0200 | [diff] [blame] | 62 | malloc_tsd_cleanup_register(bool (*f)(void)) |
Jason Evans | cd9a134 | 2012-03-21 18:33:03 -0700 | [diff] [blame] | 63 | { |
| 64 | |
| 65 | assert(ncleanups < MALLOC_TSD_CLEANUPS_MAX); |
Mike Hommey | 13067ec | 2012-04-18 18:29:49 +0200 | [diff] [blame] | 66 | cleanups[ncleanups] = f; |
Jason Evans | cd9a134 | 2012-03-21 18:33:03 -0700 | [diff] [blame] | 67 | ncleanups++; |
| 68 | } |
| 69 | |
| 70 | void |
Jason Evans | 5460aa6 | 2014-09-22 21:09:23 -0700 | [diff] [blame] | 71 | tsd_cleanup(void *arg) |
| 72 | { |
| 73 | tsd_t *tsd = (tsd_t *)arg; |
| 74 | |
Jason Evans | 5460aa6 | 2014-09-22 21:09:23 -0700 | [diff] [blame] | 75 | switch (tsd->state) { |
Jason Evans | d36c7eb | 2015-09-24 16:53:18 -0700 | [diff] [blame] | 76 | case tsd_state_uninitialized: |
| 77 | /* Do nothing. */ |
| 78 | break; |
Jason Evans | 5460aa6 | 2014-09-22 21:09:23 -0700 | [diff] [blame] | 79 | case tsd_state_nominal: |
| 80 | #define O(n, t) \ |
| 81 | n##_cleanup(tsd); |
| 82 | MALLOC_TSD |
| 83 | #undef O |
| 84 | tsd->state = tsd_state_purgatory; |
| 85 | tsd_set(tsd); |
| 86 | break; |
| 87 | case tsd_state_purgatory: |
| 88 | /* |
| 89 | * The previous time this destructor was called, we set the |
| 90 | * state to tsd_state_purgatory so that other destructors |
| 91 | * wouldn't cause re-creation of the tsd. This time, do |
| 92 | * nothing, and do not request another callback. |
| 93 | */ |
| 94 | break; |
| 95 | case tsd_state_reincarnated: |
| 96 | /* |
| 97 | * Another destructor deallocated memory after this destructor |
| 98 | * was called. Reset state to tsd_state_purgatory and request |
| 99 | * another callback. |
| 100 | */ |
| 101 | tsd->state = tsd_state_purgatory; |
| 102 | tsd_set(tsd); |
| 103 | break; |
| 104 | default: |
| 105 | not_reached(); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | bool |
Jason Evans | 8bb3198 | 2014-10-07 23:14:57 -0700 | [diff] [blame] | 110 | malloc_tsd_boot0(void) |
Jason Evans | cd9a134 | 2012-03-21 18:33:03 -0700 | [diff] [blame] | 111 | { |
| 112 | |
| 113 | ncleanups = 0; |
Jason Evans | 8bb3198 | 2014-10-07 23:14:57 -0700 | [diff] [blame] | 114 | if (tsd_boot0()) |
Jason Evans | 5460aa6 | 2014-09-22 21:09:23 -0700 | [diff] [blame] | 115 | return (true); |
Jason Evans | db927b6 | 2016-02-19 19:37:10 -0800 | [diff] [blame] | 116 | *tsd_arenas_tdata_bypassp_get(tsd_fetch()) = true; |
Jason Evans | 5460aa6 | 2014-09-22 21:09:23 -0700 | [diff] [blame] | 117 | return (false); |
Jason Evans | cd9a134 | 2012-03-21 18:33:03 -0700 | [diff] [blame] | 118 | } |
Mike Hommey | a19e87f | 2012-04-21 21:27:46 -0700 | [diff] [blame] | 119 | |
Jason Evans | 8bb3198 | 2014-10-07 23:14:57 -0700 | [diff] [blame] | 120 | void |
| 121 | malloc_tsd_boot1(void) |
| 122 | { |
| 123 | |
| 124 | tsd_boot1(); |
Jason Evans | db927b6 | 2016-02-19 19:37:10 -0800 | [diff] [blame] | 125 | *tsd_arenas_tdata_bypassp_get(tsd_fetch()) = false; |
Jason Evans | 8bb3198 | 2014-10-07 23:14:57 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Mike Hommey | a19e87f | 2012-04-21 21:27:46 -0700 | [diff] [blame] | 128 | #ifdef _WIN32 |
| 129 | static BOOL WINAPI |
| 130 | _tls_callback(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) |
| 131 | { |
| 132 | |
| 133 | switch (fdwReason) { |
| 134 | #ifdef JEMALLOC_LAZY_LOCK |
| 135 | case DLL_THREAD_ATTACH: |
| 136 | isthreaded = true; |
| 137 | break; |
| 138 | #endif |
| 139 | case DLL_THREAD_DETACH: |
| 140 | _malloc_thread_cleanup(); |
| 141 | break; |
| 142 | default: |
| 143 | break; |
| 144 | } |
| 145 | return (true); |
| 146 | } |
| 147 | |
Mike Hommey | fd97b1d | 2012-04-30 12:38:31 +0200 | [diff] [blame] | 148 | #ifdef _MSC_VER |
| 149 | # ifdef _M_IX86 |
| 150 | # pragma comment(linker, "/INCLUDE:__tls_used") |
rustyx | efbee86 | 2016-02-02 11:27:18 +0100 | [diff] [blame] | 151 | # pragma comment(linker, "/INCLUDE:_tls_callback") |
Mike Hommey | fd97b1d | 2012-04-30 12:38:31 +0200 | [diff] [blame] | 152 | # else |
| 153 | # pragma comment(linker, "/INCLUDE:_tls_used") |
rustyx | efbee86 | 2016-02-02 11:27:18 +0100 | [diff] [blame] | 154 | # pragma comment(linker, "/INCLUDE:tls_callback") |
Mike Hommey | fd97b1d | 2012-04-30 12:38:31 +0200 | [diff] [blame] | 155 | # endif |
| 156 | # pragma section(".CRT$XLY",long,read) |
| 157 | #endif |
Mike Hommey | da99e31 | 2012-04-30 12:38:29 +0200 | [diff] [blame] | 158 | JEMALLOC_SECTION(".CRT$XLY") JEMALLOC_ATTR(used) |
rustyx | efbee86 | 2016-02-02 11:27:18 +0100 | [diff] [blame] | 159 | BOOL (WINAPI *const tls_callback)(HINSTANCE hinstDLL, |
Jason Evans | d508ec7 | 2015-07-07 20:28:22 -0700 | [diff] [blame] | 160 | DWORD fdwReason, LPVOID lpvReserved) = _tls_callback; |
Mike Hommey | a19e87f | 2012-04-21 21:27:46 -0700 | [diff] [blame] | 161 | #endif |
Leonard Crestez | cb17fc6 | 2013-10-22 00:12:16 +0300 | [diff] [blame] | 162 | |
| 163 | #if (!defined(JEMALLOC_MALLOC_THREAD_CLEANUP) && !defined(JEMALLOC_TLS) && \ |
| 164 | !defined(_WIN32)) |
| 165 | void * |
| 166 | tsd_init_check_recursion(tsd_init_head_t *head, tsd_init_block_t *block) |
| 167 | { |
| 168 | pthread_t self = pthread_self(); |
| 169 | tsd_init_block_t *iter; |
| 170 | |
| 171 | /* Check whether this thread has already inserted into the list. */ |
| 172 | malloc_mutex_lock(&head->lock); |
| 173 | ql_foreach(iter, &head->blocks, link) { |
| 174 | if (iter->thread == self) { |
| 175 | malloc_mutex_unlock(&head->lock); |
| 176 | return (iter->data); |
| 177 | } |
| 178 | } |
| 179 | /* Insert block into list. */ |
| 180 | ql_elm_new(block, link); |
| 181 | block->thread = self; |
| 182 | ql_tail_insert(&head->blocks, block, link); |
| 183 | malloc_mutex_unlock(&head->lock); |
| 184 | return (NULL); |
| 185 | } |
| 186 | |
| 187 | void |
| 188 | tsd_init_finish(tsd_init_head_t *head, tsd_init_block_t *block) |
| 189 | { |
| 190 | |
| 191 | malloc_mutex_lock(&head->lock); |
| 192 | ql_remove(&head->blocks, block, link); |
| 193 | malloc_mutex_unlock(&head->lock); |
| 194 | } |
| 195 | #endif |