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