8 #include "snapshot-interface.h"
10 #include "clockvector.h"
11 #include "cyclegraph.h"
15 #define INITIAL_THREAD_ID 0
19 /** @brief Constructor */
20 ModelChecker::ModelChecker(struct model_params params) :
21 /* Initialize default scheduler */
22 scheduler(new Scheduler()),
23 /* First thread created will have id INITIAL_THREAD_ID */
24 next_thread_id(INITIAL_THREAD_ID),
25 used_sequence_numbers(0),
31 action_trace(new action_list_t()),
32 thread_map(new HashTable<int, Thread *, int>()),
33 obj_map(new HashTable<const void *, action_list_t, uintptr_t, 4>()),
34 obj_thrd_map(new HashTable<void *, std::vector<action_list_t>, uintptr_t, 4 >()),
35 promises(new std::vector<Promise *>()),
36 lazy_sync_with_release(new HashTable<void *, std::list<ModelAction *>, uintptr_t, 4>()),
37 thrd_last_action(new std::vector<ModelAction *>(1)),
38 node_stack(new NodeStack()),
40 mo_graph(new CycleGraph()),
41 failed_promise(false),
46 /** @brief Destructor */
47 ModelChecker::~ModelChecker()
49 for (int i = 0; i < get_num_threads(); i++)
50 delete thread_map->get(i);
57 for (unsigned int i = 0; i < promises->size(); i++)
58 delete (*promises)[i];
61 delete lazy_sync_with_release;
63 delete thrd_last_action;
70 * Restores user program to initial state and resets all model-checker data
73 void ModelChecker::reset_to_initial_state()
75 DEBUG("+++ Resetting to initial state +++\n");
76 node_stack->reset_execution();
77 current_action = NULL;
78 next_thread_id = INITIAL_THREAD_ID;
79 used_sequence_numbers = 0;
81 next_backtrack = NULL;
82 failed_promise = false;
84 snapshotObject->backTrackBeforeStep(0);
87 /** @returns a thread ID for a new Thread */
88 thread_id_t ModelChecker::get_next_id()
90 return next_thread_id++;
93 /** @returns the number of user threads created during this execution */
94 int ModelChecker::get_num_threads()
96 return next_thread_id;
99 /** @returns a sequence number for a new ModelAction */
100 modelclock_t ModelChecker::get_next_seq_num()
102 return ++used_sequence_numbers;
106 * Choose the next thread in the replay sequence.
108 * If the replay sequence has reached the 'diverge' point, returns a thread
109 * from the backtracking set. Otherwise, simply returns the next thread in the
110 * sequence that is being replayed.
112 Thread * ModelChecker::get_next_replay_thread()
116 /* Have we completed exploring the preselected path? */
120 /* Else, we are trying to replay an execution */
121 ModelAction *next = node_stack->get_next()->get_action();
123 if (next == diverge) {
124 Node *nextnode = next->get_node();
125 /* Reached divergence point */
126 if (nextnode->increment_promise()) {
127 /* The next node will try to satisfy a different set of promises. */
128 tid = next->get_tid();
129 node_stack->pop_restofstack(2);
130 } else if (nextnode->increment_read_from()) {
131 /* The next node will read from a different value. */
132 tid = next->get_tid();
133 node_stack->pop_restofstack(2);
134 } else if (nextnode->increment_future_value()) {
135 /* The next node will try to read from a different future value. */
136 tid = next->get_tid();
137 node_stack->pop_restofstack(2);
139 /* Make a different thread execute for next step */
140 Node *node = nextnode->get_parent();
141 tid = node->get_next_backtrack();
142 node_stack->pop_restofstack(1);
144 DEBUG("*** Divergence point ***\n");
147 tid = next->get_tid();
149 DEBUG("*** ModelChecker chose next thread = %d ***\n", tid);
150 ASSERT(tid != THREAD_ID_T_NONE);
151 return thread_map->get(id_to_int(tid));
155 * Queries the model-checker for more executions to explore and, if one
156 * exists, resets the model-checker state to execute a new execution.
158 * @return If there are more executions to explore, return true. Otherwise,
161 bool ModelChecker::next_execution()
167 if (isfinalfeasible() || DBG_ENABLED())
170 if ((diverge = get_next_backtrack()) == NULL)
174 printf("Next execution will diverge at:\n");
178 reset_to_initial_state();
182 ModelAction * ModelChecker::get_last_conflict(ModelAction *act)
184 action_type type = act->get_type();
194 /* linear search: from most recent to oldest */
195 action_list_t *list = obj_map->get_safe_ptr(act->get_location());
196 action_list_t::reverse_iterator rit;
197 for (rit = list->rbegin(); rit != list->rend(); rit++) {
198 ModelAction *prev = *rit;
199 if (act->is_synchronizing(prev))
205 void ModelChecker::set_backtracking(ModelAction *act)
209 Thread *t = get_thread(act->get_tid());
211 prev = get_last_conflict(act);
215 node = prev->get_node()->get_parent();
217 while (!node->is_enabled(t))
220 /* Check if this has been explored already */
221 if (node->has_been_explored(t->get_id()))
224 /* Cache the latest backtracking point */
225 if (!next_backtrack || *prev > *next_backtrack)
226 next_backtrack = prev;
228 /* If this is a new backtracking point, mark the tree */
229 if (!node->set_backtrack(t->get_id()))
231 DEBUG("Setting backtrack: conflict = %d, instead tid = %d\n",
232 prev->get_tid(), t->get_id());
240 * Returns last backtracking point. The model checker will explore a different
241 * path for this point in the next execution.
242 * @return The ModelAction at which the next execution should diverge.
244 ModelAction * ModelChecker::get_next_backtrack()
246 ModelAction *next = next_backtrack;
247 next_backtrack = NULL;
252 * This is the heart of the model checker routine. It performs model-checking
253 * actions corresponding to a given "current action." Among other processes, it
254 * calculates reads-from relationships, updates synchronization clock vectors,
255 * forms a memory_order constraints graph, and handles replay/backtrack
256 * execution when running permutations of previously-observed executions.
258 * @param curr The current action to process
259 * @return The next Thread that must be executed. May be NULL if ModelChecker
260 * makes no choice (e.g., according to replay execution, combining RMW actions,
263 Thread * ModelChecker::check_current_action(ModelAction *curr)
265 bool already_added = false;
269 if (curr->is_rmwc() || curr->is_rmw()) {
270 ModelAction *tmp = process_rmw(curr);
271 already_added = true;
275 ModelAction *tmp = node_stack->explore_action(curr);
277 /* Discard duplicate ModelAction; use action from NodeStack */
278 /* First restore type and order in case of RMW operation */
280 tmp->copy_typeandorder(curr);
282 /* If we have diverged, we need to reset the clock vector. */
284 tmp->create_cv(get_parent_action(tmp->get_tid()));
290 * Perform one-time actions when pushing new ModelAction onto
293 curr->create_cv(get_parent_action(curr->get_tid()));
294 /* Build may_read_from set */
296 build_reads_from_past(curr);
297 if (curr->is_write())
298 compute_promises(curr);
302 /* Assign 'creation' parent */
303 if (curr->get_type() == THREAD_CREATE) {
304 Thread *th = (Thread *)curr->get_location();
305 th->set_creation(curr);
306 } else if (curr->get_type() == THREAD_JOIN) {
308 wait = get_thread(curr->get_tid());
309 join = (Thread *)curr->get_location();
310 if (!join->is_complete())
311 scheduler->wait(wait, join);
312 } else if (curr->get_type() == THREAD_FINISH) {
313 Thread *th = get_thread(curr->get_tid());
314 while (!th->wait_list_empty()) {
315 Thread *wake = th->pop_wait_list();
316 scheduler->wake(wake);
321 /* Deal with new thread */
322 if (curr->get_type() == THREAD_START)
323 check_promises(NULL, curr->get_cv());
325 /* Assign reads_from values */
326 Thread *th = get_thread(curr->get_tid());
327 uint64_t value = VALUE_NONE;
328 bool updated = false;
329 if (curr->is_read()) {
330 const ModelAction *reads_from = curr->get_node()->get_read_from();
331 if (reads_from != NULL) {
332 value = reads_from->get_value();
333 /* Assign reads_from, perform release/acquire synchronization */
334 curr->read_from(reads_from);
335 if (r_modification_order(curr,reads_from))
338 /* Read from future value */
339 value = curr->get_node()->get_future_value();
340 curr->read_from(NULL);
341 Promise *valuepromise = new Promise(curr, value);
342 promises->push_back(valuepromise);
344 } else if (curr->is_write()) {
345 if (w_modification_order(curr))
347 if (resolve_promises(curr))
352 resolve_release_sequences(curr->get_location());
354 th->set_return_value(value);
356 /* Add action to list. */
358 add_action_to_lists(curr);
360 Node *currnode = curr->get_node();
361 Node *parnode = currnode->get_parent();
363 if (!parnode->backtrack_empty() || !currnode->read_from_empty() ||
364 !currnode->future_value_empty() || !currnode->promise_empty())
365 if (!next_backtrack || *curr > *next_backtrack)
366 next_backtrack = curr;
368 set_backtracking(curr);
370 /* Do not split atomic actions. */
372 return thread_current();
373 /* The THREAD_CREATE action points to the created Thread */
374 else if (curr->get_type() == THREAD_CREATE)
375 return (Thread *)curr->get_location();
377 return get_next_replay_thread();
380 /** @returns whether the current partial trace must be a prefix of a
383 bool ModelChecker::isfeasibleprefix() {
384 return promises->size()==0;
387 /** @returns whether the current partial trace is feasible. */
388 bool ModelChecker::isfeasible() {
389 return !mo_graph->checkForCycles() && !failed_promise;
392 /** Returns whether the current completed trace is feasible. */
393 bool ModelChecker::isfinalfeasible() {
394 return isfeasible() && promises->size() == 0;
397 /** Close out a RMWR by converting previous RMWR into a RMW or READ. */
398 ModelAction * ModelChecker::process_rmw(ModelAction *act) {
399 int tid = id_to_int(act->get_tid());
400 ModelAction *lastread = get_last_action(tid);
401 lastread->process_rmw(act);
403 mo_graph->addRMWEdge(lastread->get_reads_from(), lastread);
408 * Updates the mo_graph with the constraints imposed from the current read.
409 * @param curr The current action. Must be a read.
410 * @param rf The action that curr reads from. Must be a write.
411 * @return True if modification order edges were added; false otherwise
413 bool ModelChecker::r_modification_order(ModelAction *curr, const ModelAction *rf)
415 std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(curr->get_location());
418 ASSERT(curr->is_read());
420 /* Iterate over all threads */
421 for (i = 0; i < thrd_lists->size(); i++) {
422 /* Iterate over actions in thread, starting from most recent */
423 action_list_t *list = &(*thrd_lists)[i];
424 action_list_t::reverse_iterator rit;
425 for (rit = list->rbegin(); rit != list->rend(); rit++) {
426 ModelAction *act = *rit;
428 /* Include at most one act per-thread that "happens before" curr */
429 if (act->happens_before(curr)) {
430 if (act->is_read()) {
431 const ModelAction *prevreadfrom = act->get_reads_from();
432 if (prevreadfrom != NULL && rf != prevreadfrom) {
433 mo_graph->addEdge(prevreadfrom, rf);
436 } else if (rf != act) {
437 mo_graph->addEdge(act, rf);
448 /** Updates the mo_graph with the constraints imposed from the current read. */
449 void ModelChecker::post_r_modification_order(ModelAction *curr, const ModelAction *rf)
451 std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(curr->get_location());
453 ASSERT(curr->is_read());
455 /* Iterate over all threads */
456 for (i = 0; i < thrd_lists->size(); i++) {
457 /* Iterate over actions in thread, starting from most recent */
458 action_list_t *list = &(*thrd_lists)[i];
459 action_list_t::reverse_iterator rit;
460 ModelAction *lastact = NULL;
462 /* Find last action that happens after curr */
463 for (rit = list->rbegin(); rit != list->rend(); rit++) {
464 ModelAction *act = *rit;
465 if (curr->happens_before(act)) {
471 /* Include at most one act per-thread that "happens before" curr */
472 if (lastact != NULL) {
473 if (lastact->is_read()) {
474 const ModelAction *postreadfrom = lastact->get_reads_from();
475 if (postreadfrom != NULL&&rf != postreadfrom)
476 mo_graph->addEdge(rf, postreadfrom);
477 } else if (rf != lastact) {
478 mo_graph->addEdge(rf, lastact);
486 * Updates the mo_graph with the constraints imposed from the current write.
487 * @param curr The current action. Must be a write.
488 * @return True if modification order edges were added; false otherwise
490 bool ModelChecker::w_modification_order(ModelAction *curr)
492 std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(curr->get_location());
495 ASSERT(curr->is_write());
497 if (curr->is_seqcst()) {
498 /* We have to at least see the last sequentially consistent write,
499 so we are initialized. */
500 ModelAction *last_seq_cst = get_last_seq_cst(curr->get_location());
501 if (last_seq_cst != NULL) {
502 mo_graph->addEdge(last_seq_cst, curr);
507 /* Iterate over all threads */
508 for (i = 0; i < thrd_lists->size(); i++) {
509 /* Iterate over actions in thread, starting from most recent */
510 action_list_t *list = &(*thrd_lists)[i];
511 action_list_t::reverse_iterator rit;
512 for (rit = list->rbegin(); rit != list->rend(); rit++) {
513 ModelAction *act = *rit;
515 /* Include at most one act per-thread that "happens before" curr */
516 if (act->happens_before(curr)) {
518 mo_graph->addEdge(act->get_reads_from(), curr);
520 mo_graph->addEdge(act, curr);
523 } else if (act->is_read() && !act->is_synchronizing(curr) &&
524 !act->same_thread(curr)) {
525 /* We have an action that:
526 (1) did not happen before us
527 (2) is a read and we are a write
528 (3) cannot synchronize with us
529 (4) is in a different thread
531 that read could potentially read from our write.
533 if (act->get_node()->add_future_value(curr->get_value()) &&
534 (!next_backtrack || *act > *next_backtrack))
535 next_backtrack = act;
544 * Finds the head(s) of the release sequence(s) containing a given ModelAction.
545 * The ModelAction under consideration is expected to be taking part in
546 * release/acquire synchronization as an object of the "reads from" relation.
547 * Note that this can only provide release sequence support for RMW chains
548 * which do not read from the future, as those actions cannot be traced until
549 * their "promise" is fulfilled. Similarly, we may not even establish the
550 * presence of a release sequence with certainty, as some modification order
551 * constraints may be decided further in the future. Thus, this function
552 * "returns" two pieces of data: a pass-by-reference vector of @a release_heads
553 * and a boolean representing certainty.
555 * @todo Finish lazy updating, when promises are fulfilled in the future
556 * @param rf The action that might be part of a release sequence. Must be a
558 * @param release_heads A pass-by-reference style return parameter. After
559 * execution of this function, release_heads will contain the heads of all the
560 * relevant release sequences, if any exists
561 * @return true, if the ModelChecker is certain that release_heads is complete;
564 bool ModelChecker::release_seq_head(const ModelAction *rf,
565 std::vector<const ModelAction *> *release_heads) const
567 ASSERT(rf->is_write());
569 /* read from future: need to settle this later */
570 return false; /* incomplete */
572 if (rf->is_release())
573 release_heads->push_back(rf);
575 if (rf->is_acquire())
576 return true; /* complete */
577 return release_seq_head(rf->get_reads_from(), release_heads);
579 if (rf->is_release())
580 return true; /* complete */
582 /* else relaxed write; check modification order for contiguous subsequence
583 * -> rf must be same thread as release */
584 int tid = id_to_int(rf->get_tid());
585 std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(rf->get_location());
586 action_list_t *list = &(*thrd_lists)[tid];
587 action_list_t::const_reverse_iterator rit;
589 /* Find rf in the thread list */
590 rit = std::find(list->rbegin(), list->rend(), rf);
591 ASSERT(rit != list->rend());
593 /* Find the last write/release */
594 for (; rit != list->rend(); rit++)
595 if ((*rit)->is_release())
597 if (rit == list->rend()) {
598 /* No write-release in this thread */
599 return true; /* complete */
601 ModelAction *release = *rit;
603 ASSERT(rf->same_thread(release));
606 for (unsigned int i = 0; i < thrd_lists->size(); i++) {
607 if (id_to_int(rf->get_tid()) == (int)i)
609 list = &(*thrd_lists)[i];
611 /* Can we ensure no future writes from this thread may break
612 * the release seq? */
613 bool future_ordered = false;
615 for (rit = list->rbegin(); rit != list->rend(); rit++) {
616 const ModelAction *act = *rit;
617 if (!act->is_write())
619 /* Reach synchronization -> this thread is complete */
620 if (act->happens_before(release))
622 if (rf->happens_before(act)) {
623 future_ordered = true;
627 /* Check modification order */
628 if (mo_graph->checkReachable(rf, act)) {
630 future_ordered = true;
633 if (mo_graph->checkReachable(act, release))
634 /* act --mo--> release */
636 if (mo_graph->checkReachable(release, act) &&
637 mo_graph->checkReachable(act, rf)) {
638 /* release --mo-> act --mo--> rf */
639 return true; /* complete */
644 return false; /* This thread is uncertain */
648 release_heads->push_back(release);
653 * A public interface for getting the release sequence head(s) with which a
654 * given ModelAction must synchronize. This function only returns a non-empty
655 * result when it can locate a release sequence head with certainty. Otherwise,
656 * it may mark the internal state of the ModelChecker so that it will handle
657 * the release sequence at a later time, causing @a act to update its
658 * synchronization at some later point in execution.
659 * @param act The 'acquire' action that may read from a release sequence
660 * @param release_heads A pass-by-reference return parameter. Will be filled
661 * with the head(s) of the release sequence(s), if they exists with certainty.
662 * @see ModelChecker::release_seq_head
664 void ModelChecker::get_release_seq_heads(ModelAction *act,
665 std::vector<const ModelAction *> *release_heads)
667 const ModelAction *rf = act->get_reads_from();
669 complete = release_seq_head(rf, release_heads);
671 /* add act to 'lazy checking' list */
672 std::list<ModelAction *> *list;
673 list = lazy_sync_with_release->get_safe_ptr(act->get_location());
674 list->push_back(act);
679 * Attempt to resolve all stashed operations that might synchronize with a
680 * release sequence for a given location. This implements the "lazy" portion of
681 * determining whether or not a release sequence was contiguous, since not all
682 * modification order information is present at the time an action occurs.
684 * @param location The location/object that should be checked for release
685 * sequence resolutions
686 * @return True if any updates occurred (new synchronization, new mo_graph edges)
688 bool ModelChecker::resolve_release_sequences(void *location)
690 std::list<ModelAction *> *list;
691 list = lazy_sync_with_release->getptr(location);
695 bool updated = false;
696 std::list<ModelAction *>::iterator it = list->begin();
697 while (it != list->end()) {
698 ModelAction *act = *it;
699 const ModelAction *rf = act->get_reads_from();
700 std::vector<const ModelAction *> release_heads;
702 complete = release_seq_head(rf, &release_heads);
703 for (unsigned int i = 0; i < release_heads.size(); i++) {
704 if (!act->has_synchronized_with(release_heads[i])) {
706 act->synchronize_with(release_heads[i]);
711 /* propagate synchronization to later actions */
712 action_list_t::reverse_iterator it = action_trace->rbegin();
713 while ((*it) != act) {
714 ModelAction *propagate = *it;
715 if (act->happens_before(propagate))
716 /** @todo new mo_graph edges along with
717 * this synchronization? */
718 propagate->synchronize_with(act);
722 it = list->erase(it);
727 // If we resolved promises or data races, see if we have realized a data race.
728 if (checkDataRaces()) {
736 * Performs various bookkeeping operations for the current ModelAction. For
737 * instance, adds action to the per-object, per-thread action vector and to the
738 * action trace list of all thread actions.
740 * @param act is the ModelAction to add.
742 void ModelChecker::add_action_to_lists(ModelAction *act)
744 int tid = id_to_int(act->get_tid());
745 action_trace->push_back(act);
747 obj_map->get_safe_ptr(act->get_location())->push_back(act);
749 std::vector<action_list_t> *vec = obj_thrd_map->get_safe_ptr(act->get_location());
750 if (tid >= (int)vec->size())
751 vec->resize(next_thread_id);
752 (*vec)[tid].push_back(act);
754 if ((int)thrd_last_action->size() <= tid)
755 thrd_last_action->resize(get_num_threads());
756 (*thrd_last_action)[tid] = act;
759 ModelAction * ModelChecker::get_last_action(thread_id_t tid)
761 int nthreads = get_num_threads();
762 if ((int)thrd_last_action->size() < nthreads)
763 thrd_last_action->resize(nthreads);
764 return (*thrd_last_action)[id_to_int(tid)];
768 * Gets the last memory_order_seq_cst action (in the total global sequence)
769 * performed on a particular object (i.e., memory location).
770 * @param location The object location to check
771 * @return The last seq_cst action performed
773 ModelAction * ModelChecker::get_last_seq_cst(const void *location)
775 action_list_t *list = obj_map->get_safe_ptr(location);
776 /* Find: max({i in dom(S) | seq_cst(t_i) && isWrite(t_i) && samevar(t_i, t)}) */
777 action_list_t::reverse_iterator rit;
778 for (rit = list->rbegin(); rit != list->rend(); rit++)
779 if ((*rit)->is_write() && (*rit)->is_seqcst())
784 ModelAction * ModelChecker::get_parent_action(thread_id_t tid)
786 ModelAction *parent = get_last_action(tid);
788 parent = get_thread(tid)->get_creation();
793 * Returns the clock vector for a given thread.
794 * @param tid The thread whose clock vector we want
795 * @return Desired clock vector
797 ClockVector * ModelChecker::get_cv(thread_id_t tid)
799 return get_parent_action(tid)->get_cv();
803 * Resolve a set of Promises with a current write. The set is provided in the
804 * Node corresponding to @a write.
805 * @param write The ModelAction that is fulfilling Promises
806 * @return True if promises were resolved; false otherwise
808 bool ModelChecker::resolve_promises(ModelAction *write)
810 bool resolved = false;
811 for (unsigned int i = 0, promise_index = 0; promise_index < promises->size(); i++) {
812 Promise *promise = (*promises)[promise_index];
813 if (write->get_node()->get_promise(i)) {
814 ModelAction *read = promise->get_action();
815 read->read_from(write);
816 r_modification_order(read, write);
817 post_r_modification_order(read, write);
818 promises->erase(promises->begin() + promise_index);
828 * Compute the set of promises that could potentially be satisfied by this
829 * action. Note that the set computation actually appears in the Node, not in
831 * @param curr The ModelAction that may satisfy promises
833 void ModelChecker::compute_promises(ModelAction *curr)
835 for (unsigned int i = 0; i < promises->size(); i++) {
836 Promise *promise = (*promises)[i];
837 const ModelAction *act = promise->get_action();
838 if (!act->happens_before(curr) &&
840 !act->is_synchronizing(curr) &&
841 !act->same_thread(curr) &&
842 promise->get_value() == curr->get_value()) {
843 curr->get_node()->set_promise(i);
848 /** Checks promises in response to change in ClockVector Threads. */
849 void ModelChecker::check_promises(ClockVector *old_cv, ClockVector *merge_cv)
851 for (unsigned int i = 0; i < promises->size(); i++) {
852 Promise *promise = (*promises)[i];
853 const ModelAction *act = promise->get_action();
854 if ((old_cv == NULL || !old_cv->synchronized_since(act)) &&
855 merge_cv->synchronized_since(act)) {
856 //This thread is no longer able to send values back to satisfy the promise
857 int num_synchronized_threads = promise->increment_threads();
858 if (num_synchronized_threads == get_num_threads()) {
860 failed_promise = true;
868 * Build up an initial set of all past writes that this 'read' action may read
869 * from. This set is determined by the clock vector's "happens before"
871 * @param curr is the current ModelAction that we are exploring; it must be a
874 void ModelChecker::build_reads_from_past(ModelAction *curr)
876 std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(curr->get_location());
878 ASSERT(curr->is_read());
880 ModelAction *last_seq_cst = NULL;
882 /* Track whether this object has been initialized */
883 bool initialized = false;
885 if (curr->is_seqcst()) {
886 last_seq_cst = get_last_seq_cst(curr->get_location());
887 /* We have to at least see the last sequentially consistent write,
888 so we are initialized. */
889 if (last_seq_cst != NULL)
893 /* Iterate over all threads */
894 for (i = 0; i < thrd_lists->size(); i++) {
895 /* Iterate over actions in thread, starting from most recent */
896 action_list_t *list = &(*thrd_lists)[i];
897 action_list_t::reverse_iterator rit;
898 for (rit = list->rbegin(); rit != list->rend(); rit++) {
899 ModelAction *act = *rit;
901 /* Only consider 'write' actions */
902 if (!act->is_write())
905 /* Don't consider more than one seq_cst write if we are a seq_cst read. */
906 if (!act->is_seqcst() || !curr->is_seqcst() || act == last_seq_cst) {
907 DEBUG("Adding action to may_read_from:\n");
912 curr->get_node()->add_read_from(act);
915 /* Include at most one act per-thread that "happens before" curr */
916 if (act->happens_before(curr)) {
924 /** @todo Need a more informative way of reporting errors. */
925 printf("ERROR: may read from uninitialized atomic\n");
928 if (DBG_ENABLED() || !initialized) {
929 printf("Reached read action:\n");
931 printf("Printing may_read_from\n");
932 curr->get_node()->print_may_read_from();
933 printf("End printing may_read_from\n");
939 static void print_list(action_list_t *list)
941 action_list_t::iterator it;
943 printf("---------------------------------------------------------------------\n");
946 for (it = list->begin(); it != list->end(); it++) {
949 printf("---------------------------------------------------------------------\n");
952 void ModelChecker::print_summary()
955 printf("Number of executions: %d\n", num_executions);
956 printf("Total nodes created: %d\n", node_stack->get_total_nodes());
960 if (!isfinalfeasible())
961 printf("INFEASIBLE EXECUTION!\n");
962 print_list(action_trace);
967 * Add a Thread to the system for the first time. Should only be called once
969 * @param t The Thread to add
971 void ModelChecker::add_thread(Thread *t)
973 thread_map->put(id_to_int(t->get_id()), t);
974 scheduler->add_thread(t);
977 void ModelChecker::remove_thread(Thread *t)
979 scheduler->remove_thread(t);
983 * Switch from a user-context to the "master thread" context (a.k.a. system
984 * context). This switch is made with the intention of exploring a particular
985 * model-checking action (described by a ModelAction object). Must be called
986 * from a user-thread context.
987 * @param act The current action that will be explored. Must not be NULL.
988 * @return Return status from the 'swap' call (i.e., success/fail, 0/-1)
990 int ModelChecker::switch_to_master(ModelAction *act)
993 Thread *old = thread_current();
994 set_current_action(act);
995 old->set_state(THREAD_READY);
996 return Thread::swap(old, &system_context);
1000 * Takes the next step in the execution, if possible.
1001 * @return Returns true (success) if a step was taken and false otherwise.
1003 bool ModelChecker::take_step() {
1004 Thread *curr, *next;
1009 curr = thread_current();
1011 if (curr->get_state() == THREAD_READY) {
1012 ASSERT(current_action);
1013 nextThread = check_current_action(current_action);
1014 current_action = NULL;
1015 if (!curr->is_blocked() && !curr->is_complete())
1016 scheduler->add_thread(curr);
1021 next = scheduler->next_thread(nextThread);
1023 /* Infeasible -> don't take any more steps */
1028 next->set_state(THREAD_RUNNING);
1029 DEBUG("(%d, %d)\n", curr ? curr->get_id() : -1, next ? next->get_id() : -1);
1031 /* next == NULL -> don't take any more steps */
1034 /* Return false only if swap fails with an error */
1035 return (Thread::swap(&system_context, next) == 0);
1038 /** Runs the current execution until threre are no more steps to take. */
1039 void ModelChecker::finish_execution() {
1042 while (take_step());