Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1 | #define JEMALLOC_WITNESS_C_ |
| 2 | #include "jemalloc/internal/jemalloc_internal.h" |
| 3 | |
| 4 | void |
| 5 | witness_init(witness_t *witness, const char *name, witness_rank_t rank, |
| 6 | witness_comp_t *comp) |
| 7 | { |
| 8 | |
| 9 | witness->name = name; |
| 10 | witness->rank = rank; |
| 11 | witness->comp = comp; |
| 12 | } |
| 13 | |
| 14 | #ifdef JEMALLOC_JET |
| 15 | #undef witness_lock_error |
| 16 | #define witness_lock_error JEMALLOC_N(witness_lock_error_impl) |
| 17 | #endif |
| 18 | static void |
| 19 | witness_lock_error(const witness_list_t *witnesses, const witness_t *witness) |
| 20 | { |
| 21 | witness_t *w; |
| 22 | |
| 23 | malloc_printf("<jemalloc>: Lock rank order reversal:"); |
| 24 | ql_foreach(w, witnesses, link) { |
| 25 | malloc_printf(" %s(%u)", w->name, w->rank); |
| 26 | } |
| 27 | malloc_printf(" %s(%u)\n", witness->name, witness->rank); |
| 28 | abort(); |
| 29 | } |
| 30 | #ifdef JEMALLOC_JET |
| 31 | #undef witness_lock_error |
| 32 | #define witness_lock_error JEMALLOC_N(witness_lock_error) |
| 33 | witness_lock_error_t *witness_lock_error = JEMALLOC_N(witness_lock_error_impl); |
| 34 | #endif |
| 35 | |
| 36 | void |
| 37 | witness_lock(tsd_t *tsd, witness_t *witness) |
| 38 | { |
| 39 | witness_list_t *witnesses; |
| 40 | witness_t *w; |
| 41 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 42 | if (tsd == NULL) |
| 43 | return; |
| 44 | if (witness->rank == WITNESS_RANK_OMIT) |
| 45 | return; |
| 46 | |
| 47 | witness_assert_not_owner(tsd, witness); |
| 48 | |
| 49 | witnesses = tsd_witnessesp_get(tsd); |
| 50 | w = ql_last(witnesses, link); |
Jason Evans | 174c0c3 | 2016-04-25 23:14:40 -0700 | [diff] [blame] | 51 | if (w == NULL) { |
| 52 | /* No other locks; do nothing. */ |
| 53 | } else if (tsd_witness_fork_get(tsd) && w->rank <= witness->rank) { |
| 54 | /* Forking, and relaxed ranking satisfied. */ |
| 55 | } else if (w->rank > witness->rank) { |
| 56 | /* Not forking, rank order reversal. */ |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 57 | witness_lock_error(witnesses, witness); |
Jason Evans | 174c0c3 | 2016-04-25 23:14:40 -0700 | [diff] [blame] | 58 | } else if (w->rank == witness->rank && (w->comp == NULL || w->comp != |
| 59 | witness->comp || w->comp(w, witness) > 0)) { |
| 60 | /* |
| 61 | * Missing/incompatible comparison function, or comparison |
| 62 | * function indicates rank order reversal. |
| 63 | */ |
| 64 | witness_lock_error(witnesses, witness); |
| 65 | } |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 66 | |
| 67 | ql_elm_new(witness, link); |
| 68 | ql_tail_insert(witnesses, witness, link); |
| 69 | } |
| 70 | |
| 71 | void |
| 72 | witness_unlock(tsd_t *tsd, witness_t *witness) |
| 73 | { |
| 74 | witness_list_t *witnesses; |
| 75 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 76 | if (tsd == NULL) |
| 77 | return; |
| 78 | if (witness->rank == WITNESS_RANK_OMIT) |
| 79 | return; |
| 80 | |
| 81 | witness_assert_owner(tsd, witness); |
| 82 | |
| 83 | witnesses = tsd_witnessesp_get(tsd); |
| 84 | ql_remove(witnesses, witness, link); |
| 85 | } |
| 86 | |
| 87 | #ifdef JEMALLOC_JET |
| 88 | #undef witness_owner_error |
| 89 | #define witness_owner_error JEMALLOC_N(witness_owner_error_impl) |
| 90 | #endif |
| 91 | static void |
| 92 | witness_owner_error(const witness_t *witness) |
| 93 | { |
| 94 | |
| 95 | malloc_printf("<jemalloc>: Should own %s(%u)\n", witness->name, |
| 96 | witness->rank); |
| 97 | abort(); |
| 98 | } |
| 99 | #ifdef JEMALLOC_JET |
| 100 | #undef witness_owner_error |
| 101 | #define witness_owner_error JEMALLOC_N(witness_owner_error) |
| 102 | witness_owner_error_t *witness_owner_error = |
| 103 | JEMALLOC_N(witness_owner_error_impl); |
| 104 | #endif |
| 105 | |
| 106 | void |
| 107 | witness_assert_owner(tsd_t *tsd, const witness_t *witness) |
| 108 | { |
| 109 | witness_list_t *witnesses; |
| 110 | witness_t *w; |
| 111 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 112 | if (tsd == NULL) |
| 113 | return; |
| 114 | if (witness->rank == WITNESS_RANK_OMIT) |
| 115 | return; |
| 116 | |
| 117 | witnesses = tsd_witnessesp_get(tsd); |
| 118 | ql_foreach(w, witnesses, link) { |
| 119 | if (w == witness) |
| 120 | return; |
| 121 | } |
| 122 | witness_owner_error(witness); |
| 123 | } |
| 124 | |
| 125 | #ifdef JEMALLOC_JET |
| 126 | #undef witness_not_owner_error |
| 127 | #define witness_not_owner_error JEMALLOC_N(witness_not_owner_error_impl) |
| 128 | #endif |
| 129 | static void |
| 130 | witness_not_owner_error(const witness_t *witness) |
| 131 | { |
| 132 | |
| 133 | malloc_printf("<jemalloc>: Should not own %s(%u)\n", witness->name, |
| 134 | witness->rank); |
| 135 | abort(); |
| 136 | } |
| 137 | #ifdef JEMALLOC_JET |
| 138 | #undef witness_not_owner_error |
| 139 | #define witness_not_owner_error JEMALLOC_N(witness_not_owner_error) |
| 140 | witness_not_owner_error_t *witness_not_owner_error = |
| 141 | JEMALLOC_N(witness_not_owner_error_impl); |
| 142 | #endif |
| 143 | |
| 144 | void |
| 145 | witness_assert_not_owner(tsd_t *tsd, const witness_t *witness) |
| 146 | { |
| 147 | witness_list_t *witnesses; |
| 148 | witness_t *w; |
| 149 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 150 | if (tsd == NULL) |
| 151 | return; |
| 152 | if (witness->rank == WITNESS_RANK_OMIT) |
| 153 | return; |
| 154 | |
| 155 | witnesses = tsd_witnessesp_get(tsd); |
| 156 | ql_foreach(w, witnesses, link) { |
| 157 | if (w == witness) |
| 158 | witness_not_owner_error(witness); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | #ifdef JEMALLOC_JET |
| 163 | #undef witness_lockless_error |
| 164 | #define witness_lockless_error JEMALLOC_N(witness_lockless_error_impl) |
| 165 | #endif |
| 166 | static void |
| 167 | witness_lockless_error(const witness_list_t *witnesses) |
| 168 | { |
| 169 | witness_t *w; |
| 170 | |
| 171 | malloc_printf("<jemalloc>: Should not own any locks:"); |
| 172 | ql_foreach(w, witnesses, link) { |
| 173 | malloc_printf(" %s(%u)", w->name, w->rank); |
| 174 | } |
| 175 | malloc_printf("\n"); |
| 176 | abort(); |
| 177 | } |
| 178 | #ifdef JEMALLOC_JET |
| 179 | #undef witness_lockless_error |
| 180 | #define witness_lockless_error JEMALLOC_N(witness_lockless_error) |
| 181 | witness_lockless_error_t *witness_lockless_error = |
| 182 | JEMALLOC_N(witness_lockless_error_impl); |
| 183 | #endif |
| 184 | |
| 185 | void |
| 186 | witness_assert_lockless(tsd_t *tsd) |
| 187 | { |
| 188 | witness_list_t *witnesses; |
| 189 | witness_t *w; |
| 190 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 191 | if (tsd == NULL) |
| 192 | return; |
| 193 | |
| 194 | witnesses = tsd_witnessesp_get(tsd); |
| 195 | w = ql_last(witnesses, link); |
| 196 | if (w != NULL) { |
| 197 | witness_lockless_error(witnesses); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | void |
| 202 | witnesses_cleanup(tsd_t *tsd) |
| 203 | { |
| 204 | |
| 205 | witness_assert_lockless(tsd); |
| 206 | |
| 207 | /* Do nothing. */ |
| 208 | } |
Jason Evans | 174c0c3 | 2016-04-25 23:14:40 -0700 | [diff] [blame] | 209 | |
| 210 | void |
| 211 | witness_fork_cleanup(tsd_t *tsd) |
| 212 | { |
| 213 | |
| 214 | /* Do nothing. */ |
| 215 | } |
| 216 | |
| 217 | void |
| 218 | witness_prefork(tsd_t *tsd) |
| 219 | { |
| 220 | |
| 221 | tsd_witness_fork_set(tsd, true); |
| 222 | } |
| 223 | |
| 224 | void |
Jason Evans | 108c4a1 | 2016-04-26 10:47:22 -0700 | [diff] [blame^] | 225 | witness_postfork_parent(tsd_t *tsd) |
Jason Evans | 174c0c3 | 2016-04-25 23:14:40 -0700 | [diff] [blame] | 226 | { |
| 227 | |
| 228 | tsd_witness_fork_set(tsd, false); |
| 229 | } |
Jason Evans | 108c4a1 | 2016-04-26 10:47:22 -0700 | [diff] [blame^] | 230 | |
| 231 | void |
| 232 | witness_postfork_child(tsd_t *tsd) |
| 233 | { |
| 234 | #ifndef JEMALLOC_MUTEX_INIT_CB |
| 235 | witness_list_t *witnesses; |
| 236 | |
| 237 | witnesses = tsd_witnessesp_get(tsd); |
| 238 | ql_new(witnesses); |
| 239 | #endif |
| 240 | tsd_witness_fork_set(tsd, false); |
| 241 | } |