thread.hpp (12573B)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 | /* Relacy Race Detector * Copyright (c) 2008-2010, Dmitry S. Vyukov * All rights reserved. * This software is provided AS-IS with no warranty, either express or implied. * This software is distributed under a license and may not be copied, * modified or distributed except as expressly authorized under the * terms of the license contained in the file LICENSE.TXT in this distribution. */ #ifndef RL_THREAD_HPP #define RL_THREAD_HPP #ifdef _MSC_VER # pragma once #endif #include "base.hpp" #include "context_base.hpp" #include "dyn_thread_ctx.hpp" #include "thread_base.hpp" #include "test_suite.hpp" #include "memory_order.hpp" #include "foreach.hpp" namespace rl { struct atomic_data; struct var_data; template<thread_id_t thread_count> struct atomic_data_impl; template<thread_id_t thread_count> struct var_data_impl; template<thread_id_t thread_count> struct thread_info : thread_info_base { thread_info(thread_id_t index = 0) : thread_info_base(index, acq_rel_order_) { } void iteration_begin() { sync_object_.iteration_begin(); last_yield_ = 0; dynamic_thread_func_ = 0; dynamic_thread_param_ = 0; for (thread_id_t j = 0; j != thread_count; ++j) { acq_rel_order_[j] = 0; } acq_rel_order_[index_] = 1; temp_switch_from_ = -1; saved_disable_preemption_ = -1; } thread_sync_object<thread_count> sync_object_; timestamp_t acq_rel_order_ [thread_count]; timestamp_t acquire_fence_order_ [thread_count]; timestamp_t release_fence_order_ [thread_count]; #ifdef RL_IMPROVED_SEQ_CST_FENCE timestamp_t imp_seq_cst_order_ [thread_count]; #endif virtual void on_start() { RL_VERIFY(temp_switch_from_ == -1); RL_VERIFY(saved_disable_preemption_ == -1); sync_object_.on_start(); } virtual void on_finish() { RL_VERIFY(temp_switch_from_ == -1); RL_VERIFY(saved_disable_preemption_ == -1); sync_object_.on_finish(); } void atomic_thread_fence_acquire() { foreach<thread_count>( acq_rel_order_, acquire_fence_order_, &assign_max); } void atomic_thread_fence_release() { foreach<thread_count>( release_fence_order_, acq_rel_order_, &assign); } void atomic_thread_fence_acq_rel() { atomic_thread_fence_acquire(); atomic_thread_fence_release(); } void atomic_thread_fence_seq_cst(timestamp_t* seq_cst_fence_order) { #ifdef RL_IMPROVED_SEQ_CST_FENCE foreach<thread_count>(acq_rel_order_, imp_seq_cst_order_, assign_max); #endif atomic_thread_fence_acquire(); foreach<thread_count>( acq_rel_order_, seq_cst_fence_order, &assign_max); foreach<thread_count>( seq_cst_fence_order, acq_rel_order_, &assign); atomic_thread_fence_release(); } virtual ~thread_info() {} // just to calm down gcc private: thread_info(thread_info const&); thread_info& operator = (thread_info const&); virtual unsigned atomic_load_relaxed(atomic_data* RL_RESTRICT data) { return atomic_load<mo_relaxed, false>(data); } virtual unsigned atomic_load_acquire(atomic_data* RL_RESTRICT data) { return atomic_load<mo_acquire, false>(data); } virtual unsigned atomic_load_seq_cst(atomic_data* RL_RESTRICT data) { return atomic_load<mo_seq_cst, false>(data); } virtual unsigned atomic_load_relaxed_rmw(atomic_data* RL_RESTRICT data) { return atomic_load<mo_relaxed, true>(data); } virtual unsigned atomic_load_acquire_rmw(atomic_data* RL_RESTRICT data) { return atomic_load<mo_acquire, true>(data); } virtual unsigned atomic_load_seq_cst_rmw(atomic_data* RL_RESTRICT data) { return atomic_load<mo_seq_cst, true>(data); } virtual unsigned atomic_store_relaxed(atomic_data* RL_RESTRICT data) { return atomic_store<mo_relaxed, false>(data); } virtual unsigned atomic_store_release(atomic_data* RL_RESTRICT data) { return atomic_store<mo_release, false>(data); } virtual unsigned atomic_store_seq_cst(atomic_data* RL_RESTRICT data) { return atomic_store<mo_seq_cst, false>(data); } virtual unsigned atomic_rmw_relaxed(atomic_data* RL_RESTRICT data, bool& aba) { return atomic_rmw<mo_relaxed>(data, aba); } virtual unsigned atomic_rmw_acquire(atomic_data* RL_RESTRICT data, bool& aba) { return atomic_rmw<mo_acquire>(data, aba); } virtual unsigned atomic_rmw_release(atomic_data* RL_RESTRICT data, bool& aba) { return atomic_rmw<mo_release>(data, aba); } virtual unsigned atomic_rmw_acq_rel(atomic_data* RL_RESTRICT data, bool& aba) { return atomic_rmw<mo_acq_rel>(data, aba); } virtual unsigned atomic_rmw_seq_cst(atomic_data* RL_RESTRICT data, bool& aba) { return atomic_rmw<mo_seq_cst>(data, aba); } template<memory_order mo, bool rmw> unsigned get_load_index(atomic_data_impl<thread_count>& var) { typedef typename atomic_data_impl<thread_count>::history_record history_t; unsigned index = var.current_index_; context& c = ctx(); if (false == val(rmw)) { size_t const limit = c.is_random_sched() ? atomic_history_size - 1: 1; for (size_t i = 0; i != limit; ++i, --index) { history_t const& rec = var.history_[index % atomic_history_size]; if (false == rec.busy_) return (unsigned)-1; // access to unitialized var history_t const& prev = var.history_[(index - 1) % atomic_history_size]; if (prev.busy_ && prev.last_seen_order_[index_] <= last_yield_) break; if (mo_seq_cst == val(mo) && rec.seq_cst_) break; timestamp_t acq_rel_order = acq_rel_order_[rec.thread_id_]; if (acq_rel_order >= rec.acq_rel_timestamp_) break; bool stop = false; for (thread_id_t i = 0; i != thread_count; ++i) { timestamp_t acq_rel_order2 = acq_rel_order_[i]; if (acq_rel_order2 >= rec.last_seen_order_[i]) { stop = true; break; } } if (stop) break; if (0 == c.rand(2, sched_type_atomic_load)) break; } } if (false == var.history_[index % atomic_history_size].busy_) return (unsigned)-1; return index; } template<memory_order mo, bool rmw> unsigned atomic_load(atomic_data* RL_RESTRICT data) { RL_VERIFY(mo_release != mo || rmw); RL_VERIFY(mo_acq_rel != mo || rmw); atomic_data_impl<thread_count>& var = *static_cast<atomic_data_impl<thread_count>*>(data); typedef typename atomic_data_impl<thread_count>::history_record history_t; unsigned index = get_load_index<mo, rmw>(var); if ((unsigned)-1 == index) return (unsigned)-1; index %= atomic_history_size; history_t& rec = var.history_[index]; RL_VERIFY(rec.busy_); own_acq_rel_order_ += 1; rec.last_seen_order_[index_] = own_acq_rel_order_; bool const synch = (mo_acquire == mo || mo_acq_rel == mo || mo_seq_cst == mo); timestamp_t* acq_rel_order = (synch ? acq_rel_order_ : acquire_fence_order_); foreach<thread_count>(acq_rel_order, rec.acq_rel_order_, assign_max); return index; } virtual unsigned atomic_init(atomic_data* RL_RESTRICT data) { atomic_data_impl<thread_count>& var = *static_cast<atomic_data_impl<thread_count>*>(data); typedef typename atomic_data_impl<thread_count>::history_record history_t; unsigned const idx = ++var.current_index_ % atomic_history_size; history_t& rec = var.history_[idx]; rec.busy_ = true; rec.thread_id_ = index_; rec.seq_cst_ = false; rec.acq_rel_timestamp_ = 0; foreach<thread_count>(rec.acq_rel_order_, assign_zero); return idx; } template<memory_order mo, bool rmw> unsigned atomic_store(atomic_data* RL_RESTRICT data) { RL_VERIFY(mo_consume != mo || rmw); RL_VERIFY(mo_acquire != mo || rmw); RL_VERIFY(mo_acq_rel != mo || rmw); atomic_data_impl<thread_count>& var = *static_cast<atomic_data_impl<thread_count>*>(data); typedef typename atomic_data_impl<thread_count>::history_record history_t; unsigned const idx = ++var.current_index_ % atomic_history_size; history_t& rec = var.history_[idx]; rec.busy_ = true; rec.thread_id_ = index_; rec.seq_cst_ = (mo_seq_cst == mo); own_acq_rel_order_ += 1; rec.acq_rel_timestamp_ = own_acq_rel_order_; foreach<thread_count>(rec.last_seen_order_, assign<(timestamp_t)-1>); rec.last_seen_order_[index_] = own_acq_rel_order_; unsigned const prev_idx = (var.current_index_ - 1) % atomic_history_size; history_t& prev = var.history_[prev_idx]; #ifdef RL_IMPROVED_SEQ_CST_FENCE if (val(mo) == mo_release && val(rmw) == false) foreach<thread_count>(imp_seq_cst_order_, prev.acq_rel_order_, assign_max); #endif bool const synch = (mo_release == mo || mo_acq_rel == mo || mo_seq_cst == mo); bool const preserve = prev.busy_ && (rmw || (index_ == prev.thread_id_)); timestamp_t* acq_rel_order = (synch ? acq_rel_order_ : release_fence_order_); if (preserve) { foreach<thread_count>(rec.acq_rel_order_, prev.acq_rel_order_, assign); foreach<thread_count>(rec.acq_rel_order_, acq_rel_order, assign_max); } else { foreach<thread_count>(rec.acq_rel_order_, acq_rel_order, assign); } return idx; } template<memory_order mo> unsigned atomic_rmw(atomic_data* RL_RESTRICT data, bool& aba) { atomic_data_impl<thread_count>& var = *static_cast<atomic_data_impl<thread_count>*>(data); timestamp_t const last_seen = var.history_[var.current_index_ % atomic_history_size].last_seen_order_[index_]; aba = (last_seen > own_acq_rel_order_); atomic_load<mo, true>(data); unsigned result = atomic_store<mo, true>(data); #ifdef RL_IMPROVED_SEQ_CST_RMW atomic_thread_fence_seq_cst(ctx_->seq_cst_fence_order_); #endif return result; } virtual unpark_reason atomic_wait(atomic_data* RL_RESTRICT data, bool is_timed, bool allow_spurious_wakeup, debug_info_param info) { context& c = ctx(); atomic_data_impl<thread_count>& var = *static_cast<atomic_data_impl<thread_count>*>(data); unpark_reason const res = var.futex_ws_.park_current(c, is_timed, allow_spurious_wakeup, false, info); if (res == unpark_reason_normal) var.futex_sync_.acquire(this); return res; } virtual thread_id_t atomic_wake(atomic_data* RL_RESTRICT data, thread_id_t count, debug_info_param info) { context& c = ctx(); atomic_data_impl<thread_count>& var = *static_cast<atomic_data_impl<thread_count>*>(data); thread_id_t unblocked = 0; for (; count != 0; count -= 1, unblocked += 1) { if (var.futex_ws_.unpark_one(c, info) == false) break; } if (unblocked != 0) var.futex_sync_.release(this); return unblocked; } }; } #endif |