6 #include "clockvector.h"
8 #include "threads-model.h"
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
19 ClockVector::ClockVector(ClockVector *parent, const ModelAction *act)
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;
25 clock = (modelclock_t *)snapshot_calloc(num_threads, sizeof(int));
27 std::memcpy(clock, parent->clock, parent->num_threads * sizeof(modelclock_t));
30 clock[id_to_int(act->get_tid())] = act->get_seq_number();
33 /** @brief Destructor */
34 ClockVector::~ClockVector()
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.
44 bool ClockVector::merge(const ClockVector *cv)
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++)
52 num_threads = cv->num_threads;
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];
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.
70 bool ClockVector::minmerge(const ClockVector *cv)
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++)
78 num_threads = cv->num_threads;
81 /* Element-wise maximum */
82 for (int i = 0;i < cv->num_threads;i++)
83 if (cv->clock[i] < clock[i]) {
84 clock[i] = cv->clock[i];
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.
97 * @see ModelAction::happens_before
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>
103 bool ClockVector::synchronized_since(const ModelAction *act) const
105 int i = id_to_int(act->get_tid());
108 return act->get_seq_number() <= clock[i];
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);
116 if (threadid < num_threads)
117 return clock[threadid];
122 /** @brief Formats and prints this ClockVector's data. */
123 void ClockVector::print() const
127 for (i = 0;i < num_threads;i++)
128 model_print("%2u%s", clock[i], (i == num_threads - 1) ? ")\n" : ", ");