model: pull "may_read_from set" calculation out of initialization
[c11tester.git] / model.cc
1 #include <stdio.h>
2 #include <algorithm>
3
4 #include "model.h"
5 #include "action.h"
6 #include "nodestack.h"
7 #include "schedule.h"
8 #include "snapshot-interface.h"
9 #include "common.h"
10 #include "clockvector.h"
11 #include "cyclegraph.h"
12 #include "promise.h"
13 #include "datarace.h"
14
15 #define INITIAL_THREAD_ID       0
16
17 ModelChecker *model;
18
19 /** @brief Constructor */
20 ModelChecker::ModelChecker(struct model_params params) :
21         /* Initialize default scheduler */
22         scheduler(new Scheduler()),
23         num_executions(0),
24         num_feasible_executions(0),
25         params(params),
26         diverge(NULL),
27         action_trace(new action_list_t()),
28         thread_map(new HashTable<int, Thread *, int>()),
29         obj_map(new HashTable<const void *, action_list_t, uintptr_t, 4>()),
30         obj_thrd_map(new HashTable<void *, std::vector<action_list_t>, uintptr_t, 4 >()),
31         promises(new std::vector<Promise *>()),
32         futurevalues(new std::vector<struct PendingFutureValue>()),
33         lazy_sync_with_release(new HashTable<void *, std::list<ModelAction *>, uintptr_t, 4>()),
34         thrd_last_action(new std::vector<ModelAction *>(1)),
35         node_stack(new NodeStack()),
36         mo_graph(new CycleGraph()),
37         failed_promise(false),
38         too_many_reads(false),
39         asserted(false)
40 {
41         /* Allocate this "size" on the snapshotting heap */
42         priv = (struct model_snapshot_members *)calloc(1, sizeof(*priv));
43         /* First thread created will have id INITIAL_THREAD_ID */
44         priv->next_thread_id = INITIAL_THREAD_ID;
45
46         lazy_sync_size = &priv->lazy_sync_size;
47 }
48
49 /** @brief Destructor */
50 ModelChecker::~ModelChecker()
51 {
52         for (int i = 0; i < get_num_threads(); i++)
53                 delete thread_map->get(i);
54         delete thread_map;
55
56         delete obj_thrd_map;
57         delete obj_map;
58         delete action_trace;
59
60         for (unsigned int i = 0; i < promises->size(); i++)
61                 delete (*promises)[i];
62         delete promises;
63
64         delete lazy_sync_with_release;
65
66         delete thrd_last_action;
67         delete node_stack;
68         delete scheduler;
69         delete mo_graph;
70 }
71
72 /**
73  * Restores user program to initial state and resets all model-checker data
74  * structures.
75  */
76 void ModelChecker::reset_to_initial_state()
77 {
78         DEBUG("+++ Resetting to initial state +++\n");
79         node_stack->reset_execution();
80         failed_promise = false;
81         too_many_reads = false;
82         reset_asserted();
83         snapshotObject->backTrackBeforeStep(0);
84 }
85
86 /** @returns a thread ID for a new Thread */
87 thread_id_t ModelChecker::get_next_id()
88 {
89         return priv->next_thread_id++;
90 }
91
92 /** @returns the number of user threads created during this execution */
93 int ModelChecker::get_num_threads()
94 {
95         return priv->next_thread_id;
96 }
97
98 /** @returns a sequence number for a new ModelAction */
99 modelclock_t ModelChecker::get_next_seq_num()
100 {
101         return ++priv->used_sequence_numbers;
102 }
103
104 /**
105  * @brief Choose the next thread to execute.
106  *
107  * This function chooses the next thread that should execute. It can force the
108  * adjacency of read/write portions of a RMW action, force THREAD_CREATE to be
109  * followed by a THREAD_START, or it can enforce execution replay/backtracking.
110  * The model-checker may have no preference regarding the next thread (i.e.,
111  * when exploring a new execution ordering), in which case this will return
112  * NULL.
113  * @param curr The current ModelAction. This action might guide the choice of
114  * next thread.
115  * @return The next thread to run. If the model-checker has no preference, NULL.
116  */
117 Thread * ModelChecker::get_next_thread(ModelAction *curr)
118 {
119         thread_id_t tid;
120
121         /* Do not split atomic actions. */
122         if (curr->is_rmwr())
123                 return thread_current();
124         /* The THREAD_CREATE action points to the created Thread */
125         else if (curr->get_type() == THREAD_CREATE)
126                 return (Thread *)curr->get_location();
127
128         /* Have we completed exploring the preselected path? */
129         if (diverge == NULL)
130                 return NULL;
131
132         /* Else, we are trying to replay an execution */
133         ModelAction *next = node_stack->get_next()->get_action();
134
135         if (next == diverge) {
136                 Node *nextnode = next->get_node();
137                 /* Reached divergence point */
138                 if (nextnode->increment_promise()) {
139                         /* The next node will try to satisfy a different set of promises. */
140                         tid = next->get_tid();
141                         node_stack->pop_restofstack(2);
142                 } else if (nextnode->increment_read_from()) {
143                         /* The next node will read from a different value. */
144                         tid = next->get_tid();
145                         node_stack->pop_restofstack(2);
146                 } else if (nextnode->increment_future_value()) {
147                         /* The next node will try to read from a different future value. */
148                         tid = next->get_tid();
149                         node_stack->pop_restofstack(2);
150                 } else {
151                         /* Make a different thread execute for next step */
152                         Node *node = nextnode->get_parent();
153                         tid = node->get_next_backtrack();
154                         node_stack->pop_restofstack(1);
155                 }
156                 DEBUG("*** Divergence point ***\n");
157                 diverge = NULL;
158         } else {
159                 tid = next->get_tid();
160         }
161         DEBUG("*** ModelChecker chose next thread = %d ***\n", tid);
162         ASSERT(tid != THREAD_ID_T_NONE);
163         return thread_map->get(id_to_int(tid));
164 }
165
166 /**
167  * Queries the model-checker for more executions to explore and, if one
168  * exists, resets the model-checker state to execute a new execution.
169  *
170  * @return If there are more executions to explore, return true. Otherwise,
171  * return false.
172  */
173 bool ModelChecker::next_execution()
174 {
175         DBG();
176
177         num_executions++;
178         if (isfinalfeasible())
179                 num_feasible_executions++;
180
181         if (isfinalfeasible() || DBG_ENABLED())
182                 print_summary();
183
184         if ((diverge = get_next_backtrack()) == NULL)
185                 return false;
186
187         if (DBG_ENABLED()) {
188                 printf("Next execution will diverge at:\n");
189                 diverge->print();
190         }
191
192         reset_to_initial_state();
193         return true;
194 }
195
196 ModelAction * ModelChecker::get_last_conflict(ModelAction *act)
197 {
198         action_type type = act->get_type();
199
200         switch (type) {
201                 case ATOMIC_READ:
202                 case ATOMIC_WRITE:
203                 case ATOMIC_RMW:
204                         break;
205                 default:
206                         return NULL;
207         }
208         /* linear search: from most recent to oldest */
209         action_list_t *list = obj_map->get_safe_ptr(act->get_location());
210         action_list_t::reverse_iterator rit;
211         for (rit = list->rbegin(); rit != list->rend(); rit++) {
212                 ModelAction *prev = *rit;
213                 if (act->is_synchronizing(prev))
214                         return prev;
215         }
216         return NULL;
217 }
218
219 void ModelChecker::set_backtracking(ModelAction *act)
220 {
221         ModelAction *prev;
222         Node *node;
223         Thread *t = get_thread(act);
224
225         prev = get_last_conflict(act);
226         if (prev == NULL)
227                 return;
228
229         node = prev->get_node()->get_parent();
230
231         while (!node->is_enabled(t))
232                 t = t->get_parent();
233
234         /* Check if this has been explored already */
235         if (node->has_been_explored(t->get_id()))
236                 return;
237
238         /* Cache the latest backtracking point */
239         if (!priv->next_backtrack || *prev > *priv->next_backtrack)
240                 priv->next_backtrack = prev;
241
242         /* If this is a new backtracking point, mark the tree */
243         if (!node->set_backtrack(t->get_id()))
244                 return;
245         DEBUG("Setting backtrack: conflict = %d, instead tid = %d\n",
246                         prev->get_tid(), t->get_id());
247         if (DBG_ENABLED()) {
248                 prev->print();
249                 act->print();
250         }
251 }
252
253 /**
254  * Returns last backtracking point. The model checker will explore a different
255  * path for this point in the next execution.
256  * @return The ModelAction at which the next execution should diverge.
257  */
258 ModelAction * ModelChecker::get_next_backtrack()
259 {
260         ModelAction *next = priv->next_backtrack;
261         priv->next_backtrack = NULL;
262         return next;
263 }
264
265 /**
266  * Processes a read or rmw model action.
267  * @param curr is the read model action to process.
268  * @param second_part_of_rmw is boolean that is true is this is the second action of a rmw.
269  * @return True if processing this read updates the mo_graph.
270  */
271 bool ModelChecker::process_read(ModelAction *curr, bool second_part_of_rmw)
272 {
273         uint64_t value;
274         bool updated = false;
275         while (true) {
276                 const ModelAction *reads_from = curr->get_node()->get_read_from();
277                 if (reads_from != NULL) {
278                         mo_graph->startChanges();
279
280                         value = reads_from->get_value();
281                         bool r_status = false;
282
283                         if (!second_part_of_rmw) {
284                                 check_recency(curr);
285                                 r_status = r_modification_order(curr, reads_from);
286                         }
287
288
289                         if (!second_part_of_rmw&&!isfeasible()&&(curr->get_node()->increment_read_from()||curr->get_node()->increment_future_value())) {
290                                 mo_graph->rollbackChanges();
291                                 too_many_reads = false;
292                                 continue;
293                         }
294
295                         curr->read_from(reads_from);
296                         mo_graph->commitChanges();
297                         updated |= r_status;
298                 } else if (!second_part_of_rmw) {
299                         /* Read from future value */
300                         value = curr->get_node()->get_future_value();
301                         modelclock_t expiration = curr->get_node()->get_future_value_expiration();
302                         curr->read_from(NULL);
303                         Promise *valuepromise = new Promise(curr, value, expiration);
304                         promises->push_back(valuepromise);
305                 }
306                 get_thread(curr)->set_return_value(value);
307                 return updated;
308         }
309 }
310
311 /**
312  * Process a write ModelAction
313  * @param curr The ModelAction to process
314  * @return True if the mo_graph was updated or promises were resolved
315  */
316 bool ModelChecker::process_write(ModelAction *curr)
317 {
318         bool updated_mod_order = w_modification_order(curr);
319         bool updated_promises = resolve_promises(curr);
320
321         if (promises->size() == 0) {
322                 for (unsigned int i = 0; i<futurevalues->size(); i++) {
323                         struct PendingFutureValue pfv = (*futurevalues)[i];
324                         if (pfv.act->get_node()->add_future_value(pfv.value, pfv.expiration) &&
325                                         (!priv->next_backtrack || *pfv.act > *priv->next_backtrack))
326                                 priv->next_backtrack = pfv.act;
327                 }
328                 futurevalues->resize(0);
329         }
330
331         mo_graph->commitChanges();
332         get_thread(curr)->set_return_value(VALUE_NONE);
333         return updated_mod_order || updated_promises;
334 }
335
336 ModelAction * ModelChecker::initialize_curr_action(ModelAction *curr)
337 {
338         ModelAction *newcurr;
339
340         if (curr->is_rmwc() || curr->is_rmw()) {
341                 newcurr = process_rmw(curr);
342                 delete curr;
343                 compute_promises(newcurr);
344                 return newcurr;
345         }
346
347         newcurr = node_stack->explore_action(curr);
348         if (newcurr) {
349                 /* First restore type and order in case of RMW operation */
350                 if (curr->is_rmwr())
351                         newcurr->copy_typeandorder(curr);
352
353                 /* Discard duplicate ModelAction; use action from NodeStack */
354                 delete curr;
355
356                 /* If we have diverged, we need to reset the clock vector. */
357                 if (diverge == NULL)
358                         newcurr->create_cv(get_parent_action(newcurr->get_tid()));
359         } else {
360                 newcurr = curr;
361                 /*
362                  * Perform one-time actions when pushing new ModelAction onto
363                  * NodeStack
364                  */
365                 curr->create_cv(get_parent_action(curr->get_tid()));
366                 if (curr->is_write())
367                         compute_promises(curr);
368         }
369         return newcurr;
370 }
371
372 /**
373  * This is the heart of the model checker routine. It performs model-checking
374  * actions corresponding to a given "current action." Among other processes, it
375  * calculates reads-from relationships, updates synchronization clock vectors,
376  * forms a memory_order constraints graph, and handles replay/backtrack
377  * execution when running permutations of previously-observed executions.
378  *
379  * @param curr The current action to process
380  * @return The next Thread that must be executed. May be NULL if ModelChecker
381  * makes no choice (e.g., according to replay execution, combining RMW actions,
382  * etc.)
383  */
384 Thread * ModelChecker::check_current_action(ModelAction *curr)
385 {
386         ASSERT(curr);
387
388         bool second_part_of_rmw = curr->is_rmwc() || curr->is_rmw();
389
390         ModelAction *newcurr = initialize_curr_action(curr);
391
392         /* Build may_read_from set for newly-created actions */
393         if (curr == newcurr && curr->is_read())
394                 build_reads_from_past(curr);
395         curr = newcurr;
396
397         /* Thread specific actions */
398         switch (curr->get_type()) {
399         case THREAD_CREATE: {
400                 Thread *th = (Thread *)curr->get_location();
401                 th->set_creation(curr);
402                 break;
403         }
404         case THREAD_JOIN: {
405                 Thread *waiting, *blocking;
406                 waiting = get_thread(curr);
407                 blocking = (Thread *)curr->get_location();
408                 if (!blocking->is_complete()) {
409                         blocking->push_wait_list(curr);
410                         scheduler->sleep(waiting);
411                 }
412                 break;
413         }
414         case THREAD_FINISH: {
415                 Thread *th = get_thread(curr);
416                 while (!th->wait_list_empty()) {
417                         ModelAction *act = th->pop_wait_list();
418                         Thread *wake = get_thread(act);
419                         scheduler->wake(wake);
420                 }
421                 th->complete();
422                 break;
423         }
424         case THREAD_START: {
425                 check_promises(NULL, curr->get_cv());
426                 break;
427         }
428         default:
429                 break;
430         }
431
432         /* Add current action to lists before work_queue loop */
433         if (!second_part_of_rmw)
434                 add_action_to_lists(curr);
435
436         work_queue_t work_queue(1, CheckCurrWorkEntry(curr));
437
438         while (!work_queue.empty()) {
439                 WorkQueueEntry work = work_queue.front();
440                 work_queue.pop_front();
441
442                 switch (work.type) {
443                 case WORK_CHECK_CURR_ACTION: {
444                         ModelAction *act = work.action;
445                         bool updated = false;
446                         if (act->is_read() && process_read(act, second_part_of_rmw))
447                                 updated = true;
448
449                         if (act->is_write() && process_write(act))
450                                 updated = true;
451
452                         if (updated)
453                                 work_queue.push_back(CheckRelSeqWorkEntry(act->get_location()));
454                         break;
455                 }
456                 case WORK_CHECK_RELEASE_SEQ:
457                         resolve_release_sequences(work.location, &work_queue);
458                         break;
459                 case WORK_CHECK_MO_EDGES: {
460                         /** @todo Complete verification of work_queue */
461                         ModelAction *act = work.action;
462                         bool updated = false;
463
464                         if (act->is_read()) {
465                                 if (r_modification_order(act, act->get_reads_from()))
466                                         updated = true;
467                         }
468                         if (act->is_write()) {
469                                 if (w_modification_order(act))
470                                         updated = true;
471                         }
472
473                         if (updated)
474                                 work_queue.push_back(CheckRelSeqWorkEntry(act->get_location()));
475                         break;
476                 }
477                 default:
478                         ASSERT(false);
479                         break;
480                 }
481         }
482
483         check_curr_backtracking(curr);
484
485         set_backtracking(curr);
486
487         return get_next_thread(curr);
488 }
489
490 void ModelChecker::check_curr_backtracking(ModelAction * curr) {
491         Node *currnode = curr->get_node();
492         Node *parnode = currnode->get_parent();
493
494         if ((!parnode->backtrack_empty() ||
495                          !currnode->read_from_empty() ||
496                          !currnode->future_value_empty() ||
497                          !currnode->promise_empty())
498                         && (!priv->next_backtrack ||
499                                         *curr > *priv->next_backtrack)) {
500                 priv->next_backtrack = curr;
501         }
502 }
503
504 bool ModelChecker::promises_expired() {
505         for (unsigned int promise_index = 0; promise_index < promises->size(); promise_index++) {
506                 Promise *promise = (*promises)[promise_index];
507                 if (promise->get_expiration()<priv->used_sequence_numbers) {
508                         return true;
509                 }
510         }
511         return false;
512 }
513
514 /** @returns whether the current partial trace must be a prefix of a
515  * feasible trace. */
516 bool ModelChecker::isfeasibleprefix() {
517         return promises->size() == 0 && *lazy_sync_size == 0;
518 }
519
520 /** @returns whether the current partial trace is feasible. */
521 bool ModelChecker::isfeasible() {
522         return !mo_graph->checkForRMWViolation() && isfeasibleotherthanRMW();
523 }
524
525 /** @returns whether the current partial trace is feasible other than
526  * multiple RMW reading from the same store. */
527 bool ModelChecker::isfeasibleotherthanRMW() {
528         if (DBG_ENABLED()) {
529                 if (mo_graph->checkForCycles())
530                         DEBUG("Infeasible: modification order cycles\n");
531                 if (failed_promise)
532                         DEBUG("Infeasible: failed promise\n");
533                 if (too_many_reads)
534                         DEBUG("Infeasible: too many reads\n");
535                 if (promises_expired())
536                         DEBUG("Infeasible: promises expired\n");
537         }
538         return !mo_graph->checkForCycles() && !failed_promise && !too_many_reads && !promises_expired();
539 }
540
541 /** Returns whether the current completed trace is feasible. */
542 bool ModelChecker::isfinalfeasible() {
543         if (DBG_ENABLED() && promises->size() != 0)
544                 DEBUG("Infeasible: unrevolved promises\n");
545
546         return isfeasible() && promises->size() == 0;
547 }
548
549 /** Close out a RMWR by converting previous RMWR into a RMW or READ. */
550 ModelAction * ModelChecker::process_rmw(ModelAction *act) {
551         int tid = id_to_int(act->get_tid());
552         ModelAction *lastread = get_last_action(tid);
553         lastread->process_rmw(act);
554         if (act->is_rmw() && lastread->get_reads_from()!=NULL) {
555                 mo_graph->addRMWEdge(lastread->get_reads_from(), lastread);
556                 mo_graph->commitChanges();
557         }
558         return lastread;
559 }
560
561 /**
562  * Checks whether a thread has read from the same write for too many times
563  * without seeing the effects of a later write.
564  *
565  * Basic idea:
566  * 1) there must a different write that we could read from that would satisfy the modification order,
567  * 2) we must have read from the same value in excess of maxreads times, and
568  * 3) that other write must have been in the reads_from set for maxreads times.
569  *
570  * If so, we decide that the execution is no longer feasible.
571  */
572 void ModelChecker::check_recency(ModelAction *curr) {
573         if (params.maxreads != 0) {
574                 if (curr->get_node()->get_read_from_size() <= 1)
575                         return;
576
577                 //Must make sure that execution is currently feasible...  We could
578                 //accidentally clear by rolling back
579                 if (!isfeasible())
580                         return;
581
582                 std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(curr->get_location());
583                 int tid = id_to_int(curr->get_tid());
584
585                 /* Skip checks */
586                 if ((int)thrd_lists->size() <= tid)
587                         return;
588
589                 action_list_t *list = &(*thrd_lists)[tid];
590
591                 action_list_t::reverse_iterator rit = list->rbegin();
592                 /* Skip past curr */
593                 for (; (*rit) != curr; rit++)
594                         ;
595                 /* go past curr now */
596                 rit++;
597
598                 action_list_t::reverse_iterator ritcopy = rit;
599                 //See if we have enough reads from the same value
600                 int count = 0;
601                 for (; count < params.maxreads; rit++,count++) {
602                         if (rit==list->rend())
603                                 return;
604                         ModelAction *act = *rit;
605                         if (!act->is_read())
606                                 return;
607                         if (act->get_reads_from() != curr->get_reads_from())
608                                 return;
609                         if (act->get_node()->get_read_from_size() <= 1)
610                                 return;
611                 }
612
613                 for (int i = 0; i<curr->get_node()->get_read_from_size(); i++) {
614                         //Get write
615                         const ModelAction * write = curr->get_node()->get_read_from_at(i);
616                         //Need a different write
617                         if (write==curr->get_reads_from())
618                                 continue;
619
620                         /* Test to see whether this is a feasible write to read from*/
621                         mo_graph->startChanges();
622                         r_modification_order(curr, write);
623                         bool feasiblereadfrom = isfeasible();
624                         mo_graph->rollbackChanges();
625
626                         if (!feasiblereadfrom)
627                                 continue;
628                         rit = ritcopy;
629
630                         bool feasiblewrite = true;
631                         //new we need to see if this write works for everyone
632
633                         for (int loop = count; loop>0; loop--,rit++) {
634                                 ModelAction *act=*rit;
635                                 bool foundvalue = false;
636                                 for (int j = 0; j<act->get_node()->get_read_from_size(); j++) {
637                                         if (act->get_node()->get_read_from_at(i)==write) {
638                                                 foundvalue = true;
639                                                 break;
640                                         }
641                                 }
642                                 if (!foundvalue) {
643                                         feasiblewrite = false;
644                                         break;
645                                 }
646                         }
647                         if (feasiblewrite) {
648                                 too_many_reads = true;
649                                 return;
650                         }
651                 }
652         }
653 }
654
655 /**
656  * Updates the mo_graph with the constraints imposed from the current
657  * read.  
658  *
659  * Basic idea is the following: Go through each other thread and find
660  * the lastest action that happened before our read.  Two cases:
661  *
662  * (1) The action is a write => that write must either occur before
663  * the write we read from or be the write we read from.
664  *
665  * (2) The action is a read => the write that that action read from
666  * must occur before the write we read from or be the same write.
667  *
668  * @param curr The current action. Must be a read.
669  * @param rf The action that curr reads from. Must be a write.
670  * @return True if modification order edges were added; false otherwise
671  */
672 bool ModelChecker::r_modification_order(ModelAction *curr, const ModelAction *rf)
673 {
674         std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(curr->get_location());
675         unsigned int i;
676         bool added = false;
677         ASSERT(curr->is_read());
678
679         /* Iterate over all threads */
680         for (i = 0; i < thrd_lists->size(); i++) {
681                 /* Iterate over actions in thread, starting from most recent */
682                 action_list_t *list = &(*thrd_lists)[i];
683                 action_list_t::reverse_iterator rit;
684                 for (rit = list->rbegin(); rit != list->rend(); rit++) {
685                         ModelAction *act = *rit;
686
687                         /*
688                          * Include at most one act per-thread that "happens
689                          * before" curr. Don't consider reflexively.
690                          */
691                         if (act->happens_before(curr) && act != curr) {
692                                 if (act->is_write()) {
693                                         if (rf != act) {
694                                                 mo_graph->addEdge(act, rf);
695                                                 added = true;
696                                         }
697                                 } else {
698                                         const ModelAction *prevreadfrom = act->get_reads_from();
699                                         if (prevreadfrom != NULL && rf != prevreadfrom) {
700                                                 mo_graph->addEdge(prevreadfrom, rf);
701                                                 added = true;
702                                         }
703                                 }
704                                 break;
705                         }
706                 }
707         }
708
709         return added;
710 }
711
712 /** This method fixes up the modification order when we resolve a
713  *  promises.  The basic problem is that actions that occur after the
714  *  read curr could not property add items to the modification order
715  *  for our read.
716  *  
717  *  So for each thread, we find the earliest item that happens after
718  *  the read curr.  This is the item we have to fix up with additional
719  *  constraints.  If that action is write, we add a MO edge between
720  *  the Action rf and that action.  If the action is a read, we add a
721  *  MO edge between the Action rf, and whatever the read accessed.
722  *
723  * @param curr is the read ModelAction that we are fixing up MO edges for.
724  * @param rf is the write ModelAction that curr reads from.
725  *
726  */
727
728 void ModelChecker::post_r_modification_order(ModelAction *curr, const ModelAction *rf)
729 {
730         std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(curr->get_location());
731         unsigned int i;
732         ASSERT(curr->is_read());
733
734         /* Iterate over all threads */
735         for (i = 0; i < thrd_lists->size(); i++) {
736                 /* Iterate over actions in thread, starting from most recent */
737                 action_list_t *list = &(*thrd_lists)[i];
738                 action_list_t::reverse_iterator rit;
739                 ModelAction *lastact = NULL;
740
741                 /* Find last action that happens after curr */
742                 for (rit = list->rbegin(); rit != list->rend(); rit++) {
743                         ModelAction *act = *rit;
744                         if (curr->happens_before(act)) {
745                                 lastact = act;
746                         } else
747                                 break;
748                 }
749
750                         /* Include at most one act per-thread that "happens before" curr */
751                 if (lastact != NULL) {
752                         if (lastact->is_read()) {
753                                 const ModelAction *postreadfrom = lastact->get_reads_from();
754                                 if (postreadfrom != NULL&&rf != postreadfrom)
755                                         mo_graph->addEdge(rf, postreadfrom);
756                         } else if (rf != lastact) {
757                                 mo_graph->addEdge(rf, lastact);
758                         }
759                         break;
760                 }
761         }
762 }
763
764 /**
765  * Updates the mo_graph with the constraints imposed from the current write.
766  *
767  * Basic idea is the following: Go through each other thread and find
768  * the lastest action that happened before our write.  Two cases:
769  *
770  * (1) The action is a write => that write must occur before
771  * the current write
772  *
773  * (2) The action is a read => the write that that action read from
774  * must occur before the current write.
775  *
776  * This method also handles two other issues:
777  *
778  * (I) Sequential Consistency: Making sure that if the current write is
779  * seq_cst, that it occurs after the previous seq_cst write.
780  *
781  * (II) Sending the write back to non-synchronizing reads.
782  *
783  * @param curr The current action. Must be a write.
784  * @return True if modification order edges were added; false otherwise
785  */
786 bool ModelChecker::w_modification_order(ModelAction *curr)
787 {
788         std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(curr->get_location());
789         unsigned int i;
790         bool added = false;
791         ASSERT(curr->is_write());
792
793         if (curr->is_seqcst()) {
794                 /* We have to at least see the last sequentially consistent write,
795                          so we are initialized. */
796                 ModelAction *last_seq_cst = get_last_seq_cst(curr);
797                 if (last_seq_cst != NULL) {
798                         mo_graph->addEdge(last_seq_cst, curr);
799                         added = true;
800                 }
801         }
802
803         /* Iterate over all threads */
804         for (i = 0; i < thrd_lists->size(); i++) {
805                 /* Iterate over actions in thread, starting from most recent */
806                 action_list_t *list = &(*thrd_lists)[i];
807                 action_list_t::reverse_iterator rit;
808                 for (rit = list->rbegin(); rit != list->rend(); rit++) {
809                         ModelAction *act = *rit;
810                         if (act == curr) {
811                                 /*
812                                  * If RMW, we already have all relevant edges,
813                                  * so just skip to next thread.
814                                  * If normal write, we need to look at earlier
815                                  * actions, so continue processing list.
816                                  */
817                                 if (curr->is_rmw())
818                                         break;
819                                 else
820                                         continue;
821                         }
822
823                         /*
824                          * Include at most one act per-thread that "happens
825                          * before" curr
826                          */
827                         if (act->happens_before(curr)) {
828                                 /*
829                                  * Note: if act is RMW, just add edge:
830                                  *   act --mo--> curr
831                                  * The following edge should be handled elsewhere:
832                                  *   readfrom(act) --mo--> act
833                                  */
834                                 if (act->is_write())
835                                         mo_graph->addEdge(act, curr);
836                                 else if (act->is_read() && act->get_reads_from() != NULL)
837                                         mo_graph->addEdge(act->get_reads_from(), curr);
838                                 added = true;
839                                 break;
840                         } else if (act->is_read() && !act->is_synchronizing(curr) &&
841                                                      !act->same_thread(curr)) {
842                                 /* We have an action that:
843                                    (1) did not happen before us
844                                    (2) is a read and we are a write
845                                    (3) cannot synchronize with us
846                                    (4) is in a different thread
847                                    =>
848                                    that read could potentially read from our write.
849                                  */
850                                 if (thin_air_constraint_may_allow(curr, act)) {
851                                         if (isfeasible() ||
852                                                         (curr->is_rmw() && act->is_rmw() && curr->get_reads_from()==act->get_reads_from() && isfeasibleotherthanRMW())) {
853                                                 struct PendingFutureValue pfv = {curr->get_value(),curr->get_seq_number()+params.maxfuturedelay,act};
854                                                 futurevalues->push_back(pfv);
855                                         }
856                                 }
857                         }
858                 }
859         }
860
861         return added;
862 }
863
864 /** Arbitrary reads from the future are not allowed.  Section 29.3
865  * part 9 places some constraints.  This method checks one result of constraint
866  * constraint.  Others require compiler support. */
867
868 bool ModelChecker::thin_air_constraint_may_allow(const ModelAction * writer, const ModelAction *reader) {
869         if (!writer->is_rmw())
870                 return true;
871
872         if (!reader->is_rmw())
873                 return true;
874
875         for (const ModelAction *search = writer->get_reads_from(); search != NULL; search = search->get_reads_from()) {
876                 if (search==reader)
877                         return false;
878                 if (search->get_tid() == reader->get_tid() &&
879                                 search->happens_before(reader))
880                         break;
881         }
882
883         return true;
884 }
885
886 /**
887  * Finds the head(s) of the release sequence(s) containing a given ModelAction.
888  * The ModelAction under consideration is expected to be taking part in
889  * release/acquire synchronization as an object of the "reads from" relation.
890  * Note that this can only provide release sequence support for RMW chains
891  * which do not read from the future, as those actions cannot be traced until
892  * their "promise" is fulfilled. Similarly, we may not even establish the
893  * presence of a release sequence with certainty, as some modification order
894  * constraints may be decided further in the future. Thus, this function
895  * "returns" two pieces of data: a pass-by-reference vector of @a release_heads
896  * and a boolean representing certainty.
897  *
898  * @todo Finish lazy updating, when promises are fulfilled in the future
899  * @param rf The action that might be part of a release sequence. Must be a
900  * write.
901  * @param release_heads A pass-by-reference style return parameter.  After
902  * execution of this function, release_heads will contain the heads of all the
903  * relevant release sequences, if any exists
904  * @return true, if the ModelChecker is certain that release_heads is complete;
905  * false otherwise
906  */
907 bool ModelChecker::release_seq_head(const ModelAction *rf,
908                 std::vector< const ModelAction *, MyAlloc<const ModelAction *> > *release_heads) const
909 {
910         if (!rf) {
911                 /* read from future: need to settle this later */
912                 return false; /* incomplete */
913         }
914
915         ASSERT(rf->is_write());
916
917         if (rf->is_release())
918                 release_heads->push_back(rf);
919         if (rf->is_rmw()) {
920                 /* We need a RMW action that is both an acquire and release to stop */
921                 /** @todo Need to be smarter here...  In the linux lock
922                  * example, this will run to the beginning of the program for
923                  * every acquire. */
924                 if (rf->is_acquire() && rf->is_release())
925                         return true; /* complete */
926                 return release_seq_head(rf->get_reads_from(), release_heads);
927         }
928         if (rf->is_release())
929                 return true; /* complete */
930
931         /* else relaxed write; check modification order for contiguous subsequence
932          * -> rf must be same thread as release */
933         int tid = id_to_int(rf->get_tid());
934         std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(rf->get_location());
935         action_list_t *list = &(*thrd_lists)[tid];
936         action_list_t::const_reverse_iterator rit;
937
938         /* Find rf in the thread list */
939         rit = std::find(list->rbegin(), list->rend(), rf);
940         ASSERT(rit != list->rend());
941
942         /* Find the last write/release */
943         for (; rit != list->rend(); rit++)
944                 if ((*rit)->is_release())
945                         break;
946         if (rit == list->rend()) {
947                 /* No write-release in this thread */
948                 return true; /* complete */
949         }
950         ModelAction *release = *rit;
951
952         ASSERT(rf->same_thread(release));
953
954         bool certain = true;
955         for (unsigned int i = 0; i < thrd_lists->size(); i++) {
956                 if (id_to_int(rf->get_tid()) == (int)i)
957                         continue;
958                 list = &(*thrd_lists)[i];
959
960                 /* Can we ensure no future writes from this thread may break
961                  * the release seq? */
962                 bool future_ordered = false;
963
964                 for (rit = list->rbegin(); rit != list->rend(); rit++) {
965                         const ModelAction *act = *rit;
966                         if (!act->is_write())
967                                 continue;
968                         /* Reach synchronization -> this thread is complete */
969                         if (act->happens_before(release))
970                                 break;
971                         if (rf->happens_before(act)) {
972                                 future_ordered = true;
973                                 continue;
974                         }
975
976                         /* Check modification order */
977                         if (mo_graph->checkReachable(rf, act)) {
978                                 /* rf --mo--> act */
979                                 future_ordered = true;
980                                 continue;
981                         }
982                         if (mo_graph->checkReachable(act, release))
983                                 /* act --mo--> release */
984                                 break;
985                         if (mo_graph->checkReachable(release, act) &&
986                                       mo_graph->checkReachable(act, rf)) {
987                                 /* release --mo-> act --mo--> rf */
988                                 return true; /* complete */
989                         }
990                         certain = false;
991                 }
992                 if (!future_ordered)
993                         return false; /* This thread is uncertain */
994         }
995
996         if (certain)
997                 release_heads->push_back(release);
998         return certain;
999 }
1000
1001 /**
1002  * A public interface for getting the release sequence head(s) with which a
1003  * given ModelAction must synchronize. This function only returns a non-empty
1004  * result when it can locate a release sequence head with certainty. Otherwise,
1005  * it may mark the internal state of the ModelChecker so that it will handle
1006  * the release sequence at a later time, causing @a act to update its
1007  * synchronization at some later point in execution.
1008  * @param act The 'acquire' action that may read from a release sequence
1009  * @param release_heads A pass-by-reference return parameter. Will be filled
1010  * with the head(s) of the release sequence(s), if they exists with certainty.
1011  * @see ModelChecker::release_seq_head
1012  */
1013 void ModelChecker::get_release_seq_heads(ModelAction *act,
1014                 std::vector< const ModelAction *, MyAlloc<const ModelAction *> > *release_heads)
1015 {
1016         const ModelAction *rf = act->get_reads_from();
1017         bool complete;
1018         complete = release_seq_head(rf, release_heads);
1019         if (!complete) {
1020                 /* add act to 'lazy checking' list */
1021                 std::list<ModelAction *> *list;
1022                 list = lazy_sync_with_release->get_safe_ptr(act->get_location());
1023                 list->push_back(act);
1024                 (*lazy_sync_size)++;
1025         }
1026 }
1027
1028 /**
1029  * Attempt to resolve all stashed operations that might synchronize with a
1030  * release sequence for a given location. This implements the "lazy" portion of
1031  * determining whether or not a release sequence was contiguous, since not all
1032  * modification order information is present at the time an action occurs.
1033  *
1034  * @param location The location/object that should be checked for release
1035  * sequence resolutions
1036  * @param work_queue The work queue to which to add work items as they are
1037  * generated
1038  * @return True if any updates occurred (new synchronization, new mo_graph
1039  * edges)
1040  */
1041 bool ModelChecker::resolve_release_sequences(void *location, work_queue_t *work_queue)
1042 {
1043         std::list<ModelAction *> *list;
1044         list = lazy_sync_with_release->getptr(location);
1045         if (!list)
1046                 return false;
1047
1048         bool updated = false;
1049         std::list<ModelAction *>::iterator it = list->begin();
1050         while (it != list->end()) {
1051                 ModelAction *act = *it;
1052                 const ModelAction *rf = act->get_reads_from();
1053                 std::vector< const ModelAction *, MyAlloc<const ModelAction *> > release_heads;
1054                 bool complete;
1055                 complete = release_seq_head(rf, &release_heads);
1056                 for (unsigned int i = 0; i < release_heads.size(); i++) {
1057                         if (!act->has_synchronized_with(release_heads[i])) {
1058                                 updated = true;
1059                                 act->synchronize_with(release_heads[i]);
1060                         }
1061                 }
1062
1063                 if (updated) {
1064                         /* Re-check act for mo_graph edges */
1065                         work_queue->push_back(MOEdgeWorkEntry(act));
1066
1067                         /* propagate synchronization to later actions */
1068                         action_list_t::reverse_iterator it = action_trace->rbegin();
1069                         while ((*it) != act) {
1070                                 ModelAction *propagate = *it;
1071                                 if (act->happens_before(propagate)) {
1072                                         propagate->synchronize_with(act);
1073                                         /* Re-check 'propagate' for mo_graph edges */
1074                                         work_queue->push_back(MOEdgeWorkEntry(propagate));
1075                                 }
1076                         }
1077                 }
1078                 if (complete) {
1079                         it = list->erase(it);
1080                         (*lazy_sync_size)--;
1081                 } else
1082                         it++;
1083         }
1084
1085         // If we resolved promises or data races, see if we have realized a data race.
1086         if (checkDataRaces()) {
1087                 set_assert();
1088         }
1089
1090         return updated;
1091 }
1092
1093 /**
1094  * Performs various bookkeeping operations for the current ModelAction. For
1095  * instance, adds action to the per-object, per-thread action vector and to the
1096  * action trace list of all thread actions.
1097  *
1098  * @param act is the ModelAction to add.
1099  */
1100 void ModelChecker::add_action_to_lists(ModelAction *act)
1101 {
1102         int tid = id_to_int(act->get_tid());
1103         action_trace->push_back(act);
1104
1105         obj_map->get_safe_ptr(act->get_location())->push_back(act);
1106
1107         std::vector<action_list_t> *vec = obj_thrd_map->get_safe_ptr(act->get_location());
1108         if (tid >= (int)vec->size())
1109                 vec->resize(priv->next_thread_id);
1110         (*vec)[tid].push_back(act);
1111
1112         if ((int)thrd_last_action->size() <= tid)
1113                 thrd_last_action->resize(get_num_threads());
1114         (*thrd_last_action)[tid] = act;
1115 }
1116
1117 ModelAction * ModelChecker::get_last_action(thread_id_t tid)
1118 {
1119         int nthreads = get_num_threads();
1120         if ((int)thrd_last_action->size() < nthreads)
1121                 thrd_last_action->resize(nthreads);
1122         return (*thrd_last_action)[id_to_int(tid)];
1123 }
1124
1125 /**
1126  * Gets the last memory_order_seq_cst write (in the total global sequence)
1127  * performed on a particular object (i.e., memory location), not including the
1128  * current action.
1129  * @param curr The current ModelAction; also denotes the object location to
1130  * check
1131  * @return The last seq_cst write
1132  */
1133 ModelAction * ModelChecker::get_last_seq_cst(ModelAction *curr)
1134 {
1135         void *location = curr->get_location();
1136         action_list_t *list = obj_map->get_safe_ptr(location);
1137         /* Find: max({i in dom(S) | seq_cst(t_i) && isWrite(t_i) && samevar(t_i, t)}) */
1138         action_list_t::reverse_iterator rit;
1139         for (rit = list->rbegin(); rit != list->rend(); rit++)
1140                 if ((*rit)->is_write() && (*rit)->is_seqcst() && (*rit) != curr)
1141                         return *rit;
1142         return NULL;
1143 }
1144
1145 ModelAction * ModelChecker::get_parent_action(thread_id_t tid)
1146 {
1147         ModelAction *parent = get_last_action(tid);
1148         if (!parent)
1149                 parent = get_thread(tid)->get_creation();
1150         return parent;
1151 }
1152
1153 /**
1154  * Returns the clock vector for a given thread.
1155  * @param tid The thread whose clock vector we want
1156  * @return Desired clock vector
1157  */
1158 ClockVector * ModelChecker::get_cv(thread_id_t tid)
1159 {
1160         return get_parent_action(tid)->get_cv();
1161 }
1162
1163 /**
1164  * Resolve a set of Promises with a current write. The set is provided in the
1165  * Node corresponding to @a write.
1166  * @param write The ModelAction that is fulfilling Promises
1167  * @return True if promises were resolved; false otherwise
1168  */
1169 bool ModelChecker::resolve_promises(ModelAction *write)
1170 {
1171         bool resolved = false;
1172
1173         for (unsigned int i = 0, promise_index = 0; promise_index < promises->size(); i++) {
1174                 Promise *promise = (*promises)[promise_index];
1175                 if (write->get_node()->get_promise(i)) {
1176                         ModelAction *read = promise->get_action();
1177                         read->read_from(write);
1178                         if (read->is_rmw()) {
1179                                 mo_graph->addRMWEdge(write, read);
1180                         }
1181                         //First fix up the modification order for actions that happened
1182                         //before the read
1183                         r_modification_order(read, write);
1184                         //Next fix up the modification order for actions that happened
1185                         //after the read.
1186                         post_r_modification_order(read, write);
1187                         promises->erase(promises->begin() + promise_index);
1188                         resolved = true;
1189                 } else
1190                         promise_index++;
1191         }
1192         return resolved;
1193 }
1194
1195 /**
1196  * Compute the set of promises that could potentially be satisfied by this
1197  * action. Note that the set computation actually appears in the Node, not in
1198  * ModelChecker.
1199  * @param curr The ModelAction that may satisfy promises
1200  */
1201 void ModelChecker::compute_promises(ModelAction *curr)
1202 {
1203         for (unsigned int i = 0; i < promises->size(); i++) {
1204                 Promise *promise = (*promises)[i];
1205                 const ModelAction *act = promise->get_action();
1206                 if (!act->happens_before(curr) &&
1207                                 act->is_read() &&
1208                                 !act->is_synchronizing(curr) &&
1209                                 !act->same_thread(curr) &&
1210                                 promise->get_value() == curr->get_value()) {
1211                         curr->get_node()->set_promise(i);
1212                 }
1213         }
1214 }
1215
1216 /** Checks promises in response to change in ClockVector Threads. */
1217 void ModelChecker::check_promises(ClockVector *old_cv, ClockVector *merge_cv)
1218 {
1219         for (unsigned int i = 0; i < promises->size(); i++) {
1220                 Promise *promise = (*promises)[i];
1221                 const ModelAction *act = promise->get_action();
1222                 if ((old_cv == NULL || !old_cv->synchronized_since(act)) &&
1223                                 merge_cv->synchronized_since(act)) {
1224                         //This thread is no longer able to send values back to satisfy the promise
1225                         int num_synchronized_threads = promise->increment_threads();
1226                         if (num_synchronized_threads == get_num_threads()) {
1227                                 //Promise has failed
1228                                 failed_promise = true;
1229                                 return;
1230                         }
1231                 }
1232         }
1233 }
1234
1235 /**
1236  * Build up an initial set of all past writes that this 'read' action may read
1237  * from. This set is determined by the clock vector's "happens before"
1238  * relationship.
1239  * @param curr is the current ModelAction that we are exploring; it must be a
1240  * 'read' operation.
1241  */
1242 void ModelChecker::build_reads_from_past(ModelAction *curr)
1243 {
1244         std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(curr->get_location());
1245         unsigned int i;
1246         ASSERT(curr->is_read());
1247
1248         ModelAction *last_seq_cst = NULL;
1249
1250         /* Track whether this object has been initialized */
1251         bool initialized = false;
1252
1253         if (curr->is_seqcst()) {
1254                 last_seq_cst = get_last_seq_cst(curr);
1255                 /* We have to at least see the last sequentially consistent write,
1256                          so we are initialized. */
1257                 if (last_seq_cst != NULL)
1258                         initialized = true;
1259         }
1260
1261         /* Iterate over all threads */
1262         for (i = 0; i < thrd_lists->size(); i++) {
1263                 /* Iterate over actions in thread, starting from most recent */
1264                 action_list_t *list = &(*thrd_lists)[i];
1265                 action_list_t::reverse_iterator rit;
1266                 for (rit = list->rbegin(); rit != list->rend(); rit++) {
1267                         ModelAction *act = *rit;
1268
1269                         /* Only consider 'write' actions */
1270                         if (!act->is_write() || act == curr)
1271                                 continue;
1272
1273                         /* Don't consider more than one seq_cst write if we are a seq_cst read. */
1274                         if (!curr->is_seqcst()|| (!act->is_seqcst() && (last_seq_cst==NULL||!act->happens_before(last_seq_cst))) || act == last_seq_cst) {
1275                                 DEBUG("Adding action to may_read_from:\n");
1276                                 if (DBG_ENABLED()) {
1277                                         act->print();
1278                                         curr->print();
1279                                 }
1280                                 curr->get_node()->add_read_from(act);
1281                         }
1282
1283                         /* Include at most one act per-thread that "happens before" curr */
1284                         if (act->happens_before(curr)) {
1285                                 initialized = true;
1286                                 break;
1287                         }
1288                 }
1289         }
1290
1291         if (!initialized) {
1292                 /** @todo Need a more informative way of reporting errors. */
1293                 printf("ERROR: may read from uninitialized atomic\n");
1294         }
1295
1296         if (DBG_ENABLED() || !initialized) {
1297                 printf("Reached read action:\n");
1298                 curr->print();
1299                 printf("Printing may_read_from\n");
1300                 curr->get_node()->print_may_read_from();
1301                 printf("End printing may_read_from\n");
1302         }
1303
1304         ASSERT(initialized);
1305 }
1306
1307 static void print_list(action_list_t *list)
1308 {
1309         action_list_t::iterator it;
1310
1311         printf("---------------------------------------------------------------------\n");
1312         printf("Trace:\n");
1313
1314         for (it = list->begin(); it != list->end(); it++) {
1315                 (*it)->print();
1316         }
1317         printf("---------------------------------------------------------------------\n");
1318 }
1319
1320 void ModelChecker::print_summary()
1321 {
1322         printf("\n");
1323         printf("Number of executions: %d\n", num_executions);
1324         printf("Number of feasible executions: %d\n", num_feasible_executions);
1325         printf("Total nodes created: %d\n", node_stack->get_total_nodes());
1326
1327 #if SUPPORT_MOD_ORDER_DUMP
1328         scheduler->print();
1329         char buffername[100];
1330         sprintf(buffername, "exec%u",num_executions);
1331         mo_graph->dumpGraphToFile(buffername);
1332 #endif
1333
1334         if (!isfinalfeasible())
1335                 printf("INFEASIBLE EXECUTION!\n");
1336         print_list(action_trace);
1337         printf("\n");
1338 }
1339
1340 /**
1341  * Add a Thread to the system for the first time. Should only be called once
1342  * per thread.
1343  * @param t The Thread to add
1344  */
1345 void ModelChecker::add_thread(Thread *t)
1346 {
1347         thread_map->put(id_to_int(t->get_id()), t);
1348         scheduler->add_thread(t);
1349 }
1350
1351 void ModelChecker::remove_thread(Thread *t)
1352 {
1353         scheduler->remove_thread(t);
1354 }
1355
1356 /**
1357  * Switch from a user-context to the "master thread" context (a.k.a. system
1358  * context). This switch is made with the intention of exploring a particular
1359  * model-checking action (described by a ModelAction object). Must be called
1360  * from a user-thread context.
1361  * @param act The current action that will be explored. Must not be NULL.
1362  * @return Return status from the 'swap' call (i.e., success/fail, 0/-1)
1363  */
1364 int ModelChecker::switch_to_master(ModelAction *act)
1365 {
1366         DBG();
1367         Thread *old = thread_current();
1368         set_current_action(act);
1369         old->set_state(THREAD_READY);
1370         return Thread::swap(old, &system_context);
1371 }
1372
1373 /**
1374  * Takes the next step in the execution, if possible.
1375  * @return Returns true (success) if a step was taken and false otherwise.
1376  */
1377 bool ModelChecker::take_step() {
1378         Thread *curr, *next;
1379
1380         if (has_asserted())
1381                 return false;
1382
1383         curr = thread_current();
1384         if (curr) {
1385                 if (curr->get_state() == THREAD_READY) {
1386                         ASSERT(priv->current_action);
1387
1388                         priv->nextThread = check_current_action(priv->current_action);
1389                         priv->current_action = NULL;
1390                         if (!curr->is_blocked() && !curr->is_complete())
1391                                 scheduler->add_thread(curr);
1392                 } else {
1393                         ASSERT(false);
1394                 }
1395         }
1396         next = scheduler->next_thread(priv->nextThread);
1397
1398         /* Infeasible -> don't take any more steps */
1399         if (!isfeasible())
1400                 return false;
1401
1402         if (next)
1403                 next->set_state(THREAD_RUNNING);
1404         DEBUG("(%d, %d)\n", curr ? curr->get_id() : -1, next ? next->get_id() : -1);
1405
1406         /* next == NULL -> don't take any more steps */
1407         if (!next)
1408                 return false;
1409         /* Return false only if swap fails with an error */
1410         return (Thread::swap(&system_context, next) == 0);
1411 }
1412
1413 /** Runs the current execution until threre are no more steps to take. */
1414 void ModelChecker::finish_execution() {
1415         DBG();
1416
1417         while (take_step());
1418 }