6 #include "clockvector.h"
8 #include "threads-model.h"
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
18 ClockVector::ClockVector(ClockVector *parent, ModelAction *act)
20 num_threads = model->get_num_threads();
21 clock = (modelclock_t *)snapshot_calloc(num_threads, sizeof(int));
23 std::memcpy(clock, parent->clock, parent->num_threads * sizeof(modelclock_t));
26 clock[id_to_int(act->get_tid())] = act->get_seq_number();
29 /** @brief Destructor */
30 ClockVector::~ClockVector()
36 * Merge a clock vector into this vector, using a pairwise comparison. The
37 * resulting vector length will be the maximum length of the two being merged.
38 * @param cv is the ClockVector being merged into this vector.
40 void ClockVector::merge(const ClockVector *cv)
44 if (cv->num_threads > num_threads) {
45 clock = (modelclock_t *)snapshot_realloc(clock, cv->num_threads * sizeof(modelclock_t));
46 for (int i= num_threads; i < cv->num_threads; i++)
48 num_threads = cv->num_threads;
51 /* Element-wise maximum */
52 for (int i = 0; i < cv->num_threads; i++)
53 if (cv->clock[i] > clock[i])
54 clock[i] = cv->clock[i];
58 * Check whether this vector's thread has synchronized with another action's
59 * thread. This effectively checks the happens-before relation (or actually,
60 * happens after), but it's easier to compare two ModelAction events directly,
61 * using ModelAction::happens_before.
63 * @see ModelAction::happens_before
65 * @return true if this ClockVector's thread has synchronized with act's
66 * thread, false otherwise. That is, this function returns:
67 * <BR><CODE>act <= cv[act->tid]</CODE>
69 bool ClockVector::synchronized_since(const ModelAction *act) const
71 int i = id_to_int(act->get_tid());
74 return act->get_seq_number() <= clock[i];
78 /** Gets the clock corresponding to a given thread id from the clock vector. */
79 modelclock_t ClockVector::getClock(thread_id_t thread) {
80 int threadid = id_to_int(thread);
82 if (threadid < num_threads)
83 return clock[threadid];
88 /** @brief Formats and prints this ClockVector's data. */
89 void ClockVector::print() const
93 for (i = 0; i < num_threads; i++)
94 printf("%2u%s", clock[i], (i == num_threads - 1) ? ")\n" : ", ");