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; |
bart | 6bbefaf | 2008-04-19 15:16:45 +0000 | [diff] [blame^] | 63 | static ULong s_barrier_segment_creation_count; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 64 | |
| 65 | |
| 66 | // Function definitions. |
| 67 | |
| 68 | void barrier_set_trace(const Bool trace_barrier) |
| 69 | { |
| 70 | s_trace_barrier = trace_barrier; |
| 71 | } |
| 72 | |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 73 | /** Initialize the structure *p with the specified thread ID and iteration |
| 74 | * information. */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 75 | static void barrier_thread_initialize(struct barrier_thread_info* const p, |
| 76 | const DrdThreadId tid, |
| 77 | const Word iteration) |
| 78 | { |
| 79 | p->tid = tid; |
| 80 | p->iteration = iteration; |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 81 | p->sg[0] = 0; |
| 82 | p->sg[1] = 0; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 83 | } |
| 84 | |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 85 | /** Deallocate the memory that was allocated in barrier_thread_initialize(). */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 86 | static void barrier_thread_destroy(struct barrier_thread_info* const p) |
| 87 | { |
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)); |
| 115 | p->oset = VG_(OSetGen_Create)(0, 0, VG_(malloc), VG_(free)); |
| 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 | |
| 187 | p = barrier_get_or_allocate(barrier, barrier_type, count); |
| 188 | |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 189 | if (s_trace_barrier) |
| 190 | { |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 191 | if (reinitialization) |
| 192 | { |
| 193 | VG_(message)(Vg_UserMsg, |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 194 | "[%d/%d] barrier_reinit %s 0x%lx count %ld -> %ld", |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 195 | VG_(get_running_tid)(), |
| 196 | thread_get_running_tid(), |
| 197 | barrier_get_typename(p), |
| 198 | barrier, |
| 199 | p->count, |
| 200 | count); |
| 201 | } |
| 202 | else |
| 203 | { |
| 204 | VG_(message)(Vg_UserMsg, |
| 205 | "[%d/%d] barrier_init %s 0x%lx", |
| 206 | VG_(get_running_tid)(), |
| 207 | thread_get_running_tid(), |
| 208 | barrier_get_typename(p), |
| 209 | barrier); |
| 210 | } |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 211 | } |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 212 | |
| 213 | if (reinitialization && p->count != count) |
| 214 | { |
| 215 | if (p->pre_waiters_left != p->count || p->post_waiters_left != p->count) |
| 216 | { |
| 217 | VG_(message)(Vg_UserMsg, "Error: reinitialization with active waiters"); |
| 218 | } |
| 219 | p->count = count; |
| 220 | } |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 221 | } |
| 222 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 223 | /** Called after pthread_barrier_destroy(). */ |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 224 | void barrier_destroy(const Addr barrier, const BarrierT barrier_type) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 225 | { |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 226 | struct barrier_info* p; |
| 227 | |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 228 | p = barrier_get(barrier); |
| 229 | |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 230 | if (s_trace_barrier) |
| 231 | { |
| 232 | VG_(message)(Vg_UserMsg, |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 233 | "[%d/%d] barrier_destroy %s 0x%lx", |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 234 | VG_(get_running_tid)(), |
| 235 | thread_get_running_tid(), |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 236 | barrier_get_typename(p), |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 237 | barrier); |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 238 | } |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 239 | |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 240 | if (p == 0) |
| 241 | { |
| 242 | GenericErrInfo GEI; |
| 243 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 244 | GenericErr, |
| 245 | VG_(get_IP)(VG_(get_running_tid)()), |
| 246 | "Not a barrier", |
| 247 | &GEI); |
| 248 | return; |
| 249 | } |
| 250 | |
| 251 | clientobj_remove(p->a1, ClientBarrier); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 252 | } |
| 253 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 254 | /** Called before pthread_barrier_wait(). */ |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 255 | void barrier_pre_wait(const DrdThreadId tid, const Addr barrier, |
| 256 | const BarrierT barrier_type) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 257 | { |
| 258 | struct barrier_info* p; |
| 259 | struct barrier_thread_info* q; |
| 260 | const UWord word_tid = tid; |
| 261 | |
| 262 | p = barrier_get(barrier); |
bart | c68bd60 | 2008-03-16 10:04:58 +0000 | [diff] [blame] | 263 | if (p == 0 && barrier_type == gomp_barrier) |
| 264 | { |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 265 | VG_(message)(Vg_UserMsg, "%s", ""); |
bart | c68bd60 | 2008-03-16 10:04:58 +0000 | [diff] [blame] | 266 | VG_(message)(Vg_UserMsg, |
| 267 | "Please verify whether gcc has been configured" |
| 268 | " with option --disable-linux-futex."); |
| 269 | VG_(message)(Vg_UserMsg, |
| 270 | "See also the section about OpenMP in the DRD manual."); |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 271 | VG_(message)(Vg_UserMsg, "%s", ""); |
bart | c68bd60 | 2008-03-16 10:04:58 +0000 | [diff] [blame] | 272 | } |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 273 | tl_assert(p); |
| 274 | |
| 275 | if (s_trace_barrier) |
| 276 | { |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 277 | VG_(message)(Vg_UserMsg, |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 278 | "[%d/%d] barrier_pre_wait %s 0x%lx iteration %ld", |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 279 | VG_(get_running_tid)(), |
| 280 | thread_get_running_tid(), |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 281 | barrier_get_typename(p), |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 282 | barrier, |
| 283 | p->pre_iteration); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 284 | } |
| 285 | |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 286 | q = VG_(OSetGen_Lookup)(p->oset, &word_tid); |
| 287 | if (q == 0) |
| 288 | { |
| 289 | q = VG_(OSetGen_AllocNode)(p->oset, sizeof(*q)); |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 290 | barrier_thread_initialize(q, tid, p->pre_iteration); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 291 | VG_(OSetGen_Insert)(p->oset, q); |
| 292 | tl_assert(VG_(OSetGen_Lookup)(p->oset, &word_tid) == q); |
| 293 | } |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 294 | thread_get_latest_segment(&q->sg[p->pre_iteration], tid); |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 295 | |
| 296 | if (--p->pre_waiters_left <= 0) |
| 297 | { |
| 298 | p->pre_iteration = 1 - p->pre_iteration; |
| 299 | p->pre_waiters_left = p->count; |
| 300 | } |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 301 | } |
| 302 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 303 | /** Called after pthread_barrier_wait(). */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 304 | void barrier_post_wait(const DrdThreadId tid, const Addr barrier, |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 305 | const BarrierT barrier_type, const Bool waited) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 306 | { |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 307 | struct barrier_info* p; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 308 | |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 309 | p = barrier_get(barrier); |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 310 | |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 311 | if (s_trace_barrier) |
| 312 | { |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 313 | VG_(message)(Vg_UserMsg, |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 314 | "[%d/%d] barrier_post_wait %s 0x%lx iteration %ld", |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 315 | VG_(get_running_tid)(), |
| 316 | tid, |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 317 | p ? barrier_get_typename(p) : "(?)", |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 318 | barrier, |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 319 | p ? p->post_iteration : -1); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 320 | } |
| 321 | |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 322 | /* If p == 0, this means that the barrier has been destroyed after */ |
| 323 | /* *_barrier_wait() returned and before this function was called. Just */ |
| 324 | /* return in that case. */ |
| 325 | if (p == 0) |
| 326 | return; |
| 327 | |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 328 | if (waited) |
| 329 | { |
| 330 | const UWord word_tid = tid; |
| 331 | struct barrier_thread_info* q; |
| 332 | struct barrier_thread_info* r; |
| 333 | |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 334 | q = VG_(OSetGen_Lookup)(p->oset, &word_tid); |
| 335 | tl_assert(q); |
| 336 | VG_(OSetGen_ResetIter)(p->oset); |
| 337 | for ( ; (r = VG_(OSetGen_Next)(p->oset)) != 0; ) |
| 338 | { |
| 339 | if (r != q) |
| 340 | { |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 341 | tl_assert(r->sg[p->post_iteration]); |
| 342 | thread_combine_vc2(tid, &r->sg[p->post_iteration]->vc); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 343 | } |
| 344 | } |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 345 | |
| 346 | thread_new_segment(tid); |
bart | 6bbefaf | 2008-04-19 15:16:45 +0000 | [diff] [blame^] | 347 | s_barrier_segment_creation_count++; |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 348 | |
| 349 | if (--p->post_waiters_left <= 0) |
| 350 | { |
| 351 | p->post_iteration = 1 - p->post_iteration; |
| 352 | p->post_waiters_left = p->count; |
| 353 | } |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 354 | } |
| 355 | } |
| 356 | |
bart | bebc5d6 | 2008-02-24 18:42:53 +0000 | [diff] [blame] | 357 | /** Call this function when thread tid stops to exist. */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 358 | void barrier_thread_delete(const DrdThreadId tid) |
| 359 | { |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 360 | struct barrier_info* p; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 361 | |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 362 | clientobj_resetiter(); |
| 363 | for ( ; (p = &clientobj_next(ClientBarrier)->barrier) != 0; ) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 364 | { |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 365 | struct barrier_thread_info* q; |
| 366 | const UWord word_tid = tid; |
| 367 | q = VG_(OSetGen_Remove)(p->oset, &word_tid); |
| 368 | barrier_thread_destroy(q); |
| 369 | VG_(OSetGen_FreeNode)(p->oset, q); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 370 | } |
| 371 | } |
bart | 306527d | 2008-03-12 17:23:07 +0000 | [diff] [blame] | 372 | |
| 373 | static const char* barrier_get_typename(struct barrier_info* const p) |
| 374 | { |
| 375 | tl_assert(p); |
| 376 | |
| 377 | return barrier_type_name(p->barrier_type); |
| 378 | } |
| 379 | |
| 380 | static const char* barrier_type_name(const BarrierT bt) |
| 381 | { |
| 382 | switch (bt) |
| 383 | { |
| 384 | case pthread_barrier: |
| 385 | return "pthread barrier"; |
| 386 | case gomp_barrier: |
| 387 | return "gomp barrier"; |
| 388 | } |
| 389 | return "?"; |
| 390 | } |
bart | 6bbefaf | 2008-04-19 15:16:45 +0000 | [diff] [blame^] | 391 | |
| 392 | ULong get_barrier_segment_creation_count(void) |
| 393 | { |
| 394 | return s_barrier_segment_creation_count; |
| 395 | } |