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