+ /* Look for non-conflicting action */
+ ModelAction *nonconflict=NULL;
+ for(int a=0;a<count;a++) {
+ ModelAction *act=array[a];
+ for (int i = 0; i <= maxthreads && act != NULL; i++) {
+ thread_id_t tid = int_to_id(i);
+ if (tid == act->get_tid())
+ continue;
+
+ action_list_t *list = &threadlists[id_to_int(tid)];
+ for (action_list_t::iterator rit = list->begin(); rit != list->end(); rit++) {
+ ModelAction *write = *rit;
+ if (!write->is_write())
+ continue;
+ ClockVector *writecv = cvmap.get(write);
+ if (writecv->synchronized_since(act))
+ break;
+ if (write->get_location() == act->get_location()) {
+ //write is sc after act
+ act = NULL;
+ break;
+ }
+ }
+ }
+ if (act != NULL) {
+ if (nonconflict == NULL || nonconflict->get_seq_number() > act->get_seq_number())
+ nonconflict=act;
+ }
+ }
+ return nonconflict;
+}
+
+action_list_t * SCAnalysis::generateSC(action_list_t *list) {
+ int numactions=buildVectors(list);
+ computeCV(list);
+
+ action_list_t *sclist = new action_list_t();
+ ModelAction **array = (ModelAction **)model_calloc(1, (maxthreads + 1) * sizeof(ModelAction *));
+ int * choices = (int *) model_calloc(1, sizeof(int)*numactions);
+ int endchoice = 0;
+ int currchoice = 0;
+ int lastchoice = -1;
+ while (true) {
+ int numActions = getNextActions(array);
+ if (numActions == 0)
+ break;
+ ModelAction * act=pruneArray(array, numActions);
+ if (act == NULL) {
+ if (currchoice < endchoice) {
+ act = array[choices[currchoice]];
+ //check whether there is still another option
+ if ((choices[currchoice]+1)<numActions)
+ lastchoice=currchoice;
+ currchoice++;
+ } else {
+ act = array[0];
+ choices[currchoice]=0;
+ if (numActions>1)
+ lastchoice=currchoice;
+ currchoice++;
+ }
+ }
+ thread_id_t tid = act->get_tid();
+ //remove action
+ threadlists[id_to_int(tid)].pop_front();
+ //add ordering constraints from this choice
+ if (updateConstraints(act)) {
+ //propagate changes if we have them
+ bool prevc=cyclic;
+ computeCV(list);
+ if (!prevc && cyclic) {
+ model_print("ROLLBACK in SC\n");
+ //check whether we have another choice
+ if (lastchoice != -1) {
+ //have to reset everything
+ choices[lastchoice]++;
+ endchoice=lastchoice+1;
+ currchoice=0;
+ lastchoice=-1;
+ reset(list);
+ buildVectors(list);
+ computeCV(list);
+ sclist->clear();
+ continue;
+ }
+ }
+ }
+ //add action to end
+ sclist->push_back(act);
+ }
+ model_free(array);
+ return sclist;
+}
+
+int SCAnalysis::buildVectors(action_list_t *list) {
+ maxthreads = 0;
+ int numactions = 0;
+ for (action_list_t::iterator it = list->begin(); it != list->end(); it++) {
+ ModelAction *act = *it;
+ numactions++;
+ int threadid = id_to_int(act->get_tid());
+ if (threadid > maxthreads) {
+ threadlists.resize(threadid + 1);
+ maxthreads = threadid;
+ }
+ threadlists[threadid].push_back(act);
+ }
+ return numactions;
+}
+
+void SCAnalysis::reset(action_list_t *list) {
+ for (int t = 0; t <= maxthreads; t++) {
+ action_list_t *tlt = &threadlists[t];
+ tlt->clear();
+ }
+ for (action_list_t::iterator it = list->begin(); it != list->end(); it++) {
+ ModelAction *act = *it;
+ delete cvmap.get(act);
+ cvmap.put(act, NULL);
+ }
+
+ cyclic=false;
+}
+
+bool SCAnalysis::updateConstraints(ModelAction *act) {
+ bool changed = false;
+ for (int i = 0; i <= maxthreads; i++) {
+ thread_id_t tid = int_to_id(i);
+ if (tid == act->get_tid())
+ continue;
+
+ action_list_t *list = &threadlists[id_to_int(tid)];
+ for (action_list_t::iterator rit = list->begin(); rit != list->end(); rit++) {
+ ModelAction *write = *rit;
+ if (!write->is_write())
+ continue;
+ ClockVector *writecv = cvmap.get(write);
+ if (writecv->synchronized_since(act))
+ break;
+ if (write->get_location() == act->get_location()) {
+ //write is sc after act
+ merge(writecv, write, act);
+ changed = true;
+ break;
+ }
+ }
+ }
+ return changed;
+}
+
+bool SCAnalysis::processRead(ModelAction *read, ClockVector *cv) {
+ bool changed = false;
+
+ /* Merge in the clock vector from the write */
+ const ModelAction *write = read->get_reads_from();
+ ClockVector *writecv = cvmap.get(write);
+ changed |= merge(cv, read, write) && (*read < *write);
+
+ for (int i = 0; i <= maxthreads; i++) {
+ thread_id_t tid = int_to_id(i);
+ if (tid == read->get_tid())
+ continue;
+ if (tid == write->get_tid())
+ continue;
+ action_list_t *list = execution->get_actions_on_obj(read->get_location(), tid);
+ if (list == NULL)
+ continue;
+ for (action_list_t::reverse_iterator rit = list->rbegin(); rit != list->rend(); rit++) {
+ ModelAction *write2 = *rit;
+ if (!write2->is_write())
+ continue;
+
+ ClockVector *write2cv = cvmap.get(write2);
+ if (write2cv == NULL)
+ continue;
+
+ /* write -sc-> write2 &&
+ write -rf-> R =>
+ R -sc-> write2 */
+ if (write2cv->synchronized_since(write)) {
+ changed |= merge(write2cv, write2, read);
+ }
+
+ //looking for earliest write2 in iteration to satisfy this
+ /* write2 -sc-> R &&
+ write -rf-> R =>
+ write2 -sc-> write */
+ if (cv->synchronized_since(write2)) {
+ changed |= writecv == NULL || merge(writecv, write, write2);
+ break;
+ }
+ }
+ }
+ return changed;
+}
+
+void SCAnalysis::computeCV(action_list_t *list) {
+ bool changed = true;
+ bool firsttime = true;
+ ModelAction **last_act = (ModelAction **)model_calloc(1, (maxthreads + 1) * sizeof(ModelAction *));
+ while (changed) {
+ changed = changed&firsttime;
+ firsttime = false;