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 | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 42 | /* Information associated with one thread participating in a barrier. */ |
| 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)); |
| 113 | } |
| 114 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 115 | /** Deallocate the memory allocated by barrier_initialize() and in p->oset. |
| 116 | * Called by drd_clientobj_destroy(). |
| 117 | */ |
| 118 | void barrier_cleanup(struct barrier_info* p) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 119 | { |
| 120 | struct barrier_thread_info* q; |
| 121 | |
| 122 | tl_assert(p); |
| 123 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 124 | if (p->pre_waiters_left != p->count || p->post_waiters_left != p->count) |
| 125 | { |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame^] | 126 | BarrierErrInfo bei = { p->a1 }; |
| 127 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 128 | BarrierErr, |
| 129 | VG_(get_IP)(VG_(get_running_tid)()), |
| 130 | "Destruction of barrier that is being waited" |
| 131 | " upon", |
| 132 | &bei); |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 133 | } |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 134 | |
| 135 | VG_(OSetGen_ResetIter)(p->oset); |
| 136 | for ( ; (q = VG_(OSetGen_Next)(p->oset)) != 0; ) |
| 137 | { |
| 138 | barrier_thread_destroy(q); |
| 139 | } |
| 140 | VG_(OSetGen_Destroy)(p->oset); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 141 | } |
| 142 | |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 143 | /** Look up the client-side barrier address barrier in s_barrier[]. If not |
| 144 | * found, add it. */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 145 | static |
| 146 | struct barrier_info* |
| 147 | barrier_get_or_allocate(const Addr barrier, const SizeT size, const Word count) |
| 148 | { |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 149 | struct barrier_info *p; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 150 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 151 | tl_assert(offsetof(DrdClientobj, barrier) == 0); |
| 152 | p = &drd_clientobj_get(barrier, ClientBarrier)->barrier; |
| 153 | if (p == 0) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 154 | { |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 155 | p = &drd_clientobj_add(barrier, barrier + size, ClientBarrier)->barrier; |
| 156 | barrier_initialize(p, barrier, size, count); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 157 | } |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 158 | return p; |
| 159 | } |
| 160 | |
| 161 | /** Look up the address of the information associated with the client-side |
| 162 | * barrier object. */ |
| 163 | struct barrier_info* barrier_get(const Addr barrier) |
| 164 | { |
| 165 | tl_assert(offsetof(DrdClientobj, barrier) == 0); |
| 166 | return &drd_clientobj_get(barrier, ClientBarrier)->barrier; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 167 | } |
| 168 | |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 169 | /** Initialize a barrier with client address barrier, client size size, and |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 170 | * where count threads participate in each barrier. |
| 171 | * Called before pthread_barrier_init(). |
| 172 | */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 173 | struct barrier_info* |
| 174 | barrier_init(const Addr barrier, const SizeT size, const Word count) |
| 175 | { |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame^] | 176 | if (s_trace_barrier) |
| 177 | { |
| 178 | VG_(message)(Vg_UserMsg, |
| 179 | "[%d/%d] barrier_init 0x%lx", |
| 180 | VG_(get_running_tid)(), |
| 181 | thread_get_running_tid(), |
| 182 | barrier); |
| 183 | } |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 184 | tl_assert(barrier_get(barrier) == 0); |
| 185 | return barrier_get_or_allocate(barrier, size, count); |
| 186 | } |
| 187 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 188 | /** Called after pthread_barrier_destroy(). */ |
| 189 | void barrier_destroy(struct barrier_info* const p) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 190 | { |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame^] | 191 | if (s_trace_barrier) |
| 192 | { |
| 193 | VG_(message)(Vg_UserMsg, |
| 194 | "[%d/%d] barrier_destroy 0x%lx", |
| 195 | VG_(get_running_tid)(), |
| 196 | thread_get_running_tid(), |
| 197 | p->a1); |
| 198 | } |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 199 | tl_assert(p); |
| 200 | drd_clientobj_remove(p->a1, ClientBarrier); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 201 | } |
| 202 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 203 | /** Called before pthread_barrier_wait(). */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 204 | void barrier_pre_wait(const DrdThreadId tid, const Addr barrier) |
| 205 | { |
| 206 | struct barrier_info* p; |
| 207 | struct barrier_thread_info* q; |
| 208 | const UWord word_tid = tid; |
| 209 | |
| 210 | p = barrier_get(barrier); |
| 211 | tl_assert(p); |
| 212 | |
| 213 | if (s_trace_barrier) |
| 214 | { |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame^] | 215 | VG_(message)(Vg_UserMsg, |
| 216 | "[%d/%d] barrier_pre_wait 0x%lx iteration %d", |
| 217 | VG_(get_running_tid)(), |
| 218 | thread_get_running_tid(), |
| 219 | barrier, |
| 220 | p->pre_iteration); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 221 | } |
| 222 | |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 223 | q = VG_(OSetGen_Lookup)(p->oset, &word_tid); |
| 224 | if (q == 0) |
| 225 | { |
| 226 | q = VG_(OSetGen_AllocNode)(p->oset, sizeof(*q)); |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 227 | barrier_thread_initialize(q, tid, p->pre_iteration); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 228 | VG_(OSetGen_Insert)(p->oset, q); |
| 229 | tl_assert(VG_(OSetGen_Lookup)(p->oset, &word_tid) == q); |
| 230 | } |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 231 | vc_assign(&q->vc[p->pre_iteration], &thread_get_segment(tid)->vc); |
| 232 | tl_assert(q->vc[p->pre_iteration].size > 0); |
| 233 | |
| 234 | if (--p->pre_waiters_left <= 0) |
| 235 | { |
| 236 | p->pre_iteration = 1 - p->pre_iteration; |
| 237 | p->pre_waiters_left = p->count; |
| 238 | } |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 239 | } |
| 240 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 241 | /** Called after pthread_barrier_wait(). */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 242 | void barrier_post_wait(const DrdThreadId tid, const Addr barrier, |
| 243 | const Bool waited) |
| 244 | { |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame^] | 245 | struct barrier_info* p; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 246 | |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame^] | 247 | p = barrier_get(barrier); |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 248 | tl_assert(p); |
| 249 | |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 250 | if (s_trace_barrier) |
| 251 | { |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame^] | 252 | VG_(message)(Vg_UserMsg, |
| 253 | "[%d/%d] barrier_post_wait 0x%lx iteration %d", |
| 254 | VG_(get_running_tid)(), |
| 255 | tid, |
| 256 | barrier, |
| 257 | p->post_iteration); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | if (waited) |
| 261 | { |
| 262 | const UWord word_tid = tid; |
| 263 | struct barrier_thread_info* q; |
| 264 | struct barrier_thread_info* r; |
| 265 | |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 266 | q = VG_(OSetGen_Lookup)(p->oset, &word_tid); |
| 267 | tl_assert(q); |
| 268 | VG_(OSetGen_ResetIter)(p->oset); |
| 269 | for ( ; (r = VG_(OSetGen_Next)(p->oset)) != 0; ) |
| 270 | { |
| 271 | if (r != q) |
| 272 | { |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 273 | thread_combine_vc2(tid, &r->vc[p->post_iteration]); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 274 | } |
| 275 | } |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 276 | |
| 277 | thread_new_segment(tid); |
| 278 | |
| 279 | if (--p->post_waiters_left <= 0) |
| 280 | { |
| 281 | p->post_iteration = 1 - p->post_iteration; |
| 282 | p->post_waiters_left = p->count; |
| 283 | } |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 287 | /** Call this function when thread tid stops to exist. */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 288 | void barrier_thread_delete(const DrdThreadId tid) |
| 289 | { |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 290 | struct barrier_info* p; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 291 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 292 | drd_clientobj_resetiter(); |
| 293 | for ( ; (p = &drd_clientobj_next(ClientBarrier)->barrier) != 0; ) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 294 | { |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 295 | struct barrier_thread_info* q; |
| 296 | const UWord word_tid = tid; |
| 297 | q = VG_(OSetGen_Remove)(p->oset, &word_tid); |
| 298 | barrier_thread_destroy(q); |
| 299 | VG_(OSetGen_FreeNode)(p->oset, q); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 300 | } |
| 301 | } |