Add an exit node in predicate trees
[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  * Check whether this vector's thread has synchronized with another action's
67  * thread. This effectively checks the happens-before relation (or actually,
68  * happens after), but it's easier to compare two ModelAction events directly,
69  * using ModelAction::happens_before.
70  *
71  * @see ModelAction::happens_before
72  *
73  * @return true if this ClockVector's thread has synchronized with act's
74  * thread, false otherwise. That is, this function returns:
75  * <BR><CODE>act <= cv[act->tid]</CODE>
76  */
77 bool ClockVector::synchronized_since(const ModelAction *act) const
78 {
79         int i = id_to_int(act->get_tid());
80
81         if (i < num_threads)
82                 return act->get_seq_number() <= clock[i];
83         return false;
84 }
85
86 /** Gets the clock corresponding to a given thread id from the clock vector. */
87 modelclock_t ClockVector::getClock(thread_id_t thread) {
88         int threadid = id_to_int(thread);
89
90         if (threadid < num_threads)
91                 return clock[threadid];
92         else
93                 return 0;
94 }
95
96 /** @brief Formats and prints this ClockVector's data. */
97 void ClockVector::print() const
98 {
99         int i;
100         model_print("(");
101         for (i = 0;i < num_threads;i++)
102                 model_print("%2u%s", clock[i], (i == num_threads - 1) ? ")\n" : ", ");
103 }