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),
36 too_many_reads(false),
39 /* Allocate this "size" on the snapshotting heap */
40 priv = (struct model_snapshot_members *)calloc(1, sizeof(*priv));
41 /* First thread created will have id INITIAL_THREAD_ID */
42 priv->next_thread_id = INITIAL_THREAD_ID;
44 lazy_sync_size = &priv->lazy_sync_size;
47 /** @brief Destructor */
48 ModelChecker::~ModelChecker()
50 for (int i = 0; i < get_num_threads(); i++)
51 delete thread_map->get(i);
58 for (unsigned int i = 0; i < promises->size(); i++)
59 delete (*promises)[i];
62 delete lazy_sync_with_release;
64 delete thrd_last_action;
71 * Restores user program to initial state and resets all model-checker data
74 void ModelChecker::reset_to_initial_state()
76 DEBUG("+++ Resetting to initial state +++\n");
77 node_stack->reset_execution();
78 failed_promise = false;
79 too_many_reads = false;
81 snapshotObject->backTrackBeforeStep(0);
84 /** @returns a thread ID for a new Thread */
85 thread_id_t ModelChecker::get_next_id()
87 return priv->next_thread_id++;
90 /** @returns the number of user threads created during this execution */
91 int ModelChecker::get_num_threads()
93 return priv->next_thread_id;
96 /** @returns a sequence number for a new ModelAction */
97 modelclock_t ModelChecker::get_next_seq_num()
99 return ++priv->used_sequence_numbers;
103 * Choose the next thread in the replay sequence.
105 * If the replay sequence has reached the 'diverge' point, returns a thread
106 * from the backtracking set. Otherwise, simply returns the next thread in the
107 * sequence that is being replayed.
109 Thread * ModelChecker::get_next_replay_thread()
113 /* Have we completed exploring the preselected path? */
117 /* Else, we are trying to replay an execution */
118 ModelAction *next = node_stack->get_next()->get_action();
120 if (next == diverge) {
121 Node *nextnode = next->get_node();
122 /* Reached divergence point */
123 if (nextnode->increment_promise()) {
124 /* The next node will try to satisfy a different set of promises. */
125 tid = next->get_tid();
126 node_stack->pop_restofstack(2);
127 } else if (nextnode->increment_read_from()) {
128 /* The next node will read from a different value. */
129 tid = next->get_tid();
130 node_stack->pop_restofstack(2);
131 } else if (nextnode->increment_future_value()) {
132 /* The next node will try to read from a different future value. */
133 tid = next->get_tid();
134 node_stack->pop_restofstack(2);
136 /* Make a different thread execute for next step */
137 Node *node = nextnode->get_parent();
138 tid = node->get_next_backtrack();
139 node_stack->pop_restofstack(1);
141 DEBUG("*** Divergence point ***\n");
144 tid = next->get_tid();
146 DEBUG("*** ModelChecker chose next thread = %d ***\n", tid);
147 ASSERT(tid != THREAD_ID_T_NONE);
148 return thread_map->get(id_to_int(tid));
152 * Queries the model-checker for more executions to explore and, if one
153 * exists, resets the model-checker state to execute a new execution.
155 * @return If there are more executions to explore, return true. Otherwise,
158 bool ModelChecker::next_execution()
164 if (isfinalfeasible() || DBG_ENABLED())
167 if ((diverge = get_next_backtrack()) == NULL)
171 printf("Next execution will diverge at:\n");
175 reset_to_initial_state();
179 ModelAction * ModelChecker::get_last_conflict(ModelAction *act)
181 action_type type = act->get_type();
191 /* linear search: from most recent to oldest */
192 action_list_t *list = obj_map->get_safe_ptr(act->get_location());
193 action_list_t::reverse_iterator rit;
194 for (rit = list->rbegin(); rit != list->rend(); rit++) {
195 ModelAction *prev = *rit;
196 if (act->is_synchronizing(prev))
202 void ModelChecker::set_backtracking(ModelAction *act)
206 Thread *t = get_thread(act->get_tid());
208 prev = get_last_conflict(act);
212 node = prev->get_node()->get_parent();
214 while (!node->is_enabled(t))
217 /* Check if this has been explored already */
218 if (node->has_been_explored(t->get_id()))
221 /* Cache the latest backtracking point */
222 if (!priv->next_backtrack || *prev > *priv->next_backtrack)
223 priv->next_backtrack = prev;
225 /* If this is a new backtracking point, mark the tree */
226 if (!node->set_backtrack(t->get_id()))
228 DEBUG("Setting backtrack: conflict = %d, instead tid = %d\n",
229 prev->get_tid(), t->get_id());
237 * Returns last backtracking point. The model checker will explore a different
238 * path for this point in the next execution.
239 * @return The ModelAction at which the next execution should diverge.
241 ModelAction * ModelChecker::get_next_backtrack()
243 ModelAction *next = priv->next_backtrack;
244 priv->next_backtrack = NULL;
249 * This is the heart of the model checker routine. It performs model-checking
250 * actions corresponding to a given "current action." Among other processes, it
251 * calculates reads-from relationships, updates synchronization clock vectors,
252 * forms a memory_order constraints graph, and handles replay/backtrack
253 * execution when running permutations of previously-observed executions.
255 * @param curr The current action to process
256 * @return The next Thread that must be executed. May be NULL if ModelChecker
257 * makes no choice (e.g., according to replay execution, combining RMW actions,
260 Thread * ModelChecker::check_current_action(ModelAction *curr)
262 bool second_part_of_rmw = false;
266 if (curr->is_rmwc() || curr->is_rmw()) {
267 ModelAction *tmp = process_rmw(curr);
268 second_part_of_rmw = true;
272 ModelAction *tmp = node_stack->explore_action(curr);
274 /* Discard duplicate ModelAction; use action from NodeStack */
275 /* First restore type and order in case of RMW operation */
277 tmp->copy_typeandorder(curr);
279 /* If we have diverged, we need to reset the clock vector. */
281 tmp->create_cv(get_parent_action(tmp->get_tid()));
287 * Perform one-time actions when pushing new ModelAction onto
290 curr->create_cv(get_parent_action(curr->get_tid()));
291 /* Build may_read_from set */
293 build_reads_from_past(curr);
294 if (curr->is_write())
295 compute_promises(curr);
299 /* Assign 'creation' parent */
300 if (curr->get_type() == THREAD_CREATE) {
301 Thread *th = (Thread *)curr->get_location();
302 th->set_creation(curr);
303 } else if (curr->get_type() == THREAD_JOIN) {
305 wait = get_thread(curr->get_tid());
306 join = (Thread *)curr->get_location();
307 if (!join->is_complete())
308 scheduler->wait(wait, join);
309 } else if (curr->get_type() == THREAD_FINISH) {
310 Thread *th = get_thread(curr->get_tid());
311 while (!th->wait_list_empty()) {
312 Thread *wake = th->pop_wait_list();
313 scheduler->wake(wake);
318 /* Deal with new thread */
319 if (curr->get_type() == THREAD_START)
320 check_promises(NULL, curr->get_cv());
322 /* Assign reads_from values */
323 Thread *th = get_thread(curr->get_tid());
324 uint64_t value = VALUE_NONE;
325 bool updated = false;
326 if (curr->is_read()) {
328 const ModelAction *reads_from = curr->get_node()->get_read_from();
329 if (reads_from != NULL) {
330 value = reads_from->get_value();
331 /* Assign reads_from, perform release/acquire synchronization */
332 curr->read_from(reads_from);
333 if (!second_part_of_rmw)
334 check_recency(curr,false);
336 bool r_status=r_modification_order(curr,reads_from);
338 if (!second_part_of_rmw&&!isfeasible()&&(curr->get_node()->increment_read_from()||!curr->get_node()->future_value_empty())) {
339 mo_graph->rollbackChanges();
340 too_many_reads=false;
344 mo_graph->commitChanges();
347 /* Read from future value */
348 value = curr->get_node()->get_future_value();
349 curr->read_from(NULL);
350 Promise *valuepromise = new Promise(curr, value);
351 promises->push_back(valuepromise);
355 } else if (curr->is_write()) {
356 if (w_modification_order(curr))
358 if (resolve_promises(curr))
360 mo_graph->commitChanges();
364 resolve_release_sequences(curr->get_location());
366 th->set_return_value(value);
368 /* Add action to list. */
369 if (!second_part_of_rmw)
370 add_action_to_lists(curr);
372 Node *currnode = curr->get_node();
373 Node *parnode = currnode->get_parent();
375 if (!parnode->backtrack_empty() || !currnode->read_from_empty() ||
376 !currnode->future_value_empty() || !currnode->promise_empty())
377 if (!priv->next_backtrack || *curr > *priv->next_backtrack)
378 priv->next_backtrack = curr;
380 set_backtracking(curr);
382 /* Do not split atomic actions. */
384 return thread_current();
385 /* The THREAD_CREATE action points to the created Thread */
386 else if (curr->get_type() == THREAD_CREATE)
387 return (Thread *)curr->get_location();
389 return get_next_replay_thread();
392 /** @returns whether the current partial trace must be a prefix of a
394 bool ModelChecker::isfeasibleprefix() {
395 return promises->size() == 0 && *lazy_sync_size == 0;
398 /** @returns whether the current partial trace is feasible. */
399 bool ModelChecker::isfeasible() {
400 return !mo_graph->checkForCycles() && !failed_promise && !too_many_reads;
403 /** Returns whether the current completed trace is feasible. */
404 bool ModelChecker::isfinalfeasible() {
405 return isfeasible() && promises->size() == 0;
408 /** Close out a RMWR by converting previous RMWR into a RMW or READ. */
409 ModelAction * ModelChecker::process_rmw(ModelAction *act) {
410 int tid = id_to_int(act->get_tid());
411 ModelAction *lastread = get_last_action(tid);
412 lastread->process_rmw(act);
414 mo_graph->addRMWEdge(lastread->get_reads_from(), lastread);
419 * Checks whether a thread has read from the same write for too many times.
420 * @todo This may be more subtle than this code segment addresses at this
421 * point... Potential problems to ponder and fix:
422 * (1) What if the reads_from set keeps changing such that there is no common
424 * (2) What if the problem is that the other writes would break modification
427 void ModelChecker::check_recency(ModelAction *curr, bool already_added) {
428 if (params.maxreads != 0) {
429 if (curr->get_node()->get_read_from_size() <= 1)
432 std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(curr->get_location());
433 int tid = id_to_int(curr->get_tid());
436 if ((int)thrd_lists->size() <= tid)
439 action_list_t *list = &(*thrd_lists)[tid];
441 action_list_t::reverse_iterator rit = list->rbegin();
443 if (!already_added) {
444 for (; (*rit) != curr; rit++)
446 /* go past curr now */
451 for (; rit != list->rend(); rit++) {
452 ModelAction *act = *rit;
455 if (act->get_reads_from() != curr->get_reads_from())
457 if (act->get_node()->get_read_from_size() <= 1)
460 if (count >= params.maxreads) {
461 /* We've read from the same write for too many times */
462 too_many_reads = true;
469 * Updates the mo_graph with the constraints imposed from the current read.
470 * @param curr The current action. Must be a read.
471 * @param rf The action that curr reads from. Must be a write.
472 * @return True if modification order edges were added; false otherwise
474 bool ModelChecker::r_modification_order(ModelAction *curr, const ModelAction *rf)
476 std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(curr->get_location());
479 ASSERT(curr->is_read());
481 /* Iterate over all threads */
482 for (i = 0; i < thrd_lists->size(); i++) {
483 /* Iterate over actions in thread, starting from most recent */
484 action_list_t *list = &(*thrd_lists)[i];
485 action_list_t::reverse_iterator rit;
486 for (rit = list->rbegin(); rit != list->rend(); rit++) {
487 ModelAction *act = *rit;
489 /* Include at most one act per-thread that "happens before" curr */
490 if (act->happens_before(curr)) {
491 if (act->is_read()) {
492 const ModelAction *prevreadfrom = act->get_reads_from();
493 if (prevreadfrom != NULL && rf != prevreadfrom) {
494 mo_graph->addEdge(prevreadfrom, rf);
497 } else if (rf != act) {
498 mo_graph->addEdge(act, rf);
509 /** Updates the mo_graph with the constraints imposed from the current read. */
510 void ModelChecker::post_r_modification_order(ModelAction *curr, const ModelAction *rf)
512 std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(curr->get_location());
514 ASSERT(curr->is_read());
516 /* Iterate over all threads */
517 for (i = 0; i < thrd_lists->size(); i++) {
518 /* Iterate over actions in thread, starting from most recent */
519 action_list_t *list = &(*thrd_lists)[i];
520 action_list_t::reverse_iterator rit;
521 ModelAction *lastact = NULL;
523 /* Find last action that happens after curr */
524 for (rit = list->rbegin(); rit != list->rend(); rit++) {
525 ModelAction *act = *rit;
526 if (curr->happens_before(act)) {
532 /* Include at most one act per-thread that "happens before" curr */
533 if (lastact != NULL) {
534 if (lastact->is_read()) {
535 const ModelAction *postreadfrom = lastact->get_reads_from();
536 if (postreadfrom != NULL&&rf != postreadfrom)
537 mo_graph->addEdge(rf, postreadfrom);
538 } else if (rf != lastact) {
539 mo_graph->addEdge(rf, lastact);
547 * Updates the mo_graph with the constraints imposed from the current write.
548 * @param curr The current action. Must be a write.
549 * @return True if modification order edges were added; false otherwise
551 bool ModelChecker::w_modification_order(ModelAction *curr)
553 std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(curr->get_location());
556 ASSERT(curr->is_write());
558 if (curr->is_seqcst()) {
559 /* We have to at least see the last sequentially consistent write,
560 so we are initialized. */
561 ModelAction *last_seq_cst = get_last_seq_cst(curr->get_location());
562 if (last_seq_cst != NULL) {
563 mo_graph->addEdge(last_seq_cst, curr);
568 /* Iterate over all threads */
569 for (i = 0; i < thrd_lists->size(); i++) {
570 /* Iterate over actions in thread, starting from most recent */
571 action_list_t *list = &(*thrd_lists)[i];
572 action_list_t::reverse_iterator rit;
573 for (rit = list->rbegin(); rit != list->rend(); rit++) {
574 ModelAction *act = *rit;
576 /* Include at most one act per-thread that "happens before" curr */
577 if (act->happens_before(curr)) {
579 mo_graph->addEdge(act->get_reads_from(), curr);
581 mo_graph->addEdge(act, curr);
584 } else if (act->is_read() && !act->is_synchronizing(curr) &&
585 !act->same_thread(curr)) {
586 /* We have an action that:
587 (1) did not happen before us
588 (2) is a read and we are a write
589 (3) cannot synchronize with us
590 (4) is in a different thread
592 that read could potentially read from our write.
594 if (act->get_node()->add_future_value(curr->get_value()) &&
595 (!priv->next_backtrack || *act > *priv->next_backtrack))
596 priv->next_backtrack = act;
605 * Finds the head(s) of the release sequence(s) containing a given ModelAction.
606 * The ModelAction under consideration is expected to be taking part in
607 * release/acquire synchronization as an object of the "reads from" relation.
608 * Note that this can only provide release sequence support for RMW chains
609 * which do not read from the future, as those actions cannot be traced until
610 * their "promise" is fulfilled. Similarly, we may not even establish the
611 * presence of a release sequence with certainty, as some modification order
612 * constraints may be decided further in the future. Thus, this function
613 * "returns" two pieces of data: a pass-by-reference vector of @a release_heads
614 * and a boolean representing certainty.
616 * @todo Finish lazy updating, when promises are fulfilled in the future
617 * @param rf The action that might be part of a release sequence. Must be a
619 * @param release_heads A pass-by-reference style return parameter. After
620 * execution of this function, release_heads will contain the heads of all the
621 * relevant release sequences, if any exists
622 * @return true, if the ModelChecker is certain that release_heads is complete;
625 bool ModelChecker::release_seq_head(const ModelAction *rf,
626 std::vector<const ModelAction *> *release_heads) const
628 ASSERT(rf->is_write());
630 /* read from future: need to settle this later */
631 return false; /* incomplete */
633 if (rf->is_release())
634 release_heads->push_back(rf);
636 /* We need a RMW action that is both an acquire and release to stop */
637 /** @todo Need to be smarter here... In the linux lock
638 * example, this will run to the beginning of the program for
640 if (rf->is_acquire() && rf->is_release())
641 return true; /* complete */
642 return release_seq_head(rf->get_reads_from(), release_heads);
644 if (rf->is_release())
645 return true; /* complete */
647 /* else relaxed write; check modification order for contiguous subsequence
648 * -> rf must be same thread as release */
649 int tid = id_to_int(rf->get_tid());
650 std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(rf->get_location());
651 action_list_t *list = &(*thrd_lists)[tid];
652 action_list_t::const_reverse_iterator rit;
654 /* Find rf in the thread list */
655 rit = std::find(list->rbegin(), list->rend(), rf);
656 ASSERT(rit != list->rend());
658 /* Find the last write/release */
659 for (; rit != list->rend(); rit++)
660 if ((*rit)->is_release())
662 if (rit == list->rend()) {
663 /* No write-release in this thread */
664 return true; /* complete */
666 ModelAction *release = *rit;
668 ASSERT(rf->same_thread(release));
671 for (unsigned int i = 0; i < thrd_lists->size(); i++) {
672 if (id_to_int(rf->get_tid()) == (int)i)
674 list = &(*thrd_lists)[i];
676 /* Can we ensure no future writes from this thread may break
677 * the release seq? */
678 bool future_ordered = false;
680 for (rit = list->rbegin(); rit != list->rend(); rit++) {
681 const ModelAction *act = *rit;
682 if (!act->is_write())
684 /* Reach synchronization -> this thread is complete */
685 if (act->happens_before(release))
687 if (rf->happens_before(act)) {
688 future_ordered = true;
692 /* Check modification order */
693 if (mo_graph->checkReachable(rf, act)) {
695 future_ordered = true;
698 if (mo_graph->checkReachable(act, release))
699 /* act --mo--> release */
701 if (mo_graph->checkReachable(release, act) &&
702 mo_graph->checkReachable(act, rf)) {
703 /* release --mo-> act --mo--> rf */
704 return true; /* complete */
709 return false; /* This thread is uncertain */
713 release_heads->push_back(release);
718 * A public interface for getting the release sequence head(s) with which a
719 * given ModelAction must synchronize. This function only returns a non-empty
720 * result when it can locate a release sequence head with certainty. Otherwise,
721 * it may mark the internal state of the ModelChecker so that it will handle
722 * the release sequence at a later time, causing @a act to update its
723 * synchronization at some later point in execution.
724 * @param act The 'acquire' action that may read from a release sequence
725 * @param release_heads A pass-by-reference return parameter. Will be filled
726 * with the head(s) of the release sequence(s), if they exists with certainty.
727 * @see ModelChecker::release_seq_head
729 void ModelChecker::get_release_seq_heads(ModelAction *act,
730 std::vector<const ModelAction *> *release_heads)
732 const ModelAction *rf = act->get_reads_from();
734 complete = release_seq_head(rf, release_heads);
736 /* add act to 'lazy checking' list */
737 std::list<ModelAction *> *list;
738 list = lazy_sync_with_release->get_safe_ptr(act->get_location());
739 list->push_back(act);
745 * Attempt to resolve all stashed operations that might synchronize with a
746 * release sequence for a given location. This implements the "lazy" portion of
747 * determining whether or not a release sequence was contiguous, since not all
748 * modification order information is present at the time an action occurs.
750 * @param location The location/object that should be checked for release
751 * sequence resolutions
752 * @return True if any updates occurred (new synchronization, new mo_graph edges)
754 bool ModelChecker::resolve_release_sequences(void *location)
756 std::list<ModelAction *> *list;
757 list = lazy_sync_with_release->getptr(location);
761 bool updated = false;
762 std::list<ModelAction *>::iterator it = list->begin();
763 while (it != list->end()) {
764 ModelAction *act = *it;
765 const ModelAction *rf = act->get_reads_from();
766 std::vector<const ModelAction *> release_heads;
768 complete = release_seq_head(rf, &release_heads);
769 for (unsigned int i = 0; i < release_heads.size(); i++) {
770 if (!act->has_synchronized_with(release_heads[i])) {
772 act->synchronize_with(release_heads[i]);
777 /* propagate synchronization to later actions */
778 action_list_t::reverse_iterator it = action_trace->rbegin();
779 while ((*it) != act) {
780 ModelAction *propagate = *it;
781 if (act->happens_before(propagate))
782 /** @todo new mo_graph edges along with
783 * this synchronization? */
784 propagate->synchronize_with(act);
788 it = list->erase(it);
794 // If we resolved promises or data races, see if we have realized a data race.
795 if (checkDataRaces()) {
803 * Performs various bookkeeping operations for the current ModelAction. For
804 * instance, adds action to the per-object, per-thread action vector and to the
805 * action trace list of all thread actions.
807 * @param act is the ModelAction to add.
809 void ModelChecker::add_action_to_lists(ModelAction *act)
811 int tid = id_to_int(act->get_tid());
812 action_trace->push_back(act);
814 obj_map->get_safe_ptr(act->get_location())->push_back(act);
816 std::vector<action_list_t> *vec = obj_thrd_map->get_safe_ptr(act->get_location());
817 if (tid >= (int)vec->size())
818 vec->resize(priv->next_thread_id);
819 (*vec)[tid].push_back(act);
821 if ((int)thrd_last_action->size() <= tid)
822 thrd_last_action->resize(get_num_threads());
823 (*thrd_last_action)[tid] = act;
826 ModelAction * ModelChecker::get_last_action(thread_id_t tid)
828 int nthreads = get_num_threads();
829 if ((int)thrd_last_action->size() < nthreads)
830 thrd_last_action->resize(nthreads);
831 return (*thrd_last_action)[id_to_int(tid)];
835 * Gets the last memory_order_seq_cst action (in the total global sequence)
836 * performed on a particular object (i.e., memory location).
837 * @param location The object location to check
838 * @return The last seq_cst action performed
840 ModelAction * ModelChecker::get_last_seq_cst(const void *location)
842 action_list_t *list = obj_map->get_safe_ptr(location);
843 /* Find: max({i in dom(S) | seq_cst(t_i) && isWrite(t_i) && samevar(t_i, t)}) */
844 action_list_t::reverse_iterator rit;
845 for (rit = list->rbegin(); rit != list->rend(); rit++)
846 if ((*rit)->is_write() && (*rit)->is_seqcst())
851 ModelAction * ModelChecker::get_parent_action(thread_id_t tid)
853 ModelAction *parent = get_last_action(tid);
855 parent = get_thread(tid)->get_creation();
860 * Returns the clock vector for a given thread.
861 * @param tid The thread whose clock vector we want
862 * @return Desired clock vector
864 ClockVector * ModelChecker::get_cv(thread_id_t tid)
866 return get_parent_action(tid)->get_cv();
870 * Resolve a set of Promises with a current write. The set is provided in the
871 * Node corresponding to @a write.
872 * @param write The ModelAction that is fulfilling Promises
873 * @return True if promises were resolved; false otherwise
875 bool ModelChecker::resolve_promises(ModelAction *write)
877 bool resolved = false;
878 for (unsigned int i = 0, promise_index = 0; promise_index < promises->size(); i++) {
879 Promise *promise = (*promises)[promise_index];
880 if (write->get_node()->get_promise(i)) {
881 ModelAction *read = promise->get_action();
882 read->read_from(write);
883 r_modification_order(read, write);
884 post_r_modification_order(read, write);
885 promises->erase(promises->begin() + promise_index);
895 * Compute the set of promises that could potentially be satisfied by this
896 * action. Note that the set computation actually appears in the Node, not in
898 * @param curr The ModelAction that may satisfy promises
900 void ModelChecker::compute_promises(ModelAction *curr)
902 for (unsigned int i = 0; i < promises->size(); i++) {
903 Promise *promise = (*promises)[i];
904 const ModelAction *act = promise->get_action();
905 if (!act->happens_before(curr) &&
907 !act->is_synchronizing(curr) &&
908 !act->same_thread(curr) &&
909 promise->get_value() == curr->get_value()) {
910 curr->get_node()->set_promise(i);
915 /** Checks promises in response to change in ClockVector Threads. */
916 void ModelChecker::check_promises(ClockVector *old_cv, ClockVector *merge_cv)
918 for (unsigned int i = 0; i < promises->size(); i++) {
919 Promise *promise = (*promises)[i];
920 const ModelAction *act = promise->get_action();
921 if ((old_cv == NULL || !old_cv->synchronized_since(act)) &&
922 merge_cv->synchronized_since(act)) {
923 //This thread is no longer able to send values back to satisfy the promise
924 int num_synchronized_threads = promise->increment_threads();
925 if (num_synchronized_threads == get_num_threads()) {
927 failed_promise = true;
935 * Build up an initial set of all past writes that this 'read' action may read
936 * from. This set is determined by the clock vector's "happens before"
938 * @param curr is the current ModelAction that we are exploring; it must be a
941 void ModelChecker::build_reads_from_past(ModelAction *curr)
943 std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(curr->get_location());
945 ASSERT(curr->is_read());
947 ModelAction *last_seq_cst = NULL;
949 /* Track whether this object has been initialized */
950 bool initialized = false;
952 if (curr->is_seqcst()) {
953 last_seq_cst = get_last_seq_cst(curr->get_location());
954 /* We have to at least see the last sequentially consistent write,
955 so we are initialized. */
956 if (last_seq_cst != NULL)
960 /* Iterate over all threads */
961 for (i = 0; i < thrd_lists->size(); i++) {
962 /* Iterate over actions in thread, starting from most recent */
963 action_list_t *list = &(*thrd_lists)[i];
964 action_list_t::reverse_iterator rit;
965 for (rit = list->rbegin(); rit != list->rend(); rit++) {
966 ModelAction *act = *rit;
968 /* Only consider 'write' actions */
969 if (!act->is_write())
972 /* Don't consider more than one seq_cst write if we are a seq_cst read. */
973 if (!act->is_seqcst() || !curr->is_seqcst() || act == last_seq_cst) {
974 DEBUG("Adding action to may_read_from:\n");
979 curr->get_node()->add_read_from(act);
982 /* Include at most one act per-thread that "happens before" curr */
983 if (act->happens_before(curr)) {
991 /** @todo Need a more informative way of reporting errors. */
992 printf("ERROR: may read from uninitialized atomic\n");
995 if (DBG_ENABLED() || !initialized) {
996 printf("Reached read action:\n");
998 printf("Printing may_read_from\n");
999 curr->get_node()->print_may_read_from();
1000 printf("End printing may_read_from\n");
1003 ASSERT(initialized);
1006 static void print_list(action_list_t *list)
1008 action_list_t::iterator it;
1010 printf("---------------------------------------------------------------------\n");
1013 for (it = list->begin(); it != list->end(); it++) {
1016 printf("---------------------------------------------------------------------\n");
1019 void ModelChecker::print_summary()
1022 printf("Number of executions: %d\n", num_executions);
1023 printf("Total nodes created: %d\n", node_stack->get_total_nodes());
1027 if (!isfinalfeasible())
1028 printf("INFEASIBLE EXECUTION!\n");
1029 print_list(action_trace);
1034 * Add a Thread to the system for the first time. Should only be called once
1036 * @param t The Thread to add
1038 void ModelChecker::add_thread(Thread *t)
1040 thread_map->put(id_to_int(t->get_id()), t);
1041 scheduler->add_thread(t);
1044 void ModelChecker::remove_thread(Thread *t)
1046 scheduler->remove_thread(t);
1050 * Switch from a user-context to the "master thread" context (a.k.a. system
1051 * context). This switch is made with the intention of exploring a particular
1052 * model-checking action (described by a ModelAction object). Must be called
1053 * from a user-thread context.
1054 * @param act The current action that will be explored. Must not be NULL.
1055 * @return Return status from the 'swap' call (i.e., success/fail, 0/-1)
1057 int ModelChecker::switch_to_master(ModelAction *act)
1060 Thread *old = thread_current();
1061 set_current_action(act);
1062 old->set_state(THREAD_READY);
1063 return Thread::swap(old, &system_context);
1067 * Takes the next step in the execution, if possible.
1068 * @return Returns true (success) if a step was taken and false otherwise.
1070 bool ModelChecker::take_step() {
1071 Thread *curr, *next;
1076 curr = thread_current();
1078 if (curr->get_state() == THREAD_READY) {
1079 ASSERT(priv->current_action);
1080 priv->nextThread = check_current_action(priv->current_action);
1081 priv->current_action = NULL;
1082 if (!curr->is_blocked() && !curr->is_complete())
1083 scheduler->add_thread(curr);
1088 next = scheduler->next_thread(priv->nextThread);
1090 /* Infeasible -> don't take any more steps */
1095 next->set_state(THREAD_RUNNING);
1096 DEBUG("(%d, %d)\n", curr ? curr->get_id() : -1, next ? next->get_id() : -1);
1098 /* next == NULL -> don't take any more steps */
1101 /* Return false only if swap fails with an error */
1102 return (Thread::swap(&system_context, next) == 0);
1105 /** Runs the current execution until threre are no more steps to take. */
1106 void ModelChecker::finish_execution() {
1109 while (take_step());