bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 1 | /* -*- mode: C; c-basic-offset: 3; -*- */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 2 | /* |
bart | 86562bd | 2009-02-16 19:43:56 +0000 | [diff] [blame] | 3 | This file is part of drd, a thread error detector. |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 4 | |
bart | 86562bd | 2009-02-16 19:43:56 +0000 | [diff] [blame] | 5 | Copyright (C) 2006-2009 Bart Van Assche <bart.vanassche@gmail.com>. |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 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" |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 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 | |
bart | a8cf765 | 2009-02-15 11:00:29 +0000 | [diff] [blame] | 39 | /* Type definitions. */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 40 | |
bart | 8ddef88 | 2008-03-09 08:48:01 +0000 | [diff] [blame] | 41 | /** Information associated with one thread participating in a barrier. */ |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 42 | struct barrier_thread_info |
| 43 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 44 | UWord tid; // A DrdThreadId declared as UWord because |
| 45 | // this member variable is the key of an OSet. |
| 46 | Word iteration; // iteration of last pthread_barrier_wait() |
| 47 | // call thread tid participated in. |
| 48 | Segment* sg[2]; // Segments of the last two |
| 49 | // pthread_barrier() calls by thread tid. |
| 50 | ExeContext* wait_call_ctxt;// call stack for *_barrier_wait() call. |
| 51 | Segment* post_wait_sg; // Segment created after *_barrier_wait() finished |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | |
bart | a8cf765 | 2009-02-15 11:00:29 +0000 | [diff] [blame] | 55 | /* Local functions. */ |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 56 | |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame] | 57 | static void barrier_cleanup(struct barrier_info* p); |
| 58 | static void barrier_delete_thread(struct barrier_info* const p, |
| 59 | const DrdThreadId tid); |
| 60 | static const char* barrier_get_typename(struct barrier_info* const p); |
| 61 | static const char* barrier_type_name(const BarrierT bt); |
| 62 | static |
| 63 | void barrier_report_wait_delete_race(const struct barrier_info* const p, |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 64 | const struct barrier_thread_info* const q); |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 65 | |
| 66 | |
bart | a8cf765 | 2009-02-15 11:00:29 +0000 | [diff] [blame] | 67 | /* Local variables. */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 68 | |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame] | 69 | static Bool s_trace_barrier = False; |
| 70 | static ULong s_barrier_segment_creation_count; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 71 | |
| 72 | |
bart | a8cf765 | 2009-02-15 11:00:29 +0000 | [diff] [blame] | 73 | /* Function definitions. */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 74 | |
bart | a8cf765 | 2009-02-15 11:00:29 +0000 | [diff] [blame] | 75 | void DRD_(barrier_set_trace)(const Bool trace_barrier) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 76 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 77 | s_trace_barrier = trace_barrier; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 78 | } |
| 79 | |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame] | 80 | /** |
| 81 | * Initialize the structure *p with the specified thread ID and iteration |
| 82 | * information. |
| 83 | */ |
bart | a8cf765 | 2009-02-15 11:00:29 +0000 | [diff] [blame] | 84 | static |
| 85 | void DRD_(barrier_thread_initialize)(struct barrier_thread_info* const p, |
| 86 | const DrdThreadId tid, |
| 87 | const Word iteration) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 88 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 89 | p->tid = tid; |
| 90 | p->iteration = iteration; |
| 91 | p->sg[0] = 0; |
| 92 | p->sg[1] = 0; |
| 93 | p->wait_call_ctxt = 0; |
| 94 | p->post_wait_sg = 0; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 95 | } |
| 96 | |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame] | 97 | /** |
| 98 | * Deallocate the memory that is owned by members of |
| 99 | * struct barrier_thread_info. |
| 100 | */ |
bart | a8cf765 | 2009-02-15 11:00:29 +0000 | [diff] [blame] | 101 | static void DRD_(barrier_thread_destroy)(struct barrier_thread_info* const p) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 102 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 103 | tl_assert(p); |
| 104 | DRD_(sg_put)(p->sg[0]); |
| 105 | DRD_(sg_put)(p->sg[1]); |
| 106 | DRD_(sg_put)(p->post_wait_sg); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 107 | } |
| 108 | |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame] | 109 | /** |
| 110 | * Initialize the structure *p with the specified client-side barrier address, |
| 111 | * barrier object size and number of participants in each barrier. |
| 112 | */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 113 | static |
bart | a8cf765 | 2009-02-15 11:00:29 +0000 | [diff] [blame] | 114 | void DRD_(barrier_initialize)(struct barrier_info* const p, |
| 115 | const Addr barrier, |
| 116 | const BarrierT barrier_type, |
| 117 | const Word count) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 118 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 119 | tl_assert(barrier != 0); |
| 120 | tl_assert(barrier_type == pthread_barrier || barrier_type == gomp_barrier); |
| 121 | tl_assert(p->a1 == barrier); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 122 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 123 | p->cleanup = (void(*)(DrdClientobj*))barrier_cleanup; |
| 124 | p->delete_thread |
| 125 | = (void(*)(DrdClientobj*, DrdThreadId))barrier_delete_thread; |
| 126 | p->barrier_type = barrier_type; |
| 127 | p->count = count; |
| 128 | p->pre_iteration = 0; |
| 129 | p->post_iteration = 0; |
| 130 | p->pre_waiters_left = count; |
| 131 | p->post_waiters_left = count; |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame] | 132 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 133 | tl_assert(sizeof(((struct barrier_thread_info*)0)->tid) == sizeof(Word)); |
| 134 | tl_assert(sizeof(((struct barrier_thread_info*)0)->tid) |
| 135 | >= sizeof(DrdThreadId)); |
| 136 | p->oset = VG_(OSetGen_Create)(0, 0, VG_(malloc), "drd.barrier.bi.1", |
| 137 | VG_(free)); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 138 | } |
| 139 | |
bart | 195e41f | 2009-02-15 11:34:57 +0000 | [diff] [blame] | 140 | /** |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame] | 141 | * Deallocate the memory owned by the struct barrier_info object and also |
| 142 | * all the nodes in the OSet p->oset. |
| 143 | * |
bart | 195e41f | 2009-02-15 11:34:57 +0000 | [diff] [blame] | 144 | * Called by clientobj_destroy(). |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 145 | */ |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame] | 146 | static void barrier_cleanup(struct barrier_info* p) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 147 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 148 | struct barrier_thread_info* q; |
| 149 | Segment* latest_sg = 0; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 150 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 151 | tl_assert(p); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 152 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 153 | if (p->pre_waiters_left != p->count) |
| 154 | { |
| 155 | BarrierErrInfo bei = { p->a1, 0, 0 }; |
| 156 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 157 | BarrierErr, |
| 158 | VG_(get_IP)(VG_(get_running_tid)()), |
| 159 | "Destruction of barrier that is being waited" |
| 160 | " upon", |
| 161 | &bei); |
| 162 | } |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 163 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 164 | DRD_(thread_get_latest_segment)(&latest_sg, DRD_(thread_get_running_tid)()); |
| 165 | tl_assert(latest_sg); |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame] | 166 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 167 | VG_(OSetGen_ResetIter)(p->oset); |
| 168 | for ( ; (q = VG_(OSetGen_Next)(p->oset)) != 0; ) |
| 169 | { |
| 170 | if (q->post_wait_sg |
| 171 | && ! DRD_(vc_lte)(&q->post_wait_sg->vc, &latest_sg->vc)) |
| 172 | { |
| 173 | barrier_report_wait_delete_race(p, q); |
| 174 | } |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame] | 175 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 176 | DRD_(barrier_thread_destroy)(q); |
| 177 | } |
| 178 | VG_(OSetGen_Destroy)(p->oset); |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame] | 179 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 180 | DRD_(sg_put)(latest_sg); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 181 | } |
| 182 | |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame] | 183 | /** |
| 184 | * Look up the client-side barrier address barrier in s_barrier[]. If not |
| 185 | * found, add it. |
| 186 | */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 187 | static |
| 188 | struct barrier_info* |
bart | a8cf765 | 2009-02-15 11:00:29 +0000 | [diff] [blame] | 189 | DRD_(barrier_get_or_allocate)(const Addr barrier, |
| 190 | const BarrierT barrier_type, const Word count) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 191 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 192 | struct barrier_info *p; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 193 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 194 | tl_assert(barrier_type == pthread_barrier || barrier_type == gomp_barrier); |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 195 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 196 | tl_assert(offsetof(DrdClientobj, barrier) == 0); |
| 197 | p = &(DRD_(clientobj_get)(barrier, ClientBarrier)->barrier); |
| 198 | if (p == 0) |
| 199 | { |
| 200 | p = &(DRD_(clientobj_add)(barrier, ClientBarrier)->barrier); |
| 201 | DRD_(barrier_initialize)(p, barrier, barrier_type, count); |
| 202 | } |
| 203 | return p; |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 204 | } |
| 205 | |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame] | 206 | /** |
| 207 | * Look up the address of the information associated with the client-side |
| 208 | * barrier object. |
| 209 | */ |
bart | a8cf765 | 2009-02-15 11:00:29 +0000 | [diff] [blame] | 210 | static struct barrier_info* DRD_(barrier_get)(const Addr barrier) |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 211 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 212 | tl_assert(offsetof(DrdClientobj, barrier) == 0); |
| 213 | return &(DRD_(clientobj_get)(barrier, ClientBarrier)->barrier); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 214 | } |
| 215 | |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame] | 216 | /** |
| 217 | * Initialize a barrier with client address barrier, client size size, and |
| 218 | * where count threads participate in each barrier. |
| 219 | * |
| 220 | * Called before pthread_barrier_init(). |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 221 | */ |
bart | a8cf765 | 2009-02-15 11:00:29 +0000 | [diff] [blame] | 222 | void DRD_(barrier_init)(const Addr barrier, |
| 223 | const BarrierT barrier_type, const Word count, |
| 224 | const Bool reinitialization) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 225 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 226 | struct barrier_info* p; |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 227 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 228 | tl_assert(barrier_type == pthread_barrier || barrier_type == gomp_barrier); |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 229 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 230 | if (count == 0) |
| 231 | { |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame] | 232 | BarrierErrInfo bei = { barrier, 0, 0 }; |
bart | d9e39ec | 2008-06-28 15:03:26 +0000 | [diff] [blame] | 233 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 234 | BarrierErr, |
| 235 | VG_(get_IP)(VG_(get_running_tid)()), |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 236 | "pthread_barrier_init: 'count' argument is zero", |
bart | d9e39ec | 2008-06-28 15:03:26 +0000 | [diff] [blame] | 237 | &bei); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 238 | } |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 239 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 240 | if (! reinitialization && barrier_type == pthread_barrier) |
| 241 | { |
| 242 | p = DRD_(barrier_get)(barrier); |
| 243 | if (p) |
| 244 | { |
| 245 | BarrierErrInfo bei = { barrier, 0, 0 }; |
| 246 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 247 | BarrierErr, |
| 248 | VG_(get_IP)(VG_(get_running_tid)()), |
| 249 | "Barrier reinitialization", |
| 250 | &bei); |
| 251 | } |
| 252 | } |
| 253 | p = DRD_(barrier_get_or_allocate)(barrier, barrier_type, count); |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 254 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 255 | if (s_trace_barrier) |
| 256 | { |
| 257 | if (reinitialization) |
| 258 | { |
| 259 | VG_(message)(Vg_UserMsg, |
| 260 | "[%d/%d] barrier_reinit %s 0x%lx count %ld -> %ld", |
| 261 | VG_(get_running_tid)(), |
| 262 | DRD_(thread_get_running_tid)(), |
| 263 | barrier_get_typename(p), |
| 264 | barrier, |
| 265 | p->count, |
| 266 | count); |
| 267 | } |
| 268 | else |
| 269 | { |
| 270 | VG_(message)(Vg_UserMsg, |
| 271 | "[%d/%d] barrier_init %s 0x%lx", |
| 272 | VG_(get_running_tid)(), |
| 273 | DRD_(thread_get_running_tid)(), |
| 274 | barrier_get_typename(p), |
| 275 | barrier); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | if (reinitialization && p->count != count) |
| 280 | { |
| 281 | if (p->pre_waiters_left != p->count || p->post_waiters_left != p->count) |
| 282 | { |
| 283 | BarrierErrInfo bei = { p->a1, 0, 0 }; |
| 284 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 285 | BarrierErr, |
| 286 | VG_(get_IP)(VG_(get_running_tid)()), |
| 287 | "Reinitialization of barrier with active" |
| 288 | " waiters", |
| 289 | &bei); |
| 290 | } |
| 291 | p->count = count; |
| 292 | } |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 293 | } |
| 294 | |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame] | 295 | /** Called after pthread_barrier_destroy() / gomp_barrier_destroy(). */ |
bart | a8cf765 | 2009-02-15 11:00:29 +0000 | [diff] [blame] | 296 | void DRD_(barrier_destroy)(const Addr barrier, const BarrierT barrier_type) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 297 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 298 | struct barrier_info* p; |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 299 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 300 | p = DRD_(barrier_get)(barrier); |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 301 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 302 | if (s_trace_barrier) |
| 303 | { |
| 304 | VG_(message)(Vg_UserMsg, |
| 305 | "[%d/%d] barrier_destroy %s 0x%lx", |
| 306 | VG_(get_running_tid)(), |
| 307 | DRD_(thread_get_running_tid)(), |
| 308 | barrier_get_typename(p), |
| 309 | barrier); |
| 310 | } |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 311 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 312 | if (p == 0) |
| 313 | { |
| 314 | GenericErrInfo GEI; |
| 315 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 316 | GenericErr, |
| 317 | VG_(get_IP)(VG_(get_running_tid)()), |
| 318 | "Not a barrier", |
| 319 | &GEI); |
| 320 | return; |
| 321 | } |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 322 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 323 | if (p->pre_waiters_left != p->count || p->post_waiters_left != p->count) |
| 324 | { |
| 325 | BarrierErrInfo bei = { p->a1, 0, 0 }; |
| 326 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 327 | BarrierErr, |
| 328 | VG_(get_IP)(VG_(get_running_tid)()), |
| 329 | "Destruction of a barrier with active waiters", |
| 330 | &bei); |
| 331 | } |
bart | 195a398 | 2008-07-01 13:15:31 +0000 | [diff] [blame] | 332 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 333 | DRD_(clientobj_remove)(p->a1, ClientBarrier); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 334 | } |
| 335 | |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame] | 336 | /** Called before pthread_barrier_wait() / gomp_barrier_wait(). */ |
bart | a8cf765 | 2009-02-15 11:00:29 +0000 | [diff] [blame] | 337 | void DRD_(barrier_pre_wait)(const DrdThreadId tid, const Addr barrier, |
| 338 | const BarrierT barrier_type) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 339 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 340 | struct barrier_info* p; |
| 341 | struct barrier_thread_info* q; |
| 342 | const UWord word_tid = tid; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 343 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 344 | p = DRD_(barrier_get)(barrier); |
| 345 | if (p == 0 && barrier_type == gomp_barrier) |
| 346 | { |
| 347 | /* |
| 348 | * gomp_barrier_wait() call has been intercepted but gomp_barrier_init() |
| 349 | * not. The only cause I know of that can trigger this is that libgomp.so |
| 350 | * has been compiled with --enable-linux-futex. |
| 351 | */ |
| 352 | VG_(message)(Vg_UserMsg, ""); |
| 353 | VG_(message)(Vg_UserMsg, |
| 354 | "Please verify whether gcc has been configured" |
| 355 | " with option --disable-linux-futex."); |
| 356 | VG_(message)(Vg_UserMsg, |
| 357 | "See also the section about OpenMP in the DRD manual."); |
| 358 | VG_(message)(Vg_UserMsg, ""); |
| 359 | } |
| 360 | tl_assert(p); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 361 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 362 | if (s_trace_barrier) |
| 363 | { |
| 364 | VG_(message)(Vg_UserMsg, |
| 365 | "[%d/%d] barrier_pre_wait %s 0x%lx iteration %ld", |
| 366 | VG_(get_running_tid)(), |
| 367 | DRD_(thread_get_running_tid)(), |
| 368 | barrier_get_typename(p), |
| 369 | barrier, |
| 370 | p->pre_iteration); |
| 371 | } |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 372 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 373 | /* Allocate the per-thread data structure if necessary. */ |
| 374 | q = VG_(OSetGen_Lookup)(p->oset, &word_tid); |
| 375 | if (q == 0) |
| 376 | { |
| 377 | q = VG_(OSetGen_AllocNode)(p->oset, sizeof(*q)); |
| 378 | DRD_(barrier_thread_initialize)(q, tid, p->pre_iteration); |
| 379 | VG_(OSetGen_Insert)(p->oset, q); |
| 380 | tl_assert(VG_(OSetGen_Lookup)(p->oset, &word_tid) == q); |
| 381 | } |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame] | 382 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 383 | /* Record *_barrier_wait() call context. */ |
| 384 | q->wait_call_ctxt = VG_(record_ExeContext)(VG_(get_running_tid)(), 0); |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame] | 385 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 386 | /* |
| 387 | * Store a pointer to the latest segment of the current thread in the |
| 388 | * per-thread data structure. |
| 389 | */ |
| 390 | DRD_(thread_get_latest_segment)(&q->sg[p->pre_iteration], tid); |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 391 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 392 | /* |
| 393 | * If the same number of threads as the barrier count indicates have |
| 394 | * called the pre *_barrier_wait() wrapper, toggle p->pre_iteration and |
| 395 | * reset the p->pre_waiters_left counter. |
| 396 | */ |
| 397 | if (--p->pre_waiters_left <= 0) |
| 398 | { |
| 399 | p->pre_iteration = 1 - p->pre_iteration; |
| 400 | p->pre_waiters_left = p->count; |
| 401 | } |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 402 | } |
| 403 | |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame] | 404 | /** Called after pthread_barrier_wait() / gomp_barrier_wait(). */ |
bart | a8cf765 | 2009-02-15 11:00:29 +0000 | [diff] [blame] | 405 | void DRD_(barrier_post_wait)(const DrdThreadId tid, const Addr barrier, |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame] | 406 | const BarrierT barrier_type, const Bool waited, |
| 407 | const Bool serializing) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 408 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 409 | struct barrier_info* p; |
| 410 | const UWord word_tid = tid; |
| 411 | struct barrier_thread_info* q; |
| 412 | struct barrier_thread_info* r; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 413 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 414 | p = DRD_(barrier_get)(barrier); |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 415 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 416 | if (s_trace_barrier) |
| 417 | { |
| 418 | VG_(message)(Vg_UserMsg, |
| 419 | "[%d/%d] barrier_post_wait %s 0x%lx iteration %ld%s", |
| 420 | VG_(get_running_tid)(), |
| 421 | tid, |
| 422 | p ? barrier_get_typename(p) : "(?)", |
| 423 | barrier, |
| 424 | p ? p->post_iteration : -1, |
| 425 | serializing ? " (serializing)" : ""); |
| 426 | } |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 427 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 428 | /* |
| 429 | * If p == 0, this means that the barrier has been destroyed after |
| 430 | * *_barrier_wait() returned and before this function was called. Just |
| 431 | * return in that case -- race conditions between *_barrier_wait() |
| 432 | * and *_barrier_destroy() are detected by the *_barrier_destroy() wrapper. |
| 433 | */ |
| 434 | if (p == 0) |
| 435 | return; |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 436 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 437 | /* If the *_barrier_wait() call returned an error code, exit. */ |
| 438 | if (! waited) |
| 439 | return; |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame] | 440 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 441 | q = VG_(OSetGen_Lookup)(p->oset, &word_tid); |
| 442 | if (q == 0) |
| 443 | { |
| 444 | BarrierErrInfo bei = { p->a1, 0, 0 }; |
| 445 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 446 | BarrierErr, |
| 447 | VG_(get_IP)(VG_(get_running_tid)()), |
| 448 | "Error in barrier implementation" |
| 449 | " -- barrier_wait() started before" |
| 450 | " barrier_destroy() and finished after" |
| 451 | " barrier_destroy()", |
| 452 | &bei); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 453 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 454 | q = VG_(OSetGen_AllocNode)(p->oset, sizeof(*q)); |
| 455 | DRD_(barrier_thread_initialize)(q, tid, p->pre_iteration); |
| 456 | VG_(OSetGen_Insert)(p->oset, q); |
| 457 | tl_assert(VG_(OSetGen_Lookup)(p->oset, &word_tid) == q); |
| 458 | } |
| 459 | /* |
| 460 | * Combine all vector clocks that were stored in the pre_barrier_wait |
| 461 | * wrapper with the vector clock of the current thread. |
| 462 | */ |
| 463 | VG_(OSetGen_ResetIter)(p->oset); |
| 464 | for ( ; (r = VG_(OSetGen_Next)(p->oset)) != 0; ) |
| 465 | { |
| 466 | if (r != q) |
| 467 | { |
| 468 | tl_assert(r->sg[p->post_iteration]); |
| 469 | DRD_(thread_combine_vc2)(tid, &r->sg[p->post_iteration]->vc); |
| 470 | } |
| 471 | } |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 472 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 473 | /* Create a new segment and store a pointer to that segment. */ |
| 474 | DRD_(thread_new_segment)(tid); |
| 475 | DRD_(thread_get_latest_segment)(&q->post_wait_sg, tid); |
| 476 | s_barrier_segment_creation_count++; |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 477 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 478 | /* |
| 479 | * If the same number of threads as the barrier count indicates have |
| 480 | * called the post *_barrier_wait() wrapper, toggle p->post_iteration and |
| 481 | * reset the p->post_waiters_left counter. |
| 482 | */ |
| 483 | if (--p->post_waiters_left <= 0) |
| 484 | { |
| 485 | p->post_iteration = 1 - p->post_iteration; |
| 486 | p->post_waiters_left = p->count; |
| 487 | } |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 488 | } |
| 489 | |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame] | 490 | /** Called when thread tid stops to exist. */ |
| 491 | static void barrier_delete_thread(struct barrier_info* const p, |
| 492 | const DrdThreadId tid) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 493 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 494 | struct barrier_thread_info* q; |
| 495 | const UWord word_tid = tid; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 496 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 497 | q = VG_(OSetGen_Remove)(p->oset, &word_tid); |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame] | 498 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 499 | /* |
| 500 | * q is only non-zero if the barrier object has been used by thread tid |
| 501 | * after the barrier_init() call and before the thread finished. |
| 502 | */ |
| 503 | if (q) |
| 504 | { |
| 505 | DRD_(barrier_thread_destroy)(q); |
| 506 | VG_(OSetGen_FreeNode)(p->oset, q); |
| 507 | } |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 508 | } |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 509 | |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame] | 510 | /** |
| 511 | * Report that *_barrier_destroy() has been called but that this call was |
| 512 | * not synchronized with the last *_barrier_wait() call on the same barrier. |
bart | 776a91e | 2009-02-22 09:29:07 +0000 | [diff] [blame] | 513 | * |
| 514 | * This topic has been discussed extensively on comp.programming.threads |
| 515 | * (February 3, 2009). See also |
| 516 | * <a href="http://groups.google.com/group/comp.programming.threads/browse_thread/thread/4f65535d6192aa50/a5f4bf1e3b437c4d">Immediately destroying pthread barriers</a>. |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame] | 517 | */ |
| 518 | static |
| 519 | void barrier_report_wait_delete_race(const struct barrier_info* const p, |
| 520 | const struct barrier_thread_info* const q) |
| 521 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 522 | tl_assert(p); |
| 523 | tl_assert(q); |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame] | 524 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 525 | { |
| 526 | BarrierErrInfo bei |
| 527 | = { p->a1, q->tid, q->wait_call_ctxt }; |
| 528 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 529 | BarrierErr, |
| 530 | VG_(get_IP)(VG_(get_running_tid)()), |
| 531 | "Destruction of barrier not synchronized with" |
| 532 | " barrier wait call", |
| 533 | &bei); |
| 534 | } |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | static const char* barrier_get_typename(struct barrier_info* const p) |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 538 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 539 | tl_assert(p); |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 540 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 541 | return barrier_type_name(p->barrier_type); |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 542 | } |
| 543 | |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame] | 544 | static const char* barrier_type_name(const BarrierT bt) |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 545 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 546 | switch (bt) |
| 547 | { |
| 548 | case pthread_barrier: |
| 549 | return "pthread barrier"; |
| 550 | case gomp_barrier: |
| 551 | return "gomp barrier"; |
| 552 | } |
| 553 | return "?"; |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 554 | } |
bart | 6bbefaf | 2008-04-19 15:16:45 +0000 | [diff] [blame] | 555 | |
bart | a8cf765 | 2009-02-15 11:00:29 +0000 | [diff] [blame] | 556 | ULong DRD_(get_barrier_segment_creation_count)(void) |
bart | 6bbefaf | 2008-04-19 15:16:45 +0000 | [diff] [blame] | 557 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame^] | 558 | return s_barrier_segment_creation_count; |
bart | 6bbefaf | 2008-04-19 15:16:45 +0000 | [diff] [blame] | 559 | } |