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