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