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" |
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 | |
| 39 | // Type definitions. |
| 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 | { |
| 44 | UWord tid; // A DrdThreadId |
| 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. |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 52 | // Local functions. |
| 53 | |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 54 | static void barrier_cleanup(struct barrier_info* p); |
| 55 | static const char* barrier_get_typename(struct barrier_info* const p); |
| 56 | static const char* barrier_type_name(const BarrierT bt); |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 57 | |
| 58 | |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 59 | // Local variables. |
| 60 | |
| 61 | static Bool s_trace_barrier = False; |
bart | 6bbefaf | 2008-04-19 15:16:45 +0000 | [diff] [blame] | 62 | static ULong s_barrier_segment_creation_count; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 63 | |
| 64 | |
| 65 | // Function definitions. |
| 66 | |
| 67 | void barrier_set_trace(const Bool trace_barrier) |
| 68 | { |
| 69 | s_trace_barrier = trace_barrier; |
| 70 | } |
| 71 | |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 72 | /** Initialize the structure *p with the specified thread ID and iteration |
| 73 | * information. */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 74 | static void barrier_thread_initialize(struct barrier_thread_info* const p, |
| 75 | const DrdThreadId tid, |
| 76 | const Word iteration) |
| 77 | { |
| 78 | p->tid = tid; |
| 79 | p->iteration = iteration; |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 80 | p->sg[0] = 0; |
| 81 | p->sg[1] = 0; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 82 | } |
| 83 | |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 84 | /** Deallocate the memory that was allocated in barrier_thread_initialize(). */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 85 | static void barrier_thread_destroy(struct barrier_thread_info* const p) |
| 86 | { |
bart | 22f7457 | 2008-07-07 08:17:55 +0000 | [diff] [blame] | 87 | tl_assert(p); |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 88 | sg_put(p->sg[0]); |
| 89 | sg_put(p->sg[1]); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 90 | } |
| 91 | |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 92 | /** Initialize the structure *p with the specified client-side barrier address, |
| 93 | * barrier object size and number of participants in each barrier. */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 94 | static |
| 95 | void barrier_initialize(struct barrier_info* const p, |
| 96 | const Addr barrier, |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 97 | const BarrierT barrier_type, |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 98 | const Word count) |
| 99 | { |
| 100 | tl_assert(barrier != 0); |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 101 | tl_assert(barrier_type == pthread_barrier || barrier_type == gomp_barrier); |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 102 | tl_assert(p->a1 == barrier); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 103 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 104 | p->cleanup = (void(*)(DrdClientobj*))barrier_cleanup; |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 105 | p->barrier_type = barrier_type; |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 106 | p->count = count; |
| 107 | p->pre_iteration = 0; |
| 108 | p->post_iteration = 0; |
| 109 | p->pre_waiters_left = count; |
| 110 | p->post_waiters_left = count; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 111 | tl_assert(sizeof(((struct barrier_thread_info*)0)->tid) == sizeof(Word)); |
| 112 | tl_assert(sizeof(((struct barrier_thread_info*)0)->tid) |
| 113 | >= sizeof(DrdThreadId)); |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 114 | p->oset = VG_(OSetGen_Create)(0, 0, VG_(malloc), "drd.barrier.bi.1", |
| 115 | VG_(free)); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 116 | } |
| 117 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 118 | /** Deallocate the memory allocated by barrier_initialize() and in p->oset. |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 119 | * Called by clientobj_destroy(). |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 120 | */ |
| 121 | void barrier_cleanup(struct barrier_info* p) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 122 | { |
| 123 | struct barrier_thread_info* q; |
| 124 | |
| 125 | tl_assert(p); |
| 126 | |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 127 | if (p->pre_waiters_left != p->count) |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 128 | { |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 129 | BarrierErrInfo bei = { p->a1 }; |
| 130 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 131 | BarrierErr, |
| 132 | VG_(get_IP)(VG_(get_running_tid)()), |
| 133 | "Destruction of barrier that is being waited" |
| 134 | " upon", |
| 135 | &bei); |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 136 | } |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 137 | |
| 138 | VG_(OSetGen_ResetIter)(p->oset); |
| 139 | for ( ; (q = VG_(OSetGen_Next)(p->oset)) != 0; ) |
| 140 | { |
| 141 | barrier_thread_destroy(q); |
| 142 | } |
| 143 | VG_(OSetGen_Destroy)(p->oset); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 144 | } |
| 145 | |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 146 | /** Look up the client-side barrier address barrier in s_barrier[]. If not |
| 147 | * found, add it. */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 148 | static |
| 149 | struct barrier_info* |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 150 | barrier_get_or_allocate(const Addr barrier, |
| 151 | const BarrierT barrier_type, const Word count) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 152 | { |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 153 | struct barrier_info *p; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 154 | |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 155 | tl_assert(barrier_type == pthread_barrier || barrier_type == gomp_barrier); |
| 156 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 157 | tl_assert(offsetof(DrdClientobj, barrier) == 0); |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 158 | p = &clientobj_get(barrier, ClientBarrier)->barrier; |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 159 | if (p == 0) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 160 | { |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 161 | p = &clientobj_add(barrier, ClientBarrier)->barrier; |
| 162 | barrier_initialize(p, barrier, barrier_type, count); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 163 | } |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 164 | return p; |
| 165 | } |
| 166 | |
| 167 | /** Look up the address of the information associated with the client-side |
| 168 | * barrier object. */ |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 169 | static struct barrier_info* barrier_get(const Addr barrier) |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 170 | { |
| 171 | tl_assert(offsetof(DrdClientobj, barrier) == 0); |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 172 | return &clientobj_get(barrier, ClientBarrier)->barrier; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 173 | } |
| 174 | |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 175 | /** Initialize a barrier with client address barrier, client size size, and |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 176 | * where count threads participate in each barrier. |
| 177 | * Called before pthread_barrier_init(). |
| 178 | */ |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 179 | void barrier_init(const Addr barrier, |
| 180 | const BarrierT barrier_type, const Word count, |
| 181 | const Bool reinitialization) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 182 | { |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 183 | struct barrier_info* p; |
| 184 | |
| 185 | tl_assert(barrier_type == pthread_barrier || barrier_type == gomp_barrier); |
| 186 | |
bart | fcff0b9 | 2008-11-17 17:35:26 +0000 | [diff] [blame] | 187 | if (count == 0) |
| 188 | { |
| 189 | BarrierErrInfo bei = { barrier }; |
| 190 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 191 | BarrierErr, |
| 192 | VG_(get_IP)(VG_(get_running_tid)()), |
| 193 | "pthread_barrier_init: 'count' argument is zero", |
| 194 | &bei); |
| 195 | } |
| 196 | |
bart | d9e39ec | 2008-06-28 15:03:26 +0000 | [diff] [blame] | 197 | if (! reinitialization && barrier_type == pthread_barrier) |
| 198 | { |
| 199 | p = barrier_get(barrier); |
| 200 | if (p) |
| 201 | { |
| 202 | BarrierErrInfo bei = { barrier }; |
| 203 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 204 | BarrierErr, |
| 205 | VG_(get_IP)(VG_(get_running_tid)()), |
bart | fcff0b9 | 2008-11-17 17:35:26 +0000 | [diff] [blame] | 206 | "Barrier reinitialization", |
bart | d9e39ec | 2008-06-28 15:03:26 +0000 | [diff] [blame] | 207 | &bei); |
| 208 | } |
| 209 | } |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 210 | p = barrier_get_or_allocate(barrier, barrier_type, count); |
| 211 | |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 212 | if (s_trace_barrier) |
| 213 | { |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 214 | if (reinitialization) |
| 215 | { |
| 216 | VG_(message)(Vg_UserMsg, |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 217 | "[%d/%d] barrier_reinit %s 0x%lx count %ld -> %ld", |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 218 | VG_(get_running_tid)(), |
| 219 | thread_get_running_tid(), |
| 220 | barrier_get_typename(p), |
| 221 | barrier, |
| 222 | p->count, |
| 223 | count); |
| 224 | } |
| 225 | else |
| 226 | { |
| 227 | VG_(message)(Vg_UserMsg, |
| 228 | "[%d/%d] barrier_init %s 0x%lx", |
| 229 | VG_(get_running_tid)(), |
| 230 | thread_get_running_tid(), |
| 231 | barrier_get_typename(p), |
| 232 | barrier); |
| 233 | } |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 234 | } |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 235 | |
| 236 | if (reinitialization && p->count != count) |
| 237 | { |
| 238 | if (p->pre_waiters_left != p->count || p->post_waiters_left != p->count) |
| 239 | { |
bart | 195a398 | 2008-07-01 13:15:31 +0000 | [diff] [blame] | 240 | BarrierErrInfo bei = { p->a1 }; |
| 241 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 242 | BarrierErr, |
| 243 | VG_(get_IP)(VG_(get_running_tid)()), |
| 244 | "Reinitialization of barrier with active" |
| 245 | " waiters", |
| 246 | &bei); |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 247 | } |
| 248 | p->count = count; |
| 249 | } |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 250 | } |
| 251 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 252 | /** Called after pthread_barrier_destroy(). */ |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 253 | void barrier_destroy(const Addr barrier, const BarrierT barrier_type) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 254 | { |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 255 | struct barrier_info* p; |
| 256 | |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 257 | p = barrier_get(barrier); |
| 258 | |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 259 | if (s_trace_barrier) |
| 260 | { |
| 261 | VG_(message)(Vg_UserMsg, |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 262 | "[%d/%d] barrier_destroy %s 0x%lx", |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 263 | VG_(get_running_tid)(), |
| 264 | thread_get_running_tid(), |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 265 | barrier_get_typename(p), |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 266 | barrier); |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 267 | } |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 268 | |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 269 | if (p == 0) |
| 270 | { |
| 271 | GenericErrInfo GEI; |
| 272 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 273 | GenericErr, |
| 274 | VG_(get_IP)(VG_(get_running_tid)()), |
| 275 | "Not a barrier", |
| 276 | &GEI); |
| 277 | return; |
| 278 | } |
| 279 | |
bart | 195a398 | 2008-07-01 13:15:31 +0000 | [diff] [blame] | 280 | if (p->pre_waiters_left != p->count || p->post_waiters_left != p->count) |
| 281 | { |
| 282 | BarrierErrInfo bei = { p->a1 }; |
| 283 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 284 | BarrierErr, |
| 285 | VG_(get_IP)(VG_(get_running_tid)()), |
| 286 | "Destruction of a barrier with active waiters", |
| 287 | &bei); |
| 288 | } |
| 289 | |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 290 | clientobj_remove(p->a1, ClientBarrier); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 291 | } |
| 292 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 293 | /** Called before pthread_barrier_wait(). */ |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 294 | void barrier_pre_wait(const DrdThreadId tid, const Addr barrier, |
| 295 | const BarrierT barrier_type) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 296 | { |
| 297 | struct barrier_info* p; |
| 298 | struct barrier_thread_info* q; |
| 299 | const UWord word_tid = tid; |
| 300 | |
| 301 | p = barrier_get(barrier); |
bart | c68bd60 | 2008-03-16 10:04:58 +0000 | [diff] [blame] | 302 | if (p == 0 && barrier_type == gomp_barrier) |
| 303 | { |
bart | 654013c | 2008-06-23 12:41:00 +0000 | [diff] [blame] | 304 | VG_(message)(Vg_UserMsg, ""); |
bart | c68bd60 | 2008-03-16 10:04:58 +0000 | [diff] [blame] | 305 | VG_(message)(Vg_UserMsg, |
| 306 | "Please verify whether gcc has been configured" |
| 307 | " with option --disable-linux-futex."); |
| 308 | VG_(message)(Vg_UserMsg, |
| 309 | "See also the section about OpenMP in the DRD manual."); |
bart | 654013c | 2008-06-23 12:41:00 +0000 | [diff] [blame] | 310 | VG_(message)(Vg_UserMsg, ""); |
bart | c68bd60 | 2008-03-16 10:04:58 +0000 | [diff] [blame] | 311 | } |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 312 | tl_assert(p); |
| 313 | |
| 314 | if (s_trace_barrier) |
| 315 | { |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 316 | VG_(message)(Vg_UserMsg, |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 317 | "[%d/%d] barrier_pre_wait %s 0x%lx iteration %ld", |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 318 | VG_(get_running_tid)(), |
| 319 | thread_get_running_tid(), |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 320 | barrier_get_typename(p), |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 321 | barrier, |
| 322 | p->pre_iteration); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 323 | } |
| 324 | |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 325 | q = VG_(OSetGen_Lookup)(p->oset, &word_tid); |
| 326 | if (q == 0) |
| 327 | { |
| 328 | q = VG_(OSetGen_AllocNode)(p->oset, sizeof(*q)); |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 329 | barrier_thread_initialize(q, tid, p->pre_iteration); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 330 | VG_(OSetGen_Insert)(p->oset, q); |
| 331 | tl_assert(VG_(OSetGen_Lookup)(p->oset, &word_tid) == q); |
| 332 | } |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 333 | thread_get_latest_segment(&q->sg[p->pre_iteration], tid); |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 334 | |
| 335 | if (--p->pre_waiters_left <= 0) |
| 336 | { |
| 337 | p->pre_iteration = 1 - p->pre_iteration; |
| 338 | p->pre_waiters_left = p->count; |
| 339 | } |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 340 | } |
| 341 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 342 | /** Called after pthread_barrier_wait(). */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 343 | void barrier_post_wait(const DrdThreadId tid, const Addr barrier, |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 344 | const BarrierT barrier_type, const Bool waited) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 345 | { |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 346 | struct barrier_info* p; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 347 | |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 348 | p = barrier_get(barrier); |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 349 | |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 350 | if (s_trace_barrier) |
| 351 | { |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 352 | VG_(message)(Vg_UserMsg, |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 353 | "[%d/%d] barrier_post_wait %s 0x%lx iteration %ld", |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 354 | VG_(get_running_tid)(), |
| 355 | tid, |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 356 | p ? barrier_get_typename(p) : "(?)", |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 357 | barrier, |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 358 | p ? p->post_iteration : -1); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 359 | } |
| 360 | |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 361 | /* If p == 0, this means that the barrier has been destroyed after */ |
| 362 | /* *_barrier_wait() returned and before this function was called. Just */ |
| 363 | /* return in that case. */ |
| 364 | if (p == 0) |
| 365 | return; |
| 366 | |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 367 | if (waited) |
| 368 | { |
| 369 | const UWord word_tid = tid; |
| 370 | struct barrier_thread_info* q; |
| 371 | struct barrier_thread_info* r; |
| 372 | |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 373 | q = VG_(OSetGen_Lookup)(p->oset, &word_tid); |
bart | 195a398 | 2008-07-01 13:15:31 +0000 | [diff] [blame] | 374 | if (q == 0) |
| 375 | { |
| 376 | BarrierErrInfo bei = { p->a1 }; |
| 377 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 378 | BarrierErr, |
| 379 | VG_(get_IP)(VG_(get_running_tid)()), |
| 380 | "Error in barrier implementation" |
| 381 | " -- barrier_wait() started before" |
| 382 | " barrier_destroy() and finished after" |
| 383 | " barrier_destroy()", |
| 384 | &bei); |
| 385 | |
| 386 | q = VG_(OSetGen_AllocNode)(p->oset, sizeof(*q)); |
| 387 | barrier_thread_initialize(q, tid, p->pre_iteration); |
| 388 | VG_(OSetGen_Insert)(p->oset, q); |
| 389 | tl_assert(VG_(OSetGen_Lookup)(p->oset, &word_tid) == q); |
| 390 | } |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 391 | VG_(OSetGen_ResetIter)(p->oset); |
| 392 | for ( ; (r = VG_(OSetGen_Next)(p->oset)) != 0; ) |
| 393 | { |
| 394 | if (r != q) |
| 395 | { |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 396 | tl_assert(r->sg[p->post_iteration]); |
| 397 | thread_combine_vc2(tid, &r->sg[p->post_iteration]->vc); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 398 | } |
| 399 | } |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 400 | |
| 401 | thread_new_segment(tid); |
bart | 6bbefaf | 2008-04-19 15:16:45 +0000 | [diff] [blame] | 402 | s_barrier_segment_creation_count++; |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 403 | |
| 404 | if (--p->post_waiters_left <= 0) |
| 405 | { |
| 406 | p->post_iteration = 1 - p->post_iteration; |
| 407 | p->post_waiters_left = p->count; |
| 408 | } |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 412 | /** Call this function when thread tid stops to exist. */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 413 | void barrier_thread_delete(const DrdThreadId tid) |
| 414 | { |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 415 | struct barrier_info* p; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 416 | |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 417 | clientobj_resetiter(); |
| 418 | for ( ; (p = &clientobj_next(ClientBarrier)->barrier) != 0; ) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 419 | { |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 420 | struct barrier_thread_info* q; |
| 421 | const UWord word_tid = tid; |
| 422 | q = VG_(OSetGen_Remove)(p->oset, &word_tid); |
bart | 7f912c0 | 2008-07-07 08:45:55 +0000 | [diff] [blame] | 423 | /* q is only non-zero if the barrier object has been used by thread tid |
| 424 | * after the barrier_init() call and before the thread finished. |
| 425 | */ |
bart | 22f7457 | 2008-07-07 08:17:55 +0000 | [diff] [blame] | 426 | if (q) |
| 427 | { |
| 428 | barrier_thread_destroy(q); |
| 429 | VG_(OSetGen_FreeNode)(p->oset, q); |
| 430 | } |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 431 | } |
| 432 | } |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 433 | |
| 434 | static const char* barrier_get_typename(struct barrier_info* const p) |
| 435 | { |
| 436 | tl_assert(p); |
| 437 | |
| 438 | return barrier_type_name(p->barrier_type); |
| 439 | } |
| 440 | |
| 441 | static const char* barrier_type_name(const BarrierT bt) |
| 442 | { |
| 443 | switch (bt) |
| 444 | { |
| 445 | case pthread_barrier: |
| 446 | return "pthread barrier"; |
| 447 | case gomp_barrier: |
| 448 | return "gomp barrier"; |
| 449 | } |
| 450 | return "?"; |
| 451 | } |
bart | 6bbefaf | 2008-04-19 15:16:45 +0000 | [diff] [blame] | 452 | |
| 453 | ULong get_barrier_segment_creation_count(void) |
| 454 | { |
| 455 | return s_barrier_segment_creation_count; |
| 456 | } |