Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1 | /* |
Jonathan Peyton | de4749b | 2016-12-14 23:01:24 +0000 | [diff] [blame] | 2 | * kmp_threadprivate.cpp -- OpenMP threadprivate support library |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3 | */ |
| 4 | |
| 5 | |
| 6 | //===----------------------------------------------------------------------===// |
| 7 | // |
| 8 | // The LLVM Compiler Infrastructure |
| 9 | // |
| 10 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 11 | // Source Licenses. See LICENSE.txt for details. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | |
| 16 | #include "kmp.h" |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 17 | #include "kmp_i18n.h" |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 18 | #include "kmp_itt.h" |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 19 | |
| 20 | #define USE_CHECKS_COMMON |
| 21 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 22 | #define KMP_INLINE_SUBR 1 |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 23 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 24 | void kmp_threadprivate_insert_private_data(int gtid, void *pc_addr, |
| 25 | void *data_addr, size_t pc_size); |
| 26 | struct private_common *kmp_threadprivate_insert(int gtid, void *pc_addr, |
| 27 | void *data_addr, |
| 28 | size_t pc_size); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 29 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 30 | struct shared_table __kmp_threadprivate_d_table; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 31 | |
| 32 | static |
| 33 | #ifdef KMP_INLINE_SUBR |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 34 | __forceinline |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 35 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 36 | struct private_common * |
| 37 | __kmp_threadprivate_find_task_common(struct common_table *tbl, int gtid, |
| 38 | void *pc_addr) |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 39 | |
| 40 | { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 41 | struct private_common *tn; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 42 | |
| 43 | #ifdef KMP_TASK_COMMON_DEBUG |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 44 | KC_TRACE(10, ("__kmp_threadprivate_find_task_common: thread#%d, called with " |
| 45 | "address %p\n", |
| 46 | gtid, pc_addr)); |
| 47 | dump_list(); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 48 | #endif |
| 49 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 50 | for (tn = tbl->data[KMP_HASH(pc_addr)]; tn; tn = tn->next) { |
| 51 | if (tn->gbl_addr == pc_addr) { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 52 | #ifdef KMP_TASK_COMMON_DEBUG |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 53 | KC_TRACE(10, ("__kmp_threadprivate_find_task_common: thread#%d, found " |
| 54 | "node %p on list\n", |
| 55 | gtid, pc_addr)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 56 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 57 | return tn; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 58 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 59 | } |
| 60 | return 0; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | static |
| 64 | #ifdef KMP_INLINE_SUBR |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 65 | __forceinline |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 66 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 67 | struct shared_common * |
| 68 | __kmp_find_shared_task_common(struct shared_table *tbl, int gtid, |
| 69 | void *pc_addr) { |
| 70 | struct shared_common *tn; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 71 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 72 | for (tn = tbl->data[KMP_HASH(pc_addr)]; tn; tn = tn->next) { |
| 73 | if (tn->gbl_addr == pc_addr) { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 74 | #ifdef KMP_TASK_COMMON_DEBUG |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 75 | KC_TRACE( |
| 76 | 10, |
| 77 | ("__kmp_find_shared_task_common: thread#%d, found node %p on list\n", |
| 78 | gtid, pc_addr)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 79 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 80 | return tn; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 81 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 82 | } |
| 83 | return 0; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 86 | // Create a template for the data initialized storage. Either the template is |
| 87 | // NULL indicating zero fill, or the template is a copy of the original data. |
| 88 | static struct private_data *__kmp_init_common_data(void *pc_addr, |
| 89 | size_t pc_size) { |
| 90 | struct private_data *d; |
| 91 | size_t i; |
| 92 | char *p; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 93 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 94 | d = (struct private_data *)__kmp_allocate(sizeof(struct private_data)); |
| 95 | /* |
| 96 | d->data = 0; // AC: commented out because __kmp_allocate zeroes the |
| 97 | memory |
| 98 | d->next = 0; |
| 99 | */ |
| 100 | d->size = pc_size; |
| 101 | d->more = 1; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 102 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 103 | p = (char *)pc_addr; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 104 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 105 | for (i = pc_size; i > 0; --i) { |
| 106 | if (*p++ != '\0') { |
| 107 | d->data = __kmp_allocate(pc_size); |
| 108 | KMP_MEMCPY(d->data, pc_addr, pc_size); |
| 109 | break; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 110 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 111 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 112 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 113 | return d; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 116 | // Initialize the data area from the template. |
| 117 | static void __kmp_copy_common_data(void *pc_addr, struct private_data *d) { |
| 118 | char *addr = (char *)pc_addr; |
| 119 | int i, offset; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 120 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 121 | for (offset = 0; d != 0; d = d->next) { |
| 122 | for (i = d->more; i > 0; --i) { |
| 123 | if (d->data == 0) |
| 124 | memset(&addr[offset], '\0', d->size); |
| 125 | else |
| 126 | KMP_MEMCPY(&addr[offset], d->data, d->size); |
| 127 | offset += d->size; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 128 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 129 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 132 | /* we are called from __kmp_serial_initialize() with __kmp_initz_lock held. */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 133 | void __kmp_common_initialize(void) { |
| 134 | if (!TCR_4(__kmp_init_common)) { |
| 135 | int q; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 136 | #ifdef KMP_DEBUG |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 137 | int gtid; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 138 | #endif |
| 139 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 140 | __kmp_threadpriv_cache_list = NULL; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 141 | |
| 142 | #ifdef KMP_DEBUG |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 143 | /* verify the uber masters were initialized */ |
| 144 | for (gtid = 0; gtid < __kmp_threads_capacity; gtid++) |
| 145 | if (__kmp_root[gtid]) { |
| 146 | KMP_DEBUG_ASSERT(__kmp_root[gtid]->r.r_uber_thread); |
| 147 | for (q = 0; q < KMP_HASH_TABLE_SIZE; ++q) |
| 148 | KMP_DEBUG_ASSERT( |
| 149 | !__kmp_root[gtid]->r.r_uber_thread->th.th_pri_common->data[q]); |
| 150 | /* __kmp_root[ gitd ]-> r.r_uber_thread -> |
| 151 | * th.th_pri_common -> data[ q ] = 0;*/ |
| 152 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 153 | #endif /* KMP_DEBUG */ |
| 154 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 155 | for (q = 0; q < KMP_HASH_TABLE_SIZE; ++q) |
| 156 | __kmp_threadprivate_d_table.data[q] = 0; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 157 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 158 | TCW_4(__kmp_init_common, TRUE); |
| 159 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /* Call all destructors for threadprivate data belonging to all threads. |
| 163 | Currently unused! */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 164 | void __kmp_common_destroy(void) { |
| 165 | if (TCR_4(__kmp_init_common)) { |
| 166 | int q; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 167 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 168 | TCW_4(__kmp_init_common, FALSE); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 169 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 170 | for (q = 0; q < KMP_HASH_TABLE_SIZE; ++q) { |
| 171 | int gtid; |
| 172 | struct private_common *tn; |
| 173 | struct shared_common *d_tn; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 174 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 175 | /* C++ destructors need to be called once per thread before exiting. |
| 176 | Don't call destructors for master thread though unless we used copy |
| 177 | constructor */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 178 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 179 | for (d_tn = __kmp_threadprivate_d_table.data[q]; d_tn; |
| 180 | d_tn = d_tn->next) { |
| 181 | if (d_tn->is_vec) { |
| 182 | if (d_tn->dt.dtorv != 0) { |
| 183 | for (gtid = 0; gtid < __kmp_all_nth; ++gtid) { |
| 184 | if (__kmp_threads[gtid]) { |
| 185 | if ((__kmp_foreign_tp) ? (!KMP_INITIAL_GTID(gtid)) |
| 186 | : (!KMP_UBER_GTID(gtid))) { |
| 187 | tn = __kmp_threadprivate_find_task_common( |
| 188 | __kmp_threads[gtid]->th.th_pri_common, gtid, |
| 189 | d_tn->gbl_addr); |
| 190 | if (tn) { |
| 191 | (*d_tn->dt.dtorv)(tn->par_addr, d_tn->vec_len); |
| 192 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 193 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 194 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 195 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 196 | if (d_tn->obj_init != 0) { |
| 197 | (*d_tn->dt.dtorv)(d_tn->obj_init, d_tn->vec_len); |
| 198 | } |
| 199 | } |
| 200 | } else { |
| 201 | if (d_tn->dt.dtor != 0) { |
| 202 | for (gtid = 0; gtid < __kmp_all_nth; ++gtid) { |
| 203 | if (__kmp_threads[gtid]) { |
| 204 | if ((__kmp_foreign_tp) ? (!KMP_INITIAL_GTID(gtid)) |
| 205 | : (!KMP_UBER_GTID(gtid))) { |
| 206 | tn = __kmp_threadprivate_find_task_common( |
| 207 | __kmp_threads[gtid]->th.th_pri_common, gtid, |
| 208 | d_tn->gbl_addr); |
| 209 | if (tn) { |
| 210 | (*d_tn->dt.dtor)(tn->par_addr); |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | if (d_tn->obj_init != 0) { |
| 216 | (*d_tn->dt.dtor)(d_tn->obj_init); |
| 217 | } |
| 218 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 219 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 220 | } |
| 221 | __kmp_threadprivate_d_table.data[q] = 0; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 222 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 223 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | /* Call all destructors for threadprivate data belonging to this thread */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 227 | void __kmp_common_destroy_gtid(int gtid) { |
| 228 | struct private_common *tn; |
| 229 | struct shared_common *d_tn; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 230 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 231 | KC_TRACE(10, ("__kmp_common_destroy_gtid: T#%d called\n", gtid)); |
| 232 | if ((__kmp_foreign_tp) ? (!KMP_INITIAL_GTID(gtid)) : (!KMP_UBER_GTID(gtid))) { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 233 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 234 | if (TCR_4(__kmp_init_common)) { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 235 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 236 | /* Cannot do this here since not all threads have destroyed their data */ |
| 237 | /* TCW_4(__kmp_init_common, FALSE); */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 238 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 239 | for (tn = __kmp_threads[gtid]->th.th_pri_head; tn; tn = tn->link) { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 240 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 241 | d_tn = __kmp_find_shared_task_common(&__kmp_threadprivate_d_table, gtid, |
| 242 | tn->gbl_addr); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 243 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 244 | KMP_DEBUG_ASSERT(d_tn); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 245 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 246 | if (d_tn->is_vec) { |
| 247 | if (d_tn->dt.dtorv != 0) { |
| 248 | (void)(*d_tn->dt.dtorv)(tn->par_addr, d_tn->vec_len); |
| 249 | } |
| 250 | if (d_tn->obj_init != 0) { |
| 251 | (void)(*d_tn->dt.dtorv)(d_tn->obj_init, d_tn->vec_len); |
| 252 | } |
| 253 | } else { |
| 254 | if (d_tn->dt.dtor != 0) { |
| 255 | (void)(*d_tn->dt.dtor)(tn->par_addr); |
| 256 | } |
| 257 | if (d_tn->obj_init != 0) { |
| 258 | (void)(*d_tn->dt.dtor)(d_tn->obj_init); |
| 259 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 260 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 261 | } |
| 262 | KC_TRACE(30, ("__kmp_common_destroy_gtid: T#%d threadprivate destructors " |
| 263 | "complete\n", |
| 264 | gtid)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 265 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 266 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 269 | #ifdef KMP_TASK_COMMON_DEBUG |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 270 | static void dump_list(void) { |
| 271 | int p, q; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 272 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 273 | for (p = 0; p < __kmp_all_nth; ++p) { |
| 274 | if (!__kmp_threads[p]) |
| 275 | continue; |
| 276 | for (q = 0; q < KMP_HASH_TABLE_SIZE; ++q) { |
| 277 | if (__kmp_threads[p]->th.th_pri_common->data[q]) { |
| 278 | struct private_common *tn; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 279 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 280 | KC_TRACE(10, ("\tdump_list: gtid:%d addresses\n", p)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 281 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 282 | for (tn = __kmp_threads[p]->th.th_pri_common->data[q]; tn; |
| 283 | tn = tn->next) { |
| 284 | KC_TRACE(10, |
| 285 | ("\tdump_list: THREADPRIVATE: Serial %p -> Parallel %p\n", |
| 286 | tn->gbl_addr, tn->par_addr)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 287 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 288 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 289 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 290 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 291 | } |
| 292 | #endif /* KMP_TASK_COMMON_DEBUG */ |
| 293 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 294 | // NOTE: this routine is to be called only from the serial part of the program. |
| 295 | void kmp_threadprivate_insert_private_data(int gtid, void *pc_addr, |
| 296 | void *data_addr, size_t pc_size) { |
| 297 | struct shared_common **lnk_tn, *d_tn; |
| 298 | KMP_DEBUG_ASSERT(__kmp_threads[gtid] && |
| 299 | __kmp_threads[gtid]->th.th_root->r.r_active == 0); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 300 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 301 | d_tn = __kmp_find_shared_task_common(&__kmp_threadprivate_d_table, gtid, |
| 302 | pc_addr); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 303 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 304 | if (d_tn == 0) { |
| 305 | d_tn = (struct shared_common *)__kmp_allocate(sizeof(struct shared_common)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 306 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 307 | d_tn->gbl_addr = pc_addr; |
| 308 | d_tn->pod_init = __kmp_init_common_data(data_addr, pc_size); |
| 309 | /* |
| 310 | d_tn->obj_init = 0; // AC: commented out because __kmp_allocate |
| 311 | zeroes the memory |
| 312 | d_tn->ct.ctor = 0; |
| 313 | d_tn->cct.cctor = 0;; |
| 314 | d_tn->dt.dtor = 0; |
| 315 | d_tn->is_vec = FALSE; |
| 316 | d_tn->vec_len = 0L; |
| 317 | */ |
| 318 | d_tn->cmn_size = pc_size; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 319 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 320 | __kmp_acquire_lock(&__kmp_global_lock, gtid); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 321 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 322 | lnk_tn = &(__kmp_threadprivate_d_table.data[KMP_HASH(pc_addr)]); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 323 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 324 | d_tn->next = *lnk_tn; |
| 325 | *lnk_tn = d_tn; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 326 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 327 | __kmp_release_lock(&__kmp_global_lock, gtid); |
| 328 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 331 | struct private_common *kmp_threadprivate_insert(int gtid, void *pc_addr, |
| 332 | void *data_addr, |
| 333 | size_t pc_size) { |
| 334 | struct private_common *tn, **tt; |
| 335 | struct shared_common *d_tn; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 336 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 337 | /* +++++++++ START OF CRITICAL SECTION +++++++++ */ |
| 338 | __kmp_acquire_lock(&__kmp_global_lock, gtid); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 339 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 340 | tn = (struct private_common *)__kmp_allocate(sizeof(struct private_common)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 341 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 342 | tn->gbl_addr = pc_addr; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 343 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 344 | d_tn = __kmp_find_shared_task_common( |
| 345 | &__kmp_threadprivate_d_table, gtid, |
| 346 | pc_addr); /* Only the MASTER data table exists. */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 347 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 348 | if (d_tn != 0) { |
| 349 | /* This threadprivate variable has already been seen. */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 350 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 351 | if (d_tn->pod_init == 0 && d_tn->obj_init == 0) { |
| 352 | d_tn->cmn_size = pc_size; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 353 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 354 | if (d_tn->is_vec) { |
| 355 | if (d_tn->ct.ctorv != 0) { |
| 356 | /* Construct from scratch so no prototype exists */ |
| 357 | d_tn->obj_init = 0; |
| 358 | } else if (d_tn->cct.cctorv != 0) { |
| 359 | /* Now data initialize the prototype since it was previously |
| 360 | * registered */ |
| 361 | d_tn->obj_init = (void *)__kmp_allocate(d_tn->cmn_size); |
| 362 | (void)(*d_tn->cct.cctorv)(d_tn->obj_init, pc_addr, d_tn->vec_len); |
| 363 | } else { |
| 364 | d_tn->pod_init = __kmp_init_common_data(data_addr, d_tn->cmn_size); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 365 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 366 | } else { |
| 367 | if (d_tn->ct.ctor != 0) { |
| 368 | /* Construct from scratch so no prototype exists */ |
| 369 | d_tn->obj_init = 0; |
| 370 | } else if (d_tn->cct.cctor != 0) { |
| 371 | /* Now data initialize the prototype since it was previously |
| 372 | registered */ |
| 373 | d_tn->obj_init = (void *)__kmp_allocate(d_tn->cmn_size); |
| 374 | (void)(*d_tn->cct.cctor)(d_tn->obj_init, pc_addr); |
| 375 | } else { |
| 376 | d_tn->pod_init = __kmp_init_common_data(data_addr, d_tn->cmn_size); |
| 377 | } |
| 378 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 379 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 380 | } else { |
| 381 | struct shared_common **lnk_tn; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 382 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 383 | d_tn = (struct shared_common *)__kmp_allocate(sizeof(struct shared_common)); |
| 384 | d_tn->gbl_addr = pc_addr; |
| 385 | d_tn->cmn_size = pc_size; |
| 386 | d_tn->pod_init = __kmp_init_common_data(data_addr, pc_size); |
| 387 | /* |
| 388 | d_tn->obj_init = 0; // AC: commented out because __kmp_allocate |
| 389 | zeroes the memory |
| 390 | d_tn->ct.ctor = 0; |
| 391 | d_tn->cct.cctor = 0; |
| 392 | d_tn->dt.dtor = 0; |
| 393 | d_tn->is_vec = FALSE; |
| 394 | d_tn->vec_len = 0L; |
| 395 | */ |
| 396 | lnk_tn = &(__kmp_threadprivate_d_table.data[KMP_HASH(pc_addr)]); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 397 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 398 | d_tn->next = *lnk_tn; |
| 399 | *lnk_tn = d_tn; |
| 400 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 401 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 402 | tn->cmn_size = d_tn->cmn_size; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 403 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 404 | if ((__kmp_foreign_tp) ? (KMP_INITIAL_GTID(gtid)) : (KMP_UBER_GTID(gtid))) { |
| 405 | tn->par_addr = (void *)pc_addr; |
| 406 | } else { |
| 407 | tn->par_addr = (void *)__kmp_allocate(tn->cmn_size); |
| 408 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 409 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 410 | __kmp_release_lock(&__kmp_global_lock, gtid); |
| 411 | /* +++++++++ END OF CRITICAL SECTION +++++++++ */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 412 | |
| 413 | #ifdef USE_CHECKS_COMMON |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 414 | if (pc_size > d_tn->cmn_size) { |
| 415 | KC_TRACE( |
| 416 | 10, ("__kmp_threadprivate_insert: THREADPRIVATE: %p (%" KMP_UINTPTR_SPEC |
| 417 | " ,%" KMP_UINTPTR_SPEC ")\n", |
| 418 | pc_addr, pc_size, d_tn->cmn_size)); |
| 419 | KMP_FATAL(TPCommonBlocksInconsist); |
| 420 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 421 | #endif /* USE_CHECKS_COMMON */ |
| 422 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 423 | tt = &(__kmp_threads[gtid]->th.th_pri_common->data[KMP_HASH(pc_addr)]); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 424 | |
| 425 | #ifdef KMP_TASK_COMMON_DEBUG |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 426 | if (*tt != 0) { |
| 427 | KC_TRACE( |
| 428 | 10, |
| 429 | ("__kmp_threadprivate_insert: WARNING! thread#%d: collision on %p\n", |
| 430 | gtid, pc_addr)); |
| 431 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 432 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 433 | tn->next = *tt; |
| 434 | *tt = tn; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 435 | |
| 436 | #ifdef KMP_TASK_COMMON_DEBUG |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 437 | KC_TRACE(10, |
| 438 | ("__kmp_threadprivate_insert: thread#%d, inserted node %p on list\n", |
| 439 | gtid, pc_addr)); |
| 440 | dump_list(); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 441 | #endif |
| 442 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 443 | /* Link the node into a simple list */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 444 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 445 | tn->link = __kmp_threads[gtid]->th.th_pri_head; |
| 446 | __kmp_threads[gtid]->th.th_pri_head = tn; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 447 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 448 | if ((__kmp_foreign_tp) ? (KMP_INITIAL_GTID(gtid)) : (KMP_UBER_GTID(gtid))) |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 449 | return tn; |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 450 | |
| 451 | /* if C++ object with copy constructor, use it; |
| 452 | * else if C++ object with constructor, use it for the non-master copies only; |
| 453 | * else use pod_init and memcpy |
| 454 | * |
| 455 | * C++ constructors need to be called once for each non-master thread on |
| 456 | * allocate |
| 457 | * C++ copy constructors need to be called once for each thread on allocate */ |
| 458 | |
| 459 | /* C++ object with constructors/destructors; don't call constructors for |
| 460 | master thread though */ |
| 461 | if (d_tn->is_vec) { |
| 462 | if (d_tn->ct.ctorv != 0) { |
| 463 | (void)(*d_tn->ct.ctorv)(tn->par_addr, d_tn->vec_len); |
| 464 | } else if (d_tn->cct.cctorv != 0) { |
| 465 | (void)(*d_tn->cct.cctorv)(tn->par_addr, d_tn->obj_init, d_tn->vec_len); |
| 466 | } else if (tn->par_addr != tn->gbl_addr) { |
| 467 | __kmp_copy_common_data(tn->par_addr, d_tn->pod_init); |
| 468 | } |
| 469 | } else { |
| 470 | if (d_tn->ct.ctor != 0) { |
| 471 | (void)(*d_tn->ct.ctor)(tn->par_addr); |
| 472 | } else if (d_tn->cct.cctor != 0) { |
| 473 | (void)(*d_tn->cct.cctor)(tn->par_addr, d_tn->obj_init); |
| 474 | } else if (tn->par_addr != tn->gbl_addr) { |
| 475 | __kmp_copy_common_data(tn->par_addr, d_tn->pod_init); |
| 476 | } |
| 477 | } |
| 478 | /* !BUILD_OPENMP_C |
| 479 | if (tn->par_addr != tn->gbl_addr) |
| 480 | __kmp_copy_common_data( tn->par_addr, d_tn->pod_init ); */ |
| 481 | |
| 482 | return tn; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | /* ------------------------------------------------------------------------ */ |
| 486 | /* We are currently parallel, and we know the thread id. */ |
| 487 | /* ------------------------------------------------------------------------ */ |
| 488 | |
| 489 | /*! |
| 490 | @ingroup THREADPRIVATE |
| 491 | |
Jonathan Peyton | 6111849 | 2016-05-20 19:03:38 +0000 | [diff] [blame] | 492 | @param loc source location information |
| 493 | @param data pointer to data being privatized |
| 494 | @param ctor pointer to constructor function for data |
| 495 | @param cctor pointer to copy constructor function for data |
| 496 | @param dtor pointer to destructor function for data |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 497 | |
| 498 | Register constructors and destructors for thread private data. |
| 499 | This function is called when executing in parallel, when we know the thread id. |
| 500 | */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 501 | void __kmpc_threadprivate_register(ident_t *loc, void *data, kmpc_ctor ctor, |
| 502 | kmpc_cctor cctor, kmpc_dtor dtor) { |
| 503 | struct shared_common *d_tn, **lnk_tn; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 504 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 505 | KC_TRACE(10, ("__kmpc_threadprivate_register: called\n")); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 506 | |
| 507 | #ifdef USE_CHECKS_COMMON |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 508 | /* copy constructor must be zero for current code gen (Nov 2002 - jph) */ |
| 509 | KMP_ASSERT(cctor == 0); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 510 | #endif /* USE_CHECKS_COMMON */ |
| 511 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 512 | /* Only the global data table exists. */ |
| 513 | d_tn = __kmp_find_shared_task_common(&__kmp_threadprivate_d_table, -1, data); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 514 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 515 | if (d_tn == 0) { |
| 516 | d_tn = (struct shared_common *)__kmp_allocate(sizeof(struct shared_common)); |
| 517 | d_tn->gbl_addr = data; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 518 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 519 | d_tn->ct.ctor = ctor; |
| 520 | d_tn->cct.cctor = cctor; |
| 521 | d_tn->dt.dtor = dtor; |
| 522 | /* |
| 523 | d_tn->is_vec = FALSE; // AC: commented out because __kmp_allocate |
| 524 | zeroes the memory |
| 525 | d_tn->vec_len = 0L; |
| 526 | d_tn->obj_init = 0; |
| 527 | d_tn->pod_init = 0; |
| 528 | */ |
| 529 | lnk_tn = &(__kmp_threadprivate_d_table.data[KMP_HASH(data)]); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 530 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 531 | d_tn->next = *lnk_tn; |
| 532 | *lnk_tn = d_tn; |
| 533 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 534 | } |
| 535 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 536 | void *__kmpc_threadprivate(ident_t *loc, kmp_int32 global_tid, void *data, |
| 537 | size_t size) { |
| 538 | void *ret; |
| 539 | struct private_common *tn; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 540 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 541 | KC_TRACE(10, ("__kmpc_threadprivate: T#%d called\n", global_tid)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 542 | |
| 543 | #ifdef USE_CHECKS_COMMON |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 544 | if (!__kmp_init_serial) |
| 545 | KMP_FATAL(RTLNotInitialized); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 546 | #endif /* USE_CHECKS_COMMON */ |
| 547 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 548 | if (!__kmp_threads[global_tid]->th.th_root->r.r_active && !__kmp_foreign_tp) { |
| 549 | /* The parallel address will NEVER overlap with the data_address */ |
| 550 | /* dkp: 3rd arg to kmp_threadprivate_insert_private_data() is the |
| 551 | * data_address; use data_address = data */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 552 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 553 | KC_TRACE(20, ("__kmpc_threadprivate: T#%d inserting private data\n", |
| 554 | global_tid)); |
| 555 | kmp_threadprivate_insert_private_data(global_tid, data, data, size); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 556 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 557 | ret = data; |
| 558 | } else { |
| 559 | KC_TRACE( |
| 560 | 50, |
| 561 | ("__kmpc_threadprivate: T#%d try to find private data at address %p\n", |
| 562 | global_tid, data)); |
| 563 | tn = __kmp_threadprivate_find_task_common( |
| 564 | __kmp_threads[global_tid]->th.th_pri_common, global_tid, data); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 565 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 566 | if (tn) { |
| 567 | KC_TRACE(20, ("__kmpc_threadprivate: T#%d found data\n", global_tid)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 568 | #ifdef USE_CHECKS_COMMON |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 569 | if ((size_t)size > tn->cmn_size) { |
| 570 | KC_TRACE(10, ("THREADPRIVATE: %p (%" KMP_UINTPTR_SPEC |
| 571 | " ,%" KMP_UINTPTR_SPEC ")\n", |
| 572 | data, size, tn->cmn_size)); |
| 573 | KMP_FATAL(TPCommonBlocksInconsist); |
| 574 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 575 | #endif /* USE_CHECKS_COMMON */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 576 | } else { |
| 577 | /* The parallel address will NEVER overlap with the data_address */ |
| 578 | /* dkp: 3rd arg to kmp_threadprivate_insert() is the data_address; use |
| 579 | * data_address = data */ |
| 580 | KC_TRACE(20, ("__kmpc_threadprivate: T#%d inserting data\n", global_tid)); |
| 581 | tn = kmp_threadprivate_insert(global_tid, data, data, size); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 582 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 583 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 584 | ret = tn->par_addr; |
| 585 | } |
| 586 | KC_TRACE(10, ("__kmpc_threadprivate: T#%d exiting; return value = %p\n", |
| 587 | global_tid, ret)); |
| 588 | |
| 589 | return ret; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | /*! |
| 593 | @ingroup THREADPRIVATE |
Jonathan Peyton | 6111849 | 2016-05-20 19:03:38 +0000 | [diff] [blame] | 594 | @param loc source location information |
| 595 | @param global_tid global thread number |
| 596 | @param data pointer to data to privatize |
| 597 | @param size size of data to privatize |
| 598 | @param cache pointer to cache |
| 599 | @return pointer to private storage |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 600 | |
Jonathan Peyton | 6111849 | 2016-05-20 19:03:38 +0000 | [diff] [blame] | 601 | Allocate private storage for threadprivate data. |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 602 | */ |
| 603 | void * |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 604 | __kmpc_threadprivate_cached(ident_t *loc, |
| 605 | kmp_int32 global_tid, // gtid. |
| 606 | void *data, // Pointer to original global variable. |
| 607 | size_t size, // Size of original global variable. |
| 608 | void ***cache) { |
| 609 | KC_TRACE(10, ("__kmpc_threadprivate_cached: T#%d called with cache: %p, " |
| 610 | "address: %p, size: %" KMP_SIZE_T_SPEC "\n", |
| 611 | global_tid, *cache, data, size)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 612 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 613 | if (TCR_PTR(*cache) == 0) { |
| 614 | __kmp_acquire_lock(&__kmp_global_lock, global_tid); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 615 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 616 | if (TCR_PTR(*cache) == 0) { |
| 617 | __kmp_acquire_bootstrap_lock(&__kmp_tp_cached_lock); |
| 618 | __kmp_tp_cached = 1; |
| 619 | __kmp_release_bootstrap_lock(&__kmp_tp_cached_lock); |
| 620 | void **my_cache; |
| 621 | KMP_ITT_IGNORE( |
| 622 | my_cache = (void **)__kmp_allocate( |
| 623 | sizeof(void *) * __kmp_tp_capacity + sizeof(kmp_cached_addr_t));); |
| 624 | // No need to zero the allocated memory; __kmp_allocate does that. |
| 625 | KC_TRACE( |
| 626 | 50, |
| 627 | ("__kmpc_threadprivate_cached: T#%d allocated cache at address %p\n", |
| 628 | global_tid, my_cache)); |
Jonathan Peyton | 6111849 | 2016-05-20 19:03:38 +0000 | [diff] [blame] | 629 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 630 | /* TODO: free all this memory in __kmp_common_destroy using |
| 631 | * __kmp_threadpriv_cache_list */ |
| 632 | /* Add address of mycache to linked list for cleanup later */ |
| 633 | kmp_cached_addr_t *tp_cache_addr; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 634 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 635 | tp_cache_addr = (kmp_cached_addr_t *)&my_cache[__kmp_tp_capacity]; |
| 636 | tp_cache_addr->addr = my_cache; |
| 637 | tp_cache_addr->next = __kmp_threadpriv_cache_list; |
| 638 | __kmp_threadpriv_cache_list = tp_cache_addr; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 639 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 640 | KMP_MB(); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 641 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 642 | TCW_PTR(*cache, my_cache); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 643 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 644 | KMP_MB(); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 645 | } |
| 646 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 647 | __kmp_release_lock(&__kmp_global_lock, global_tid); |
| 648 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 649 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 650 | void *ret; |
| 651 | if ((ret = TCR_PTR((*cache)[global_tid])) == 0) { |
| 652 | ret = __kmpc_threadprivate(loc, global_tid, data, (size_t)size); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 653 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 654 | TCW_PTR((*cache)[global_tid], ret); |
| 655 | } |
| 656 | KC_TRACE(10, |
| 657 | ("__kmpc_threadprivate_cached: T#%d exiting; return value = %p\n", |
| 658 | global_tid, ret)); |
| 659 | |
| 660 | return ret; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | /*! |
| 664 | @ingroup THREADPRIVATE |
Jonathan Peyton | 6111849 | 2016-05-20 19:03:38 +0000 | [diff] [blame] | 665 | @param loc source location information |
| 666 | @param data pointer to data being privatized |
| 667 | @param ctor pointer to constructor function for data |
| 668 | @param cctor pointer to copy constructor function for data |
| 669 | @param dtor pointer to destructor function for data |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 670 | @param vector_length length of the vector (bytes or elements?) |
| 671 | Register vector constructors and destructors for thread private data. |
| 672 | */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 673 | void __kmpc_threadprivate_register_vec(ident_t *loc, void *data, |
| 674 | kmpc_ctor_vec ctor, kmpc_cctor_vec cctor, |
| 675 | kmpc_dtor_vec dtor, |
| 676 | size_t vector_length) { |
| 677 | struct shared_common *d_tn, **lnk_tn; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 678 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 679 | KC_TRACE(10, ("__kmpc_threadprivate_register_vec: called\n")); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 680 | |
| 681 | #ifdef USE_CHECKS_COMMON |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 682 | /* copy constructor must be zero for current code gen (Nov 2002 - jph) */ |
| 683 | KMP_ASSERT(cctor == 0); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 684 | #endif /* USE_CHECKS_COMMON */ |
| 685 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 686 | d_tn = __kmp_find_shared_task_common( |
| 687 | &__kmp_threadprivate_d_table, -1, |
| 688 | data); /* Only the global data table exists. */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 689 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 690 | if (d_tn == 0) { |
| 691 | d_tn = (struct shared_common *)__kmp_allocate(sizeof(struct shared_common)); |
| 692 | d_tn->gbl_addr = data; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 693 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 694 | d_tn->ct.ctorv = ctor; |
| 695 | d_tn->cct.cctorv = cctor; |
| 696 | d_tn->dt.dtorv = dtor; |
| 697 | d_tn->is_vec = TRUE; |
| 698 | d_tn->vec_len = (size_t)vector_length; |
| 699 | /* |
| 700 | d_tn->obj_init = 0; // AC: commented out because __kmp_allocate |
| 701 | zeroes the memory |
| 702 | d_tn->pod_init = 0; |
| 703 | */ |
| 704 | lnk_tn = &(__kmp_threadprivate_d_table.data[KMP_HASH(data)]); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 705 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 706 | d_tn->next = *lnk_tn; |
| 707 | *lnk_tn = d_tn; |
| 708 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 709 | } |