7 #include "clockvector.h"
9 #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, ModelAction *act)
21 num_threads = model->get_num_threads();
22 clock = (modelclock_t *)snapshot_calloc(num_threads, sizeof(int));
24 std::memcpy(clock, parent->clock, parent->num_threads * sizeof(modelclock_t));
27 clock[id_to_int(act->get_tid())] = act->get_seq_number();
30 /** @brief Destructor */
31 ClockVector::~ClockVector()
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.
41 void ClockVector::merge(const ClockVector *cv)
45 if (cv->num_threads > num_threads)
46 clock = (modelclock_t *)snapshot_realloc(clock, cv->num_threads * sizeof(modelclock_t));
48 /* Element-wise maximum */
49 for (int i = 0; i < cv->num_threads; i++)
50 if (cv->clock[i] > clock[i])
51 clock[i] = cv->clock[i];
55 * Check whether this vector's thread has synchronized with another action's
56 * thread. This effectively checks the happens-before relation (or actually,
57 * happens after), but it's easier to compare two ModelAction events directly,
58 * using ModelAction::happens_before.
60 * @see ModelAction::happens_before
62 * @return true if this ClockVector's thread has synchronized with act's
63 * thread, false otherwise. That is, this function returns:
64 * <BR><CODE>act <= cv[act->tid]</CODE>
66 bool ClockVector::synchronized_since(const ModelAction *act) const
68 int i = id_to_int(act->get_tid());
71 return act->get_seq_number() <= clock[i];
75 bool ClockVector::has_synchronized_with(const ClockVector *cv) const
78 if (cv->num_threads > num_threads)
80 for (int i = 0; i < cv->num_threads; i++)
81 if (cv->clock[i] > clock[i])
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);
90 if (threadid < num_threads)
91 return clock[threadid];
96 /** @brief Formats and prints this ClockVector's data. */
97 void ClockVector::print() const
101 for (i = 0; i < num_threads; i++)
102 printf("%2u%s", clock[i], (i == num_threads - 1) ? ")\n" : ", ");