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()),
26 action_trace(new action_list_t()),
27 thread_map(new HashTable<int, Thread *, int>()),
28 obj_map(new HashTable<const void *, action_list_t, uintptr_t, 4>()),
29 obj_thrd_map(new HashTable<void *, std::vector<action_list_t>, uintptr_t, 4 >()),
30 promises(new std::vector<Promise *>()),
31 lazy_sync_with_release(new HashTable<void *, std::list<ModelAction *>, uintptr_t, 4>()),
32 thrd_last_action(new std::vector<ModelAction *>(1)),
33 node_stack(new NodeStack()),
34 mo_graph(new CycleGraph()),
35 failed_promise(false),
38 /* Allocate this "size" on the snapshotting heap */
39 priv = (struct model_snapshot_members *)calloc(1, sizeof(*priv));
40 /* First thread created will have id INITIAL_THREAD_ID */
41 priv->next_thread_id = INITIAL_THREAD_ID;
43 lazy_sync_size = &priv->lazy_sync_size;
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 failed_promise = false;
79 snapshotObject->backTrackBeforeStep(0);
82 /** @returns a thread ID for a new Thread */
83 thread_id_t ModelChecker::get_next_id()
85 return priv->next_thread_id++;
88 /** @returns the number of user threads created during this execution */
89 int ModelChecker::get_num_threads()
91 return priv->next_thread_id;
94 /** @returns a sequence number for a new ModelAction */
95 modelclock_t ModelChecker::get_next_seq_num()
97 return ++priv->used_sequence_numbers;
101 * Choose the next thread in the replay sequence.
103 * If the replay sequence has reached the 'diverge' point, returns a thread
104 * from the backtracking set. Otherwise, simply returns the next thread in the
105 * sequence that is being replayed.
107 Thread * ModelChecker::get_next_replay_thread()
111 /* Have we completed exploring the preselected path? */
115 /* Else, we are trying to replay an execution */
116 ModelAction *next = node_stack->get_next()->get_action();
118 if (next == diverge) {
119 Node *nextnode = next->get_node();
120 /* Reached divergence point */
121 if (nextnode->increment_promise()) {
122 /* The next node will try to satisfy a different set of promises. */
123 tid = next->get_tid();
124 node_stack->pop_restofstack(2);
125 } else if (nextnode->increment_read_from()) {
126 /* The next node will read from a different value. */
127 tid = next->get_tid();
128 node_stack->pop_restofstack(2);
129 } else if (nextnode->increment_future_value()) {
130 /* The next node will try to read from a different future value. */
131 tid = next->get_tid();
132 node_stack->pop_restofstack(2);
134 /* Make a different thread execute for next step */
135 Node *node = nextnode->get_parent();
136 tid = node->get_next_backtrack();
137 node_stack->pop_restofstack(1);
139 DEBUG("*** Divergence point ***\n");
142 tid = next->get_tid();
144 DEBUG("*** ModelChecker chose next thread = %d ***\n", tid);
145 ASSERT(tid != THREAD_ID_T_NONE);
146 return thread_map->get(id_to_int(tid));
150 * Queries the model-checker for more executions to explore and, if one
151 * exists, resets the model-checker state to execute a new execution.
153 * @return If there are more executions to explore, return true. Otherwise,
156 bool ModelChecker::next_execution()
162 if (isfinalfeasible() || DBG_ENABLED())
165 if ((diverge = get_next_backtrack()) == NULL)
169 printf("Next execution will diverge at:\n");
173 reset_to_initial_state();
177 ModelAction * ModelChecker::get_last_conflict(ModelAction *act)
179 action_type type = act->get_type();
189 /* linear search: from most recent to oldest */
190 action_list_t *list = obj_map->get_safe_ptr(act->get_location());
191 action_list_t::reverse_iterator rit;
192 for (rit = list->rbegin(); rit != list->rend(); rit++) {
193 ModelAction *prev = *rit;
194 if (act->is_synchronizing(prev))
200 void ModelChecker::set_backtracking(ModelAction *act)
204 Thread *t = get_thread(act->get_tid());
206 prev = get_last_conflict(act);
210 node = prev->get_node()->get_parent();
212 while (!node->is_enabled(t))
215 /* Check if this has been explored already */
216 if (node->has_been_explored(t->get_id()))
219 /* Cache the latest backtracking point */
220 if (!priv->next_backtrack || *prev > *priv->next_backtrack)
221 priv->next_backtrack = prev;
223 /* If this is a new backtracking point, mark the tree */
224 if (!node->set_backtrack(t->get_id()))
226 DEBUG("Setting backtrack: conflict = %d, instead tid = %d\n",
227 prev->get_tid(), t->get_id());
235 * Returns last backtracking point. The model checker will explore a different
236 * path for this point in the next execution.
237 * @return The ModelAction at which the next execution should diverge.
239 ModelAction * ModelChecker::get_next_backtrack()
241 ModelAction *next = priv->next_backtrack;
242 priv->next_backtrack = NULL;
247 * This is the heart of the model checker routine. It performs model-checking
248 * actions corresponding to a given "current action." Among other processes, it
249 * calculates reads-from relationships, updates synchronization clock vectors,
250 * forms a memory_order constraints graph, and handles replay/backtrack
251 * execution when running permutations of previously-observed executions.
253 * @param curr The current action to process
254 * @return The next Thread that must be executed. May be NULL if ModelChecker
255 * makes no choice (e.g., according to replay execution, combining RMW actions,
258 Thread * ModelChecker::check_current_action(ModelAction *curr)
260 bool already_added = false;
264 if (curr->is_rmwc() || curr->is_rmw()) {
265 ModelAction *tmp = process_rmw(curr);
266 already_added = true;
270 ModelAction *tmp = node_stack->explore_action(curr);
272 /* Discard duplicate ModelAction; use action from NodeStack */
273 /* First restore type and order in case of RMW operation */
275 tmp->copy_typeandorder(curr);
277 /* If we have diverged, we need to reset the clock vector. */
279 tmp->create_cv(get_parent_action(tmp->get_tid()));
285 * Perform one-time actions when pushing new ModelAction onto
288 curr->create_cv(get_parent_action(curr->get_tid()));
289 /* Build may_read_from set */
291 build_reads_from_past(curr);
292 if (curr->is_write())
293 compute_promises(curr);
297 /* Assign 'creation' parent */
298 if (curr->get_type() == THREAD_CREATE) {
299 Thread *th = (Thread *)curr->get_location();
300 th->set_creation(curr);
301 } else if (curr->get_type() == THREAD_JOIN) {
303 wait = get_thread(curr->get_tid());
304 join = (Thread *)curr->get_location();
305 if (!join->is_complete())
306 scheduler->wait(wait, join);
307 } else if (curr->get_type() == THREAD_FINISH) {
308 Thread *th = get_thread(curr->get_tid());
309 while (!th->wait_list_empty()) {
310 Thread *wake = th->pop_wait_list();
311 scheduler->wake(wake);
316 /* Deal with new thread */
317 if (curr->get_type() == THREAD_START)
318 check_promises(NULL, curr->get_cv());
320 /* Assign reads_from values */
321 Thread *th = get_thread(curr->get_tid());
322 uint64_t value = VALUE_NONE;
323 bool updated = false;
324 if (curr->is_read()) {
325 const ModelAction *reads_from = curr->get_node()->get_read_from();
326 if (reads_from != NULL) {
327 value = reads_from->get_value();
328 /* Assign reads_from, perform release/acquire synchronization */
329 curr->read_from(reads_from);
330 if (r_modification_order(curr,reads_from))
333 /* Read from future value */
334 value = curr->get_node()->get_future_value();
335 curr->read_from(NULL);
336 Promise *valuepromise = new Promise(curr, value);
337 promises->push_back(valuepromise);
339 } else if (curr->is_write()) {
340 if (w_modification_order(curr))
342 if (resolve_promises(curr))
347 resolve_release_sequences(curr->get_location());
349 th->set_return_value(value);
351 /* Add action to list. */
353 add_action_to_lists(curr);
355 Node *currnode = curr->get_node();
356 Node *parnode = currnode->get_parent();
358 if (!parnode->backtrack_empty() || !currnode->read_from_empty() ||
359 !currnode->future_value_empty() || !currnode->promise_empty())
360 if (!priv->next_backtrack || *curr > *priv->next_backtrack)
361 priv->next_backtrack = curr;
363 set_backtracking(curr);
365 /* Do not split atomic actions. */
367 return thread_current();
368 /* The THREAD_CREATE action points to the created Thread */
369 else if (curr->get_type() == THREAD_CREATE)
370 return (Thread *)curr->get_location();
372 return get_next_replay_thread();
375 /** @returns whether the current partial trace must be a prefix of a
377 bool ModelChecker::isfeasibleprefix() {
378 return promises->size() == 0 && *lazy_sync_size == 0;
381 /** @returns whether the current partial trace is feasible. */
382 bool ModelChecker::isfeasible() {
383 return !mo_graph->checkForCycles() && !failed_promise;
386 /** Returns whether the current completed trace is feasible. */
387 bool ModelChecker::isfinalfeasible() {
388 return isfeasible() && promises->size() == 0;
391 /** Close out a RMWR by converting previous RMWR into a RMW or READ. */
392 ModelAction * ModelChecker::process_rmw(ModelAction *act) {
393 int tid = id_to_int(act->get_tid());
394 ModelAction *lastread = get_last_action(tid);
395 lastread->process_rmw(act);
397 mo_graph->addRMWEdge(lastread->get_reads_from(), lastread);
402 * Updates the mo_graph with the constraints imposed from the current read.
403 * @param curr The current action. Must be a read.
404 * @param rf The action that curr reads from. Must be a write.
405 * @return True if modification order edges were added; false otherwise
407 bool ModelChecker::r_modification_order(ModelAction *curr, const ModelAction *rf)
409 std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(curr->get_location());
412 ASSERT(curr->is_read());
414 /* Iterate over all threads */
415 for (i = 0; i < thrd_lists->size(); i++) {
416 /* Iterate over actions in thread, starting from most recent */
417 action_list_t *list = &(*thrd_lists)[i];
418 action_list_t::reverse_iterator rit;
419 for (rit = list->rbegin(); rit != list->rend(); rit++) {
420 ModelAction *act = *rit;
422 /* Include at most one act per-thread that "happens before" curr */
423 if (act->happens_before(curr)) {
424 if (act->is_read()) {
425 const ModelAction *prevreadfrom = act->get_reads_from();
426 if (prevreadfrom != NULL && rf != prevreadfrom) {
427 mo_graph->addEdge(prevreadfrom, rf);
430 } else if (rf != act) {
431 mo_graph->addEdge(act, rf);
442 /** Updates the mo_graph with the constraints imposed from the current read. */
443 void ModelChecker::post_r_modification_order(ModelAction *curr, const ModelAction *rf)
445 std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(curr->get_location());
447 ASSERT(curr->is_read());
449 /* Iterate over all threads */
450 for (i = 0; i < thrd_lists->size(); i++) {
451 /* Iterate over actions in thread, starting from most recent */
452 action_list_t *list = &(*thrd_lists)[i];
453 action_list_t::reverse_iterator rit;
454 ModelAction *lastact = NULL;
456 /* Find last action that happens after curr */
457 for (rit = list->rbegin(); rit != list->rend(); rit++) {
458 ModelAction *act = *rit;
459 if (curr->happens_before(act)) {
465 /* Include at most one act per-thread that "happens before" curr */
466 if (lastact != NULL) {
467 if (lastact->is_read()) {
468 const ModelAction *postreadfrom = lastact->get_reads_from();
469 if (postreadfrom != NULL&&rf != postreadfrom)
470 mo_graph->addEdge(rf, postreadfrom);
471 } else if (rf != lastact) {
472 mo_graph->addEdge(rf, lastact);
480 * Updates the mo_graph with the constraints imposed from the current write.
481 * @param curr The current action. Must be a write.
482 * @return True if modification order edges were added; false otherwise
484 bool ModelChecker::w_modification_order(ModelAction *curr)
486 std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(curr->get_location());
489 ASSERT(curr->is_write());
491 if (curr->is_seqcst()) {
492 /* We have to at least see the last sequentially consistent write,
493 so we are initialized. */
494 ModelAction *last_seq_cst = get_last_seq_cst(curr->get_location());
495 if (last_seq_cst != NULL) {
496 mo_graph->addEdge(last_seq_cst, curr);
501 /* Iterate over all threads */
502 for (i = 0; i < thrd_lists->size(); i++) {
503 /* Iterate over actions in thread, starting from most recent */
504 action_list_t *list = &(*thrd_lists)[i];
505 action_list_t::reverse_iterator rit;
506 for (rit = list->rbegin(); rit != list->rend(); rit++) {
507 ModelAction *act = *rit;
509 /* Include at most one act per-thread that "happens before" curr */
510 if (act->happens_before(curr)) {
512 mo_graph->addEdge(act->get_reads_from(), curr);
514 mo_graph->addEdge(act, curr);
517 } else if (act->is_read() && !act->is_synchronizing(curr) &&
518 !act->same_thread(curr)) {
519 /* We have an action that:
520 (1) did not happen before us
521 (2) is a read and we are a write
522 (3) cannot synchronize with us
523 (4) is in a different thread
525 that read could potentially read from our write.
527 if (act->get_node()->add_future_value(curr->get_value()) &&
528 (!priv->next_backtrack || *act > *priv->next_backtrack))
529 priv->next_backtrack = act;
538 * Finds the head(s) of the release sequence(s) containing a given ModelAction.
539 * The ModelAction under consideration is expected to be taking part in
540 * release/acquire synchronization as an object of the "reads from" relation.
541 * Note that this can only provide release sequence support for RMW chains
542 * which do not read from the future, as those actions cannot be traced until
543 * their "promise" is fulfilled. Similarly, we may not even establish the
544 * presence of a release sequence with certainty, as some modification order
545 * constraints may be decided further in the future. Thus, this function
546 * "returns" two pieces of data: a pass-by-reference vector of @a release_heads
547 * and a boolean representing certainty.
549 * @todo Finish lazy updating, when promises are fulfilled in the future
550 * @param rf The action that might be part of a release sequence. Must be a
552 * @param release_heads A pass-by-reference style return parameter. After
553 * execution of this function, release_heads will contain the heads of all the
554 * relevant release sequences, if any exists
555 * @return true, if the ModelChecker is certain that release_heads is complete;
558 bool ModelChecker::release_seq_head(const ModelAction *rf,
559 std::vector<const ModelAction *> *release_heads) const
561 ASSERT(rf->is_write());
563 /* read from future: need to settle this later */
564 return false; /* incomplete */
566 if (rf->is_release())
567 release_heads->push_back(rf);
569 if (rf->is_acquire())
570 return true; /* complete */
571 return release_seq_head(rf->get_reads_from(), release_heads);
573 if (rf->is_release())
574 return true; /* complete */
576 /* else relaxed write; check modification order for contiguous subsequence
577 * -> rf must be same thread as release */
578 int tid = id_to_int(rf->get_tid());
579 std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(rf->get_location());
580 action_list_t *list = &(*thrd_lists)[tid];
581 action_list_t::const_reverse_iterator rit;
583 /* Find rf in the thread list */
584 rit = std::find(list->rbegin(), list->rend(), rf);
585 ASSERT(rit != list->rend());
587 /* Find the last write/release */
588 for (; rit != list->rend(); rit++)
589 if ((*rit)->is_release())
591 if (rit == list->rend()) {
592 /* No write-release in this thread */
593 return true; /* complete */
595 ModelAction *release = *rit;
597 ASSERT(rf->same_thread(release));
600 for (unsigned int i = 0; i < thrd_lists->size(); i++) {
601 if (id_to_int(rf->get_tid()) == (int)i)
603 list = &(*thrd_lists)[i];
605 /* Can we ensure no future writes from this thread may break
606 * the release seq? */
607 bool future_ordered = false;
609 for (rit = list->rbegin(); rit != list->rend(); rit++) {
610 const ModelAction *act = *rit;
611 if (!act->is_write())
613 /* Reach synchronization -> this thread is complete */
614 if (act->happens_before(release))
616 if (rf->happens_before(act)) {
617 future_ordered = true;
621 /* Check modification order */
622 if (mo_graph->checkReachable(rf, act)) {
624 future_ordered = true;
627 if (mo_graph->checkReachable(act, release))
628 /* act --mo--> release */
630 if (mo_graph->checkReachable(release, act) &&
631 mo_graph->checkReachable(act, rf)) {
632 /* release --mo-> act --mo--> rf */
633 return true; /* complete */
638 return false; /* This thread is uncertain */
642 release_heads->push_back(release);
647 * A public interface for getting the release sequence head(s) with which a
648 * given ModelAction must synchronize. This function only returns a non-empty
649 * result when it can locate a release sequence head with certainty. Otherwise,
650 * it may mark the internal state of the ModelChecker so that it will handle
651 * the release sequence at a later time, causing @a act to update its
652 * synchronization at some later point in execution.
653 * @param act The 'acquire' action that may read from a release sequence
654 * @param release_heads A pass-by-reference return parameter. Will be filled
655 * with the head(s) of the release sequence(s), if they exists with certainty.
656 * @see ModelChecker::release_seq_head
658 void ModelChecker::get_release_seq_heads(ModelAction *act,
659 std::vector<const ModelAction *> *release_heads)
661 const ModelAction *rf = act->get_reads_from();
663 complete = release_seq_head(rf, release_heads);
665 /* add act to 'lazy checking' list */
666 std::list<ModelAction *> *list;
667 list = lazy_sync_with_release->get_safe_ptr(act->get_location());
668 list->push_back(act);
674 * Attempt to resolve all stashed operations that might synchronize with a
675 * release sequence for a given location. This implements the "lazy" portion of
676 * determining whether or not a release sequence was contiguous, since not all
677 * modification order information is present at the time an action occurs.
679 * @param location The location/object that should be checked for release
680 * sequence resolutions
681 * @return True if any updates occurred (new synchronization, new mo_graph edges)
683 bool ModelChecker::resolve_release_sequences(void *location)
685 std::list<ModelAction *> *list;
686 list = lazy_sync_with_release->getptr(location);
690 bool updated = false;
691 std::list<ModelAction *>::iterator it = list->begin();
692 while (it != list->end()) {
693 ModelAction *act = *it;
694 const ModelAction *rf = act->get_reads_from();
695 std::vector<const ModelAction *> release_heads;
697 complete = release_seq_head(rf, &release_heads);
698 for (unsigned int i = 0; i < release_heads.size(); i++) {
699 if (!act->has_synchronized_with(release_heads[i])) {
701 act->synchronize_with(release_heads[i]);
706 /* propagate synchronization to later actions */
707 action_list_t::reverse_iterator it = action_trace->rbegin();
708 while ((*it) != act) {
709 ModelAction *propagate = *it;
710 if (act->happens_before(propagate))
711 /** @todo new mo_graph edges along with
712 * this synchronization? */
713 propagate->synchronize_with(act);
717 it = list->erase(it);
723 // If we resolved promises or data races, see if we have realized a data race.
724 if (checkDataRaces()) {
732 * Performs various bookkeeping operations for the current ModelAction. For
733 * instance, adds action to the per-object, per-thread action vector and to the
734 * action trace list of all thread actions.
736 * @param act is the ModelAction to add.
738 void ModelChecker::add_action_to_lists(ModelAction *act)
740 int tid = id_to_int(act->get_tid());
741 action_trace->push_back(act);
743 obj_map->get_safe_ptr(act->get_location())->push_back(act);
745 std::vector<action_list_t> *vec = obj_thrd_map->get_safe_ptr(act->get_location());
746 if (tid >= (int)vec->size())
747 vec->resize(priv->next_thread_id);
748 (*vec)[tid].push_back(act);
750 if ((int)thrd_last_action->size() <= tid)
751 thrd_last_action->resize(get_num_threads());
752 (*thrd_last_action)[tid] = act;
755 ModelAction * ModelChecker::get_last_action(thread_id_t tid)
757 int nthreads = get_num_threads();
758 if ((int)thrd_last_action->size() < nthreads)
759 thrd_last_action->resize(nthreads);
760 return (*thrd_last_action)[id_to_int(tid)];
764 * Gets the last memory_order_seq_cst action (in the total global sequence)
765 * performed on a particular object (i.e., memory location).
766 * @param location The object location to check
767 * @return The last seq_cst action performed
769 ModelAction * ModelChecker::get_last_seq_cst(const void *location)
771 action_list_t *list = obj_map->get_safe_ptr(location);
772 /* Find: max({i in dom(S) | seq_cst(t_i) && isWrite(t_i) && samevar(t_i, t)}) */
773 action_list_t::reverse_iterator rit;
774 for (rit = list->rbegin(); rit != list->rend(); rit++)
775 if ((*rit)->is_write() && (*rit)->is_seqcst())
780 ModelAction * ModelChecker::get_parent_action(thread_id_t tid)
782 ModelAction *parent = get_last_action(tid);
784 parent = get_thread(tid)->get_creation();
789 * Returns the clock vector for a given thread.
790 * @param tid The thread whose clock vector we want
791 * @return Desired clock vector
793 ClockVector * ModelChecker::get_cv(thread_id_t tid)
795 return get_parent_action(tid)->get_cv();
799 * Resolve a set of Promises with a current write. The set is provided in the
800 * Node corresponding to @a write.
801 * @param write The ModelAction that is fulfilling Promises
802 * @return True if promises were resolved; false otherwise
804 bool ModelChecker::resolve_promises(ModelAction *write)
806 bool resolved = false;
807 for (unsigned int i = 0, promise_index = 0; promise_index < promises->size(); i++) {
808 Promise *promise = (*promises)[promise_index];
809 if (write->get_node()->get_promise(i)) {
810 ModelAction *read = promise->get_action();
811 read->read_from(write);
812 r_modification_order(read, write);
813 post_r_modification_order(read, write);
814 promises->erase(promises->begin() + promise_index);
824 * Compute the set of promises that could potentially be satisfied by this
825 * action. Note that the set computation actually appears in the Node, not in
827 * @param curr The ModelAction that may satisfy promises
829 void ModelChecker::compute_promises(ModelAction *curr)
831 for (unsigned int i = 0; i < promises->size(); i++) {
832 Promise *promise = (*promises)[i];
833 const ModelAction *act = promise->get_action();
834 if (!act->happens_before(curr) &&
836 !act->is_synchronizing(curr) &&
837 !act->same_thread(curr) &&
838 promise->get_value() == curr->get_value()) {
839 curr->get_node()->set_promise(i);
844 /** Checks promises in response to change in ClockVector Threads. */
845 void ModelChecker::check_promises(ClockVector *old_cv, ClockVector *merge_cv)
847 for (unsigned int i = 0; i < promises->size(); i++) {
848 Promise *promise = (*promises)[i];
849 const ModelAction *act = promise->get_action();
850 if ((old_cv == NULL || !old_cv->synchronized_since(act)) &&
851 merge_cv->synchronized_since(act)) {
852 //This thread is no longer able to send values back to satisfy the promise
853 int num_synchronized_threads = promise->increment_threads();
854 if (num_synchronized_threads == get_num_threads()) {
856 failed_promise = true;
864 * Build up an initial set of all past writes that this 'read' action may read
865 * from. This set is determined by the clock vector's "happens before"
867 * @param curr is the current ModelAction that we are exploring; it must be a
870 void ModelChecker::build_reads_from_past(ModelAction *curr)
872 std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(curr->get_location());
874 ASSERT(curr->is_read());
876 ModelAction *last_seq_cst = NULL;
878 /* Track whether this object has been initialized */
879 bool initialized = false;
881 if (curr->is_seqcst()) {
882 last_seq_cst = get_last_seq_cst(curr->get_location());
883 /* We have to at least see the last sequentially consistent write,
884 so we are initialized. */
885 if (last_seq_cst != NULL)
889 /* Iterate over all threads */
890 for (i = 0; i < thrd_lists->size(); i++) {
891 /* Iterate over actions in thread, starting from most recent */
892 action_list_t *list = &(*thrd_lists)[i];
893 action_list_t::reverse_iterator rit;
894 for (rit = list->rbegin(); rit != list->rend(); rit++) {
895 ModelAction *act = *rit;
897 /* Only consider 'write' actions */
898 if (!act->is_write())
901 /* Don't consider more than one seq_cst write if we are a seq_cst read. */
902 if (!act->is_seqcst() || !curr->is_seqcst() || act == last_seq_cst) {
903 DEBUG("Adding action to may_read_from:\n");
908 curr->get_node()->add_read_from(act);
911 /* Include at most one act per-thread that "happens before" curr */
912 if (act->happens_before(curr)) {
920 /** @todo Need a more informative way of reporting errors. */
921 printf("ERROR: may read from uninitialized atomic\n");
924 if (DBG_ENABLED() || !initialized) {
925 printf("Reached read action:\n");
927 printf("Printing may_read_from\n");
928 curr->get_node()->print_may_read_from();
929 printf("End printing may_read_from\n");
935 static void print_list(action_list_t *list)
937 action_list_t::iterator it;
939 printf("---------------------------------------------------------------------\n");
942 for (it = list->begin(); it != list->end(); it++) {
945 printf("---------------------------------------------------------------------\n");
948 void ModelChecker::print_summary()
951 printf("Number of executions: %d\n", num_executions);
952 printf("Total nodes created: %d\n", node_stack->get_total_nodes());
956 if (!isfinalfeasible())
957 printf("INFEASIBLE EXECUTION!\n");
958 print_list(action_trace);
963 * Add a Thread to the system for the first time. Should only be called once
965 * @param t The Thread to add
967 void ModelChecker::add_thread(Thread *t)
969 thread_map->put(id_to_int(t->get_id()), t);
970 scheduler->add_thread(t);
973 void ModelChecker::remove_thread(Thread *t)
975 scheduler->remove_thread(t);
979 * Switch from a user-context to the "master thread" context (a.k.a. system
980 * context). This switch is made with the intention of exploring a particular
981 * model-checking action (described by a ModelAction object). Must be called
982 * from a user-thread context.
983 * @param act The current action that will be explored. Must not be NULL.
984 * @return Return status from the 'swap' call (i.e., success/fail, 0/-1)
986 int ModelChecker::switch_to_master(ModelAction *act)
989 Thread *old = thread_current();
990 set_current_action(act);
991 old->set_state(THREAD_READY);
992 return Thread::swap(old, &system_context);
996 * Takes the next step in the execution, if possible.
997 * @return Returns true (success) if a step was taken and false otherwise.
999 bool ModelChecker::take_step() {
1000 Thread *curr, *next;
1005 curr = thread_current();
1007 if (curr->get_state() == THREAD_READY) {
1008 ASSERT(priv->current_action);
1009 priv->nextThread = check_current_action(priv->current_action);
1010 priv->current_action = NULL;
1011 if (!curr->is_blocked() && !curr->is_complete())
1012 scheduler->add_thread(curr);
1017 next = scheduler->next_thread(priv->nextThread);
1019 /* Infeasible -> don't take any more steps */
1024 next->set_state(THREAD_RUNNING);
1025 DEBUG("(%d, %d)\n", curr ? curr->get_id() : -1, next ? next->get_id() : -1);
1027 /* next == NULL -> don't take any more steps */
1030 /* Return false only if swap fails with an error */
1031 return (Thread::swap(&system_context, next) == 0);
1034 /** Runs the current execution until threre are no more steps to take. */
1035 void ModelChecker::finish_execution() {
1038 while (take_step());