add basic parameter handling
[c11tester.git] / model.cc
1 #include <stdio.h>
2
3 #include "model.h"
4 #include "action.h"
5 #include "nodestack.h"
6 #include "schedule.h"
7 #include "snapshot-interface.h"
8 #include "common.h"
9 #include "clockvector.h"
10 #include "cyclegraph.h"
11 #include "promise.h"
12
13 #define INITIAL_THREAD_ID       0
14
15 ModelChecker *model;
16
17 /** @brief Constructor */
18 ModelChecker::ModelChecker(struct model_params params) :
19         /* Initialize default scheduler */
20         scheduler(new Scheduler()),
21         /* First thread created will have id INITIAL_THREAD_ID */
22         next_thread_id(INITIAL_THREAD_ID),
23         used_sequence_numbers(0),
24         num_executions(0),
25         params(params),
26         current_action(NULL),
27         diverge(NULL),
28         nextThread(THREAD_ID_T_NONE),
29         action_trace(new action_list_t()),
30         thread_map(new HashTable<int, Thread *, int>()),
31         obj_map(new HashTable<const void *, action_list_t, uintptr_t, 4>()),
32         obj_thrd_map(new HashTable<void *, std::vector<action_list_t>, uintptr_t, 4 >()),
33         promises(new std::vector<Promise *>()),
34         thrd_last_action(new std::vector<ModelAction *>(1)),
35         node_stack(new NodeStack()),
36         next_backtrack(NULL),
37         cyclegraph(new CycleGraph()),
38         failed_promise(false)
39 {
40 }
41
42 /** @brief Destructor */
43 ModelChecker::~ModelChecker()
44 {
45         for (int i = 0; i < get_num_threads(); i++)
46                 delete thread_map->get(i);
47         delete thread_map;
48
49         delete obj_thrd_map;
50         delete obj_map;
51         delete action_trace;
52         delete thrd_last_action;
53         delete node_stack;
54         delete scheduler;
55         delete cyclegraph;
56 }
57
58 /**
59  * Restores user program to initial state and resets all model-checker data
60  * structures.
61  */
62 void ModelChecker::reset_to_initial_state()
63 {
64         DEBUG("+++ Resetting to initial state +++\n");
65         node_stack->reset_execution();
66         current_action = NULL;
67         next_thread_id = INITIAL_THREAD_ID;
68         used_sequence_numbers = 0;
69         nextThread = 0;
70         next_backtrack = NULL;
71         failed_promise = false;
72         snapshotObject->backTrackBeforeStep(0);
73 }
74
75 /** @returns a thread ID for a new Thread */
76 thread_id_t ModelChecker::get_next_id()
77 {
78         return next_thread_id++;
79 }
80
81 /** @returns the number of user threads created during this execution */
82 int ModelChecker::get_num_threads()
83 {
84         return next_thread_id;
85 }
86
87 /** @returns a sequence number for a new ModelAction */
88 modelclock_t ModelChecker::get_next_seq_num()
89 {
90         return ++used_sequence_numbers;
91 }
92
93 /**
94  * Performs the "scheduling" for the model-checker. That is, it checks if the
95  * model-checker has selected a "next thread to run" and returns it, if
96  * available. This function should be called from the Scheduler routine, where
97  * the Scheduler falls back to a default scheduling routine if needed.
98  *
99  * @return The next thread chosen by the model-checker. If the model-checker
100  * makes no selection, retuns NULL.
101  */
102 Thread * ModelChecker::schedule_next_thread()
103 {
104         Thread *t;
105         if (nextThread == THREAD_ID_T_NONE)
106                 return NULL;
107         t = thread_map->get(id_to_int(nextThread));
108
109         ASSERT(t != NULL);
110
111         return t;
112 }
113
114 /**
115  * Choose the next thread in the replay sequence.
116  *
117  * If the replay sequence has reached the 'diverge' point, returns a thread
118  * from the backtracking set. Otherwise, simply returns the next thread in the
119  * sequence that is being replayed.
120  */
121 thread_id_t ModelChecker::get_next_replay_thread()
122 {
123         thread_id_t tid;
124
125         /* Have we completed exploring the preselected path? */
126         if (diverge == NULL)
127                 return THREAD_ID_T_NONE;
128
129         /* Else, we are trying to replay an execution */
130         ModelAction * next = node_stack->get_next()->get_action();
131
132         if (next == diverge) {
133                 Node *nextnode = next->get_node();
134                 /* Reached divergence point */
135                 if (nextnode->increment_promise()) {
136                         /* The next node will try to satisfy a different set of promises. */
137                         tid = next->get_tid();
138                         node_stack->pop_restofstack(2);
139                 } else if (nextnode->increment_read_from()) {
140                         /* The next node will read from a different value. */
141                         tid = next->get_tid();
142                         node_stack->pop_restofstack(2);
143                 } else if (nextnode->increment_future_value()) {
144                         /* The next node will try to read from a different future value. */
145                         tid = next->get_tid();
146                         node_stack->pop_restofstack(2);
147                 } else {
148                         /* Make a different thread execute for next step */
149                         Node *node = nextnode->get_parent();
150                         tid = node->get_next_backtrack();
151                         node_stack->pop_restofstack(1);
152                 }
153                 DEBUG("*** Divergence point ***\n");
154                 diverge = NULL;
155         } else {
156                 tid = next->get_tid();
157         }
158         DEBUG("*** ModelChecker chose next thread = %d ***\n", tid);
159         return tid;
160 }
161
162 /**
163  * Queries the model-checker for more executions to explore and, if one
164  * exists, resets the model-checker state to execute a new execution.
165  *
166  * @return If there are more executions to explore, return true. Otherwise,
167  * return false.
168  */
169 bool ModelChecker::next_execution()
170 {
171         DBG();
172
173         num_executions++;
174
175         bool feasible = isfinalfeasible();
176         if (feasible || DBG_ENABLED())
177                 print_summary(feasible);
178
179         if ((diverge = model->get_next_backtrack()) == NULL)
180                 return false;
181
182         if (DBG_ENABLED()) {
183                 printf("Next execution will diverge at:\n");
184                 diverge->print();
185         }
186
187         model->reset_to_initial_state();
188         return true;
189 }
190
191 ModelAction * ModelChecker::get_last_conflict(ModelAction *act)
192 {
193         action_type type = act->get_type();
194
195         switch (type) {
196                 case ATOMIC_READ:
197                 case ATOMIC_WRITE:
198                 case ATOMIC_RMW:
199                         break;
200                 default:
201                         return NULL;
202         }
203         /* linear search: from most recent to oldest */
204         action_list_t *list = obj_map->ensureptr(act->get_location());
205         action_list_t::reverse_iterator rit;
206         for (rit = list->rbegin(); rit != list->rend(); rit++) {
207                 ModelAction *prev = *rit;
208                 if (act->is_synchronizing(prev))
209                         return prev;
210         }
211         return NULL;
212 }
213
214 void ModelChecker::set_backtracking(ModelAction *act)
215 {
216         ModelAction *prev;
217         Node *node;
218         Thread *t = get_thread(act->get_tid());
219
220         prev = get_last_conflict(act);
221         if (prev == NULL)
222                 return;
223
224         node = prev->get_node()->get_parent();
225
226         while (!node->is_enabled(t))
227                 t = t->get_parent();
228
229         /* Check if this has been explored already */
230         if (node->has_been_explored(t->get_id()))
231                 return;
232
233         /* Cache the latest backtracking point */
234         if (!next_backtrack || *prev > *next_backtrack)
235                 next_backtrack = prev;
236
237         /* If this is a new backtracking point, mark the tree */
238         if (!node->set_backtrack(t->get_id()))
239                 return;
240         DEBUG("Setting backtrack: conflict = %d, instead tid = %d\n",
241                         prev->get_tid(), t->get_id());
242         if (DBG_ENABLED()) {
243                 prev->print();
244                 act->print();
245         }
246 }
247
248 /**
249  * Returns last backtracking point. The model checker will explore a different
250  * path for this point in the next execution.
251  * @return The ModelAction at which the next execution should diverge.
252  */
253 ModelAction * ModelChecker::get_next_backtrack()
254 {
255         ModelAction *next = next_backtrack;
256         next_backtrack = NULL;
257         return next;
258 }
259
260 void ModelChecker::check_current_action(void)
261 {
262         ModelAction *curr = this->current_action;
263         bool already_added = false;
264         this->current_action = NULL;
265         if (!curr) {
266                 DEBUG("trying to push NULL action...\n");
267                 return;
268         }
269
270         if (curr->is_rmwc() || curr->is_rmw()) {
271                 ModelAction *tmp = process_rmw(curr);
272                 already_added = true;
273                 delete curr;
274                 curr = tmp;
275         } else {
276                 ModelAction * tmp = node_stack->explore_action(curr);
277                 if (tmp) {
278                         /* Discard duplicate ModelAction; use action from NodeStack */
279                         /* First restore type and order in case of RMW operation */
280                         if (curr->is_rmwr())
281                                 tmp->copy_typeandorder(curr);
282
283                         /* If we have diverged, we need to reset the clock vector. */
284                         if (diverge == NULL)
285                                 tmp->create_cv(get_parent_action(tmp->get_tid()));
286
287                         delete curr;
288                         curr = tmp;
289                 } else {
290                         /*
291                          * Perform one-time actions when pushing new ModelAction onto
292                          * NodeStack
293                          */
294                         curr->create_cv(get_parent_action(curr->get_tid()));
295                         /* Build may_read_from set */
296                         if (curr->is_read())
297                                 build_reads_from_past(curr);
298                         if (curr->is_write())
299                                 compute_promises(curr);
300                 }
301         }
302
303         /* Assign 'creation' parent */
304         if (curr->get_type() == THREAD_CREATE) {
305                 Thread *th = (Thread *)curr->get_location();
306                 th->set_creation(curr);
307         }
308
309         /* Deal with new thread */
310         if (curr->get_type() == THREAD_START)
311                 check_promises(NULL, curr->get_cv());
312
313         /* Assign reads_from values */
314         Thread *th = get_thread(curr->get_tid());
315         uint64_t value = VALUE_NONE;
316         if (curr->is_read()) {
317                 const ModelAction *reads_from = curr->get_node()->get_read_from();
318                 if (reads_from != NULL) {
319                         value = reads_from->get_value();
320                         /* Assign reads_from, perform release/acquire synchronization */
321                         curr->read_from(reads_from);
322                         r_modification_order(curr,reads_from);
323                 } else {
324                         /* Read from future value */
325                         value = curr->get_node()->get_future_value();
326                         curr->read_from(NULL);
327                         Promise * valuepromise = new Promise(curr, value);
328                         promises->push_back(valuepromise);
329                 }
330         } else if (curr->is_write()) {
331                 w_modification_order(curr);
332                 resolve_promises(curr);
333         }
334
335         th->set_return_value(value);
336
337         /* Add action to list.  */
338         if (!already_added)
339                 add_action_to_lists(curr);
340
341         /** @todo Is there a better interface for setting the next thread rather
342                  than this field/convoluted approach?  Perhaps like just returning
343                  it or something? */
344
345         /* Do not split atomic actions. */
346         if (curr->is_rmwr())
347                 nextThread = thread_current()->get_id();
348         else
349                 nextThread = get_next_replay_thread();
350
351         Node *currnode = curr->get_node();
352         Node *parnode = currnode->get_parent();
353
354         if (!parnode->backtrack_empty() || !currnode->read_from_empty() ||
355                   !currnode->future_value_empty() || !currnode->promise_empty())
356                 if (!next_backtrack || *curr > *next_backtrack)
357                         next_backtrack = curr;
358
359         set_backtracking(curr);
360 }
361
362 /** @returns whether the current partial trace is feasible. */
363 bool ModelChecker::isfeasible() {
364         return !cyclegraph->checkForCycles() && !failed_promise;
365 }
366
367 /** Returns whether the current completed trace is feasible. */
368 bool ModelChecker::isfinalfeasible() {
369         return isfeasible() && promises->size() == 0;
370 }
371
372 /** Close out a RMWR by converting previous RMWR into a RMW or READ. */
373 ModelAction * ModelChecker::process_rmw(ModelAction * act) {
374         int tid = id_to_int(act->get_tid());
375         ModelAction *lastread = get_last_action(tid);
376         lastread->process_rmw(act);
377         if (act->is_rmw())
378                 cyclegraph->addRMWEdge(lastread, lastread->get_reads_from());
379         return lastread;
380 }
381
382 /**
383  * Updates the cyclegraph with the constraints imposed from the current read.
384  * @param curr The current action. Must be a read.
385  * @param rf The action that curr reads from. Must be a write.
386  */
387 void ModelChecker::r_modification_order(ModelAction * curr, const ModelAction *rf) {
388         std::vector<action_list_t> *thrd_lists = obj_thrd_map->ensureptr(curr->get_location());
389         unsigned int i;
390         ASSERT(curr->is_read());
391
392         /* Iterate over all threads */
393         for (i = 0; i < thrd_lists->size(); i++) {
394                 /* Iterate over actions in thread, starting from most recent */
395                 action_list_t *list = &(*thrd_lists)[i];
396                 action_list_t::reverse_iterator rit;
397                 for (rit = list->rbegin(); rit != list->rend(); rit++) {
398                         ModelAction *act = *rit;
399
400                         /* Include at most one act per-thread that "happens before" curr */
401                         if (act->happens_before(curr)) {
402                                 if (act->is_read()) {
403                                         const ModelAction * prevreadfrom = act->get_reads_from();
404                                         if (prevreadfrom != NULL && rf != prevreadfrom)
405                                                 cyclegraph->addEdge(rf, prevreadfrom);
406                                 } else if (rf != act) {
407                                         cyclegraph->addEdge(rf, act);
408                                 }
409                                 break;
410                         }
411                 }
412         }
413 }
414
415 /** Updates the cyclegraph with the constraints imposed from the
416  *  current read. */
417 void ModelChecker::post_r_modification_order(ModelAction * curr, const ModelAction *rf) {
418         std::vector<action_list_t> *thrd_lists = obj_thrd_map->ensureptr(curr->get_location());
419         unsigned int i;
420         ASSERT(curr->is_read());
421
422         /* Iterate over all threads */
423         for (i = 0; i < thrd_lists->size(); i++) {
424                 /* Iterate over actions in thread, starting from most recent */
425                 action_list_t *list = &(*thrd_lists)[i];
426                 action_list_t::reverse_iterator rit;
427                 ModelAction *lastact = NULL;
428
429                 /* Find last action that happens after curr */
430                 for (rit = list->rbegin(); rit != list->rend(); rit++) {
431                         ModelAction *act = *rit;
432                         if (curr->happens_before(act)) {
433                                 lastact = act;
434                         } else
435                                 break;
436                 }
437
438                         /* Include at most one act per-thread that "happens before" curr */
439                 if (lastact != NULL) {
440                         if (lastact->is_read()) {
441                                 const ModelAction * postreadfrom = lastact->get_reads_from();
442                                 if (postreadfrom != NULL&&rf != postreadfrom)
443                                         cyclegraph->addEdge(postreadfrom, rf);
444                         } else if (rf != lastact) {
445                                 cyclegraph->addEdge(lastact, rf);
446                         }
447                         break;
448                 }
449         }
450 }
451
452 /**
453  * Updates the cyclegraph with the constraints imposed from the current write.
454  * @param curr The current action. Must be a write.
455  */
456 void ModelChecker::w_modification_order(ModelAction * curr) {
457         std::vector<action_list_t> *thrd_lists = obj_thrd_map->ensureptr(curr->get_location());
458         unsigned int i;
459         ASSERT(curr->is_write());
460
461         if (curr->is_seqcst()) {
462                 /* We have to at least see the last sequentially consistent write,
463                          so we are initialized. */
464                 ModelAction * last_seq_cst = get_last_seq_cst(curr->get_location());
465                 if (last_seq_cst != NULL)
466                         cyclegraph->addEdge(curr, last_seq_cst);
467         }
468
469         /* Iterate over all threads */
470         for (i = 0; i < thrd_lists->size(); i++) {
471                 /* Iterate over actions in thread, starting from most recent */
472                 action_list_t *list = &(*thrd_lists)[i];
473                 action_list_t::reverse_iterator rit;
474                 for (rit = list->rbegin(); rit != list->rend(); rit++) {
475                         ModelAction *act = *rit;
476
477                         /* Include at most one act per-thread that "happens before" curr */
478                         if (act->happens_before(curr)) {
479                                 if (act->is_read()) {
480                                         cyclegraph->addEdge(curr, act->get_reads_from());
481                                 } else
482                                         cyclegraph->addEdge(curr, act);
483                                 break;
484                         } else {
485                                 if (act->is_read()&&!act->is_synchronizing(curr)&&!act->same_thread(curr)) {
486                                         /* We have an action that:
487                                                  (1) did not happen before us
488                                                  (2) is a read and we are a write
489                                                  (3) cannot synchronize with us
490                                                  (4) is in a different thread
491                                                  =>
492                                                  that read could potentially read from our write.
493                                         */
494                                         if (act->get_node()->add_future_value(curr->get_value())&&
495                                                         (!next_backtrack || *act > * next_backtrack))
496                                                 next_backtrack = act;
497                                 }
498                         }
499                 }
500         }
501 }
502
503 /**
504  * Performs various bookkeeping operations for the current ModelAction. For
505  * instance, adds action to the per-object, per-thread action vector and to the
506  * action trace list of all thread actions.
507  *
508  * @param act is the ModelAction to add.
509  */
510 void ModelChecker::add_action_to_lists(ModelAction *act)
511 {
512         int tid = id_to_int(act->get_tid());
513         action_trace->push_back(act);
514
515         obj_map->ensureptr(act->get_location())->push_back(act);
516
517         std::vector<action_list_t> *vec = obj_thrd_map->ensureptr(act->get_location());
518         if (tid >= (int)vec->size())
519                 vec->resize(next_thread_id);
520         (*vec)[tid].push_back(act);
521
522         if ((int)thrd_last_action->size() <= tid)
523                 thrd_last_action->resize(get_num_threads());
524         (*thrd_last_action)[tid] = act;
525 }
526
527 ModelAction * ModelChecker::get_last_action(thread_id_t tid)
528 {
529         int nthreads = get_num_threads();
530         if ((int)thrd_last_action->size() < nthreads)
531                 thrd_last_action->resize(nthreads);
532         return (*thrd_last_action)[id_to_int(tid)];
533 }
534
535 /**
536  * Gets the last memory_order_seq_cst action (in the total global sequence)
537  * performed on a particular object (i.e., memory location).
538  * @param location The object location to check
539  * @return The last seq_cst action performed
540  */
541 ModelAction * ModelChecker::get_last_seq_cst(const void *location)
542 {
543         action_list_t *list = obj_map->ensureptr(location);
544         /* Find: max({i in dom(S) | seq_cst(t_i) && isWrite(t_i) && samevar(t_i, t)}) */
545         action_list_t::reverse_iterator rit;
546         for (rit = list->rbegin(); rit != list->rend(); rit++)
547                 if ((*rit)->is_write() && (*rit)->is_seqcst())
548                         return *rit;
549         return NULL;
550 }
551
552 ModelAction * ModelChecker::get_parent_action(thread_id_t tid)
553 {
554         ModelAction *parent = get_last_action(tid);
555         if (!parent)
556                 parent = get_thread(tid)->get_creation();
557         return parent;
558 }
559
560 /**
561  * Returns the clock vector for a given thread.
562  * @param tid The thread whose clock vector we want
563  * @return Desired clock vector
564  */
565 ClockVector * ModelChecker::get_cv(thread_id_t tid) {
566         return get_parent_action(tid)->get_cv();
567 }
568
569
570 /** Resolve the given promises. */
571
572 void ModelChecker::resolve_promises(ModelAction *write) {
573         for (unsigned int i = 0, promise_index = 0;promise_index<promises->size(); i++) {
574                 Promise * promise = (*promises)[promise_index];
575                 if (write->get_node()->get_promise(i)) {
576                         ModelAction * read = promise->get_action();
577                         read->read_from(write);
578                         r_modification_order(read, write);
579                         post_r_modification_order(read, write);
580                         promises->erase(promises->begin()+promise_index);
581                 } else
582                         promise_index++;
583         }
584 }
585
586 /** Compute the set of promises that could potentially be satisfied by
587  *  this action. */
588
589 void ModelChecker::compute_promises(ModelAction *curr) {
590         for (unsigned int i = 0;i<promises->size();i++) {
591                 Promise * promise = (*promises)[i];
592                 const ModelAction * act = promise->get_action();
593                 if (!act->happens_before(curr)&&
594                                 act->is_read()&&
595                                 !act->is_synchronizing(curr)&&
596                                 !act->same_thread(curr)&&
597                                 promise->get_value() == curr->get_value()) {
598                         curr->get_node()->set_promise(i);
599                 }
600         }
601 }
602
603 /** Checks promises in response to change in ClockVector Threads. */
604
605 void ModelChecker::check_promises(ClockVector *old_cv, ClockVector * merge_cv) {
606         for (unsigned int i = 0;i<promises->size();i++) {
607                 Promise * promise = (*promises)[i];
608                 const ModelAction * act = promise->get_action();
609                 if ((old_cv == NULL||!old_cv->synchronized_since(act))&&
610                                 merge_cv->synchronized_since(act)) {
611                         //This thread is no longer able to send values back to satisfy the promise
612                         int num_synchronized_threads = promise->increment_threads();
613                         if (num_synchronized_threads == model->get_num_threads()) {
614                                 //Promise has failed
615                                 failed_promise = true;
616                                 return;
617                         }
618                 }
619         }
620 }
621
622 /**
623  * Build up an initial set of all past writes that this 'read' action may read
624  * from. This set is determined by the clock vector's "happens before"
625  * relationship.
626  * @param curr is the current ModelAction that we are exploring; it must be a
627  * 'read' operation.
628  */
629 void ModelChecker::build_reads_from_past(ModelAction *curr)
630 {
631         std::vector<action_list_t> *thrd_lists = obj_thrd_map->ensureptr(curr->get_location());
632         unsigned int i;
633         ASSERT(curr->is_read());
634
635         ModelAction *last_seq_cst = NULL;
636
637         /* Track whether this object has been initialized */
638         bool initialized = false;
639
640         if (curr->is_seqcst()) {
641                 last_seq_cst = get_last_seq_cst(curr->get_location());
642                 /* We have to at least see the last sequentially consistent write,
643                          so we are initialized. */
644                 if (last_seq_cst != NULL)
645                         initialized = true;
646         }
647
648         /* Iterate over all threads */
649         for (i = 0; i < thrd_lists->size(); i++) {
650                 /* Iterate over actions in thread, starting from most recent */
651                 action_list_t *list = &(*thrd_lists)[i];
652                 action_list_t::reverse_iterator rit;
653                 for (rit = list->rbegin(); rit != list->rend(); rit++) {
654                         ModelAction *act = *rit;
655
656                         /* Only consider 'write' actions */
657                         if (!act->is_write())
658                                 continue;
659
660                         /* Don't consider more than one seq_cst write if we are a seq_cst read. */
661                         if (!act->is_seqcst() || !curr->is_seqcst() || act == last_seq_cst) {
662                                 DEBUG("Adding action to may_read_from:\n");
663                                 if (DBG_ENABLED()) {
664                                         act->print();
665                                         curr->print();
666                                 }
667                                 curr->get_node()->add_read_from(act);
668                         }
669
670                         /* Include at most one act per-thread that "happens before" curr */
671                         if (act->happens_before(curr)) {
672                                 initialized = true;
673                                 break;
674                         }
675                 }
676         }
677
678         if (!initialized) {
679                 /** @todo Need a more informative way of reporting errors. */
680                 printf("ERROR: may read from uninitialized atomic\n");
681         }
682
683         if (DBG_ENABLED() || !initialized) {
684                 printf("Reached read action:\n");
685                 curr->print();
686                 printf("Printing may_read_from\n");
687                 curr->get_node()->print_may_read_from();
688                 printf("End printing may_read_from\n");
689         }
690
691         ASSERT(initialized);
692 }
693
694 static void print_list(action_list_t *list)
695 {
696         action_list_t::iterator it;
697
698         printf("---------------------------------------------------------------------\n");
699         printf("Trace:\n");
700
701         for (it = list->begin(); it != list->end(); it++) {
702                 (*it)->print();
703         }
704         printf("---------------------------------------------------------------------\n");
705 }
706
707 void ModelChecker::print_summary(bool feasible)
708 {
709         printf("\n");
710         printf("Number of executions: %d\n", num_executions);
711         printf("Total nodes created: %d\n", node_stack->get_total_nodes());
712
713         scheduler->print();
714
715         if (!feasible)
716                 printf("INFEASIBLE EXECUTION!\n");
717         print_list(action_trace);
718         printf("\n");
719 }
720
721 int ModelChecker::add_thread(Thread *t)
722 {
723         thread_map->put(id_to_int(t->get_id()), t);
724         scheduler->add_thread(t);
725         return 0;
726 }
727
728 void ModelChecker::remove_thread(Thread *t)
729 {
730         scheduler->remove_thread(t);
731 }
732
733 /**
734  * Switch from a user-context to the "master thread" context (a.k.a. system
735  * context). This switch is made with the intention of exploring a particular
736  * model-checking action (described by a ModelAction object). Must be called
737  * from a user-thread context.
738  * @param act The current action that will be explored. May be NULL, although
739  * there is little reason to switch to the model-checker without an action to
740  * explore (note: act == NULL is sometimes used as a hack to allow a thread to
741  * yield control without performing any progress; see thrd_join()).
742  * @return Return status from the 'swap' call (i.e., success/fail, 0/-1)
743  */
744 int ModelChecker::switch_to_master(ModelAction *act)
745 {
746         DBG();
747         Thread * old = thread_current();
748         set_current_action(act);
749         old->set_state(THREAD_READY);
750         return Thread::swap(old, get_system_context());
751 }