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