1 #include "scanalysis.h"
3 #include "threads-model.h"
4 #include "clockvector.h"
6 SCAnalysis::SCAnalysis() {
7 cvmap=new HashTable<const ModelAction *, ClockVector *, uintptr_t, 4>();
8 threadlists=new SnapVector<action_list_t>(1);
11 SCAnalysis::~SCAnalysis() {
16 void SCAnalysis::print_list(action_list_t *list) {
17 action_list_t::iterator it;
19 model_print("---------------------------------------------------------------------\n");
21 unsigned int hash = 0;
23 for (it = list->begin(); it != list->end(); it++) {
24 const ModelAction *act = *it;
25 if (act->get_seq_number() > 0)
27 hash = hash^(hash<<3)^((*it)->hash());
29 model_print("HASH %u\n", hash);
30 model_print("---------------------------------------------------------------------\n");
33 void SCAnalysis::analyze(action_list_t * actions) {
34 buildVectors(actions);
36 action_list_t *list=generateSC(actions);
40 ModelAction * SCAnalysis::getNextAction() {
41 ModelAction *act=NULL;
42 for(int i=0;i<=maxthreads;i++) {
43 action_list_t * threadlist=&(*threadlists)[i];
44 if (threadlist->empty())
46 ModelAction *first=threadlist->front();
51 ClockVector *cv=cvmap->get(act);
52 if (cv->synchronized_since(first)) {
59 action_list_t * SCAnalysis::generateSC(action_list_t *list) {
60 action_list_t *sclist=new action_list_t();
62 ModelAction * act=getNextAction();
65 thread_id_t tid=act->get_tid();
67 (*threadlists)[id_to_int(tid)].pop_front();
68 //add ordering constraints from this choice
69 if (updateConstraints(act)) {
70 //propagate changes if we have them
74 sclist->push_back(act);
79 void SCAnalysis::buildVectors(action_list_t *list) {
81 for (action_list_t::iterator it = list->begin(); it != list->end(); it++) {
82 ModelAction *act = *it;
83 int threadid=id_to_int(act->get_tid());
84 if (threadid > maxthreads) {
85 threadlists->resize(threadid+1);
88 (*threadlists)[threadid].push_back(act);
92 bool SCAnalysis::updateConstraints(ModelAction *act) {
94 ClockVector *actcv = cvmap->get(act);
95 for(int i=0;i<=maxthreads;i++) {
96 thread_id_t tid=int_to_id(i);
97 if (tid==act->get_tid())
100 action_list_t * list=&(*threadlists)[id_to_int(tid)];
101 for (action_list_t::iterator rit = list->begin(); rit != list->end(); rit++) {
102 ModelAction *write = *rit;
103 if (!write->is_write())
105 ClockVector *writecv = cvmap->get(write);
106 if (writecv->synchronized_since(act))
108 if (write->get_location() == act->get_location()) {
109 //write is sc after act
110 writecv->merge(actcv);
119 bool SCAnalysis::processRead(ModelAction *read, ClockVector *cv) {
122 /* Merge in the clock vector from the write */
123 const ModelAction *write=read->get_reads_from();
124 ClockVector *writecv=cvmap->get(write);
125 changed|= ( writecv == NULL || cv->merge(writecv) && (*read < *write));
127 for(int i=0;i<=maxthreads;i++) {
128 thread_id_t tid=int_to_id(i);
129 if (tid==read->get_tid())
131 if (tid==write->get_tid())
133 action_list_t * list=model->get_actions_on_obj(read->get_location(), tid);
136 for (action_list_t::reverse_iterator rit = list->rbegin(); rit != list->rend(); rit++) {
137 ModelAction *write2 = *rit;
138 if (!write2->is_write())
141 ClockVector *write2cv = cvmap->get(write2);
142 if (write2cv == NULL)
145 /* write -sc-> write2 &&
148 if (write2cv->synchronized_since(write)) {
149 changed |= write2cv->merge(cv);
152 //looking for earliest write2 in iteration to satisfy this
155 write2 -sc-> write */
156 if (cv->synchronized_since(write2)) {
157 changed |= writecv == NULL || writecv->merge(write2cv);
166 void SCAnalysis::computeCV(action_list_t *list) {
169 ModelAction **last_act=(ModelAction **)model_calloc(1,(maxthreads+1)*sizeof(ModelAction *));
171 changed=changed&firsttime;
174 for (action_list_t::iterator it = list->begin(); it != list->end(); it++) {
175 ModelAction *act = *it;
176 ModelAction *lastact = last_act[id_to_int(act->get_tid())];
177 if (act->is_thread_start())
178 lastact=model->get_thread(act)->get_creation();
179 ClockVector *lastcv=(lastact != NULL) ? cvmap->get(lastact) : NULL;
180 last_act[id_to_int(act->get_tid())]=act;
181 ClockVector *cv=cvmap->get(act);
183 cv = new ClockVector(lastcv, act);
185 } else if ( lastcv != NULL ) {
188 if (act->is_thread_join()) {
189 Thread *joinedthr = act->get_thread_operand();
190 ModelAction *finish = model->get_last_action(joinedthr->get_id());
191 ClockVector *finishcv = cvmap->get(finish);
192 changed |= (finishcv == NULL) || cv->merge(finishcv);
194 if (act->is_read()) {
195 changed|=processRead(act, cv);
198 /* Reset the last action array */
200 bzero(last_act, (maxthreads+1)*sizeof(ModelAction *));
203 model_free(last_act);