model: move init code to ModelChecker::initialize_curr_action
[model-checker.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         if (curr->is_rmwc() || curr->is_rmw()) {
339                 ModelAction *tmp = process_rmw(curr);
340                 delete curr;
341                 curr = tmp;
342                 compute_promises(curr);
343         } else {
344                 ModelAction *tmp = node_stack->explore_action(curr);
345                 if (tmp) {
346                         /* Discard duplicate ModelAction; use action from NodeStack */
347                         /* First restore type and order in case of RMW operation */
348                         if (curr->is_rmwr())
349                                 tmp->copy_typeandorder(curr);
350
351                         /* If we have diverged, we need to reset the clock vector. */
352                         if (diverge == NULL)
353                                 tmp->create_cv(get_parent_action(tmp->get_tid()));
354
355                         delete curr;
356                         curr = tmp;
357                 } else {
358                         /*
359                          * Perform one-time actions when pushing new ModelAction onto
360                          * NodeStack
361                          */
362                         curr->create_cv(get_parent_action(curr->get_tid()));
363                         /* Build may_read_from set */
364                         if (curr->is_read())
365                                 build_reads_from_past(curr);
366                         if (curr->is_write())
367                                 compute_promises(curr);
368                 }
369         }
370         return curr;
371 }
372
373 /**
374  * This is the heart of the model checker routine. It performs model-checking
375  * actions corresponding to a given "current action." Among other processes, it
376  * calculates reads-from relationships, updates synchronization clock vectors,
377  * forms a memory_order constraints graph, and handles replay/backtrack
378  * execution when running permutations of previously-observed executions.
379  *
380  * @param curr The current action to process
381  * @return The next Thread that must be executed. May be NULL if ModelChecker
382  * makes no choice (e.g., according to replay execution, combining RMW actions,
383  * etc.)
384  */
385 Thread * ModelChecker::check_current_action(ModelAction *curr)
386 {
387         ASSERT(curr);
388
389         bool second_part_of_rmw = curr->is_rmwc() || curr->is_rmw();
390
391         curr = initialize_curr_action(curr);
392
393         /* Thread specific actions */
394         switch (curr->get_type()) {
395         case THREAD_CREATE: {
396                 Thread *th = (Thread *)curr->get_location();
397                 th->set_creation(curr);
398                 break;
399         }
400         case THREAD_JOIN: {
401                 Thread *waiting, *blocking;
402                 waiting = get_thread(curr);
403                 blocking = (Thread *)curr->get_location();
404                 if (!blocking->is_complete()) {
405                         blocking->push_wait_list(curr);
406                         scheduler->sleep(waiting);
407                 }
408                 break;
409         }
410         case THREAD_FINISH: {
411                 Thread *th = get_thread(curr);
412                 while (!th->wait_list_empty()) {
413                         ModelAction *act = th->pop_wait_list();
414                         Thread *wake = get_thread(act);
415                         scheduler->wake(wake);
416                 }
417                 th->complete();
418                 break;
419         }
420         case THREAD_START: {
421                 check_promises(NULL, curr->get_cv());
422                 break;
423         }
424         default:
425                 break;
426         }
427
428         /* Add current action to lists before work_queue loop */
429         if (!second_part_of_rmw)
430                 add_action_to_lists(curr);
431
432         work_queue_t work_queue(1, CheckCurrWorkEntry(curr));
433
434         while (!work_queue.empty()) {
435                 WorkQueueEntry work = work_queue.front();
436                 work_queue.pop_front();
437
438                 switch (work.type) {
439                 case WORK_CHECK_CURR_ACTION: {
440                         ModelAction *act = work.action;
441                         bool updated = false;
442                         if (act->is_read() && process_read(act, second_part_of_rmw))
443                                 updated = true;
444
445                         if (act->is_write() && process_write(act))
446                                 updated = true;
447
448                         if (updated)
449                                 work_queue.push_back(CheckRelSeqWorkEntry(act->get_location()));
450                         break;
451                 }
452                 case WORK_CHECK_RELEASE_SEQ:
453                         resolve_release_sequences(work.location, &work_queue);
454                         break;
455                 case WORK_CHECK_MO_EDGES: {
456                         /** @todo Complete verification of work_queue */
457                         ModelAction *act = work.action;
458                         bool updated = false;
459
460                         if (act->is_read()) {
461                                 if (r_modification_order(act, act->get_reads_from()))
462                                         updated = true;
463                         }
464                         if (act->is_write()) {
465                                 if (w_modification_order(act))
466                                         updated = true;
467                         }
468
469                         if (updated)
470                                 work_queue.push_back(CheckRelSeqWorkEntry(act->get_location()));
471                         break;
472                 }
473                 default:
474                         ASSERT(false);
475                         break;
476                 }
477         }
478
479         check_curr_backtracking(curr);
480
481         set_backtracking(curr);
482
483         return get_next_thread(curr);
484 }
485
486 void ModelChecker::check_curr_backtracking(ModelAction * curr) {
487         Node *currnode = curr->get_node();
488         Node *parnode = currnode->get_parent();
489
490         if ((!parnode->backtrack_empty() ||
491                          !currnode->read_from_empty() ||
492                          !currnode->future_value_empty() ||
493                          !currnode->promise_empty())
494                         && (!priv->next_backtrack ||
495                                         *curr > *priv->next_backtrack)) {
496                 priv->next_backtrack = curr;
497         }
498 }
499
500 bool ModelChecker::promises_expired() {
501         for (unsigned int promise_index = 0; promise_index < promises->size(); promise_index++) {
502                 Promise *promise = (*promises)[promise_index];
503                 if (promise->get_expiration()<priv->used_sequence_numbers) {
504                         return true;
505                 }
506         }
507         return false;
508 }
509
510 /** @returns whether the current partial trace must be a prefix of a
511  * feasible trace. */
512 bool ModelChecker::isfeasibleprefix() {
513         return promises->size() == 0 && *lazy_sync_size == 0;
514 }
515
516 /** @returns whether the current partial trace is feasible. */
517 bool ModelChecker::isfeasible() {
518         return !mo_graph->checkForRMWViolation() && isfeasibleotherthanRMW();
519 }
520
521 /** @returns whether the current partial trace is feasible other than
522  * multiple RMW reading from the same store. */
523 bool ModelChecker::isfeasibleotherthanRMW() {
524         if (DBG_ENABLED()) {
525                 if (mo_graph->checkForCycles())
526                         DEBUG("Infeasible: modification order cycles\n");
527                 if (failed_promise)
528                         DEBUG("Infeasible: failed promise\n");
529                 if (too_many_reads)
530                         DEBUG("Infeasible: too many reads\n");
531                 if (promises_expired())
532                         DEBUG("Infeasible: promises expired\n");
533         }
534         return !mo_graph->checkForCycles() && !failed_promise && !too_many_reads && !promises_expired();
535 }
536
537 /** Returns whether the current completed trace is feasible. */
538 bool ModelChecker::isfinalfeasible() {
539         if (DBG_ENABLED() && promises->size() != 0)
540                 DEBUG("Infeasible: unrevolved promises\n");
541
542         return isfeasible() && promises->size() == 0;
543 }
544
545 /** Close out a RMWR by converting previous RMWR into a RMW or READ. */
546 ModelAction * ModelChecker::process_rmw(ModelAction *act) {
547         int tid = id_to_int(act->get_tid());
548         ModelAction *lastread = get_last_action(tid);
549         lastread->process_rmw(act);
550         if (act->is_rmw() && lastread->get_reads_from()!=NULL) {
551                 mo_graph->addRMWEdge(lastread->get_reads_from(), lastread);
552                 mo_graph->commitChanges();
553         }
554         return lastread;
555 }
556
557 /**
558  * Checks whether a thread has read from the same write for too many times
559  * without seeing the effects of a later write.
560  *
561  * Basic idea:
562  * 1) there must a different write that we could read from that would satisfy the modification order,
563  * 2) we must have read from the same value in excess of maxreads times, and
564  * 3) that other write must have been in the reads_from set for maxreads times.
565  *
566  * If so, we decide that the execution is no longer feasible.
567  */
568 void ModelChecker::check_recency(ModelAction *curr) {
569         if (params.maxreads != 0) {
570                 if (curr->get_node()->get_read_from_size() <= 1)
571                         return;
572
573                 //Must make sure that execution is currently feasible...  We could
574                 //accidentally clear by rolling back
575                 if (!isfeasible())
576                         return;
577
578                 std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(curr->get_location());
579                 int tid = id_to_int(curr->get_tid());
580
581                 /* Skip checks */
582                 if ((int)thrd_lists->size() <= tid)
583                         return;
584
585                 action_list_t *list = &(*thrd_lists)[tid];
586
587                 action_list_t::reverse_iterator rit = list->rbegin();
588                 /* Skip past curr */
589                 for (; (*rit) != curr; rit++)
590                         ;
591                 /* go past curr now */
592                 rit++;
593
594                 action_list_t::reverse_iterator ritcopy = rit;
595                 //See if we have enough reads from the same value
596                 int count = 0;
597                 for (; count < params.maxreads; rit++,count++) {
598                         if (rit==list->rend())
599                                 return;
600                         ModelAction *act = *rit;
601                         if (!act->is_read())
602                                 return;
603                         if (act->get_reads_from() != curr->get_reads_from())
604                                 return;
605                         if (act->get_node()->get_read_from_size() <= 1)
606                                 return;
607                 }
608
609                 for (int i = 0; i<curr->get_node()->get_read_from_size(); i++) {
610                         //Get write
611                         const ModelAction * write = curr->get_node()->get_read_from_at(i);
612                         //Need a different write
613                         if (write==curr->get_reads_from())
614                                 continue;
615
616                         /* Test to see whether this is a feasible write to read from*/
617                         mo_graph->startChanges();
618                         r_modification_order(curr, write);
619                         bool feasiblereadfrom = isfeasible();
620                         mo_graph->rollbackChanges();
621
622                         if (!feasiblereadfrom)
623                                 continue;
624                         rit = ritcopy;
625
626                         bool feasiblewrite = true;
627                         //new we need to see if this write works for everyone
628
629                         for (int loop = count; loop>0; loop--,rit++) {
630                                 ModelAction *act=*rit;
631                                 bool foundvalue = false;
632                                 for (int j = 0; j<act->get_node()->get_read_from_size(); j++) {
633                                         if (act->get_node()->get_read_from_at(i)==write) {
634                                                 foundvalue = true;
635                                                 break;
636                                         }
637                                 }
638                                 if (!foundvalue) {
639                                         feasiblewrite = false;
640                                         break;
641                                 }
642                         }
643                         if (feasiblewrite) {
644                                 too_many_reads = true;
645                                 return;
646                         }
647                 }
648         }
649 }
650
651 /**
652  * Updates the mo_graph with the constraints imposed from the current
653  * read.  
654  *
655  * Basic idea is the following: Go through each other thread and find
656  * the lastest action that happened before our read.  Two cases:
657  *
658  * (1) The action is a write => that write must either occur before
659  * the write we read from or be the write we read from.
660  *
661  * (2) The action is a read => the write that that action read from
662  * must occur before the write we read from or be the same write.
663  *
664  * @param curr The current action. Must be a read.
665  * @param rf The action that curr reads from. Must be a write.
666  * @return True if modification order edges were added; false otherwise
667  */
668 bool ModelChecker::r_modification_order(ModelAction *curr, const ModelAction *rf)
669 {
670         std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(curr->get_location());
671         unsigned int i;
672         bool added = false;
673         ASSERT(curr->is_read());
674
675         /* Iterate over all threads */
676         for (i = 0; i < thrd_lists->size(); i++) {
677                 /* Iterate over actions in thread, starting from most recent */
678                 action_list_t *list = &(*thrd_lists)[i];
679                 action_list_t::reverse_iterator rit;
680                 for (rit = list->rbegin(); rit != list->rend(); rit++) {
681                         ModelAction *act = *rit;
682
683                         /*
684                          * Include at most one act per-thread that "happens
685                          * before" curr. Don't consider reflexively.
686                          */
687                         if (act->happens_before(curr) && act != curr) {
688                                 if (act->is_write()) {
689                                         if (rf != act) {
690                                                 mo_graph->addEdge(act, rf);
691                                                 added = true;
692                                         }
693                                 } else {
694                                         const ModelAction *prevreadfrom = act->get_reads_from();
695                                         if (prevreadfrom != NULL && rf != prevreadfrom) {
696                                                 mo_graph->addEdge(prevreadfrom, rf);
697                                                 added = true;
698                                         }
699                                 }
700                                 break;
701                         }
702                 }
703         }
704
705         return added;
706 }
707
708 /** This method fixes up the modification order when we resolve a
709  *  promises.  The basic problem is that actions that occur after the
710  *  read curr could not property add items to the modification order
711  *  for our read.
712  *  
713  *  So for each thread, we find the earliest item that happens after
714  *  the read curr.  This is the item we have to fix up with additional
715  *  constraints.  If that action is write, we add a MO edge between
716  *  the Action rf and that action.  If the action is a read, we add a
717  *  MO edge between the Action rf, and whatever the read accessed.
718  *
719  * @param curr is the read ModelAction that we are fixing up MO edges for.
720  * @param rf is the write ModelAction that curr reads from.
721  *
722  */
723
724 void ModelChecker::post_r_modification_order(ModelAction *curr, const ModelAction *rf)
725 {
726         std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(curr->get_location());
727         unsigned int i;
728         ASSERT(curr->is_read());
729
730         /* Iterate over all threads */
731         for (i = 0; i < thrd_lists->size(); i++) {
732                 /* Iterate over actions in thread, starting from most recent */
733                 action_list_t *list = &(*thrd_lists)[i];
734                 action_list_t::reverse_iterator rit;
735                 ModelAction *lastact = NULL;
736
737                 /* Find last action that happens after curr */
738                 for (rit = list->rbegin(); rit != list->rend(); rit++) {
739                         ModelAction *act = *rit;
740                         if (curr->happens_before(act)) {
741                                 lastact = act;
742                         } else
743                                 break;
744                 }
745
746                         /* Include at most one act per-thread that "happens before" curr */
747                 if (lastact != NULL) {
748                         if (lastact->is_read()) {
749                                 const ModelAction *postreadfrom = lastact->get_reads_from();
750                                 if (postreadfrom != NULL&&rf != postreadfrom)
751                                         mo_graph->addEdge(rf, postreadfrom);
752                         } else if (rf != lastact) {
753                                 mo_graph->addEdge(rf, lastact);
754                         }
755                         break;
756                 }
757         }
758 }
759
760 /**
761  * Updates the mo_graph with the constraints imposed from the current write.
762  *
763  * Basic idea is the following: Go through each other thread and find
764  * the lastest action that happened before our write.  Two cases:
765  *
766  * (1) The action is a write => that write must occur before
767  * the current write
768  *
769  * (2) The action is a read => the write that that action read from
770  * must occur before the current write.
771  *
772  * This method also handles two other issues:
773  *
774  * (I) Sequential Consistency: Making sure that if the current write is
775  * seq_cst, that it occurs after the previous seq_cst write.
776  *
777  * (II) Sending the write back to non-synchronizing reads.
778  *
779  * @param curr The current action. Must be a write.
780  * @return True if modification order edges were added; false otherwise
781  */
782 bool ModelChecker::w_modification_order(ModelAction *curr)
783 {
784         std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(curr->get_location());
785         unsigned int i;
786         bool added = false;
787         ASSERT(curr->is_write());
788
789         if (curr->is_seqcst()) {
790                 /* We have to at least see the last sequentially consistent write,
791                          so we are initialized. */
792                 ModelAction *last_seq_cst = get_last_seq_cst(curr);
793                 if (last_seq_cst != NULL) {
794                         mo_graph->addEdge(last_seq_cst, curr);
795                         added = true;
796                 }
797         }
798
799         /* Iterate over all threads */
800         for (i = 0; i < thrd_lists->size(); i++) {
801                 /* Iterate over actions in thread, starting from most recent */
802                 action_list_t *list = &(*thrd_lists)[i];
803                 action_list_t::reverse_iterator rit;
804                 for (rit = list->rbegin(); rit != list->rend(); rit++) {
805                         ModelAction *act = *rit;
806                         if (act == curr) {
807                                 /*
808                                  * If RMW, we already have all relevant edges,
809                                  * so just skip to next thread.
810                                  * If normal write, we need to look at earlier
811                                  * actions, so continue processing list.
812                                  */
813                                 if (curr->is_rmw())
814                                         break;
815                                 else
816                                         continue;
817                         }
818
819                         /*
820                          * Include at most one act per-thread that "happens
821                          * before" curr
822                          */
823                         if (act->happens_before(curr)) {
824                                 /*
825                                  * Note: if act is RMW, just add edge:
826                                  *   act --mo--> curr
827                                  * The following edge should be handled elsewhere:
828                                  *   readfrom(act) --mo--> act
829                                  */
830                                 if (act->is_write())
831                                         mo_graph->addEdge(act, curr);
832                                 else if (act->is_read() && act->get_reads_from() != NULL)
833                                         mo_graph->addEdge(act->get_reads_from(), curr);
834                                 added = true;
835                                 break;
836                         } else if (act->is_read() && !act->is_synchronizing(curr) &&
837                                                      !act->same_thread(curr)) {
838                                 /* We have an action that:
839                                    (1) did not happen before us
840                                    (2) is a read and we are a write
841                                    (3) cannot synchronize with us
842                                    (4) is in a different thread
843                                    =>
844                                    that read could potentially read from our write.
845                                  */
846                                 if (thin_air_constraint_may_allow(curr, act)) {
847                                         if (isfeasible() ||
848                                                         (curr->is_rmw() && act->is_rmw() && curr->get_reads_from()==act->get_reads_from() && isfeasibleotherthanRMW())) {
849                                                 struct PendingFutureValue pfv = {curr->get_value(),curr->get_seq_number()+params.maxfuturedelay,act};
850                                                 futurevalues->push_back(pfv);
851                                         }
852                                 }
853                         }
854                 }
855         }
856
857         return added;
858 }
859
860 /** Arbitrary reads from the future are not allowed.  Section 29.3
861  * part 9 places some constraints.  This method checks one result of constraint
862  * constraint.  Others require compiler support. */
863
864 bool ModelChecker::thin_air_constraint_may_allow(const ModelAction * writer, const ModelAction *reader) {
865         if (!writer->is_rmw())
866                 return true;
867
868         if (!reader->is_rmw())
869                 return true;
870
871         for (const ModelAction *search = writer->get_reads_from(); search != NULL; search = search->get_reads_from()) {
872                 if (search==reader)
873                         return false;
874                 if (search->get_tid() == reader->get_tid() &&
875                                 search->happens_before(reader))
876                         break;
877         }
878
879         return true;
880 }
881
882 /**
883  * Finds the head(s) of the release sequence(s) containing a given ModelAction.
884  * The ModelAction under consideration is expected to be taking part in
885  * release/acquire synchronization as an object of the "reads from" relation.
886  * Note that this can only provide release sequence support for RMW chains
887  * which do not read from the future, as those actions cannot be traced until
888  * their "promise" is fulfilled. Similarly, we may not even establish the
889  * presence of a release sequence with certainty, as some modification order
890  * constraints may be decided further in the future. Thus, this function
891  * "returns" two pieces of data: a pass-by-reference vector of @a release_heads
892  * and a boolean representing certainty.
893  *
894  * @todo Finish lazy updating, when promises are fulfilled in the future
895  * @param rf The action that might be part of a release sequence. Must be a
896  * write.
897  * @param release_heads A pass-by-reference style return parameter.  After
898  * execution of this function, release_heads will contain the heads of all the
899  * relevant release sequences, if any exists
900  * @return true, if the ModelChecker is certain that release_heads is complete;
901  * false otherwise
902  */
903 bool ModelChecker::release_seq_head(const ModelAction *rf,
904                 std::vector< const ModelAction *, MyAlloc<const ModelAction *> > *release_heads) const
905 {
906         if (!rf) {
907                 /* read from future: need to settle this later */
908                 return false; /* incomplete */
909         }
910
911         ASSERT(rf->is_write());
912
913         if (rf->is_release())
914                 release_heads->push_back(rf);
915         if (rf->is_rmw()) {
916                 /* We need a RMW action that is both an acquire and release to stop */
917                 /** @todo Need to be smarter here...  In the linux lock
918                  * example, this will run to the beginning of the program for
919                  * every acquire. */
920                 if (rf->is_acquire() && rf->is_release())
921                         return true; /* complete */
922                 return release_seq_head(rf->get_reads_from(), release_heads);
923         }
924         if (rf->is_release())
925                 return true; /* complete */
926
927         /* else relaxed write; check modification order for contiguous subsequence
928          * -> rf must be same thread as release */
929         int tid = id_to_int(rf->get_tid());
930         std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(rf->get_location());
931         action_list_t *list = &(*thrd_lists)[tid];
932         action_list_t::const_reverse_iterator rit;
933
934         /* Find rf in the thread list */
935         rit = std::find(list->rbegin(), list->rend(), rf);
936         ASSERT(rit != list->rend());
937
938         /* Find the last write/release */
939         for (; rit != list->rend(); rit++)
940                 if ((*rit)->is_release())
941                         break;
942         if (rit == list->rend()) {
943                 /* No write-release in this thread */
944                 return true; /* complete */
945         }
946         ModelAction *release = *rit;
947
948         ASSERT(rf->same_thread(release));
949
950         bool certain = true;
951         for (unsigned int i = 0; i < thrd_lists->size(); i++) {
952                 if (id_to_int(rf->get_tid()) == (int)i)
953                         continue;
954                 list = &(*thrd_lists)[i];
955
956                 /* Can we ensure no future writes from this thread may break
957                  * the release seq? */
958                 bool future_ordered = false;
959
960                 for (rit = list->rbegin(); rit != list->rend(); rit++) {
961                         const ModelAction *act = *rit;
962                         if (!act->is_write())
963                                 continue;
964                         /* Reach synchronization -> this thread is complete */
965                         if (act->happens_before(release))
966                                 break;
967                         if (rf->happens_before(act)) {
968                                 future_ordered = true;
969                                 continue;
970                         }
971
972                         /* Check modification order */
973                         if (mo_graph->checkReachable(rf, act)) {
974                                 /* rf --mo--> act */
975                                 future_ordered = true;
976                                 continue;
977                         }
978                         if (mo_graph->checkReachable(act, release))
979                                 /* act --mo--> release */
980                                 break;
981                         if (mo_graph->checkReachable(release, act) &&
982                                       mo_graph->checkReachable(act, rf)) {
983                                 /* release --mo-> act --mo--> rf */
984                                 return true; /* complete */
985                         }
986                         certain = false;
987                 }
988                 if (!future_ordered)
989                         return false; /* This thread is uncertain */
990         }
991
992         if (certain)
993                 release_heads->push_back(release);
994         return certain;
995 }
996
997 /**
998  * A public interface for getting the release sequence head(s) with which a
999  * given ModelAction must synchronize. This function only returns a non-empty
1000  * result when it can locate a release sequence head with certainty. Otherwise,
1001  * it may mark the internal state of the ModelChecker so that it will handle
1002  * the release sequence at a later time, causing @a act to update its
1003  * synchronization at some later point in execution.
1004  * @param act The 'acquire' action that may read from a release sequence
1005  * @param release_heads A pass-by-reference return parameter. Will be filled
1006  * with the head(s) of the release sequence(s), if they exists with certainty.
1007  * @see ModelChecker::release_seq_head
1008  */
1009 void ModelChecker::get_release_seq_heads(ModelAction *act,
1010                 std::vector< const ModelAction *, MyAlloc<const ModelAction *> > *release_heads)
1011 {
1012         const ModelAction *rf = act->get_reads_from();
1013         bool complete;
1014         complete = release_seq_head(rf, release_heads);
1015         if (!complete) {
1016                 /* add act to 'lazy checking' list */
1017                 std::list<ModelAction *> *list;
1018                 list = lazy_sync_with_release->get_safe_ptr(act->get_location());
1019                 list->push_back(act);
1020                 (*lazy_sync_size)++;
1021         }
1022 }
1023
1024 /**
1025  * Attempt to resolve all stashed operations that might synchronize with a
1026  * release sequence for a given location. This implements the "lazy" portion of
1027  * determining whether or not a release sequence was contiguous, since not all
1028  * modification order information is present at the time an action occurs.
1029  *
1030  * @param location The location/object that should be checked for release
1031  * sequence resolutions
1032  * @param work_queue The work queue to which to add work items as they are
1033  * generated
1034  * @return True if any updates occurred (new synchronization, new mo_graph
1035  * edges)
1036  */
1037 bool ModelChecker::resolve_release_sequences(void *location, work_queue_t *work_queue)
1038 {
1039         std::list<ModelAction *> *list;
1040         list = lazy_sync_with_release->getptr(location);
1041         if (!list)
1042                 return false;
1043
1044         bool updated = false;
1045         std::list<ModelAction *>::iterator it = list->begin();
1046         while (it != list->end()) {
1047                 ModelAction *act = *it;
1048                 const ModelAction *rf = act->get_reads_from();
1049                 std::vector< const ModelAction *, MyAlloc<const ModelAction *> > release_heads;
1050                 bool complete;
1051                 complete = release_seq_head(rf, &release_heads);
1052                 for (unsigned int i = 0; i < release_heads.size(); i++) {
1053                         if (!act->has_synchronized_with(release_heads[i])) {
1054                                 updated = true;
1055                                 act->synchronize_with(release_heads[i]);
1056                         }
1057                 }
1058
1059                 if (updated) {
1060                         /* Re-check act for mo_graph edges */
1061                         work_queue->push_back(MOEdgeWorkEntry(act));
1062
1063                         /* propagate synchronization to later actions */
1064                         action_list_t::reverse_iterator it = action_trace->rbegin();
1065                         while ((*it) != act) {
1066                                 ModelAction *propagate = *it;
1067                                 if (act->happens_before(propagate)) {
1068                                         propagate->synchronize_with(act);
1069                                         /* Re-check 'propagate' for mo_graph edges */
1070                                         work_queue->push_back(MOEdgeWorkEntry(propagate));
1071                                 }
1072                         }
1073                 }
1074                 if (complete) {
1075                         it = list->erase(it);
1076                         (*lazy_sync_size)--;
1077                 } else
1078                         it++;
1079         }
1080
1081         // If we resolved promises or data races, see if we have realized a data race.
1082         if (checkDataRaces()) {
1083                 set_assert();
1084         }
1085
1086         return updated;
1087 }
1088
1089 /**
1090  * Performs various bookkeeping operations for the current ModelAction. For
1091  * instance, adds action to the per-object, per-thread action vector and to the
1092  * action trace list of all thread actions.
1093  *
1094  * @param act is the ModelAction to add.
1095  */
1096 void ModelChecker::add_action_to_lists(ModelAction *act)
1097 {
1098         int tid = id_to_int(act->get_tid());
1099         action_trace->push_back(act);
1100
1101         obj_map->get_safe_ptr(act->get_location())->push_back(act);
1102
1103         std::vector<action_list_t> *vec = obj_thrd_map->get_safe_ptr(act->get_location());
1104         if (tid >= (int)vec->size())
1105                 vec->resize(priv->next_thread_id);
1106         (*vec)[tid].push_back(act);
1107
1108         if ((int)thrd_last_action->size() <= tid)
1109                 thrd_last_action->resize(get_num_threads());
1110         (*thrd_last_action)[tid] = act;
1111 }
1112
1113 ModelAction * ModelChecker::get_last_action(thread_id_t tid)
1114 {
1115         int nthreads = get_num_threads();
1116         if ((int)thrd_last_action->size() < nthreads)
1117                 thrd_last_action->resize(nthreads);
1118         return (*thrd_last_action)[id_to_int(tid)];
1119 }
1120
1121 /**
1122  * Gets the last memory_order_seq_cst write (in the total global sequence)
1123  * performed on a particular object (i.e., memory location), not including the
1124  * current action.
1125  * @param curr The current ModelAction; also denotes the object location to
1126  * check
1127  * @return The last seq_cst write
1128  */
1129 ModelAction * ModelChecker::get_last_seq_cst(ModelAction *curr)
1130 {
1131         void *location = curr->get_location();
1132         action_list_t *list = obj_map->get_safe_ptr(location);
1133         /* Find: max({i in dom(S) | seq_cst(t_i) && isWrite(t_i) && samevar(t_i, t)}) */
1134         action_list_t::reverse_iterator rit;
1135         for (rit = list->rbegin(); rit != list->rend(); rit++)
1136                 if ((*rit)->is_write() && (*rit)->is_seqcst() && (*rit) != curr)
1137                         return *rit;
1138         return NULL;
1139 }
1140
1141 ModelAction * ModelChecker::get_parent_action(thread_id_t tid)
1142 {
1143         ModelAction *parent = get_last_action(tid);
1144         if (!parent)
1145                 parent = get_thread(tid)->get_creation();
1146         return parent;
1147 }
1148
1149 /**
1150  * Returns the clock vector for a given thread.
1151  * @param tid The thread whose clock vector we want
1152  * @return Desired clock vector
1153  */
1154 ClockVector * ModelChecker::get_cv(thread_id_t tid)
1155 {
1156         return get_parent_action(tid)->get_cv();
1157 }
1158
1159 /**
1160  * Resolve a set of Promises with a current write. The set is provided in the
1161  * Node corresponding to @a write.
1162  * @param write The ModelAction that is fulfilling Promises
1163  * @return True if promises were resolved; false otherwise
1164  */
1165 bool ModelChecker::resolve_promises(ModelAction *write)
1166 {
1167         bool resolved = false;
1168
1169         for (unsigned int i = 0, promise_index = 0; promise_index < promises->size(); i++) {
1170                 Promise *promise = (*promises)[promise_index];
1171                 if (write->get_node()->get_promise(i)) {
1172                         ModelAction *read = promise->get_action();
1173                         read->read_from(write);
1174                         if (read->is_rmw()) {
1175                                 mo_graph->addRMWEdge(write, read);
1176                         }
1177                         //First fix up the modification order for actions that happened
1178                         //before the read
1179                         r_modification_order(read, write);
1180                         //Next fix up the modification order for actions that happened
1181                         //after the read.
1182                         post_r_modification_order(read, write);
1183                         promises->erase(promises->begin() + promise_index);
1184                         resolved = true;
1185                 } else
1186                         promise_index++;
1187         }
1188         return resolved;
1189 }
1190
1191 /**
1192  * Compute the set of promises that could potentially be satisfied by this
1193  * action. Note that the set computation actually appears in the Node, not in
1194  * ModelChecker.
1195  * @param curr The ModelAction that may satisfy promises
1196  */
1197 void ModelChecker::compute_promises(ModelAction *curr)
1198 {
1199         for (unsigned int i = 0; i < promises->size(); i++) {
1200                 Promise *promise = (*promises)[i];
1201                 const ModelAction *act = promise->get_action();
1202                 if (!act->happens_before(curr) &&
1203                                 act->is_read() &&
1204                                 !act->is_synchronizing(curr) &&
1205                                 !act->same_thread(curr) &&
1206                                 promise->get_value() == curr->get_value()) {
1207                         curr->get_node()->set_promise(i);
1208                 }
1209         }
1210 }
1211
1212 /** Checks promises in response to change in ClockVector Threads. */
1213 void ModelChecker::check_promises(ClockVector *old_cv, ClockVector *merge_cv)
1214 {
1215         for (unsigned int i = 0; i < promises->size(); i++) {
1216                 Promise *promise = (*promises)[i];
1217                 const ModelAction *act = promise->get_action();
1218                 if ((old_cv == NULL || !old_cv->synchronized_since(act)) &&
1219                                 merge_cv->synchronized_since(act)) {
1220                         //This thread is no longer able to send values back to satisfy the promise
1221                         int num_synchronized_threads = promise->increment_threads();
1222                         if (num_synchronized_threads == get_num_threads()) {
1223                                 //Promise has failed
1224                                 failed_promise = true;
1225                                 return;
1226                         }
1227                 }
1228         }
1229 }
1230
1231 /**
1232  * Build up an initial set of all past writes that this 'read' action may read
1233  * from. This set is determined by the clock vector's "happens before"
1234  * relationship.
1235  * @param curr is the current ModelAction that we are exploring; it must be a
1236  * 'read' operation.
1237  */
1238 void ModelChecker::build_reads_from_past(ModelAction *curr)
1239 {
1240         std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(curr->get_location());
1241         unsigned int i;
1242         ASSERT(curr->is_read());
1243
1244         ModelAction *last_seq_cst = NULL;
1245
1246         /* Track whether this object has been initialized */
1247         bool initialized = false;
1248
1249         if (curr->is_seqcst()) {
1250                 last_seq_cst = get_last_seq_cst(curr);
1251                 /* We have to at least see the last sequentially consistent write,
1252                          so we are initialized. */
1253                 if (last_seq_cst != NULL)
1254                         initialized = true;
1255         }
1256
1257         /* Iterate over all threads */
1258         for (i = 0; i < thrd_lists->size(); i++) {
1259                 /* Iterate over actions in thread, starting from most recent */
1260                 action_list_t *list = &(*thrd_lists)[i];
1261                 action_list_t::reverse_iterator rit;
1262                 for (rit = list->rbegin(); rit != list->rend(); rit++) {
1263                         ModelAction *act = *rit;
1264
1265                         /* Only consider 'write' actions */
1266                         if (!act->is_write())
1267                                 continue;
1268
1269                         /* Don't consider more than one seq_cst write if we are a seq_cst read. */
1270                         if (!curr->is_seqcst()|| (!act->is_seqcst() && (last_seq_cst==NULL||!act->happens_before(last_seq_cst))) || act == last_seq_cst) {
1271                                 DEBUG("Adding action to may_read_from:\n");
1272                                 if (DBG_ENABLED()) {
1273                                         act->print();
1274                                         curr->print();
1275                                 }
1276                                 curr->get_node()->add_read_from(act);
1277                         }
1278
1279                         /* Include at most one act per-thread that "happens before" curr */
1280                         if (act->happens_before(curr)) {
1281                                 initialized = true;
1282                                 break;
1283                         }
1284                 }
1285         }
1286
1287         if (!initialized) {
1288                 /** @todo Need a more informative way of reporting errors. */
1289                 printf("ERROR: may read from uninitialized atomic\n");
1290         }
1291
1292         if (DBG_ENABLED() || !initialized) {
1293                 printf("Reached read action:\n");
1294                 curr->print();
1295                 printf("Printing may_read_from\n");
1296                 curr->get_node()->print_may_read_from();
1297                 printf("End printing may_read_from\n");
1298         }
1299
1300         ASSERT(initialized);
1301 }
1302
1303 static void print_list(action_list_t *list)
1304 {
1305         action_list_t::iterator it;
1306
1307         printf("---------------------------------------------------------------------\n");
1308         printf("Trace:\n");
1309
1310         for (it = list->begin(); it != list->end(); it++) {
1311                 (*it)->print();
1312         }
1313         printf("---------------------------------------------------------------------\n");
1314 }
1315
1316 void ModelChecker::print_summary()
1317 {
1318         printf("\n");
1319         printf("Number of executions: %d\n", num_executions);
1320         printf("Number of feasible executions: %d\n", num_feasible_executions);
1321         printf("Total nodes created: %d\n", node_stack->get_total_nodes());
1322
1323 #if SUPPORT_MOD_ORDER_DUMP
1324         scheduler->print();
1325         char buffername[100];
1326         sprintf(buffername, "exec%u",num_executions);
1327         mo_graph->dumpGraphToFile(buffername);
1328 #endif
1329
1330         if (!isfinalfeasible())
1331                 printf("INFEASIBLE EXECUTION!\n");
1332         print_list(action_trace);
1333         printf("\n");
1334 }
1335
1336 /**
1337  * Add a Thread to the system for the first time. Should only be called once
1338  * per thread.
1339  * @param t The Thread to add
1340  */
1341 void ModelChecker::add_thread(Thread *t)
1342 {
1343         thread_map->put(id_to_int(t->get_id()), t);
1344         scheduler->add_thread(t);
1345 }
1346
1347 void ModelChecker::remove_thread(Thread *t)
1348 {
1349         scheduler->remove_thread(t);
1350 }
1351
1352 /**
1353  * Switch from a user-context to the "master thread" context (a.k.a. system
1354  * context). This switch is made with the intention of exploring a particular
1355  * model-checking action (described by a ModelAction object). Must be called
1356  * from a user-thread context.
1357  * @param act The current action that will be explored. Must not be NULL.
1358  * @return Return status from the 'swap' call (i.e., success/fail, 0/-1)
1359  */
1360 int ModelChecker::switch_to_master(ModelAction *act)
1361 {
1362         DBG();
1363         Thread *old = thread_current();
1364         set_current_action(act);
1365         old->set_state(THREAD_READY);
1366         return Thread::swap(old, &system_context);
1367 }
1368
1369 /**
1370  * Takes the next step in the execution, if possible.
1371  * @return Returns true (success) if a step was taken and false otherwise.
1372  */
1373 bool ModelChecker::take_step() {
1374         Thread *curr, *next;
1375
1376         if (has_asserted())
1377                 return false;
1378
1379         curr = thread_current();
1380         if (curr) {
1381                 if (curr->get_state() == THREAD_READY) {
1382                         ASSERT(priv->current_action);
1383
1384                         priv->nextThread = check_current_action(priv->current_action);
1385                         priv->current_action = NULL;
1386                         if (!curr->is_blocked() && !curr->is_complete())
1387                                 scheduler->add_thread(curr);
1388                 } else {
1389                         ASSERT(false);
1390                 }
1391         }
1392         next = scheduler->next_thread(priv->nextThread);
1393
1394         /* Infeasible -> don't take any more steps */
1395         if (!isfeasible())
1396                 return false;
1397
1398         if (next)
1399                 next->set_state(THREAD_RUNNING);
1400         DEBUG("(%d, %d)\n", curr ? curr->get_id() : -1, next ? next->get_id() : -1);
1401
1402         /* next == NULL -> don't take any more steps */
1403         if (!next)
1404                 return false;
1405         /* Return false only if swap fails with an error */
1406         return (Thread::swap(&system_context, next) == 0);
1407 }
1408
1409 /** Runs the current execution until threre are no more steps to take. */
1410 void ModelChecker::finish_execution() {
1411         DBG();
1412
1413         while (take_step());
1414 }