medfall

A super great game engine
Log | Files | Refs

sync_var.hpp (1535B)


      1 /*  Relacy Race Detector
      2  *  Copyright (c) 2008-2013, Dmitry S. Vyukov
      3  *  All rights reserved.
      4  *  This software is provided AS-IS with no warranty, either express or implied.
      5  *  This software is distributed under a license and may not be copied,
      6  *  modified or distributed except as expressly authorized under the
      7  *  terms of the license contained in the file LICENSE in this distribution.
      8  */
      9 
     10 #ifndef RL_SYNC_VAR_HPP
     11 #define RL_SYNC_VAR_HPP
     12 #ifdef _MSC_VER
     13 #   pragma once
     14 #endif
     15 
     16 #include "base.hpp"
     17 #include "foreach.hpp"
     18 
     19 
     20 namespace rl
     21 {
     22 
     23 
     24 template<thread_id_t thread_count>
     25 class sync_var : nocopy<>
     26 {
     27 public:
     28     sync_var()
     29     {
     30         iteration_begin();
     31     }
     32 
     33     void iteration_begin()
     34     {
     35         foreach<thread_count>(order_, &assign_zero);
     36     }
     37 
     38     void acquire(thread_info_base* th)
     39     {
     40         th->own_acq_rel_order_ += 1;
     41         foreach<thread_count>(th->acq_rel_order_, order_, &assign_max);
     42     }
     43 
     44     void release(thread_info_base* th)
     45     {
     46         th->own_acq_rel_order_ += 1;
     47         foreach<thread_count>(order_, th->acq_rel_order_, &assign_max);
     48     }
     49 
     50     void acq_rel(thread_info_base* th)
     51     {
     52         th->own_acq_rel_order_ += 1;
     53         timestamp_t* acq_rel_order = th->acq_rel_order_;
     54         timestamp_t* order = order_;
     55         foreach<thread_count>(acq_rel_order, order, &assign_max);
     56         foreach<thread_count>(order, acq_rel_order, &assign_max);
     57     }
     58 
     59 private:
     60     timestamp_t order_ [thread_count];
     61 };
     62 
     63 
     64 }
     65 
     66 #endif