sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 1 | /* |
| 2 | This file is part of drd, a data race detector. |
| 3 | |
| 4 | Copyright (C) 2006-2008 Bart Van Assche |
| 5 | bart.vanassche@gmail.com |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or |
| 8 | modify it under the terms of the GNU General Public License as |
| 9 | published by the Free Software Foundation; either version 2 of the |
| 10 | License, or (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, but |
| 13 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program; if not, write to the Free Software |
| 19 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 20 | 02111-1307, USA. |
| 21 | |
| 22 | The GNU General Public License is contained in the file COPYING. |
| 23 | */ |
| 24 | |
| 25 | |
| 26 | #include "drd_barrier.h" |
| 27 | #include "drd_error.h" |
| 28 | #include "drd_suppression.h" |
| 29 | #include "priv_drd_clientreq.h" |
| 30 | #include "pub_tool_errormgr.h" // VG_(maybe_record_error)() |
| 31 | #include "pub_tool_libcassert.h" // tl_assert() |
| 32 | #include "pub_tool_libcprint.h" // VG_(printf)() |
| 33 | #include "pub_tool_machine.h" // VG_(get_IP)() |
| 34 | #include "pub_tool_mallocfree.h" // VG_(malloc)(), VG_(free)() |
| 35 | #include "pub_tool_oset.h" |
| 36 | #include "pub_tool_threadstate.h" // VG_(get_running_tid)() |
| 37 | |
| 38 | |
| 39 | // Type definitions. |
| 40 | |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame^] | 41 | /* Information associated with a client-side pthread_barrier_t object. */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 42 | struct barrier_info |
| 43 | { |
| 44 | Addr barrier; // Client address of barrier. |
| 45 | SizeT size; // Size in bytes of client-side object. |
| 46 | Word count; // Participant count in a barrier wait. |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame^] | 47 | Word pre_iteration; // pthread_barrier_wait() call count modulo two. |
| 48 | Word post_iteration; // pthread_barrier_wait() call count modulo two. |
| 49 | Word pre_waiters_left; // number of waiters left for a complete barrier. |
| 50 | Word post_waiters_left; // number of waiters left for a complete barrier. |
| 51 | OSet* oset; // Thread-specific barrier information. |
| 52 | }; |
| 53 | |
| 54 | /* Information associated with one thread participating in a barrier. */ |
| 55 | struct barrier_thread_info |
| 56 | { |
| 57 | UWord tid; // A DrdThreadId |
| 58 | Word iteration; // iteration of last pthread_barrier_wait() |
| 59 | // call thread tid participated in. |
| 60 | VectorClock vc[2]; // vector clocks corresponding to the last two |
| 61 | // pthread_barrier() calls by thread tid. |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | |
| 65 | // Local variables. |
| 66 | |
| 67 | static Bool s_trace_barrier = False; |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame^] | 68 | /* To do: eliminate the upper limit on the number of barriers (4). */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 69 | struct barrier_info s_barrier[4]; |
| 70 | |
| 71 | |
| 72 | // Function definitions. |
| 73 | |
| 74 | void barrier_set_trace(const Bool trace_barrier) |
| 75 | { |
| 76 | s_trace_barrier = trace_barrier; |
| 77 | } |
| 78 | |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame^] | 79 | /** Initialize the structure *p with the specified thread ID and iteration |
| 80 | * information. */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 81 | static void barrier_thread_initialize(struct barrier_thread_info* const p, |
| 82 | const DrdThreadId tid, |
| 83 | const Word iteration) |
| 84 | { |
| 85 | p->tid = tid; |
| 86 | p->iteration = iteration; |
| 87 | vc_init(&p->vc[0], 0, 0); |
| 88 | vc_init(&p->vc[1], 0, 0); |
| 89 | } |
| 90 | |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame^] | 91 | /** Deallocate the memory that was allocated in barrier_thread_initialize(). */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 92 | static void barrier_thread_destroy(struct barrier_thread_info* const p) |
| 93 | { |
| 94 | vc_cleanup(&p->vc[0]); |
| 95 | vc_cleanup(&p->vc[1]); |
| 96 | } |
| 97 | |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame^] | 98 | /** Initialize the structure *p with the specified client-side barrier address, |
| 99 | * barrier object size and number of participants in each barrier. */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 100 | static |
| 101 | void barrier_initialize(struct barrier_info* const p, |
| 102 | const Addr barrier, |
| 103 | const SizeT size, |
| 104 | const Word count) |
| 105 | { |
| 106 | tl_assert(barrier != 0); |
| 107 | tl_assert(size > 0); |
| 108 | tl_assert(count > 0); |
| 109 | |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame^] | 110 | p->barrier = barrier; |
| 111 | p->size = size; |
| 112 | p->count = count; |
| 113 | p->pre_iteration = 0; |
| 114 | p->post_iteration = 0; |
| 115 | p->pre_waiters_left = count; |
| 116 | p->post_waiters_left = count; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 117 | tl_assert(sizeof(((struct barrier_thread_info*)0)->tid) == sizeof(Word)); |
| 118 | tl_assert(sizeof(((struct barrier_thread_info*)0)->tid) |
| 119 | >= sizeof(DrdThreadId)); |
| 120 | p->oset = VG_(OSetGen_Create)(0, 0, VG_(malloc), VG_(free)); |
| 121 | } |
| 122 | |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame^] | 123 | /** Deallocate the memory allocated by barrier_initialize() and in p->oset. */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 124 | void barrier_destroy(struct barrier_info* const p) |
| 125 | { |
| 126 | struct barrier_thread_info* q; |
| 127 | |
| 128 | tl_assert(p); |
| 129 | |
| 130 | drd_finish_suppression(p->barrier, p->barrier + p->size); |
| 131 | |
| 132 | VG_(OSetGen_ResetIter)(p->oset); |
| 133 | for ( ; (q = VG_(OSetGen_Next)(p->oset)) != 0; ) |
| 134 | { |
| 135 | barrier_thread_destroy(q); |
| 136 | } |
| 137 | VG_(OSetGen_Destroy)(p->oset); |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame^] | 138 | p->barrier = 0; |
| 139 | p->size = 0; |
| 140 | p->count = 0; |
| 141 | p->pre_iteration = 0; |
| 142 | p->post_iteration = 0; |
| 143 | p->pre_waiters_left = 0; |
| 144 | p->post_waiters_left = 0; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 145 | } |
| 146 | |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame^] | 147 | /** Look up the client-side barrier address barrier in s_barrier[]. If not |
| 148 | * found, add it. */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 149 | static |
| 150 | struct barrier_info* |
| 151 | barrier_get_or_allocate(const Addr barrier, const SizeT size, const Word count) |
| 152 | { |
| 153 | int i; |
| 154 | |
| 155 | for (i = 0; i < sizeof(s_barrier)/sizeof(s_barrier[0]); i++) |
| 156 | { |
| 157 | if (s_barrier[i].barrier == barrier) |
| 158 | { |
| 159 | tl_assert(s_barrier[i].size == size); |
| 160 | return &s_barrier[i]; |
| 161 | } |
| 162 | } |
| 163 | for (i = 0; i < sizeof(s_barrier)/sizeof(s_barrier[0]); i++) |
| 164 | { |
| 165 | if (s_barrier[i].barrier == 0) |
| 166 | { |
| 167 | barrier_initialize(&s_barrier[i], barrier, size, count); |
| 168 | drd_start_suppression(barrier, barrier + size, "barrier"); |
| 169 | return &s_barrier[i]; |
| 170 | } |
| 171 | } |
| 172 | tl_assert(0); |
| 173 | return 0; |
| 174 | } |
| 175 | |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame^] | 176 | /** Initialize a barrier with client address barrier, client size size, and |
| 177 | * where count threads participate in each barrier. */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 178 | struct barrier_info* |
| 179 | barrier_init(const Addr barrier, const SizeT size, const Word count) |
| 180 | { |
| 181 | tl_assert(barrier_get(barrier) == 0); |
| 182 | return barrier_get_or_allocate(barrier, size, count); |
| 183 | } |
| 184 | |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame^] | 185 | /** Look up the address of the information associated with the client-side |
| 186 | * barrier object. */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 187 | struct barrier_info* barrier_get(const Addr barrier) |
| 188 | { |
| 189 | int i; |
| 190 | for (i = 0; i < sizeof(s_barrier)/sizeof(s_barrier[0]); i++) |
| 191 | if (s_barrier[i].barrier == barrier) |
| 192 | return &s_barrier[i]; |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | void barrier_pre_wait(const DrdThreadId tid, const Addr barrier) |
| 197 | { |
| 198 | struct barrier_info* p; |
| 199 | struct barrier_thread_info* q; |
| 200 | const UWord word_tid = tid; |
| 201 | |
| 202 | p = barrier_get(barrier); |
| 203 | tl_assert(p); |
| 204 | |
| 205 | if (s_trace_barrier) |
| 206 | { |
| 207 | VG_(message)(Vg_DebugMsg, |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame^] | 208 | "[%d] barrier_pre_wait(%p) iteration %d", |
| 209 | tid, barrier, p->pre_iteration); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 210 | } |
| 211 | |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 212 | q = VG_(OSetGen_Lookup)(p->oset, &word_tid); |
| 213 | if (q == 0) |
| 214 | { |
| 215 | q = VG_(OSetGen_AllocNode)(p->oset, sizeof(*q)); |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame^] | 216 | barrier_thread_initialize(q, tid, p->pre_iteration); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 217 | VG_(OSetGen_Insert)(p->oset, q); |
| 218 | tl_assert(VG_(OSetGen_Lookup)(p->oset, &word_tid) == q); |
| 219 | } |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame^] | 220 | vc_assign(&q->vc[p->pre_iteration], &thread_get_segment(tid)->vc); |
| 221 | tl_assert(q->vc[p->pre_iteration].size > 0); |
| 222 | |
| 223 | if (--p->pre_waiters_left <= 0) |
| 224 | { |
| 225 | p->pre_iteration = 1 - p->pre_iteration; |
| 226 | p->pre_waiters_left = p->count; |
| 227 | } |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | void barrier_post_wait(const DrdThreadId tid, const Addr barrier, |
| 231 | const Bool waited) |
| 232 | { |
| 233 | struct barrier_info* const p = barrier_get(barrier); |
| 234 | |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame^] | 235 | tl_assert(p); |
| 236 | |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 237 | if (s_trace_barrier) |
| 238 | { |
| 239 | VG_(message)(Vg_DebugMsg, "[%d] barrier_post_wait(%p) iteration %d", |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame^] | 240 | tid, barrier, p->post_iteration); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | if (waited) |
| 244 | { |
| 245 | const UWord word_tid = tid; |
| 246 | struct barrier_thread_info* q; |
| 247 | struct barrier_thread_info* r; |
| 248 | |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 249 | q = VG_(OSetGen_Lookup)(p->oset, &word_tid); |
| 250 | tl_assert(q); |
| 251 | VG_(OSetGen_ResetIter)(p->oset); |
| 252 | for ( ; (r = VG_(OSetGen_Next)(p->oset)) != 0; ) |
| 253 | { |
| 254 | if (r != q) |
| 255 | { |
| 256 | if (s_trace_barrier) |
| 257 | { |
| 258 | VG_(message)(Vg_DebugMsg, |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame^] | 259 | "[%d] barrier_post_wait: combining vc of thread %d, " |
| 260 | "iteration %d", |
| 261 | tid, r->tid, p->post_iteration); |
| 262 | vc_print(&thread_get_segment(tid)->vc); |
| 263 | VG_(printf)(", "); |
| 264 | vc_print(&r->vc[p->post_iteration]); |
| 265 | VG_(printf)(" -> "); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 266 | } |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame^] | 267 | thread_combine_vc2(tid, &r->vc[p->post_iteration]); |
| 268 | if (s_trace_barrier) |
| 269 | { |
| 270 | vc_print(&thread_get_segment(tid)->vc); |
| 271 | VG_(printf)("\n"); |
| 272 | } |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 273 | } |
| 274 | } |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame^] | 275 | |
| 276 | thread_new_segment(tid); |
| 277 | |
| 278 | if (--p->post_waiters_left <= 0) |
| 279 | { |
| 280 | p->post_iteration = 1 - p->post_iteration; |
| 281 | p->post_waiters_left = p->count; |
| 282 | } |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame^] | 286 | /** Call this function when thread tid stops to exist. */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 287 | void barrier_thread_delete(const DrdThreadId tid) |
| 288 | { |
| 289 | int i; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 290 | |
| 291 | for (i = 0; i < sizeof(s_barrier)/sizeof(s_barrier[0]); i++) |
| 292 | { |
| 293 | struct barrier_info* const p = &s_barrier[i]; |
| 294 | if (p->barrier) |
| 295 | { |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame^] | 296 | struct barrier_thread_info* q; |
| 297 | const UWord word_tid = tid; |
| 298 | q = VG_(OSetGen_Remove)(p->oset, &word_tid); |
| 299 | barrier_thread_destroy(q); |
| 300 | VG_(OSetGen_FreeNode)(p->oset, q); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | void barrier_stop_using_mem(const Addr a1, const Addr a2) |
| 306 | { |
| 307 | unsigned i; |
| 308 | for (i = 0; i < sizeof(s_barrier)/sizeof(s_barrier[0]); i++) |
| 309 | { |
| 310 | if (a1 <= s_barrier[i].barrier && s_barrier[i].barrier < a2) |
| 311 | { |
| 312 | tl_assert(s_barrier[i].barrier + s_barrier[i].size <= a2); |
| 313 | barrier_destroy(&s_barrier[i]); |
| 314 | } |
| 315 | } |
| 316 | } |