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