44
55#pragma once
66
7+ #include < chrono>
8+ #include < cstdint>
9+ #include < fstream>
10+ #include < iostream>
11+ #include < source_location>
12+ #include < thread>
13+ #include < vector>
714#include < atomic>
815
16+ #include " extrinsic_storage.h"
17+
918// No Data race:
1019// A reader enters a function and there is an active reader
1120//
1625//
1726// Wait-free? (maybe the CMPX is only lock-free?)
1827
28+ namespace scl {
29+
30+ #ifndef DATA_RACE_DETECTED
31+ #define DATA_RACE_DETECTED std::abort ();
32+ #endif
33+
1934// ==========================================
2035// ==========================================
2136struct check_state
@@ -40,19 +55,19 @@ void read_started (check_state& state)
4055 ++state.num_readers ; // must be first
4156
4257 if (state.is_writing )
43- std::terminate ();
44- // read during active write
58+ DATA_RACE_DETECTED
59+ // read during active write
4560}
4661
4762void write_started (check_state& state)
4863{
4964 if (state.is_writing .exchange (true )) // must be first
50- std::terminate ();
51- // write during active write
65+ DATA_RACE_DETECTED
66+ // write during active write
5267
5368 if (state.num_readers > 0 )
54- std::terminate ();
55- // write during active read
69+ DATA_RACE_DETECTED
70+ // write during active read
5671}
5772
5873void read_ended (check_state& state)
@@ -65,6 +80,7 @@ void write_ended (check_state& state)
6580 state.is_writing = false ;
6681}
6782
83+ #undef DATA_RACE_DETECTED
6884
6985// ==========================================
7086// ==========================================
@@ -101,24 +117,15 @@ struct scoped_check
101117
102118// ==========================================
103119// ==========================================
104- #include " extrinsic_storage.h"
105-
106- #include < chrono>
107- #include < cstdint>
108- #include < fstream>
109- #include < iostream>
110- #include < source_location>
111- #include < thread>
112- #include < vector>
113-
114120template <typename Tag = check_state>
115- class date_race_registry {
121+ class data_race_registry {
116122 static inline auto tags = extrinsic_storage<Tag>{};
117123 static inline auto log = std::ofstream{ " data-race-violations.log" };
118124public:
119125
120- static inline auto get_state (void * pobj) noexcept {
121- return *tags.find_or_insert (pobj);
126+ static inline auto & get_state (const void * pobj) noexcept {
127+ // This const cast should be tidied up
128+ return *tags.find_or_insert (const_cast <void *> (pobj));
122129 }
123130
124131 static inline auto on_destroy (void * pobj) noexcept -> void { tags.erase (pobj); }
@@ -139,17 +146,6 @@ class date_race_registry {
139146 static inline auto on_write_ended (void * pobj) noexcept -> void {
140147 if (auto p = tags.find_or_insert (pobj)) { write_ended (*p); }
141148 }
142-
143- // static inline auto on_get_alternative(void* pobj, uint32_t alt, std::source_location where = std::source_location::current()) -> void {
144- // if (auto active = tags.find(pobj);
145- // active // if we have discriminator info for this union
146- // && *active != alt // and the discriminator not what is expected
147- // && *active != unknown // and is not unknown
148- // )
149- // {
150- // log << where.file_name() << '(' << where.line()
151- // << "): union type safety violation - active member " << (*active == invalid ? "invalid" : std::to_string(*active))
152- // << ", attempted to access " << alt << "\n";
153- // }
154- // }
155149};
150+
151+ }
0 commit comments