Skip to content

Commit 6c33f51

Browse files
committed
This introduces a new option to forcefully disable threads.
1 parent 86241e2 commit 6c33f51

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

cmake/simdjson-flags.cmake

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,21 @@ if(NOT SIMDJSON_EXCEPTIONS)
105105
target_compile_definitions(simdjson-internal-flags INTERFACE SIMDJSON_EXCEPTIONS=0)
106106
endif()
107107

108-
option(SIMDJSON_ENABLE_THREADS "Enable threaded operation" ON)
108+
option(SIMDJSON_ENABLE_THREADS "Link with thread support" ON)
109109
if(SIMDJSON_ENABLE_THREADS)
110110
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
111111
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
112112
find_package(Threads REQUIRED)
113113
target_link_libraries(simdjson-flags INTERFACE Threads::Threads)
114114
target_link_libraries(simdjson-flags INTERFACE ${CMAKE_THREAD_LIBS_INIT})
115115
target_compile_options(simdjson-flags INTERFACE ${CMAKE_THREAD_LIBS_INIT})
116-
target_compile_definitions(simdjson-flags INTERFACE SIMDJSON_THREADS_ENABLED=1)
116+
target_compile_definitions(simdjson-flags INTERFACE SIMDJSON_THREADS_ENABLED=1) # This will be set in the code automatically.
117+
endif()
118+
119+
# Some users compile simdjson with thread support but still do not want simdjson to use threads.
120+
option(SIMDJSON_DO_NOT_USE_THREADS_NO_MATTER_WHAT "Whether we enabled thread support or not (SIMDJSON_ENABLE_THREADS), do not use threads. This option does nothing when thread support is not enabled." OFF)
121+
if(SIMDJSON_DO_NOT_USE_THREADS_NO_MATTER_WHAT)
122+
target_compile_definitions(simdjson-flags INTERFACE SIMDJSON_DO_NOT_USE_THREADS_NO_MATTER_WHAT=1)
117123
endif()
118124

119125
if(SIMDJSON_USE_LIBCPP)

doc/parse_many.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ A `document_stream` instance uses at most two threads: there is a main thread an
101101
You should expect the main thread to be fully occupied while the worker thread is partially busy
102102
(e.g., 80% of the time).
103103

104+
If you compile simdjson with thread support and you still do not want simdjson to use threads,
105+
you can forcefully disable them by setting the SIMDJSON_DO_NOT_USE_THREADS_NO_MATTER_WHAT macro
106+
to 1 in C++, or by passing SIMDJSON_DO_NOT_USE_THREADS_NO_MATTER_WHAT=ON to cmake. It is a
107+
compile-time decision: if you disable the threads with SIMDJSON_DO_NOT_USE_THREADS_NO_MATTER_WHAT,
108+
you will not be able to use threads in simdjson unless you recompile.
109+
104110
Support
105111
-------
106112

include/simdjson/portability.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ compiling for a known 64-bit platform."
122122
#endif
123123
#endif
124124
125-
126125
// workaround for large stack sizes under -O0.
127126
// https://github.com/simdjson/simdjson/issues/691
128127
#ifdef __APPLE__
@@ -136,6 +135,13 @@ compiling for a known 64-bit platform."
136135
#endif
137136
#endif
138137
138+
139+
#if SIMDJSON_DO_NOT_USE_THREADS_NO_MATTER_WHAT
140+
// No matter what happened, we undefine SIMDJSON_THREADS_ENABLED and so disable threads.
141+
#undef SIMDJSON_THREADS_ENABLED
142+
#endif
143+
144+
139145
#if defined(__clang__)
140146
#define NO_SANITIZE_UNDEFINED __attribute__((no_sanitize("undefined")))
141147
#elif defined(__GNUC__)

0 commit comments

Comments
 (0)