Separate the marking algorithm and action removal algorithm
[c11tester.git] / action.cc
1 #include <stdio.h>
2 #define __STDC_FORMAT_MACROS
3 #include <inttypes.h>
4 #include <stdlib.h>
5
6 #include "model.h"
7 #include "action.h"
8 #include "clockvector.h"
9 #include "common.h"
10 #include "threads-model.h"
11 #include "wildcard.h"
12
13 #define ACTION_INITIAL_CLOCK 0
14
15 /** @brief A special value to represent a successful trylock */
16 #define VALUE_TRYSUCCESS 1
17
18 /** @brief A special value to represent a failed trylock */
19 #define VALUE_TRYFAILED 0
20
21 /**
22  * @brief Construct a new ModelAction
23  *
24  * @param type The type of action
25  * @param order The memory order of this action. A "don't care" for non-ATOMIC
26  * actions (e.g., THREAD_* or MODEL_* actions).
27  * @param loc The location that this action acts upon
28  * @param value (optional) A value associated with the action (e.g., the value
29  * read or written). Defaults to a given macro constant, for debugging purposes.
30  * @param thread (optional) The Thread in which this action occurred. If NULL
31  * (default), then a Thread is assigned according to the scheduler.
32  */
33 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc,
34                                                                                                  uint64_t value, Thread *thread) :
35         location(loc),
36         position(NULL),
37         reads_from(NULL),
38         last_fence_release(NULL),
39         cv(NULL),
40         rf_cv(NULL),
41         trace_ref(NULL),
42         thrdmap_ref(NULL),
43         action_ref(NULL),
44         func_act_ref(NULL),
45         value(value),
46         type(type),
47         original_type(ATOMIC_NOP),
48         swap_flag(false),
49         order(order),
50         original_order(order),
51         seq_number(ACTION_INITIAL_CLOCK)
52 {
53         /* References to NULL atomic variables can end up here */
54         ASSERT(loc || type == ATOMIC_FENCE || type == ATOMIC_NOP);
55
56         Thread *t = thread ? thread : thread_current();
57         this->tid = t!= NULL ? t->get_id() : -1;
58 }
59
60
61 /**
62  * @brief Construct a new ModelAction for sleep actions
63  *
64  * @param type The type of action: THREAD_SLEEP
65  * @param order The memory order of this action. A "don't care" for non-ATOMIC
66  * actions (e.g., THREAD_* or MODEL_* actions).
67  * @param loc The location that this action acts upon
68  * @param value The time duration a thread is scheduled to sleep.
69  * @param _time The this sleep action is constructed
70  */
71 ModelAction::ModelAction(action_type_t type, memory_order order, uint64_t value, uint64_t _time) :
72         location(NULL),
73         position(NULL),
74         time(_time),
75         last_fence_release(NULL),
76         cv(NULL),
77         rf_cv(NULL),
78         trace_ref(NULL),
79         thrdmap_ref(NULL),
80         action_ref(NULL),
81         func_act_ref(NULL),
82         value(value),
83         type(type),
84         original_type(ATOMIC_NOP),
85         swap_flag(false),
86         order(order),
87         original_order(order),
88         seq_number(ACTION_INITIAL_CLOCK)
89 {
90         Thread *t = thread_current();
91         this->tid = t!= NULL ? t->get_id() : -1;
92 }
93
94 /**
95  * @brief Construct a new ModelAction
96  *
97  * @param type The type of action
98  * @param order The memory order of this action. A "don't care" for non-ATOMIC
99  * actions (e.g., THREAD_* or MODEL_* actions).
100  * @param loc The location that this action acts upon
101  * @param value (optional) A value associated with the action (e.g., the value
102  * read or written). Defaults to a given macro constant, for debugging purposes.
103  * @param size (optional) The Thread in which this action occurred. If NULL
104  * (default), then a Thread is assigned according to the scheduler.
105  */
106 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc,
107                                                                                                  uint64_t value, int size) :
108         location(loc),
109         position(NULL),
110         reads_from(NULL),
111         last_fence_release(NULL),
112         cv(NULL),
113         rf_cv(NULL),
114         trace_ref(NULL),
115         thrdmap_ref(NULL),
116         action_ref(NULL),
117         func_act_ref(NULL),
118         value(value),
119         type(type),
120         original_type(ATOMIC_NOP),
121         swap_flag(false),
122         order(order),
123         original_order(order),
124         seq_number(ACTION_INITIAL_CLOCK)
125 {
126         /* References to NULL atomic variables can end up here */
127         ASSERT(loc);
128         this->size = size;
129         Thread *t = thread_current();
130         this->tid = t->get_id();
131 }
132
133
134 /**
135  * @brief Construct a new ModelAction with source line number (requires llvm support)
136  *
137  * @param type The type of action
138  * @param order The memory order of this action. A "don't care" for non-ATOMIC
139  * actions (e.g., THREAD_* or MODEL_* actions).
140  * @param loc The location that this action acts upon
141  * @param value (optional) A value associated with the action (e.g., the value
142  * read or written). Defaults to a given macro constant, for debugging purposes.
143  * @param size (optional) The Thread in which this action occurred. If NULL
144  * (default), then a Thread is assigned according to the scheduler.
145  */
146 ModelAction::ModelAction(action_type_t type, const char * position, memory_order order, void *loc,
147                                                                                                  uint64_t value, int size) :
148         location(loc),
149         position(position),
150         reads_from(NULL),
151         last_fence_release(NULL),
152         cv(NULL),
153         rf_cv(NULL),
154         trace_ref(NULL),
155         thrdmap_ref(NULL),
156         action_ref(NULL),
157         func_act_ref(NULL),
158         value(value),
159         type(type),
160         original_type(ATOMIC_NOP),
161         swap_flag(false),
162         order(order),
163         original_order(order),
164         seq_number(ACTION_INITIAL_CLOCK)
165 {
166         /* References to NULL atomic variables can end up here */
167         ASSERT(loc);
168         this->size = size;
169         Thread *t = thread_current();
170         this->tid = t->get_id();
171 }
172
173
174 /**
175  * @brief Construct a new ModelAction with source line number (requires llvm support)
176  *
177  * @param type The type of action
178  * @param position The source line number of this atomic operation
179  * @param order The memory order of this action. A "don't care" for non-ATOMIC
180  * actions (e.g., THREAD_* or MODEL_* actions).
181  * @param loc The location that this action acts upon
182  * @param value (optional) A value associated with the action (e.g., the value
183  * read or written). Defaults to a given macro constant, for debugging purposes.
184  * @param thread (optional) The Thread in which this action occurred. If NULL
185  * (default), then a Thread is assigned according to the scheduler.
186  */
187 ModelAction::ModelAction(action_type_t type, const char * position, memory_order order,
188                                                                                                  void *loc, uint64_t value, Thread *thread) :
189         location(loc),
190         position(position),
191         reads_from(NULL),
192         last_fence_release(NULL),
193         cv(NULL),
194         rf_cv(NULL),
195         trace_ref(NULL),
196         thrdmap_ref(NULL),
197         action_ref(NULL),
198         func_act_ref(NULL),
199         value(value),
200         type(type),
201         original_type(ATOMIC_NOP),
202         swap_flag(false),
203         order(order),
204         original_order(order),
205         seq_number(ACTION_INITIAL_CLOCK)
206 {
207         /* References to NULL atomic variables can end up here */
208         ASSERT(loc || type == ATOMIC_FENCE);
209
210         Thread *t = thread ? thread : thread_current();
211         this->tid = t->get_id();
212 }
213
214
215 /** @brief ModelAction destructor */
216 ModelAction::~ModelAction()
217 {
218         /**
219          * We can't free the clock vector:
220          * Clock vectors are snapshotting state. When we delete model actions,
221          * they are at the end of the node list and have invalid old clock
222          * vectors which have already been rolled back to an unallocated state.
223          */
224
225         /*
226            if (cv)
227                 delete cv; */
228 }
229
230 int ModelAction::getSize() const {
231         return size;
232 }
233
234 void ModelAction::copy_from_new(ModelAction *newaction)
235 {
236         seq_number = newaction->seq_number;
237 }
238
239 void ModelAction::set_seq_number(modelclock_t num)
240 {
241         ASSERT(seq_number == ACTION_INITIAL_CLOCK);
242         seq_number = num;
243 }
244
245 void ModelAction::reset_seq_number()
246 {
247         seq_number = 0;
248 }
249
250 bool ModelAction::is_thread_start() const
251 {
252         return type == THREAD_START;
253 }
254
255 bool ModelAction::is_thread_join() const
256 {
257         return type == THREAD_JOIN || type == PTHREAD_JOIN;
258 }
259
260 bool ModelAction::is_mutex_op() const
261 {
262         return type == ATOMIC_LOCK || type == ATOMIC_TRYLOCK || type == ATOMIC_UNLOCK || type == ATOMIC_WAIT || type == ATOMIC_TIMEDWAIT || type == ATOMIC_NOTIFY_ONE || type == ATOMIC_NOTIFY_ALL;
263 }
264
265 bool ModelAction::is_lock() const
266 {
267         return type == ATOMIC_LOCK;
268 }
269
270 bool ModelAction::is_sleep() const
271 {
272         return type == THREAD_SLEEP;
273 }
274
275 bool ModelAction::is_wait() const {
276         return type == ATOMIC_WAIT || type == ATOMIC_TIMEDWAIT;
277 }
278
279 bool ModelAction::is_notify() const {
280         return type == ATOMIC_NOTIFY_ONE || type == ATOMIC_NOTIFY_ALL;
281 }
282
283 bool ModelAction::is_notify_one() const {
284         return type == ATOMIC_NOTIFY_ONE;
285 }
286
287 bool ModelAction::is_unlock() const
288 {
289         return type == ATOMIC_UNLOCK;
290 }
291
292 bool ModelAction::is_trylock() const
293 {
294         return type == ATOMIC_TRYLOCK;
295 }
296
297 bool ModelAction::is_success_lock() const
298 {
299         return type == ATOMIC_LOCK || (type == ATOMIC_TRYLOCK && value == VALUE_TRYSUCCESS);
300 }
301
302 bool ModelAction::is_failed_trylock() const
303 {
304         return (type == ATOMIC_TRYLOCK && value == VALUE_TRYFAILED);
305 }
306
307 /** @return True if this operation is performed on a C/C++ atomic variable */
308 bool ModelAction::is_atomic_var() const
309 {
310         return is_read() || could_be_write();
311 }
312
313 bool ModelAction::is_read() const
314 {
315         return type == ATOMIC_READ || type == ATOMIC_RMWR || type == ATOMIC_RMWRCAS || type == ATOMIC_RMW;
316 }
317
318 bool ModelAction::is_write() const
319 {
320         return type == ATOMIC_WRITE || type == ATOMIC_RMW || type == ATOMIC_INIT || type == NONATOMIC_WRITE;
321 }
322
323 bool ModelAction::is_create() const
324 {
325         return type == THREAD_CREATE || type == PTHREAD_CREATE;
326 }
327
328 bool ModelAction::is_free() const
329 {
330         return type == READY_FREE;
331 }
332
333 bool ModelAction::could_be_write() const
334 {
335         return is_write() || is_rmwr();
336 }
337
338 bool ModelAction::is_yield() const
339 {
340         return type == THREAD_YIELD;
341 }
342
343 bool ModelAction::is_rmwr() const
344 {
345         return type == ATOMIC_RMWR || type == ATOMIC_RMWRCAS;
346 }
347
348 bool ModelAction::is_rmwrcas() const
349 {
350         return type == ATOMIC_RMWRCAS;
351 }
352
353 bool ModelAction::is_rmw() const
354 {
355         return type == ATOMIC_RMW;
356 }
357
358 bool ModelAction::is_rmwc() const
359 {
360         return type == ATOMIC_RMWC;
361 }
362
363 bool ModelAction::is_fence() const
364 {
365         return type == ATOMIC_FENCE;
366 }
367
368 bool ModelAction::is_initialization() const
369 {
370         return type == ATOMIC_INIT;
371 }
372
373 bool ModelAction::is_annotation() const
374 {
375         return type == ATOMIC_ANNOTATION;
376 }
377
378 bool ModelAction::is_relaxed() const
379 {
380         return order == std::memory_order_relaxed;
381 }
382
383 bool ModelAction::is_acquire() const
384 {
385         switch (order) {
386         case std::memory_order_acquire:
387         case std::memory_order_acq_rel:
388         case std::memory_order_seq_cst:
389                 return true;
390         default:
391                 return false;
392         }
393 }
394
395 bool ModelAction::is_release() const
396 {
397         switch (order) {
398         case std::memory_order_release:
399         case std::memory_order_acq_rel:
400         case std::memory_order_seq_cst:
401                 return true;
402         default:
403                 return false;
404         }
405 }
406
407 bool ModelAction::is_seqcst() const
408 {
409         return order == std::memory_order_seq_cst;
410 }
411
412 bool ModelAction::same_var(const ModelAction *act) const
413 {
414         if (act->is_wait() || is_wait()) {
415                 if (act->is_wait() && is_wait()) {
416                         if (((void *)value) == ((void *)act->value))
417                                 return true;
418                 } else if (is_wait()) {
419                         if (((void *)value) == act->location)
420                                 return true;
421                 } else if (act->is_wait()) {
422                         if (location == ((void *)act->value))
423                                 return true;
424                 }
425         }
426
427         return location == act->location;
428 }
429
430 bool ModelAction::same_thread(const ModelAction *act) const
431 {
432         return tid == act->tid;
433 }
434
435 void ModelAction::copy_typeandorder(ModelAction * act)
436 {
437         this->type = act->type;
438         this->order = act->order;
439 }
440
441 /**
442  * Get the Thread which is the operand of this action. This is only valid for
443  * THREAD_* operations (currently only for THREAD_CREATE and THREAD_JOIN). Note
444  * that this provides a central place for determining the conventions of Thread
445  * storage in ModelAction, where we generally aren't very type-safe (e.g., we
446  * store object references in a (void *) address.
447  *
448  * For THREAD_CREATE, this yields the Thread which is created.
449  * For THREAD_JOIN, this yields the Thread we are joining with.
450  *
451  * @return The Thread which this action acts on, if exists; otherwise NULL
452  */
453 Thread * ModelAction::get_thread_operand() const
454 {
455         if (type == THREAD_CREATE) {
456                 /* thread_operand is set in execution.cc */
457                 return thread_operand;
458         } else if (type == PTHREAD_CREATE) {
459                 return thread_operand;
460         } else if (type == THREAD_JOIN) {
461                 /* THREAD_JOIN uses (Thread *) for location */
462                 return (Thread *)get_location();
463         } else if (type == PTHREAD_JOIN) {
464                 // return NULL;
465                 // thread_operand is stored in execution::pthread_map;
466                 return (Thread *)get_location();
467         } else
468                 return NULL;
469 }
470
471 /**
472  * @brief Convert the read portion of an RMW
473  *
474  * Changes an existing read part of an RMW action into either:
475  *  -# a full RMW action in case of the completed write or
476  *  -# a READ action in case a failed action.
477  *
478  * @todo  If the memory_order changes, we may potentially need to update our
479  * clock vector.
480  *
481  * @param act The second half of the RMW (either RMWC or RMW)
482  */
483 void ModelAction::process_rmw(ModelAction *act)
484 {
485         this->order = act->order;
486         if (act->is_rmwc())
487                 this->type = ATOMIC_READ;
488         else if (act->is_rmw()) {
489                 this->type = ATOMIC_RMW;
490                 this->value = act->value;
491         }
492 }
493
494 /**
495  * @brief Check if this action should be backtracked with another, due to
496  * potential synchronization
497  *
498  * The is_synchronizing method should only explore interleavings if:
499  *  -# the operations are seq_cst and don't commute or
500  *  -# the reordering may establish or break a synchronization relation.
501  *
502  * Other memory operations will be dealt with by using the reads_from relation.
503  *
504  * @param act The action to consider exploring a reordering
505  * @return True, if we have to explore a reordering; otherwise false
506  */
507 bool ModelAction::could_synchronize_with(const ModelAction *act) const
508 {
509         // Same thread can't be reordered
510         if (same_thread(act))
511                 return false;
512
513         // Different locations commute
514         if (!same_var(act) && !is_fence() && !act->is_fence())
515                 return false;
516
517         // Explore interleavings of seqcst writes/fences to guarantee total
518         // order of seq_cst operations that don't commute
519         if ((could_be_write() || act->could_be_write() || is_fence() || act->is_fence()) && is_seqcst() && act->is_seqcst())
520                 return true;
521
522         // Explore synchronizing read/write pairs
523         if (is_acquire() && act->is_release() && is_read() && act->could_be_write())
524                 return true;
525
526         // lock just released...we can grab lock
527         if ((is_lock() || is_trylock()) && (act->is_unlock() || act->is_wait()))
528                 return true;
529
530         // lock just acquired...we can fail to grab lock
531         if (is_trylock() && act->is_success_lock())
532                 return true;
533
534         // other thread stalling on lock...we can release lock
535         if (is_unlock() && (act->is_trylock() || act->is_lock()))
536                 return true;
537
538         if (is_trylock() && (act->is_unlock() || act->is_wait()))
539                 return true;
540
541         if (is_notify() && act->is_wait())
542                 return true;
543
544         if (is_wait() && act->is_notify())
545                 return true;
546
547         // Otherwise handle by reads_from relation
548         return false;
549 }
550
551 bool ModelAction::is_conflicting_lock(const ModelAction *act) const
552 {
553         // Must be different threads to reorder
554         if (same_thread(act))
555                 return false;
556
557         // Try to reorder a lock past a successful lock
558         if (act->is_success_lock())
559                 return true;
560
561         // Try to push a successful trylock past an unlock
562         if (act->is_unlock() && is_trylock() && value == VALUE_TRYSUCCESS)
563                 return true;
564
565         // Try to push a successful trylock past a wait
566         if (act->is_wait() && is_trylock() && value == VALUE_TRYSUCCESS)
567                 return true;
568
569         return false;
570 }
571
572 /**
573  * Create a new clock vector for this action. Note that this function allows a
574  * user to clobber (and leak) a ModelAction's existing clock vector. A user
575  * should ensure that the vector has already either been rolled back
576  * (effectively "freed") or freed.
577  *
578  * @param parent A ModelAction from which to inherit a ClockVector
579  */
580 void ModelAction::create_cv(const ModelAction *parent)
581 {
582         if (parent)
583                 cv = new ClockVector(parent->cv, this);
584         else
585                 cv = new ClockVector(NULL, this);
586 }
587
588 void ModelAction::set_try_lock(bool obtainedlock)
589 {
590         value = obtainedlock ? VALUE_TRYSUCCESS : VALUE_TRYFAILED;
591 }
592
593 /**
594  * @brief Get the value read by this load
595  *
596  * We differentiate this function from ModelAction::get_write_value and
597  * ModelAction::get_value for the purpose of RMW's, which may have both a
598  * 'read' and a 'write' value.
599  *
600  * Note: 'this' must be a load.
601  *
602  * @return The value read by this load
603  */
604 uint64_t ModelAction::get_reads_from_value() const
605 {
606         ASSERT(is_read());
607         if (reads_from)
608                 return reads_from->get_write_value();
609
610         return VALUE_NONE;      // Only for new actions with no reads-from
611 }
612
613 /**
614  * @brief Get the value written by this store
615  *
616  * We differentiate this function from ModelAction::get_reads_from_value and
617  * ModelAction::get_value for the purpose of RMW's, which may have both a
618  * 'read' and a 'write' value.
619  *
620  * Note: 'this' must be a store.
621  *
622  * @return The value written by this store
623  */
624 uint64_t ModelAction::get_write_value() const
625 {
626         ASSERT(is_write());
627         return value;
628 }
629
630 /**
631  * @brief Get the value returned by this action
632  *
633  * For atomic reads (including RMW), an operation returns the value it read.
634  * For atomic writes, an operation returns the value it wrote. For other
635  * operations, the return value varies (sometimes is a "don't care"), but the
636  * value is simply stored in the "value" field.
637  *
638  * @return This action's return value
639  */
640 uint64_t ModelAction::get_return_value() const
641 {
642         if (is_read())
643                 return get_reads_from_value();
644         else if (is_write())
645                 return get_write_value();
646         else
647                 return value;
648 }
649
650 /**
651  * Update the model action's read_from action
652  * @param act The action to read from; should be a write
653  */
654 void ModelAction::set_read_from(ModelAction *act)
655 {
656         ASSERT(act);
657
658         reads_from = act;
659 }
660
661 /**
662  * Synchronize the current thread with the thread corresponding to the
663  * ModelAction parameter.
664  * @param act The ModelAction to synchronize with
665  * @return True if this is a valid synchronization; false otherwise
666  */
667 bool ModelAction::synchronize_with(const ModelAction *act)
668 {
669         if (*this < *act)
670                 return false;
671         cv->merge(act->cv);
672         return true;
673 }
674
675 bool ModelAction::has_synchronized_with(const ModelAction *act) const
676 {
677         return cv->synchronized_since(act);
678 }
679
680 /**
681  * Check whether 'this' happens before act, according to the memory-model's
682  * happens before relation. This is checked via the ClockVector constructs.
683  * @return true if this action's thread has synchronized with act's thread
684  * since the execution of act, false otherwise.
685  */
686 bool ModelAction::happens_before(const ModelAction *act) const
687 {
688         return act->cv->synchronized_since(this);
689 }
690
691 const char * ModelAction::get_type_str() const
692 {
693         switch (this->type) {
694         case THREAD_CREATE: return "thread create";
695         case THREAD_START: return "thread start";
696         case THREAD_YIELD: return "thread yield";
697         case THREAD_JOIN: return "thread join";
698         case THREAD_FINISH: return "thread finish";
699         case THREAD_SLEEP: return "thread sleep";
700         case THREADONLY_FINISH: return "pthread_exit finish";
701
702         case PTHREAD_CREATE: return "pthread create";
703         case PTHREAD_JOIN: return "pthread join";
704
705         case NONATOMIC_WRITE: return "nonatomic write";
706         case ATOMIC_READ: return "atomic read";
707         case ATOMIC_WRITE: return "atomic write";
708         case ATOMIC_RMW: return "atomic rmw";
709         case ATOMIC_FENCE: return "fence";
710         case ATOMIC_RMWR: return "atomic rmwr";
711         case ATOMIC_RMWRCAS: return "atomic rmwrcas";
712         case ATOMIC_RMWC: return "atomic rmwc";
713         case ATOMIC_INIT: return "init atomic";
714         case ATOMIC_LOCK: return "lock";
715         case ATOMIC_UNLOCK: return "unlock";
716         case ATOMIC_TRYLOCK: return "trylock";
717         case ATOMIC_WAIT: return "wait";
718         case ATOMIC_TIMEDWAIT: return "timed wait";
719         case ATOMIC_NOTIFY_ONE: return "notify one";
720         case ATOMIC_NOTIFY_ALL: return "notify all";
721         case ATOMIC_ANNOTATION: return "annotation";
722         default: return "unknown type";
723         };
724 }
725
726 const char * ModelAction::get_mo_str() const
727 {
728         switch (this->order) {
729         case std::memory_order_relaxed: return "relaxed";
730         case std::memory_order_acquire: return "acquire";
731         case std::memory_order_release: return "release";
732         case std::memory_order_acq_rel: return "acq_rel";
733         case std::memory_order_seq_cst: return "seq_cst";
734         default: return "unknown";
735         }
736 }
737
738 /** @brief Print nicely-formatted info about this ModelAction */
739 void ModelAction::print() const
740 {
741         const char *type_str = get_type_str(), *mo_str = get_mo_str();
742
743         model_print("%-4d %-2d   %-14s  %7s  %14p   %-#18" PRIx64,
744                                                         seq_number, id_to_int(tid), type_str, mo_str, location, get_return_value());
745         if (is_read()) {
746                 if (reads_from)
747                         model_print("  %-3d", reads_from->get_seq_number());
748                 else
749                         model_print("  ?  ");
750         }
751         if (cv) {
752                 if (is_read())
753                         model_print(" ");
754                 else
755                         model_print("      ");
756                 cv->print();
757         } else
758                 model_print("\n");
759 }
760
761 /** @brief Get a (likely) unique hash for this ModelAction */
762 unsigned int ModelAction::hash() const
763 {
764         unsigned int hash = (unsigned int)this->type;
765         hash ^= ((unsigned int)this->order) << 3;
766         hash ^= seq_number << 5;
767         hash ^= id_to_int(tid) << 6;
768
769         if (is_read()) {
770                 if (reads_from)
771                         hash ^= reads_from->get_seq_number();
772                 hash ^= get_reads_from_value();
773         }
774         return hash;
775 }
776
777 /**
778  * Only valid for LOCK, TRY_LOCK, UNLOCK, and WAIT operations.
779  * @return The mutex operated on by this action, if any; otherwise NULL
780  */
781 cdsc::mutex * ModelAction::get_mutex() const
782 {
783         if (is_trylock() || is_lock() || is_unlock())
784                 return (cdsc::mutex *)get_location();
785         else if (is_wait())
786                 return (cdsc::mutex *)get_value();
787         else
788                 return NULL;
789 }
790
791 /** @brief Swap type with original type */
792 void ModelAction::use_original_type()
793 {
794         action_type_t tmp = type;
795         type = original_type;
796         original_type = tmp;
797
798         if (swap_flag)
799                 swap_flag = false;
800         else
801                 swap_flag = true;
802 }