bug
[model-checker.git] / clockvector.cc
1 #include <algorithm>
2 #include <cstring>
3 #include <stdlib.h>
4
5 #include "model.h"
6 #include "action.h"
7 #include "clockvector.h"
8 #include "common.h"
9 #include "threads-model.h"
10
11 /**
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
18  */
19 ClockVector::ClockVector(ClockVector *parent, ModelAction *act)
20 {
21         num_threads = model->get_num_threads();
22         clock = (modelclock_t *)snapshot_calloc(num_threads, sizeof(int));
23         if (parent)
24                 std::memcpy(clock, parent->clock, parent->num_threads * sizeof(modelclock_t));
25
26         if (act)
27                 clock[id_to_int(act->get_tid())] = act->get_seq_number();
28 }
29
30 /** @brief Destructor */
31 ClockVector::~ClockVector()
32 {
33         snapshot_free(clock);
34 }
35
36 /**
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.
40  */
41 void ClockVector::merge(const ClockVector *cv)
42 {
43         ASSERT(cv != NULL);
44
45         if (cv->num_threads > num_threads) {
46                 clock = (modelclock_t *)snapshot_realloc(clock, cv->num_threads * sizeof(modelclock_t));
47                 num_threads = cv->num_threads;
48         }
49
50         /* Element-wise maximum */
51         for (int i = 0; i < cv->num_threads; i++)
52                 if (cv->clock[i] > clock[i])
53                         clock[i] = cv->clock[i];
54 }
55
56 /**
57  * Check whether this vector's thread has synchronized with another action's
58  * thread. This effectively checks the happens-before relation (or actually,
59  * happens after), but it's easier to compare two ModelAction events directly,
60  * using ModelAction::happens_before.
61  *
62  * @see ModelAction::happens_before
63  *
64  * @return true if this ClockVector's thread has synchronized with act's
65  * thread, false otherwise. That is, this function returns:
66  * <BR><CODE>act <= cv[act->tid]</CODE>
67  */
68 bool ClockVector::synchronized_since(const ModelAction *act) const
69 {
70         int i = id_to_int(act->get_tid());
71
72         if (i < num_threads)
73                 return act->get_seq_number() <= clock[i];
74         return false;
75 }
76
77 bool ClockVector::has_synchronized_with(const ClockVector *cv) const
78 {
79         ASSERT(cv);
80         if (cv->num_threads > num_threads)
81                 return false;
82         for (int i = 0; i < cv->num_threads; i++)
83                 if (cv->clock[i] > clock[i])
84                         return false;
85         return true;
86 }
87
88 /** Gets the clock corresponding to a given thread id from the clock vector. */
89 modelclock_t ClockVector::getClock(thread_id_t thread) {
90         int threadid = id_to_int(thread);
91
92         if (threadid < num_threads)
93                 return clock[threadid];
94         else
95                 return 0;
96 }
97
98 /** @brief Formats and prints this ClockVector's data. */
99 void ClockVector::print() const
100 {
101         int i;
102         printf("CV: (");
103         for (i = 0; i < num_threads; i++)
104                 printf("%2u%s", clock[i], (i == num_threads - 1) ? ")\n" : ", ");
105 }