Bug fixes
[c11tester.git] / clockvector.cc
1 #include <cstring>
2 #include <stdlib.h>
3
4 #include "action.h"
5
6 #include "clockvector.h"
7 #include "common.h"
8 #include "threads-model.h"
9
10
11 /**
12  * Constructs a new ClockVector, given a parent ClockVector and a first
13  * ModelAction. This constructor can assign appropriate default settings if no
14  * parent and/or action is supplied.
15  * @param parent is the previous ClockVector to inherit (i.e., clock from the
16  * same thread or the parent that created this thread)
17  * @param act is an action with which to update the ClockVector
18  */
19 ClockVector::ClockVector(ClockVector *parent, const ModelAction *act)
20 {
21         num_threads = act != NULL ? int_to_id(act->get_tid()) + 1 : 0;
22         if (parent && parent->num_threads > num_threads)
23                 num_threads = parent->num_threads;
24
25         clock = (modelclock_t *)snapshot_calloc(num_threads, sizeof(int));
26         if (parent)
27                 std::memcpy(clock, parent->clock, parent->num_threads * sizeof(modelclock_t));
28
29         if (act != NULL)
30                 clock[id_to_int(act->get_tid())] = act->get_seq_number();
31 }
32
33 /** @brief Destructor */
34 ClockVector::~ClockVector()
35 {
36         snapshot_free(clock);
37 }
38
39 /**
40  * Merge a clock vector into this vector, using a pairwise comparison. The
41  * resulting vector length will be the maximum length of the two being merged.
42  * @param cv is the ClockVector being merged into this vector.
43  */
44 bool ClockVector::merge(const ClockVector *cv)
45 {
46         ASSERT(cv != NULL);
47         bool changed = false;
48         if (cv->num_threads > num_threads) {
49                 clock = (modelclock_t *)snapshot_realloc(clock, cv->num_threads * sizeof(modelclock_t));
50                 for (int i = num_threads;i < cv->num_threads;i++)
51                         clock[i] = 0;
52                 num_threads = cv->num_threads;
53         }
54
55         /* Element-wise maximum */
56         for (int i = 0;i < cv->num_threads;i++)
57                 if (cv->clock[i] > clock[i]) {
58                         clock[i] = cv->clock[i];
59                         changed = true;
60                 }
61
62         return changed;
63 }
64
65 /**
66  * Merge a clock vector into this vector, using a pairwise comparison. The
67  * resulting vector length will be the maximum length of the two being merged.
68  * @param cv is the ClockVector being merged into this vector.
69  */
70 bool ClockVector::minmerge(const ClockVector *cv)
71 {
72         ASSERT(cv != NULL);
73         bool changed = false;
74         if (cv->num_threads > num_threads) {
75                 clock = (modelclock_t *)snapshot_realloc(clock, cv->num_threads * sizeof(modelclock_t));
76                 for (int i = num_threads;i < cv->num_threads;i++)
77                         clock[i] = 0;
78                 num_threads = cv->num_threads;
79         }
80
81         /* Element-wise minimum */
82         for (int i = 0;i < cv->num_threads;i++)
83                 if (cv->clock[i] < clock[i]) {
84                         clock[i] = cv->clock[i];
85                         changed = true;
86                 }
87
88         return changed;
89 }
90
91 /**
92  * Check whether this vector's thread has synchronized with another action's
93  * thread. This effectively checks the happens-before relation (or actually,
94  * happens after), but it's easier to compare two ModelAction events directly,
95  * using ModelAction::happens_before.
96  *
97  * @see ModelAction::happens_before
98  *
99  * @return true if this ClockVector's thread has synchronized with act's
100  * thread, false otherwise. That is, this function returns:
101  * <BR><CODE>act <= cv[act->tid]</CODE>
102  */
103 bool ClockVector::synchronized_since(const ModelAction *act) const
104 {
105         int i = id_to_int(act->get_tid());
106
107         if (i < num_threads)
108                 return act->get_seq_number() <= clock[i];
109         return false;
110 }
111
112 /** Gets the clock corresponding to a given thread id from the clock vector. */
113 modelclock_t ClockVector::getClock(thread_id_t thread) {
114         int threadid = id_to_int(thread);
115
116         if (threadid < num_threads)
117                 return clock[threadid];
118         else
119                 return 0;
120 }
121
122 /** @brief Formats and prints this ClockVector's data. */
123 void ClockVector::print() const
124 {
125         int i;
126         model_print("(");
127         for (i = 0;i < num_threads;i++)
128                 model_print("%2u%s", clock[i], (i == num_threads - 1) ? ")\n" : ", ");
129 }