sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 1 | /* |
| 2 | This file is part of drd, a data race detector. |
| 3 | |
| 4 | Copyright (C) 2006-2008 Bart Van Assche |
| 5 | bart.vanassche@gmail.com |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or |
| 8 | modify it under the terms of the GNU General Public License as |
| 9 | published by the Free Software Foundation; either version 2 of the |
| 10 | License, or (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, but |
| 13 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program; if not, write to the Free Software |
| 19 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 20 | 02111-1307, USA. |
| 21 | |
| 22 | The GNU General Public License is contained in the file COPYING. |
| 23 | */ |
| 24 | |
| 25 | |
| 26 | #include "drd_barrier.h" |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 27 | #include "drd_clientobj.h" |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 28 | #include "drd_error.h" |
| 29 | #include "drd_suppression.h" |
| 30 | #include "priv_drd_clientreq.h" |
| 31 | #include "pub_tool_errormgr.h" // VG_(maybe_record_error)() |
| 32 | #include "pub_tool_libcassert.h" // tl_assert() |
| 33 | #include "pub_tool_libcprint.h" // VG_(printf)() |
| 34 | #include "pub_tool_machine.h" // VG_(get_IP)() |
| 35 | #include "pub_tool_mallocfree.h" // VG_(malloc)(), VG_(free)() |
| 36 | #include "pub_tool_oset.h" |
| 37 | #include "pub_tool_threadstate.h" // VG_(get_running_tid)() |
| 38 | |
| 39 | |
| 40 | // Type definitions. |
| 41 | |
bart | 8ddef88 | 2008-03-09 08:48:01 +0000 | [diff] [blame] | 42 | /** Information associated with one thread participating in a barrier. */ |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 43 | struct barrier_thread_info |
| 44 | { |
| 45 | UWord tid; // A DrdThreadId |
| 46 | Word iteration; // iteration of last pthread_barrier_wait() |
| 47 | // call thread tid participated in. |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame^] | 48 | Segment* sg[2]; // Segments of the last two |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 49 | // pthread_barrier() calls by thread tid. |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 53 | // Local functions. |
| 54 | |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 55 | static void barrier_cleanup(struct barrier_info* p); |
| 56 | static const char* barrier_get_typename(struct barrier_info* const p); |
| 57 | static const char* barrier_type_name(const BarrierT bt); |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 58 | |
| 59 | |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 60 | // Local variables. |
| 61 | |
| 62 | static Bool s_trace_barrier = False; |
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 | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame^] | 87 | sg_put(p->sg[0]); |
| 88 | sg_put(p->sg[1]); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 89 | } |
| 90 | |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 91 | /** Initialize the structure *p with the specified client-side barrier address, |
| 92 | * barrier object size and number of participants in each barrier. */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 93 | static |
| 94 | void barrier_initialize(struct barrier_info* const p, |
| 95 | const Addr barrier, |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 96 | const BarrierT barrier_type, |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 97 | const Word count) |
| 98 | { |
| 99 | tl_assert(barrier != 0); |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 100 | tl_assert(barrier_type == pthread_barrier || barrier_type == gomp_barrier); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 101 | tl_assert(count > 0); |
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)); |
| 114 | p->oset = VG_(OSetGen_Create)(0, 0, VG_(malloc), VG_(free)); |
| 115 | } |
| 116 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 117 | /** Deallocate the memory allocated by barrier_initialize() and in p->oset. |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 118 | * Called by clientobj_destroy(). |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 119 | */ |
| 120 | void barrier_cleanup(struct barrier_info* p) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 121 | { |
| 122 | struct barrier_thread_info* q; |
| 123 | |
| 124 | tl_assert(p); |
| 125 | |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 126 | if (p->pre_waiters_left != p->count) |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 127 | { |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 128 | BarrierErrInfo bei = { p->a1 }; |
| 129 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 130 | BarrierErr, |
| 131 | VG_(get_IP)(VG_(get_running_tid)()), |
| 132 | "Destruction of barrier that is being waited" |
| 133 | " upon", |
| 134 | &bei); |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 135 | } |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 136 | |
| 137 | VG_(OSetGen_ResetIter)(p->oset); |
| 138 | for ( ; (q = VG_(OSetGen_Next)(p->oset)) != 0; ) |
| 139 | { |
| 140 | barrier_thread_destroy(q); |
| 141 | } |
| 142 | VG_(OSetGen_Destroy)(p->oset); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 143 | } |
| 144 | |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 145 | /** Look up the client-side barrier address barrier in s_barrier[]. If not |
| 146 | * found, add it. */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 147 | static |
| 148 | struct barrier_info* |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 149 | barrier_get_or_allocate(const Addr barrier, |
| 150 | const BarrierT barrier_type, const Word count) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 151 | { |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 152 | struct barrier_info *p; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 153 | |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 154 | tl_assert(barrier_type == pthread_barrier || barrier_type == gomp_barrier); |
| 155 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 156 | tl_assert(offsetof(DrdClientobj, barrier) == 0); |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 157 | p = &clientobj_get(barrier, ClientBarrier)->barrier; |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 158 | if (p == 0) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 159 | { |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 160 | p = &clientobj_add(barrier, ClientBarrier)->barrier; |
| 161 | barrier_initialize(p, barrier, barrier_type, count); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 162 | } |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 163 | return p; |
| 164 | } |
| 165 | |
| 166 | /** Look up the address of the information associated with the client-side |
| 167 | * barrier object. */ |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 168 | static struct barrier_info* barrier_get(const Addr barrier) |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 169 | { |
| 170 | tl_assert(offsetof(DrdClientobj, barrier) == 0); |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 171 | return &clientobj_get(barrier, ClientBarrier)->barrier; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 172 | } |
| 173 | |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 174 | /** Initialize a barrier with client address barrier, client size size, and |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 175 | * where count threads participate in each barrier. |
| 176 | * Called before pthread_barrier_init(). |
| 177 | */ |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 178 | void barrier_init(const Addr barrier, |
| 179 | const BarrierT barrier_type, const Word count, |
| 180 | const Bool reinitialization) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 181 | { |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 182 | struct barrier_info* p; |
| 183 | |
| 184 | tl_assert(barrier_type == pthread_barrier || barrier_type == gomp_barrier); |
| 185 | |
| 186 | p = barrier_get_or_allocate(barrier, barrier_type, count); |
| 187 | |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 188 | if (s_trace_barrier) |
| 189 | { |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 190 | if (reinitialization) |
| 191 | { |
| 192 | VG_(message)(Vg_UserMsg, |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame^] | 193 | "[%d/%d] barrier_reinit %s 0x%lx count %ld -> %ld", |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 194 | VG_(get_running_tid)(), |
| 195 | thread_get_running_tid(), |
| 196 | barrier_get_typename(p), |
| 197 | barrier, |
| 198 | p->count, |
| 199 | count); |
| 200 | } |
| 201 | else |
| 202 | { |
| 203 | VG_(message)(Vg_UserMsg, |
| 204 | "[%d/%d] barrier_init %s 0x%lx", |
| 205 | VG_(get_running_tid)(), |
| 206 | thread_get_running_tid(), |
| 207 | barrier_get_typename(p), |
| 208 | barrier); |
| 209 | } |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 210 | } |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 211 | |
| 212 | if (reinitialization && p->count != count) |
| 213 | { |
| 214 | if (p->pre_waiters_left != p->count || p->post_waiters_left != p->count) |
| 215 | { |
| 216 | VG_(message)(Vg_UserMsg, "Error: reinitialization with active waiters"); |
| 217 | } |
| 218 | p->count = count; |
| 219 | } |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 220 | } |
| 221 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 222 | /** Called after pthread_barrier_destroy(). */ |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 223 | void barrier_destroy(const Addr barrier, const BarrierT barrier_type) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 224 | { |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 225 | struct barrier_info* p; |
| 226 | |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 227 | p = barrier_get(barrier); |
| 228 | |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 229 | if (s_trace_barrier) |
| 230 | { |
| 231 | VG_(message)(Vg_UserMsg, |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 232 | "[%d/%d] barrier_destroy %s 0x%lx", |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 233 | VG_(get_running_tid)(), |
| 234 | thread_get_running_tid(), |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 235 | barrier_get_typename(p), |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 236 | barrier); |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 237 | } |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 238 | |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 239 | if (p == 0) |
| 240 | { |
| 241 | GenericErrInfo GEI; |
| 242 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 243 | GenericErr, |
| 244 | VG_(get_IP)(VG_(get_running_tid)()), |
| 245 | "Not a barrier", |
| 246 | &GEI); |
| 247 | return; |
| 248 | } |
| 249 | |
| 250 | clientobj_remove(p->a1, ClientBarrier); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 251 | } |
| 252 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 253 | /** Called before pthread_barrier_wait(). */ |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 254 | void barrier_pre_wait(const DrdThreadId tid, const Addr barrier, |
| 255 | const BarrierT barrier_type) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 256 | { |
| 257 | struct barrier_info* p; |
| 258 | struct barrier_thread_info* q; |
| 259 | const UWord word_tid = tid; |
| 260 | |
| 261 | p = barrier_get(barrier); |
bart | c68bd60 | 2008-03-16 10:04:58 +0000 | [diff] [blame] | 262 | if (p == 0 && barrier_type == gomp_barrier) |
| 263 | { |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame^] | 264 | VG_(message)(Vg_UserMsg, "%s", ""); |
bart | c68bd60 | 2008-03-16 10:04:58 +0000 | [diff] [blame] | 265 | VG_(message)(Vg_UserMsg, |
| 266 | "Please verify whether gcc has been configured" |
| 267 | " with option --disable-linux-futex."); |
| 268 | VG_(message)(Vg_UserMsg, |
| 269 | "See also the section about OpenMP in the DRD manual."); |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame^] | 270 | VG_(message)(Vg_UserMsg, "%s", ""); |
bart | c68bd60 | 2008-03-16 10:04:58 +0000 | [diff] [blame] | 271 | } |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 272 | tl_assert(p); |
| 273 | |
| 274 | if (s_trace_barrier) |
| 275 | { |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 276 | VG_(message)(Vg_UserMsg, |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame^] | 277 | "[%d/%d] barrier_pre_wait %s 0x%lx iteration %ld", |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 278 | VG_(get_running_tid)(), |
| 279 | thread_get_running_tid(), |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 280 | barrier_get_typename(p), |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 281 | barrier, |
| 282 | p->pre_iteration); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 283 | } |
| 284 | |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 285 | q = VG_(OSetGen_Lookup)(p->oset, &word_tid); |
| 286 | if (q == 0) |
| 287 | { |
| 288 | q = VG_(OSetGen_AllocNode)(p->oset, sizeof(*q)); |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 289 | barrier_thread_initialize(q, tid, p->pre_iteration); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 290 | VG_(OSetGen_Insert)(p->oset, q); |
| 291 | tl_assert(VG_(OSetGen_Lookup)(p->oset, &word_tid) == q); |
| 292 | } |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame^] | 293 | thread_get_latest_segment(&q->sg[p->pre_iteration], tid); |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 294 | |
| 295 | if (--p->pre_waiters_left <= 0) |
| 296 | { |
| 297 | p->pre_iteration = 1 - p->pre_iteration; |
| 298 | p->pre_waiters_left = p->count; |
| 299 | } |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 300 | } |
| 301 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 302 | /** Called after pthread_barrier_wait(). */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 303 | void barrier_post_wait(const DrdThreadId tid, const Addr barrier, |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 304 | const BarrierT barrier_type, const Bool waited) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 305 | { |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 306 | struct barrier_info* p; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 307 | |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 308 | p = barrier_get(barrier); |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 309 | |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 310 | if (s_trace_barrier) |
| 311 | { |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 312 | VG_(message)(Vg_UserMsg, |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame^] | 313 | "[%d/%d] barrier_post_wait %s 0x%lx iteration %ld", |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 314 | VG_(get_running_tid)(), |
| 315 | tid, |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 316 | p ? barrier_get_typename(p) : "(?)", |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 317 | barrier, |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 318 | p ? p->post_iteration : -1); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 319 | } |
| 320 | |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 321 | /* If p == 0, this means that the barrier has been destroyed after */ |
| 322 | /* *_barrier_wait() returned and before this function was called. Just */ |
| 323 | /* return in that case. */ |
| 324 | if (p == 0) |
| 325 | return; |
| 326 | |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 327 | if (waited) |
| 328 | { |
| 329 | const UWord word_tid = tid; |
| 330 | struct barrier_thread_info* q; |
| 331 | struct barrier_thread_info* r; |
| 332 | |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 333 | q = VG_(OSetGen_Lookup)(p->oset, &word_tid); |
| 334 | tl_assert(q); |
| 335 | VG_(OSetGen_ResetIter)(p->oset); |
| 336 | for ( ; (r = VG_(OSetGen_Next)(p->oset)) != 0; ) |
| 337 | { |
| 338 | if (r != q) |
| 339 | { |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame^] | 340 | tl_assert(r->sg[p->post_iteration]); |
| 341 | thread_combine_vc2(tid, &r->sg[p->post_iteration]->vc); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 342 | } |
| 343 | } |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 344 | |
| 345 | thread_new_segment(tid); |
| 346 | |
| 347 | if (--p->post_waiters_left <= 0) |
| 348 | { |
| 349 | p->post_iteration = 1 - p->post_iteration; |
| 350 | p->post_waiters_left = p->count; |
| 351 | } |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 355 | /** Call this function when thread tid stops to exist. */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 356 | void barrier_thread_delete(const DrdThreadId tid) |
| 357 | { |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 358 | struct barrier_info* p; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 359 | |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 360 | clientobj_resetiter(); |
| 361 | for ( ; (p = &clientobj_next(ClientBarrier)->barrier) != 0; ) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 362 | { |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 363 | struct barrier_thread_info* q; |
| 364 | const UWord word_tid = tid; |
| 365 | q = VG_(OSetGen_Remove)(p->oset, &word_tid); |
| 366 | barrier_thread_destroy(q); |
| 367 | VG_(OSetGen_FreeNode)(p->oset, q); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 368 | } |
| 369 | } |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 370 | |
| 371 | static const char* barrier_get_typename(struct barrier_info* const p) |
| 372 | { |
| 373 | tl_assert(p); |
| 374 | |
| 375 | return barrier_type_name(p->barrier_type); |
| 376 | } |
| 377 | |
| 378 | static const char* barrier_type_name(const BarrierT bt) |
| 379 | { |
| 380 | switch (bt) |
| 381 | { |
| 382 | case pthread_barrier: |
| 383 | return "pthread barrier"; |
| 384 | case gomp_barrier: |
| 385 | return "gomp barrier"; |
| 386 | } |
| 387 | return "?"; |
| 388 | } |