5 #include "clockvector.h"
7 #include "threads-model.h"
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
17 ClockVector::ClockVector(ClockVector *parent, ModelAction *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;
24 clock = (modelclock_t *)snapshot_calloc(num_threads, sizeof(int));
26 std::memcpy(clock, parent->clock, parent->num_threads * sizeof(modelclock_t));
28 clock[id_to_int(act->get_tid())] = act->get_seq_number();
31 /** @brief Destructor */
32 ClockVector::~ClockVector()
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.
42 bool ClockVector::merge(const ClockVector *cv)
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++)
50 num_threads = cv->num_threads;
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];
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.
69 * @see ModelAction::happens_before
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>
75 bool ClockVector::synchronized_since(const ModelAction *act) const
77 int i = id_to_int(act->get_tid());
80 return act->get_seq_number() <= clock[i];
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);
88 if (threadid < num_threads)
89 return clock[threadid];
94 /** @brief Formats and prints this ClockVector's data. */
95 void ClockVector::print() const
99 for (i = 0; i < num_threads; i++)
100 model_print("%2u%s", clock[i], (i == num_threads - 1) ? ")\n" : ", ");