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" |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 27 | #include "drd_clientobj.h" |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 28 | #include "drd_error.h" |
| 29 | #include "drd_suppression.h" |
| 30 | #include "priv_drd_clientreq.h" |
| 31 | #include "pub_tool_errormgr.h" // VG_(maybe_record_error)() |
| 32 | #include "pub_tool_libcassert.h" // tl_assert() |
| 33 | #include "pub_tool_libcprint.h" // VG_(printf)() |
| 34 | #include "pub_tool_machine.h" // VG_(get_IP)() |
| 35 | #include "pub_tool_mallocfree.h" // VG_(malloc)(), VG_(free)() |
| 36 | #include "pub_tool_oset.h" |
| 37 | #include "pub_tool_threadstate.h" // VG_(get_running_tid)() |
| 38 | |
| 39 | |
| 40 | // Type definitions. |
| 41 | |
bart | 8ddef88 | 2008-03-09 08:48:01 +0000 | [diff] [blame^] | 42 | /** Information associated with one thread participating in a barrier. */ |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 43 | struct barrier_thread_info |
| 44 | { |
| 45 | UWord tid; // A DrdThreadId |
| 46 | Word iteration; // iteration of last pthread_barrier_wait() |
| 47 | // call thread tid participated in. |
| 48 | VectorClock vc[2]; // vector clocks corresponding to the last two |
| 49 | // pthread_barrier() calls by thread tid. |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 53 | // Local functions. |
| 54 | |
| 55 | void barrier_cleanup(struct barrier_info* p); |
| 56 | |
| 57 | |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 58 | // Local variables. |
| 59 | |
| 60 | static Bool s_trace_barrier = False; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 61 | |
| 62 | |
| 63 | // Function definitions. |
| 64 | |
| 65 | void barrier_set_trace(const Bool trace_barrier) |
| 66 | { |
| 67 | s_trace_barrier = trace_barrier; |
| 68 | } |
| 69 | |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 70 | /** Initialize the structure *p with the specified thread ID and iteration |
| 71 | * information. */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 72 | static void barrier_thread_initialize(struct barrier_thread_info* const p, |
| 73 | const DrdThreadId tid, |
| 74 | const Word iteration) |
| 75 | { |
| 76 | p->tid = tid; |
| 77 | p->iteration = iteration; |
| 78 | vc_init(&p->vc[0], 0, 0); |
| 79 | vc_init(&p->vc[1], 0, 0); |
| 80 | } |
| 81 | |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 82 | /** Deallocate the memory that was allocated in barrier_thread_initialize(). */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 83 | static void barrier_thread_destroy(struct barrier_thread_info* const p) |
| 84 | { |
| 85 | vc_cleanup(&p->vc[0]); |
| 86 | vc_cleanup(&p->vc[1]); |
| 87 | } |
| 88 | |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 89 | /** Initialize the structure *p with the specified client-side barrier address, |
| 90 | * barrier object size and number of participants in each barrier. */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 91 | static |
| 92 | void barrier_initialize(struct barrier_info* const p, |
| 93 | const Addr barrier, |
| 94 | const SizeT size, |
| 95 | const Word count) |
| 96 | { |
| 97 | tl_assert(barrier != 0); |
| 98 | tl_assert(size > 0); |
| 99 | tl_assert(count > 0); |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 100 | tl_assert(p->a1 == barrier); |
| 101 | tl_assert(p->a2 - p->a1 == size); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 102 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 103 | p->cleanup = (void(*)(DrdClientobj*))barrier_cleanup; |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 104 | p->count = count; |
| 105 | p->pre_iteration = 0; |
| 106 | p->post_iteration = 0; |
| 107 | p->pre_waiters_left = count; |
| 108 | p->post_waiters_left = count; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 109 | tl_assert(sizeof(((struct barrier_thread_info*)0)->tid) == sizeof(Word)); |
| 110 | tl_assert(sizeof(((struct barrier_thread_info*)0)->tid) |
| 111 | >= sizeof(DrdThreadId)); |
| 112 | p->oset = VG_(OSetGen_Create)(0, 0, VG_(malloc), VG_(free)); |
bart | 8ddef88 | 2008-03-09 08:48:01 +0000 | [diff] [blame^] | 113 | vc_init(&p->finished_threads_vc, 0, 0); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 114 | } |
| 115 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 116 | /** Deallocate the memory allocated by barrier_initialize() and in p->oset. |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 117 | * Called by clientobj_destroy(). |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 118 | */ |
| 119 | void barrier_cleanup(struct barrier_info* p) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 120 | { |
| 121 | struct barrier_thread_info* q; |
| 122 | |
| 123 | tl_assert(p); |
| 124 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 125 | if (p->pre_waiters_left != p->count || p->post_waiters_left != p->count) |
| 126 | { |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 127 | BarrierErrInfo bei = { p->a1 }; |
| 128 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 129 | BarrierErr, |
| 130 | VG_(get_IP)(VG_(get_running_tid)()), |
| 131 | "Destruction of barrier that is being waited" |
| 132 | " upon", |
| 133 | &bei); |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 134 | } |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 135 | |
| 136 | VG_(OSetGen_ResetIter)(p->oset); |
| 137 | for ( ; (q = VG_(OSetGen_Next)(p->oset)) != 0; ) |
| 138 | { |
| 139 | barrier_thread_destroy(q); |
| 140 | } |
| 141 | VG_(OSetGen_Destroy)(p->oset); |
bart | 8ddef88 | 2008-03-09 08:48:01 +0000 | [diff] [blame^] | 142 | vc_cleanup(&p->finished_threads_vc); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 143 | } |
| 144 | |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 145 | /** Look up the client-side barrier address barrier in s_barrier[]. If not |
| 146 | * found, add it. */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 147 | static |
| 148 | struct barrier_info* |
| 149 | barrier_get_or_allocate(const Addr barrier, const SizeT size, const Word count) |
| 150 | { |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 151 | struct barrier_info *p; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 152 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 153 | tl_assert(offsetof(DrdClientobj, barrier) == 0); |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 154 | p = &clientobj_get(barrier, ClientBarrier)->barrier; |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 155 | if (p == 0) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 156 | { |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 157 | p = &clientobj_add(barrier, barrier + size, ClientBarrier)->barrier; |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 158 | barrier_initialize(p, barrier, size, count); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 159 | } |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 160 | return p; |
| 161 | } |
| 162 | |
| 163 | /** Look up the address of the information associated with the client-side |
| 164 | * barrier object. */ |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 165 | static struct barrier_info* barrier_get(const Addr barrier) |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 166 | { |
| 167 | tl_assert(offsetof(DrdClientobj, barrier) == 0); |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 168 | return &clientobj_get(barrier, ClientBarrier)->barrier; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 169 | } |
| 170 | |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 171 | /** Initialize a barrier with client address barrier, client size size, and |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 172 | * where count threads participate in each barrier. |
| 173 | * Called before pthread_barrier_init(). |
| 174 | */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 175 | struct barrier_info* |
| 176 | barrier_init(const Addr barrier, const SizeT size, const Word count) |
| 177 | { |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 178 | if (s_trace_barrier) |
| 179 | { |
| 180 | VG_(message)(Vg_UserMsg, |
| 181 | "[%d/%d] barrier_init 0x%lx", |
| 182 | VG_(get_running_tid)(), |
| 183 | thread_get_running_tid(), |
| 184 | barrier); |
| 185 | } |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 186 | tl_assert(barrier_get(barrier) == 0); |
| 187 | return barrier_get_or_allocate(barrier, size, count); |
| 188 | } |
| 189 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 190 | /** Called after pthread_barrier_destroy(). */ |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 191 | void barrier_destroy(const Addr barrier) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 192 | { |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 193 | struct barrier_info* p; |
| 194 | |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 195 | if (s_trace_barrier) |
| 196 | { |
| 197 | VG_(message)(Vg_UserMsg, |
| 198 | "[%d/%d] barrier_destroy 0x%lx", |
| 199 | VG_(get_running_tid)(), |
| 200 | thread_get_running_tid(), |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 201 | barrier); |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 202 | } |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 203 | |
| 204 | p = barrier_get(barrier); |
| 205 | if (p == 0) |
| 206 | { |
| 207 | GenericErrInfo GEI; |
| 208 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 209 | GenericErr, |
| 210 | VG_(get_IP)(VG_(get_running_tid)()), |
| 211 | "Not a barrier", |
| 212 | &GEI); |
| 213 | return; |
| 214 | } |
| 215 | |
| 216 | clientobj_remove(p->a1, ClientBarrier); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 217 | } |
| 218 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 219 | /** Called before pthread_barrier_wait(). */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 220 | void barrier_pre_wait(const DrdThreadId tid, const Addr barrier) |
| 221 | { |
| 222 | struct barrier_info* p; |
| 223 | struct barrier_thread_info* q; |
| 224 | const UWord word_tid = tid; |
| 225 | |
| 226 | p = barrier_get(barrier); |
| 227 | tl_assert(p); |
| 228 | |
| 229 | if (s_trace_barrier) |
| 230 | { |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 231 | VG_(message)(Vg_UserMsg, |
| 232 | "[%d/%d] barrier_pre_wait 0x%lx iteration %d", |
| 233 | VG_(get_running_tid)(), |
| 234 | thread_get_running_tid(), |
| 235 | barrier, |
| 236 | p->pre_iteration); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 237 | } |
| 238 | |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 239 | q = VG_(OSetGen_Lookup)(p->oset, &word_tid); |
| 240 | if (q == 0) |
| 241 | { |
| 242 | q = VG_(OSetGen_AllocNode)(p->oset, sizeof(*q)); |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 243 | barrier_thread_initialize(q, tid, p->pre_iteration); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 244 | VG_(OSetGen_Insert)(p->oset, q); |
| 245 | tl_assert(VG_(OSetGen_Lookup)(p->oset, &word_tid) == q); |
| 246 | } |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 247 | vc_assign(&q->vc[p->pre_iteration], &thread_get_segment(tid)->vc); |
| 248 | tl_assert(q->vc[p->pre_iteration].size > 0); |
| 249 | |
| 250 | if (--p->pre_waiters_left <= 0) |
| 251 | { |
| 252 | p->pre_iteration = 1 - p->pre_iteration; |
| 253 | p->pre_waiters_left = p->count; |
| 254 | } |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 255 | } |
| 256 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 257 | /** Called after pthread_barrier_wait(). */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 258 | void barrier_post_wait(const DrdThreadId tid, const Addr barrier, |
| 259 | const Bool waited) |
| 260 | { |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 261 | struct barrier_info* p; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 262 | |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 263 | p = barrier_get(barrier); |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 264 | tl_assert(p); |
| 265 | |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 266 | if (s_trace_barrier) |
| 267 | { |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 268 | VG_(message)(Vg_UserMsg, |
| 269 | "[%d/%d] barrier_post_wait 0x%lx iteration %d", |
| 270 | VG_(get_running_tid)(), |
| 271 | tid, |
| 272 | barrier, |
| 273 | p->post_iteration); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | if (waited) |
| 277 | { |
| 278 | const UWord word_tid = tid; |
| 279 | struct barrier_thread_info* q; |
| 280 | struct barrier_thread_info* r; |
| 281 | |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 282 | q = VG_(OSetGen_Lookup)(p->oset, &word_tid); |
| 283 | tl_assert(q); |
| 284 | VG_(OSetGen_ResetIter)(p->oset); |
| 285 | for ( ; (r = VG_(OSetGen_Next)(p->oset)) != 0; ) |
| 286 | { |
| 287 | if (r != q) |
| 288 | { |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 289 | thread_combine_vc2(tid, &r->vc[p->post_iteration]); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 290 | } |
| 291 | } |
bart | 8ddef88 | 2008-03-09 08:48:01 +0000 | [diff] [blame^] | 292 | thread_combine_vc2(tid, &p->finished_threads_vc); |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 293 | |
| 294 | thread_new_segment(tid); |
| 295 | |
| 296 | if (--p->post_waiters_left <= 0) |
| 297 | { |
| 298 | p->post_iteration = 1 - p->post_iteration; |
| 299 | p->post_waiters_left = p->count; |
| 300 | } |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 304 | /** Call this function when thread tid stops to exist. */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 305 | void barrier_thread_delete(const DrdThreadId tid) |
| 306 | { |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 307 | struct barrier_info* p; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 308 | |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 309 | clientobj_resetiter(); |
| 310 | for ( ; (p = &clientobj_next(ClientBarrier)->barrier) != 0; ) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 311 | { |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 312 | struct barrier_thread_info* q; |
| 313 | const UWord word_tid = tid; |
| 314 | q = VG_(OSetGen_Remove)(p->oset, &word_tid); |
bart | 8ddef88 | 2008-03-09 08:48:01 +0000 | [diff] [blame^] | 315 | vc_combine(&p->finished_threads_vc, &q->vc[p->post_iteration]); |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 316 | barrier_thread_destroy(q); |
| 317 | VG_(OSetGen_FreeNode)(p->oset, q); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 318 | } |
| 319 | } |