Skip to content

Commit 0b927f0

Browse files
vitlibarlemire
authored andcommitted
Make dynamic dispatch free of TSan warnings (simdjson#256)
1 parent f3c3afd commit 0b927f0

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/jsonparser.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "simdjson/isadetection.h"
33
#include "simdjson/portability.h"
44
#include "simdjson/simdjson.h"
5+
#include <atomic>
56

67
namespace simdjson {
78

@@ -15,17 +16,17 @@ using json_parse_functype = int(const uint8_t *buf, size_t len, ParsedJson &pj,
1516

1617
// Pointer that holds the json_parse implementation corresponding to the
1718
// available SIMD instruction set
18-
extern json_parse_functype *json_parse_ptr;
19+
extern std::atomic<json_parse_functype *> json_parse_ptr;
1920

2021
int json_parse(const uint8_t *buf, size_t len, ParsedJson &pj,
2122
bool realloc_if_needed) {
22-
return json_parse_ptr(buf, len, pj, realloc_if_needed);
23+
return json_parse_ptr.load(std::memory_order_relaxed)(buf, len, pj, realloc_if_needed);
2324
}
2425

2526
int json_parse(const char *buf, size_t len, ParsedJson &pj,
2627
bool realloc_if_needed) {
27-
return json_parse_ptr(reinterpret_cast<const uint8_t *>(buf), len, pj,
28-
realloc_if_needed);
28+
return json_parse_ptr.load(std::memory_order_relaxed)(reinterpret_cast<const uint8_t *>(buf), len, pj,
29+
realloc_if_needed);
2930
}
3031

3132
Architecture find_best_supported_implementation() {
@@ -55,26 +56,26 @@ int json_parse_dispatch(const uint8_t *buf, size_t len, ParsedJson &pj,
5556
switch (best_implementation) {
5657
#ifdef IS_X86_64
5758
case Architecture::HASWELL:
58-
json_parse_ptr = &json_parse_implementation<Architecture::HASWELL>;
59+
json_parse_ptr.store(&json_parse_implementation<Architecture::HASWELL>, std::memory_order_relaxed);
5960
break;
6061
case Architecture::WESTMERE:
61-
json_parse_ptr = &json_parse_implementation<Architecture::WESTMERE>;
62+
json_parse_ptr.store(&json_parse_implementation<Architecture::WESTMERE>, std::memory_order_relaxed);
6263
break;
6364
#endif
6465
#ifdef IS_ARM64
6566
case Architecture::ARM64:
66-
json_parse_ptr = &json_parse_implementation<Architecture::ARM64>;
67+
json_parse_ptr.store(&json_parse_implementation<Architecture::ARM64>, std::memory_order_relaxed);
6768
break;
6869
#endif
6970
default:
7071
std::cerr << "The processor is not supported by simdjson." << std::endl;
7172
return simdjson::UNEXPECTED_ERROR;
7273
}
7374

74-
return json_parse_ptr(buf, len, pj, realloc_if_needed);
75+
return json_parse_ptr.load(std::memory_order_relaxed)(buf, len, pj, realloc_if_needed);
7576
}
7677

77-
json_parse_functype *json_parse_ptr = &json_parse_dispatch;
78+
std::atomic<json_parse_functype *> json_parse_ptr = &json_parse_dispatch;
7879

7980
WARN_UNUSED
8081
ParsedJson build_parsed_json(const uint8_t *buf, size_t len,

0 commit comments

Comments
 (0)