Add macro for recording atomic statics and report data races
[c11tester.git] / mutex.cc
1 #include "mutex.h"
2
3 #include "model.h"
4 #include "execution.h"
5 #include "threads-model.h"
6 #include "clockvector.h"
7 #include "action.h"
8
9 namespace cdsc {
10
11 mutex::mutex(int type)
12 {
13         state.locked = NULL;
14         thread_id_t tid = thread_current()->get_id();
15         state.alloc_tid = tid;
16         ClockVector *cv = model->get_execution()->get_cv(tid);
17         state.alloc_clock = cv  == NULL ? 0 : cv->getClock(tid);
18
19         // For recursive mutex
20         state.type = type;
21         state.lock_count = 0;
22 }
23
24 void mutex::lock()
25 {
26         model->switch_to_master(new ModelAction(ATOMIC_LOCK, std::memory_order_seq_cst, this));
27 }
28
29 bool mutex::try_lock()
30 {
31         return model->switch_to_master(new ModelAction(ATOMIC_TRYLOCK, std::memory_order_seq_cst, this));
32 }
33
34 void mutex::unlock()
35 {
36         model->switch_to_master(new ModelAction(ATOMIC_UNLOCK, std::memory_order_seq_cst, this));
37 }
38
39 }