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