7 #include "snapshot-interface.h"
9 #include "clockvector.h"
10 #include "cyclegraph.h"
13 #define INITIAL_THREAD_ID 0
17 /** @brief Constructor */
18 ModelChecker::ModelChecker(struct model_params params) :
19 /* Initialize default scheduler */
20 scheduler(new Scheduler()),
21 /* First thread created will have id INITIAL_THREAD_ID */
22 next_thread_id(INITIAL_THREAD_ID),
23 used_sequence_numbers(0),
29 action_trace(new action_list_t()),
30 thread_map(new HashTable<int, Thread *, int>()),
31 obj_map(new HashTable<const void *, action_list_t, uintptr_t, 4>()),
32 obj_thrd_map(new HashTable<void *, std::vector<action_list_t>, uintptr_t, 4 >()),
33 promises(new std::vector<Promise *>()),
34 lazy_sync_with_release(new HashTable<void *, std::list<ModelAction *>, uintptr_t, 4>()),
35 thrd_last_action(new std::vector<ModelAction *>(1)),
36 node_stack(new NodeStack()),
38 mo_graph(new CycleGraph()),
43 /** @brief Destructor */
44 ModelChecker::~ModelChecker()
46 for (int i = 0; i < get_num_threads(); i++)
47 delete thread_map->get(i);
54 for (unsigned int i = 0; i < promises->size(); i++)
55 delete (*promises)[i];
58 delete lazy_sync_with_release;
60 delete thrd_last_action;
67 * Restores user program to initial state and resets all model-checker data
70 void ModelChecker::reset_to_initial_state()
72 DEBUG("+++ Resetting to initial state +++\n");
73 node_stack->reset_execution();
74 current_action = NULL;
75 next_thread_id = INITIAL_THREAD_ID;
76 used_sequence_numbers = 0;
78 next_backtrack = NULL;
79 failed_promise = false;
80 snapshotObject->backTrackBeforeStep(0);
83 /** @returns a thread ID for a new Thread */
84 thread_id_t ModelChecker::get_next_id()
86 return next_thread_id++;
89 /** @returns the number of user threads created during this execution */
90 int ModelChecker::get_num_threads()
92 return next_thread_id;
95 /** @returns a sequence number for a new ModelAction */
96 modelclock_t ModelChecker::get_next_seq_num()
98 return ++used_sequence_numbers;
102 * Choose the next thread in the replay sequence.
104 * If the replay sequence has reached the 'diverge' point, returns a thread
105 * from the backtracking set. Otherwise, simply returns the next thread in the
106 * sequence that is being replayed.
108 Thread * ModelChecker::get_next_replay_thread()
112 /* Have we completed exploring the preselected path? */
116 /* Else, we are trying to replay an execution */
117 ModelAction *next = node_stack->get_next()->get_action();
119 if (next == diverge) {
120 Node *nextnode = next->get_node();
121 /* Reached divergence point */
122 if (nextnode->increment_promise()) {
123 /* The next node will try to satisfy a different set of promises. */
124 tid = next->get_tid();
125 node_stack->pop_restofstack(2);
126 } else if (nextnode->increment_read_from()) {
127 /* The next node will read from a different value. */
128 tid = next->get_tid();
129 node_stack->pop_restofstack(2);
130 } else if (nextnode->increment_future_value()) {
131 /* The next node will try to read from a different future value. */
132 tid = next->get_tid();
133 node_stack->pop_restofstack(2);
135 /* Make a different thread execute for next step */
136 Node *node = nextnode->get_parent();
137 tid = node->get_next_backtrack();
138 node_stack->pop_restofstack(1);
140 DEBUG("*** Divergence point ***\n");
143 tid = next->get_tid();
145 DEBUG("*** ModelChecker chose next thread = %d ***\n", tid);
146 ASSERT(tid != THREAD_ID_T_NONE);
147 return thread_map->get(id_to_int(tid));
151 * Queries the model-checker for more executions to explore and, if one
152 * exists, resets the model-checker state to execute a new execution.
154 * @return If there are more executions to explore, return true. Otherwise,
157 bool ModelChecker::next_execution()
163 if (isfinalfeasible() || DBG_ENABLED())
166 if ((diverge = model->get_next_backtrack()) == NULL)
170 printf("Next execution will diverge at:\n");
174 model->reset_to_initial_state();
178 ModelAction * ModelChecker::get_last_conflict(ModelAction *act)
180 action_type type = act->get_type();
190 /* linear search: from most recent to oldest */
191 action_list_t *list = obj_map->get_safe_ptr(act->get_location());
192 action_list_t::reverse_iterator rit;
193 for (rit = list->rbegin(); rit != list->rend(); rit++) {
194 ModelAction *prev = *rit;
195 if (act->is_synchronizing(prev))
201 void ModelChecker::set_backtracking(ModelAction *act)
205 Thread *t = get_thread(act->get_tid());
207 prev = get_last_conflict(act);
211 node = prev->get_node()->get_parent();
213 while (!node->is_enabled(t))
216 /* Check if this has been explored already */
217 if (node->has_been_explored(t->get_id()))
220 /* Cache the latest backtracking point */
221 if (!next_backtrack || *prev > *next_backtrack)
222 next_backtrack = prev;
224 /* If this is a new backtracking point, mark the tree */
225 if (!node->set_backtrack(t->get_id()))
227 DEBUG("Setting backtrack: conflict = %d, instead tid = %d\n",
228 prev->get_tid(), t->get_id());
236 * Returns last backtracking point. The model checker will explore a different
237 * path for this point in the next execution.
238 * @return The ModelAction at which the next execution should diverge.
240 ModelAction * ModelChecker::get_next_backtrack()
242 ModelAction *next = next_backtrack;
243 next_backtrack = NULL;
248 * This is the heart of the model checker routine. It performs model-checking
249 * actions corresponding to a given "current action." Among other processes, it
250 * calculates reads-from relationships, updates synchronization clock vectors,
251 * forms a memory_order constraints graph, and handles replay/backtrack
252 * execution when running permutations of previously-observed executions.
254 * @param curr The current action to process
255 * @return The next Thread that must be executed. May be NULL if ModelChecker
256 * makes no choice (e.g., according to replay execution, combining RMW actions,
259 Thread * ModelChecker::check_current_action(ModelAction *curr)
261 bool already_added = false;
265 if (curr->is_rmwc() || curr->is_rmw()) {
266 ModelAction *tmp = process_rmw(curr);
267 already_added = true;
271 ModelAction *tmp = node_stack->explore_action(curr);
273 /* Discard duplicate ModelAction; use action from NodeStack */
274 /* First restore type and order in case of RMW operation */
276 tmp->copy_typeandorder(curr);
278 /* If we have diverged, we need to reset the clock vector. */
280 tmp->create_cv(get_parent_action(tmp->get_tid()));
286 * Perform one-time actions when pushing new ModelAction onto
289 curr->create_cv(get_parent_action(curr->get_tid()));
290 /* Build may_read_from set */
292 build_reads_from_past(curr);
293 if (curr->is_write())
294 compute_promises(curr);
298 /* Assign 'creation' parent */
299 if (curr->get_type() == THREAD_CREATE) {
300 Thread *th = (Thread *)curr->get_location();
301 th->set_creation(curr);
304 /* Deal with new thread */
305 if (curr->get_type() == THREAD_START)
306 check_promises(NULL, curr->get_cv());
308 /* Assign reads_from values */
309 Thread *th = get_thread(curr->get_tid());
310 uint64_t value = VALUE_NONE;
311 bool updated = false;
312 if (curr->is_read()) {
313 const ModelAction *reads_from = curr->get_node()->get_read_from();
314 if (reads_from != NULL) {
315 value = reads_from->get_value();
316 /* Assign reads_from, perform release/acquire synchronization */
317 curr->read_from(reads_from);
318 if (r_modification_order(curr,reads_from))
321 /* Read from future value */
322 value = curr->get_node()->get_future_value();
323 curr->read_from(NULL);
324 Promise *valuepromise = new Promise(curr, value);
325 promises->push_back(valuepromise);
327 } else if (curr->is_write()) {
328 if (w_modification_order(curr))
330 if (resolve_promises(curr))
335 resolve_release_sequences(curr->get_location());
337 th->set_return_value(value);
339 /* Add action to list. */
341 add_action_to_lists(curr);
343 Node *currnode = curr->get_node();
344 Node *parnode = currnode->get_parent();
346 if (!parnode->backtrack_empty() || !currnode->read_from_empty() ||
347 !currnode->future_value_empty() || !currnode->promise_empty())
348 if (!next_backtrack || *curr > *next_backtrack)
349 next_backtrack = curr;
351 set_backtracking(curr);
353 /* Do not split atomic actions. */
355 return thread_current();
357 return get_next_replay_thread();
360 /** @returns whether the current partial trace is feasible. */
361 bool ModelChecker::isfeasible() {
362 return !mo_graph->checkForCycles() && !failed_promise;
365 /** Returns whether the current completed trace is feasible. */
366 bool ModelChecker::isfinalfeasible() {
367 return isfeasible() && promises->size() == 0;
370 /** Close out a RMWR by converting previous RMWR into a RMW or READ. */
371 ModelAction * ModelChecker::process_rmw(ModelAction *act) {
372 int tid = id_to_int(act->get_tid());
373 ModelAction *lastread = get_last_action(tid);
374 lastread->process_rmw(act);
376 mo_graph->addRMWEdge(lastread->get_reads_from(), lastread);
381 * Updates the mo_graph with the constraints imposed from the current read.
382 * @param curr The current action. Must be a read.
383 * @param rf The action that curr reads from. Must be a write.
384 * @return True if modification order edges were added; false otherwise
386 bool ModelChecker::r_modification_order(ModelAction *curr, const ModelAction *rf)
388 std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(curr->get_location());
391 ASSERT(curr->is_read());
393 /* Iterate over all threads */
394 for (i = 0; i < thrd_lists->size(); i++) {
395 /* Iterate over actions in thread, starting from most recent */
396 action_list_t *list = &(*thrd_lists)[i];
397 action_list_t::reverse_iterator rit;
398 for (rit = list->rbegin(); rit != list->rend(); rit++) {
399 ModelAction *act = *rit;
401 /* Include at most one act per-thread that "happens before" curr */
402 if (act->happens_before(curr)) {
403 if (act->is_read()) {
404 const ModelAction *prevreadfrom = act->get_reads_from();
405 if (prevreadfrom != NULL && rf != prevreadfrom) {
406 mo_graph->addEdge(prevreadfrom, rf);
409 } else if (rf != act) {
410 mo_graph->addEdge(act, rf);
421 /** Updates the mo_graph with the constraints imposed from the current read. */
422 void ModelChecker::post_r_modification_order(ModelAction *curr, const ModelAction *rf)
424 std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(curr->get_location());
426 ASSERT(curr->is_read());
428 /* Iterate over all threads */
429 for (i = 0; i < thrd_lists->size(); i++) {
430 /* Iterate over actions in thread, starting from most recent */
431 action_list_t *list = &(*thrd_lists)[i];
432 action_list_t::reverse_iterator rit;
433 ModelAction *lastact = NULL;
435 /* Find last action that happens after curr */
436 for (rit = list->rbegin(); rit != list->rend(); rit++) {
437 ModelAction *act = *rit;
438 if (curr->happens_before(act)) {
444 /* Include at most one act per-thread that "happens before" curr */
445 if (lastact != NULL) {
446 if (lastact->is_read()) {
447 const ModelAction *postreadfrom = lastact->get_reads_from();
448 if (postreadfrom != NULL&&rf != postreadfrom)
449 mo_graph->addEdge(rf, postreadfrom);
450 } else if (rf != lastact) {
451 mo_graph->addEdge(rf, lastact);
459 * Updates the mo_graph with the constraints imposed from the current write.
460 * @param curr The current action. Must be a write.
461 * @return True if modification order edges were added; false otherwise
463 bool ModelChecker::w_modification_order(ModelAction *curr)
465 std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(curr->get_location());
468 ASSERT(curr->is_write());
470 if (curr->is_seqcst()) {
471 /* We have to at least see the last sequentially consistent write,
472 so we are initialized. */
473 ModelAction *last_seq_cst = get_last_seq_cst(curr->get_location());
474 if (last_seq_cst != NULL) {
475 mo_graph->addEdge(last_seq_cst, curr);
480 /* Iterate over all threads */
481 for (i = 0; i < thrd_lists->size(); i++) {
482 /* Iterate over actions in thread, starting from most recent */
483 action_list_t *list = &(*thrd_lists)[i];
484 action_list_t::reverse_iterator rit;
485 for (rit = list->rbegin(); rit != list->rend(); rit++) {
486 ModelAction *act = *rit;
488 /* Include at most one act per-thread that "happens before" curr */
489 if (act->happens_before(curr)) {
491 mo_graph->addEdge(act->get_reads_from(), curr);
493 mo_graph->addEdge(act, curr);
496 } else if (act->is_read() && !act->is_synchronizing(curr) &&
497 !act->same_thread(curr)) {
498 /* We have an action that:
499 (1) did not happen before us
500 (2) is a read and we are a write
501 (3) cannot synchronize with us
502 (4) is in a different thread
504 that read could potentially read from our write.
506 if (act->get_node()->add_future_value(curr->get_value()) &&
507 (!next_backtrack || *act > *next_backtrack))
508 next_backtrack = act;
517 * Finds the head(s) of the release sequence(s) containing a given ModelAction.
518 * The ModelAction under consideration is expected to be taking part in
519 * release/acquire synchronization as an object of the "reads from" relation.
520 * Note that this can only provide release sequence support for RMW chains
521 * which do not read from the future, as those actions cannot be traced until
522 * their "promise" is fulfilled. Similarly, we may not even establish the
523 * presence of a release sequence with certainty, as some modification order
524 * constraints may be decided further in the future. Thus, this function
525 * "returns" two pieces of data: a pass-by-reference vector of @a release_heads
526 * and a boolean representing certainty.
528 * @todo Finish lazy updating, when promises are fulfilled in the future
529 * @param rf The action that might be part of a release sequence. Must be a
531 * @param release_heads A pass-by-reference style return parameter. After
532 * execution of this function, release_heads will contain the heads of all the
533 * relevant release sequences, if any exists
534 * @return true, if the ModelChecker is certain that release_heads is complete;
537 bool ModelChecker::release_seq_head(const ModelAction *rf,
538 std::vector<const ModelAction *> *release_heads) const
540 ASSERT(rf->is_write());
542 /* read from future: need to settle this later */
543 return false; /* incomplete */
545 if (rf->is_release())
546 release_heads->push_back(rf);
548 if (rf->is_acquire())
549 return true; /* complete */
550 return release_seq_head(rf->get_reads_from(), release_heads);
552 if (rf->is_release())
553 return true; /* complete */
555 /* else relaxed write; check modification order for contiguous subsequence
556 * -> rf must be same thread as release */
557 int tid = id_to_int(rf->get_tid());
558 std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(rf->get_location());
559 action_list_t *list = &(*thrd_lists)[tid];
560 action_list_t::const_reverse_iterator rit;
562 /* Find rf in the thread list */
563 for (rit = list->rbegin(); rit != list->rend(); rit++)
567 /* Find the last write/release */
568 for (; rit != list->rend(); rit++)
569 if ((*rit)->is_release())
571 if (rit == list->rend()) {
572 /* No write-release in this thread */
573 return true; /* complete */
575 ModelAction *release = *rit;
577 ASSERT(rf->same_thread(release));
580 for (unsigned int i = 0; i < thrd_lists->size(); i++) {
581 if (id_to_int(rf->get_tid()) == (int)i)
583 list = &(*thrd_lists)[i];
584 for (rit = list->rbegin(); rit != list->rend(); rit++) {
585 const ModelAction *act = *rit;
586 if (!act->is_write())
588 /* Reach synchronization -> this thread is complete */
589 if (act->happens_before(release))
591 if (rf->happens_before(act))
594 /* Check modification order */
595 if (mo_graph->checkReachable(rf, act))
598 if (mo_graph->checkReachable(act, release))
599 /* act --mo--> release */
601 if (mo_graph->checkReachable(release, act) &&
602 mo_graph->checkReachable(act, rf)) {
603 /* release --mo-> act --mo--> rf */
604 return true; /* complete */
611 release_heads->push_back(release);
616 * A public interface for getting the release sequence head(s) with which a
617 * given ModelAction must synchronize. This function only returns a non-empty
618 * result when it can locate a release sequence head with certainty. Otherwise,
619 * it may mark the internal state of the ModelChecker so that it will handle
620 * the release sequence at a later time, causing @a act to update its
621 * synchronization at some later point in execution.
622 * @param act The 'acquire' action that may read from a release sequence
623 * @param release_heads A pass-by-reference return parameter. Will be filled
624 * with the head(s) of the release sequence(s), if they exists with certainty.
625 * @see ModelChecker::release_seq_head
627 void ModelChecker::get_release_seq_heads(ModelAction *act,
628 std::vector<const ModelAction *> *release_heads)
630 const ModelAction *rf = act->get_reads_from();
632 complete = release_seq_head(rf, release_heads);
634 /* add act to 'lazy checking' list */
635 std::list<ModelAction *> *list;
636 list = lazy_sync_with_release->get_safe_ptr(act->get_location());
637 list->push_back(act);
642 * Attempt to resolve all stashed operations that might synchronize with a
643 * release sequence for a given location. This implements the "lazy" portion of
644 * determining whether or not a release sequence was contiguous, since not all
645 * modification order information is present at the time an action occurs.
647 * @param location The location/object that should be checked for release
648 * sequence resolutions
649 * @return True if any updates occurred (new synchronization, new mo_graph edges)
651 bool ModelChecker::resolve_release_sequences(void *location)
653 std::list<ModelAction *> *list;
654 list = lazy_sync_with_release->getptr(location);
658 bool updated = false;
659 std::list<ModelAction *>::iterator it = list->begin();
660 while (it != list->end()) {
661 ModelAction *act = *it;
662 const ModelAction *rf = act->get_reads_from();
663 std::vector<const ModelAction *> release_heads;
665 complete = release_seq_head(rf, &release_heads);
666 for (unsigned int i = 0; i < release_heads.size(); i++) {
667 if (!act->has_synchronized_with(release_heads[i])) {
669 act->synchronize_with(release_heads[i]);
674 /* propagate synchronization to later actions */
675 action_list_t::reverse_iterator it = action_trace->rbegin();
676 while ((*it) != act) {
677 ModelAction *propagate = *it;
678 if (act->happens_before(propagate))
679 /** @todo new mo_graph edges along with
680 * this synchronization? */
681 propagate->synchronize_with(act);
685 it = list->erase(it);
694 * Performs various bookkeeping operations for the current ModelAction. For
695 * instance, adds action to the per-object, per-thread action vector and to the
696 * action trace list of all thread actions.
698 * @param act is the ModelAction to add.
700 void ModelChecker::add_action_to_lists(ModelAction *act)
702 int tid = id_to_int(act->get_tid());
703 action_trace->push_back(act);
705 obj_map->get_safe_ptr(act->get_location())->push_back(act);
707 std::vector<action_list_t> *vec = obj_thrd_map->get_safe_ptr(act->get_location());
708 if (tid >= (int)vec->size())
709 vec->resize(next_thread_id);
710 (*vec)[tid].push_back(act);
712 if ((int)thrd_last_action->size() <= tid)
713 thrd_last_action->resize(get_num_threads());
714 (*thrd_last_action)[tid] = act;
717 ModelAction * ModelChecker::get_last_action(thread_id_t tid)
719 int nthreads = get_num_threads();
720 if ((int)thrd_last_action->size() < nthreads)
721 thrd_last_action->resize(nthreads);
722 return (*thrd_last_action)[id_to_int(tid)];
726 * Gets the last memory_order_seq_cst action (in the total global sequence)
727 * performed on a particular object (i.e., memory location).
728 * @param location The object location to check
729 * @return The last seq_cst action performed
731 ModelAction * ModelChecker::get_last_seq_cst(const void *location)
733 action_list_t *list = obj_map->get_safe_ptr(location);
734 /* Find: max({i in dom(S) | seq_cst(t_i) && isWrite(t_i) && samevar(t_i, t)}) */
735 action_list_t::reverse_iterator rit;
736 for (rit = list->rbegin(); rit != list->rend(); rit++)
737 if ((*rit)->is_write() && (*rit)->is_seqcst())
742 ModelAction * ModelChecker::get_parent_action(thread_id_t tid)
744 ModelAction *parent = get_last_action(tid);
746 parent = get_thread(tid)->get_creation();
751 * Returns the clock vector for a given thread.
752 * @param tid The thread whose clock vector we want
753 * @return Desired clock vector
755 ClockVector * ModelChecker::get_cv(thread_id_t tid)
757 return get_parent_action(tid)->get_cv();
761 * Resolve a set of Promises with a current write. The set is provided in the
762 * Node corresponding to @a write.
763 * @param write The ModelAction that is fulfilling Promises
764 * @return True if promises were resolved; false otherwise
766 bool ModelChecker::resolve_promises(ModelAction *write)
768 bool resolved = false;
769 for (unsigned int i = 0, promise_index = 0; promise_index < promises->size(); i++) {
770 Promise *promise = (*promises)[promise_index];
771 if (write->get_node()->get_promise(i)) {
772 ModelAction *read = promise->get_action();
773 read->read_from(write);
774 r_modification_order(read, write);
775 post_r_modification_order(read, write);
776 promises->erase(promises->begin() + promise_index);
785 * Compute the set of promises that could potentially be satisfied by this
786 * action. Note that the set computation actually appears in the Node, not in
788 * @param curr The ModelAction that may satisfy promises
790 void ModelChecker::compute_promises(ModelAction *curr)
792 for (unsigned int i = 0; i < promises->size(); i++) {
793 Promise *promise = (*promises)[i];
794 const ModelAction *act = promise->get_action();
795 if (!act->happens_before(curr) &&
797 !act->is_synchronizing(curr) &&
798 !act->same_thread(curr) &&
799 promise->get_value() == curr->get_value()) {
800 curr->get_node()->set_promise(i);
805 /** Checks promises in response to change in ClockVector Threads. */
806 void ModelChecker::check_promises(ClockVector *old_cv, ClockVector *merge_cv)
808 for (unsigned int i = 0; i < promises->size(); i++) {
809 Promise *promise = (*promises)[i];
810 const ModelAction *act = promise->get_action();
811 if ((old_cv == NULL || !old_cv->synchronized_since(act)) &&
812 merge_cv->synchronized_since(act)) {
813 //This thread is no longer able to send values back to satisfy the promise
814 int num_synchronized_threads = promise->increment_threads();
815 if (num_synchronized_threads == model->get_num_threads()) {
817 failed_promise = true;
825 * Build up an initial set of all past writes that this 'read' action may read
826 * from. This set is determined by the clock vector's "happens before"
828 * @param curr is the current ModelAction that we are exploring; it must be a
831 void ModelChecker::build_reads_from_past(ModelAction *curr)
833 std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(curr->get_location());
835 ASSERT(curr->is_read());
837 ModelAction *last_seq_cst = NULL;
839 /* Track whether this object has been initialized */
840 bool initialized = false;
842 if (curr->is_seqcst()) {
843 last_seq_cst = get_last_seq_cst(curr->get_location());
844 /* We have to at least see the last sequentially consistent write,
845 so we are initialized. */
846 if (last_seq_cst != NULL)
850 /* Iterate over all threads */
851 for (i = 0; i < thrd_lists->size(); i++) {
852 /* Iterate over actions in thread, starting from most recent */
853 action_list_t *list = &(*thrd_lists)[i];
854 action_list_t::reverse_iterator rit;
855 for (rit = list->rbegin(); rit != list->rend(); rit++) {
856 ModelAction *act = *rit;
858 /* Only consider 'write' actions */
859 if (!act->is_write())
862 /* Don't consider more than one seq_cst write if we are a seq_cst read. */
863 if (!act->is_seqcst() || !curr->is_seqcst() || act == last_seq_cst) {
864 DEBUG("Adding action to may_read_from:\n");
869 curr->get_node()->add_read_from(act);
872 /* Include at most one act per-thread that "happens before" curr */
873 if (act->happens_before(curr)) {
881 /** @todo Need a more informative way of reporting errors. */
882 printf("ERROR: may read from uninitialized atomic\n");
885 if (DBG_ENABLED() || !initialized) {
886 printf("Reached read action:\n");
888 printf("Printing may_read_from\n");
889 curr->get_node()->print_may_read_from();
890 printf("End printing may_read_from\n");
896 static void print_list(action_list_t *list)
898 action_list_t::iterator it;
900 printf("---------------------------------------------------------------------\n");
903 for (it = list->begin(); it != list->end(); it++) {
906 printf("---------------------------------------------------------------------\n");
909 void ModelChecker::print_summary()
912 printf("Number of executions: %d\n", num_executions);
913 printf("Total nodes created: %d\n", node_stack->get_total_nodes());
917 if (!isfinalfeasible())
918 printf("INFEASIBLE EXECUTION!\n");
919 print_list(action_trace);
924 * Add a Thread to the system for the first time. Should only be called once
926 * @param t The Thread to add
928 void ModelChecker::add_thread(Thread *t)
930 thread_map->put(id_to_int(t->get_id()), t);
931 scheduler->add_thread(t);
934 void ModelChecker::remove_thread(Thread *t)
936 scheduler->remove_thread(t);
940 * Switch from a user-context to the "master thread" context (a.k.a. system
941 * context). This switch is made with the intention of exploring a particular
942 * model-checking action (described by a ModelAction object). Must be called
943 * from a user-thread context.
944 * @param act The current action that will be explored. May be NULL, although
945 * there is little reason to switch to the model-checker without an action to
946 * explore (note: act == NULL is sometimes used as a hack to allow a thread to
947 * yield control without performing any progress; see thrd_join()).
948 * @return Return status from the 'swap' call (i.e., success/fail, 0/-1)
950 int ModelChecker::switch_to_master(ModelAction *act)
953 Thread *old = thread_current();
954 set_current_action(act);
955 old->set_state(THREAD_READY);
956 return Thread::swap(old, &system_context);
960 * Takes the next step in the execution, if possible.
961 * @return Returns true (success) if a step was taken and false otherwise.
963 bool ModelChecker::take_step() {
966 curr = thread_current();
968 if (curr->get_state() == THREAD_READY) {
969 if (current_action) {
970 nextThread = check_current_action(current_action);
971 current_action = NULL;
973 scheduler->add_thread(curr);
974 } else if (curr->get_state() == THREAD_RUNNING) {
975 /* Stopped while running; i.e., completed */
981 next = scheduler->next_thread(nextThread);
983 /* Infeasible -> don't take any more steps */
988 next->set_state(THREAD_RUNNING);
989 DEBUG("(%d, %d)\n", curr ? curr->get_id() : -1, next ? next->get_id() : -1);
991 /* next == NULL -> don't take any more steps */
994 /* Return false only if swap fails with an error */
995 return (Thread::swap(&system_context, next) == 0);
998 /** Runs the current execution until threre are no more steps to take. */
999 void ModelChecker::finish_execution() {
1002 while (take_step());