ce659d1ac7792793f253fa93378437f6060351c5
[c11tester.git] / datarace.h
1 /** @file datarace.h
2  *  @brief Data race detection code.
3  */
4
5 #ifndef __DATARACE_H__
6 #define __DATARACE_H__
7
8 #include "config.h"
9 #include <stdint.h>
10 #include "modeltypes.h"
11 #include "classlist.h"
12
13 struct ShadowTable {
14         void * array[65536];
15 };
16
17 struct ShadowBaseTable {
18         uint64_t array[65536];
19 };
20
21 struct DataRace {
22         /* Clock and thread associated with first action.  This won't change in
23                  response to synchronization. */
24
25         thread_id_t oldthread;
26         modelclock_t oldclock;
27         /* Record whether this is a write, so we can tell the user. */
28         bool isoldwrite;
29
30         /* Model action associated with second action.  This could change as
31                  a result of synchronization. */
32         ModelAction *newaction;
33         /* Record whether this is a write, so we can tell the user. */
34         bool isnewwrite;
35
36         /* Address of data race. */
37         const void *address;
38 };
39
40 #define MASK16BIT 0xffff
41
42 void initRaceDetector();
43 void raceCheckWrite(thread_id_t thread, void *location);
44 void raceCheckRead(thread_id_t thread, const void *location);
45 bool checkDataRaces();
46 void assert_race(struct DataRace *race);
47 bool haveUnrealizedRaces();
48
49 /**
50  * @brief A record of information for detecting data races
51  */
52 struct RaceRecord {
53         modelclock_t *readClock;
54         thread_id_t *thread;
55         int capacity;
56         int numReads;
57         thread_id_t writeThread;
58         modelclock_t writeClock;
59 };
60
61 #define INITCAPACITY 4
62
63 #define ISSHORTRECORD(x) ((x)&0x1)
64
65 #define THREADMASK 0xff
66 #define RDTHREADID(x) (((x)>>1)&THREADMASK)
67 #define READMASK 0x07fffff
68 #define READVECTOR(x) (((x)>>9)&READMASK)
69
70 #define WRTHREADID(x) (((x)>>32)&THREADMASK)
71
72 #define WRITEMASK READMASK
73 #define WRITEVECTOR(x) (((x)>>40)&WRITEMASK)
74
75 /**
76  * The basic encoding idea is that (void *) either:
77  *  -# points to a full record (RaceRecord) or
78  *  -# encodes the information in a 64 bit word. Encoding is as
79  *     follows:
80  *     - lowest bit set to 1
81  *     - next 8 bits are read thread id
82  *     - next 23 bits are read clock vector
83  *     - next 8 bits are write thread id
84  *     - next 23 bits are write clock vector
85  */
86 #define ENCODEOP(rdthread, rdtime, wrthread, wrtime) (0x1ULL | ((rdthread)<<1) | ((rdtime) << 9) | (((uint64_t)wrthread)<<32) | (((uint64_t)wrtime)<<40))
87
88 #define MAXTHREADID (THREADMASK-1)
89 #define MAXREADVECTOR (READMASK-1)
90 #define MAXWRITEVECTOR (WRITEMASK-1)
91
92 #endif/* __DATARACE_H__ */