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;
247 Thread * ModelChecker::check_current_action(ModelAction *curr)
249 bool already_added = false;
253 if (curr->is_rmwc() || curr->is_rmw()) {
254 ModelAction *tmp = process_rmw(curr);
255 already_added = true;
259 ModelAction *tmp = node_stack->explore_action(curr);
261 /* Discard duplicate ModelAction; use action from NodeStack */
262 /* First restore type and order in case of RMW operation */
264 tmp->copy_typeandorder(curr);
266 /* If we have diverged, we need to reset the clock vector. */
268 tmp->create_cv(get_parent_action(tmp->get_tid()));
274 * Perform one-time actions when pushing new ModelAction onto
277 curr->create_cv(get_parent_action(curr->get_tid()));
278 /* Build may_read_from set */
280 build_reads_from_past(curr);
281 if (curr->is_write())
282 compute_promises(curr);
286 /* Assign 'creation' parent */
287 if (curr->get_type() == THREAD_CREATE) {
288 Thread *th = (Thread *)curr->get_location();
289 th->set_creation(curr);
292 /* Deal with new thread */
293 if (curr->get_type() == THREAD_START)
294 check_promises(NULL, curr->get_cv());
296 /* Assign reads_from values */
297 Thread *th = get_thread(curr->get_tid());
298 uint64_t value = VALUE_NONE;
299 bool updated = false;
300 if (curr->is_read()) {
301 const ModelAction *reads_from = curr->get_node()->get_read_from();
302 if (reads_from != NULL) {
303 value = reads_from->get_value();
304 /* Assign reads_from, perform release/acquire synchronization */
305 curr->read_from(reads_from);
306 if (r_modification_order(curr,reads_from))
309 /* Read from future value */
310 value = curr->get_node()->get_future_value();
311 curr->read_from(NULL);
312 Promise *valuepromise = new Promise(curr, value);
313 promises->push_back(valuepromise);
315 } else if (curr->is_write()) {
316 if (w_modification_order(curr))
318 if (resolve_promises(curr))
323 resolve_release_sequences(curr->get_location());
325 th->set_return_value(value);
327 /* Add action to list. */
329 add_action_to_lists(curr);
331 Node *currnode = curr->get_node();
332 Node *parnode = currnode->get_parent();
334 if (!parnode->backtrack_empty() || !currnode->read_from_empty() ||
335 !currnode->future_value_empty() || !currnode->promise_empty())
336 if (!next_backtrack || *curr > *next_backtrack)
337 next_backtrack = curr;
339 set_backtracking(curr);
341 /* Do not split atomic actions. */
343 return thread_current();
345 return get_next_replay_thread();
348 /** @returns whether the current partial trace is feasible. */
349 bool ModelChecker::isfeasible() {
350 return !mo_graph->checkForCycles() && !failed_promise;
353 /** Returns whether the current completed trace is feasible. */
354 bool ModelChecker::isfinalfeasible() {
355 return isfeasible() && promises->size() == 0;
358 /** Close out a RMWR by converting previous RMWR into a RMW or READ. */
359 ModelAction * ModelChecker::process_rmw(ModelAction *act) {
360 int tid = id_to_int(act->get_tid());
361 ModelAction *lastread = get_last_action(tid);
362 lastread->process_rmw(act);
364 mo_graph->addRMWEdge(lastread->get_reads_from(), lastread);
369 * Updates the mo_graph with the constraints imposed from the current read.
370 * @param curr The current action. Must be a read.
371 * @param rf The action that curr reads from. Must be a write.
372 * @return True if modification order edges were added; false otherwise
374 bool ModelChecker::r_modification_order(ModelAction *curr, const ModelAction *rf)
376 std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(curr->get_location());
379 ASSERT(curr->is_read());
381 /* Iterate over all threads */
382 for (i = 0; i < thrd_lists->size(); i++) {
383 /* Iterate over actions in thread, starting from most recent */
384 action_list_t *list = &(*thrd_lists)[i];
385 action_list_t::reverse_iterator rit;
386 for (rit = list->rbegin(); rit != list->rend(); rit++) {
387 ModelAction *act = *rit;
389 /* Include at most one act per-thread that "happens before" curr */
390 if (act->happens_before(curr)) {
391 if (act->is_read()) {
392 const ModelAction *prevreadfrom = act->get_reads_from();
393 if (prevreadfrom != NULL && rf != prevreadfrom) {
394 mo_graph->addEdge(prevreadfrom, rf);
397 } else if (rf != act) {
398 mo_graph->addEdge(act, rf);
409 /** Updates the mo_graph with the constraints imposed from the current read. */
410 void ModelChecker::post_r_modification_order(ModelAction *curr, const ModelAction *rf)
412 std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(curr->get_location());
414 ASSERT(curr->is_read());
416 /* Iterate over all threads */
417 for (i = 0; i < thrd_lists->size(); i++) {
418 /* Iterate over actions in thread, starting from most recent */
419 action_list_t *list = &(*thrd_lists)[i];
420 action_list_t::reverse_iterator rit;
421 ModelAction *lastact = NULL;
423 /* Find last action that happens after curr */
424 for (rit = list->rbegin(); rit != list->rend(); rit++) {
425 ModelAction *act = *rit;
426 if (curr->happens_before(act)) {
432 /* Include at most one act per-thread that "happens before" curr */
433 if (lastact != NULL) {
434 if (lastact->is_read()) {
435 const ModelAction *postreadfrom = lastact->get_reads_from();
436 if (postreadfrom != NULL&&rf != postreadfrom)
437 mo_graph->addEdge(rf, postreadfrom);
438 } else if (rf != lastact) {
439 mo_graph->addEdge(rf, lastact);
447 * Updates the mo_graph with the constraints imposed from the current write.
448 * @param curr The current action. Must be a write.
449 * @return True if modification order edges were added; false otherwise
451 bool ModelChecker::w_modification_order(ModelAction *curr)
453 std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(curr->get_location());
456 ASSERT(curr->is_write());
458 if (curr->is_seqcst()) {
459 /* We have to at least see the last sequentially consistent write,
460 so we are initialized. */
461 ModelAction *last_seq_cst = get_last_seq_cst(curr->get_location());
462 if (last_seq_cst != NULL) {
463 mo_graph->addEdge(last_seq_cst, curr);
468 /* Iterate over all threads */
469 for (i = 0; i < thrd_lists->size(); i++) {
470 /* Iterate over actions in thread, starting from most recent */
471 action_list_t *list = &(*thrd_lists)[i];
472 action_list_t::reverse_iterator rit;
473 for (rit = list->rbegin(); rit != list->rend(); rit++) {
474 ModelAction *act = *rit;
476 /* Include at most one act per-thread that "happens before" curr */
477 if (act->happens_before(curr)) {
479 mo_graph->addEdge(act->get_reads_from(), curr);
481 mo_graph->addEdge(act, curr);
484 } else if (act->is_read() && !act->is_synchronizing(curr) &&
485 !act->same_thread(curr)) {
486 /* We have an action that:
487 (1) did not happen before us
488 (2) is a read and we are a write
489 (3) cannot synchronize with us
490 (4) is in a different thread
492 that read could potentially read from our write.
494 if (act->get_node()->add_future_value(curr->get_value()) &&
495 (!next_backtrack || *act > *next_backtrack))
496 next_backtrack = act;
505 * Finds the head(s) of the release sequence(s) containing a given ModelAction.
506 * The ModelAction under consideration is expected to be taking part in
507 * release/acquire synchronization as an object of the "reads from" relation.
508 * Note that this can only provide release sequence support for RMW chains
509 * which do not read from the future, as those actions cannot be traced until
510 * their "promise" is fulfilled. Similarly, we may not even establish the
511 * presence of a release sequence with certainty, as some modification order
512 * constraints may be decided further in the future. Thus, this function
513 * "returns" two pieces of data: a pass-by-reference vector of @a release_heads
514 * and a boolean representing certainty.
516 * @todo Finish lazy updating, when promises are fulfilled in the future
517 * @param rf The action that might be part of a release sequence. Must be a
519 * @param release_heads A pass-by-reference style return parameter. After
520 * execution of this function, release_heads will contain the heads of all the
521 * relevant release sequences, if any exists
522 * @return true, if the ModelChecker is certain that release_heads is complete;
525 bool ModelChecker::release_seq_head(const ModelAction *rf,
526 std::vector<const ModelAction *> *release_heads) const
528 ASSERT(rf->is_write());
530 /* read from future: need to settle this later */
531 return false; /* incomplete */
533 if (rf->is_release())
534 release_heads->push_back(rf);
536 if (rf->is_acquire())
537 return true; /* complete */
538 return release_seq_head(rf->get_reads_from(), release_heads);
540 if (rf->is_release())
541 return true; /* complete */
543 /* else relaxed write; check modification order for contiguous subsequence
544 * -> rf must be same thread as release */
545 int tid = id_to_int(rf->get_tid());
546 std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(rf->get_location());
547 action_list_t *list = &(*thrd_lists)[tid];
548 action_list_t::const_reverse_iterator rit;
550 /* Find rf in the thread list */
551 for (rit = list->rbegin(); rit != list->rend(); rit++)
555 /* Find the last write/release */
556 for (; rit != list->rend(); rit++)
557 if ((*rit)->is_release())
559 if (rit == list->rend()) {
560 /* No write-release in this thread */
561 return true; /* complete */
563 ModelAction *release = *rit;
565 ASSERT(rf->same_thread(release));
568 for (unsigned int i = 0; i < thrd_lists->size(); i++) {
569 if (id_to_int(rf->get_tid()) == (int)i)
571 list = &(*thrd_lists)[i];
572 for (rit = list->rbegin(); rit != list->rend(); rit++) {
573 const ModelAction *act = *rit;
574 if (!act->is_write())
576 /* Reach synchronization -> this thread is complete */
577 if (act->happens_before(release))
579 if (rf->happens_before(act))
582 /* Check modification order */
583 if (mo_graph->checkReachable(rf, act))
586 if (mo_graph->checkReachable(act, release))
587 /* act --mo--> release */
589 if (mo_graph->checkReachable(release, act) &&
590 mo_graph->checkReachable(act, rf)) {
591 /* release --mo-> act --mo--> rf */
592 return true; /* complete */
599 release_heads->push_back(release);
604 * A public interface for getting the release sequence head(s) with which a
605 * given ModelAction must synchronize. This function only returns a non-empty
606 * result when it can locate a release sequence head with certainty. Otherwise,
607 * it may mark the internal state of the ModelChecker so that it will handle
608 * the release sequence at a later time, causing @a act to update its
609 * synchronization at some later point in execution.
610 * @param act The 'acquire' action that may read from a release sequence
611 * @param release_heads A pass-by-reference return parameter. Will be filled
612 * with the head(s) of the release sequence(s), if they exists with certainty.
613 * @see ModelChecker::release_seq_head
615 void ModelChecker::get_release_seq_heads(ModelAction *act,
616 std::vector<const ModelAction *> *release_heads)
618 const ModelAction *rf = act->get_reads_from();
620 complete = release_seq_head(rf, release_heads);
622 /* add act to 'lazy checking' list */
623 std::list<ModelAction *> *list;
624 list = lazy_sync_with_release->get_safe_ptr(act->get_location());
625 list->push_back(act);
630 * Attempt to resolve all stashed operations that might synchronize with a
631 * release sequence for a given location. This implements the "lazy" portion of
632 * determining whether or not a release sequence was contiguous, since not all
633 * modification order information is present at the time an action occurs.
635 * @param location The location/object that should be checked for release
636 * sequence resolutions
637 * @return True if any updates occurred (new synchronization, new mo_graph edges)
639 bool ModelChecker::resolve_release_sequences(void *location)
641 std::list<ModelAction *> *list;
642 list = lazy_sync_with_release->getptr(location);
646 bool updated = false;
647 std::list<ModelAction *>::iterator it = list->begin();
648 while (it != list->end()) {
649 ModelAction *act = *it;
650 const ModelAction *rf = act->get_reads_from();
651 std::vector<const ModelAction *> release_heads;
653 complete = release_seq_head(rf, &release_heads);
654 for (unsigned int i = 0; i < release_heads.size(); i++) {
655 if (!act->has_synchronized_with(release_heads[i])) {
657 act->synchronize_with(release_heads[i]);
662 /* propagate synchronization to later actions */
663 action_list_t::reverse_iterator it = action_trace->rbegin();
664 while ((*it) != act) {
665 ModelAction *propagate = *it;
666 if (act->happens_before(propagate))
667 /** @todo new mo_graph edges along with
668 * this synchronization? */
669 propagate->synchronize_with(act);
673 it = list->erase(it);
682 * Performs various bookkeeping operations for the current ModelAction. For
683 * instance, adds action to the per-object, per-thread action vector and to the
684 * action trace list of all thread actions.
686 * @param act is the ModelAction to add.
688 void ModelChecker::add_action_to_lists(ModelAction *act)
690 int tid = id_to_int(act->get_tid());
691 action_trace->push_back(act);
693 obj_map->get_safe_ptr(act->get_location())->push_back(act);
695 std::vector<action_list_t> *vec = obj_thrd_map->get_safe_ptr(act->get_location());
696 if (tid >= (int)vec->size())
697 vec->resize(next_thread_id);
698 (*vec)[tid].push_back(act);
700 if ((int)thrd_last_action->size() <= tid)
701 thrd_last_action->resize(get_num_threads());
702 (*thrd_last_action)[tid] = act;
705 ModelAction * ModelChecker::get_last_action(thread_id_t tid)
707 int nthreads = get_num_threads();
708 if ((int)thrd_last_action->size() < nthreads)
709 thrd_last_action->resize(nthreads);
710 return (*thrd_last_action)[id_to_int(tid)];
714 * Gets the last memory_order_seq_cst action (in the total global sequence)
715 * performed on a particular object (i.e., memory location).
716 * @param location The object location to check
717 * @return The last seq_cst action performed
719 ModelAction * ModelChecker::get_last_seq_cst(const void *location)
721 action_list_t *list = obj_map->get_safe_ptr(location);
722 /* Find: max({i in dom(S) | seq_cst(t_i) && isWrite(t_i) && samevar(t_i, t)}) */
723 action_list_t::reverse_iterator rit;
724 for (rit = list->rbegin(); rit != list->rend(); rit++)
725 if ((*rit)->is_write() && (*rit)->is_seqcst())
730 ModelAction * ModelChecker::get_parent_action(thread_id_t tid)
732 ModelAction *parent = get_last_action(tid);
734 parent = get_thread(tid)->get_creation();
739 * Returns the clock vector for a given thread.
740 * @param tid The thread whose clock vector we want
741 * @return Desired clock vector
743 ClockVector * ModelChecker::get_cv(thread_id_t tid)
745 return get_parent_action(tid)->get_cv();
749 * Resolve a set of Promises with a current write. The set is provided in the
750 * Node corresponding to @a write.
751 * @param write The ModelAction that is fulfilling Promises
752 * @return True if promises were resolved; false otherwise
754 bool ModelChecker::resolve_promises(ModelAction *write)
756 bool resolved = false;
757 for (unsigned int i = 0, promise_index = 0; promise_index < promises->size(); i++) {
758 Promise *promise = (*promises)[promise_index];
759 if (write->get_node()->get_promise(i)) {
760 ModelAction *read = promise->get_action();
761 read->read_from(write);
762 r_modification_order(read, write);
763 post_r_modification_order(read, write);
764 promises->erase(promises->begin() + promise_index);
773 * Compute the set of promises that could potentially be satisfied by this
774 * action. Note that the set computation actually appears in the Node, not in
776 * @param curr The ModelAction that may satisfy promises
778 void ModelChecker::compute_promises(ModelAction *curr)
780 for (unsigned int i = 0; i < promises->size(); i++) {
781 Promise *promise = (*promises)[i];
782 const ModelAction *act = promise->get_action();
783 if (!act->happens_before(curr) &&
785 !act->is_synchronizing(curr) &&
786 !act->same_thread(curr) &&
787 promise->get_value() == curr->get_value()) {
788 curr->get_node()->set_promise(i);
793 /** Checks promises in response to change in ClockVector Threads. */
794 void ModelChecker::check_promises(ClockVector *old_cv, ClockVector *merge_cv)
796 for (unsigned int i = 0; i < promises->size(); i++) {
797 Promise *promise = (*promises)[i];
798 const ModelAction *act = promise->get_action();
799 if ((old_cv == NULL || !old_cv->synchronized_since(act)) &&
800 merge_cv->synchronized_since(act)) {
801 //This thread is no longer able to send values back to satisfy the promise
802 int num_synchronized_threads = promise->increment_threads();
803 if (num_synchronized_threads == model->get_num_threads()) {
805 failed_promise = true;
813 * Build up an initial set of all past writes that this 'read' action may read
814 * from. This set is determined by the clock vector's "happens before"
816 * @param curr is the current ModelAction that we are exploring; it must be a
819 void ModelChecker::build_reads_from_past(ModelAction *curr)
821 std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(curr->get_location());
823 ASSERT(curr->is_read());
825 ModelAction *last_seq_cst = NULL;
827 /* Track whether this object has been initialized */
828 bool initialized = false;
830 if (curr->is_seqcst()) {
831 last_seq_cst = get_last_seq_cst(curr->get_location());
832 /* We have to at least see the last sequentially consistent write,
833 so we are initialized. */
834 if (last_seq_cst != NULL)
838 /* Iterate over all threads */
839 for (i = 0; i < thrd_lists->size(); i++) {
840 /* Iterate over actions in thread, starting from most recent */
841 action_list_t *list = &(*thrd_lists)[i];
842 action_list_t::reverse_iterator rit;
843 for (rit = list->rbegin(); rit != list->rend(); rit++) {
844 ModelAction *act = *rit;
846 /* Only consider 'write' actions */
847 if (!act->is_write())
850 /* Don't consider more than one seq_cst write if we are a seq_cst read. */
851 if (!act->is_seqcst() || !curr->is_seqcst() || act == last_seq_cst) {
852 DEBUG("Adding action to may_read_from:\n");
857 curr->get_node()->add_read_from(act);
860 /* Include at most one act per-thread that "happens before" curr */
861 if (act->happens_before(curr)) {
869 /** @todo Need a more informative way of reporting errors. */
870 printf("ERROR: may read from uninitialized atomic\n");
873 if (DBG_ENABLED() || !initialized) {
874 printf("Reached read action:\n");
876 printf("Printing may_read_from\n");
877 curr->get_node()->print_may_read_from();
878 printf("End printing may_read_from\n");
884 static void print_list(action_list_t *list)
886 action_list_t::iterator it;
888 printf("---------------------------------------------------------------------\n");
891 for (it = list->begin(); it != list->end(); it++) {
894 printf("---------------------------------------------------------------------\n");
897 void ModelChecker::print_summary()
900 printf("Number of executions: %d\n", num_executions);
901 printf("Total nodes created: %d\n", node_stack->get_total_nodes());
905 if (!isfinalfeasible())
906 printf("INFEASIBLE EXECUTION!\n");
907 print_list(action_trace);
912 * Add a Thread to the system for the first time. Should only be called once
914 * @param t The Thread to add
916 void ModelChecker::add_thread(Thread *t)
918 thread_map->put(id_to_int(t->get_id()), t);
919 scheduler->add_thread(t);
922 void ModelChecker::remove_thread(Thread *t)
924 scheduler->remove_thread(t);
928 * Switch from a user-context to the "master thread" context (a.k.a. system
929 * context). This switch is made with the intention of exploring a particular
930 * model-checking action (described by a ModelAction object). Must be called
931 * from a user-thread context.
932 * @param act The current action that will be explored. May be NULL, although
933 * there is little reason to switch to the model-checker without an action to
934 * explore (note: act == NULL is sometimes used as a hack to allow a thread to
935 * yield control without performing any progress; see thrd_join()).
936 * @return Return status from the 'swap' call (i.e., success/fail, 0/-1)
938 int ModelChecker::switch_to_master(ModelAction *act)
941 Thread *old = thread_current();
942 set_current_action(act);
943 old->set_state(THREAD_READY);
944 return Thread::swap(old, &system_context);
948 * Takes the next step in the execution, if possible.
949 * @return Returns true (success) if a step was taken and false otherwise.
951 bool ModelChecker::take_step() {
954 curr = thread_current();
956 if (curr->get_state() == THREAD_READY) {
957 if (current_action) {
958 nextThread = check_current_action(current_action);
959 current_action = NULL;
961 scheduler->add_thread(curr);
962 } else if (curr->get_state() == THREAD_RUNNING) {
963 /* Stopped while running; i.e., completed */
969 next = scheduler->next_thread(nextThread);
971 /* Infeasible -> don't take any more steps */
976 next->set_state(THREAD_RUNNING);
977 DEBUG("(%d, %d)\n", curr ? curr->get_id() : -1, next ? next->get_id() : -1);
979 /* next == NULL -> don't take any more steps */
982 /* Return false only if swap fails with an error */
983 return (Thread::swap(&system_context, next) == 0);
986 /** Runs the current execution until threre are no more steps to take. */
987 void ModelChecker::finish_execution() {