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