Skip to content

Commit 1cec071

Browse files
Improve Windows compatibility and CMake flags
Moved <stdatomic.h> include and atomic variables to non-Windows platforms in vsort.c, and added Windows-specific initialization using InitOnce. Updated CMakeLists.txt to set compiler flags conditionally for MSVC, enforce C17 standard, and avoid non-portable flags on Windows.
1 parent 562151b commit 1cec071

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

CMakeLists.txt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,11 @@ check_ipo_supported(RESULT ipo_supported OUTPUT ipo_error)
3838
if(ipo_supported)
3939
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
4040
endif()
41-
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -Ofast -march=native -fomit-frame-pointer -flto -ffast-math")
42-
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} -flto")
41+
42+
if(NOT MSVC)
43+
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -Ofast -march=native -fomit-frame-pointer -flto -ffast-math")
44+
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} -flto")
45+
endif()
4346

4447
# Add logger source files
4548
set(VSORT_SOURCES vsort.c vsort_logger.c)
@@ -100,10 +103,10 @@ endif()
100103
if(MSVC)
101104
# MSVC (Visual Studio) compiler flags
102105
target_compile_options(vsort PRIVATE
106+
/std:c17 # Enable C17 mode for proper headers
103107
/O2 # Optimize for speed
104108
/Ob2 # Inline function expansion
105109
/Oi # Enable intrinsic functions
106-
/Ot # Favor fast code
107110
/Gy # Function-level linking
108111
/fp:fast # Fast floating point model
109112
)
@@ -161,6 +164,7 @@ foreach(example ${EXAMPLES})
161164
# Add Windows-specific configurations for examples
162165
if(MSVC)
163166
target_compile_options(${example} PRIVATE
167+
/std:c17 # Match library C standard
164168
/O2 # Optimize for speed
165169
/fp:fast # Fast floating point model
166170
)
@@ -187,6 +191,7 @@ foreach(test ${TESTS})
187191
# Add Windows-specific configurations for tests
188192
if(MSVC)
189193
target_compile_options(${test} PRIVATE
194+
/std:c17 # Match library C standard
190195
/O2 # Optimize for speed
191196
/fp:fast # Fast floating point model
192197
)

vsort.c

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include <stdbool.h>
2222
#include <stddef.h>
2323
#include <stdint.h>
24-
#include <stdatomic.h>
2524
#include <stdio.h>
2625
#include <stdlib.h>
2726
#include <string.h>
@@ -36,6 +35,7 @@
3635
#if defined(_WIN32) || defined(_MSC_VER)
3736
#include <windows.h>
3837
#else
38+
#include <stdatomic.h>
3939
#include <sched.h>
4040
#include <sys/types.h>
4141
#include <unistd.h>
@@ -119,8 +119,12 @@ static vsort_runtime_t g_runtime = {
119119
.log_level = VSORT_LOG_WARNING,
120120
.logger_ready = false};
121121

122+
#if defined(_WIN32) || defined(_MSC_VER)
123+
static INIT_ONCE g_runtime_once = INIT_ONCE_STATIC_INIT;
124+
#else
122125
static atomic_bool g_runtime_init_requested = ATOMIC_VAR_INIT(false);
123126
static atomic_bool g_runtime_ready = ATOMIC_VAR_INIT(false);
127+
#endif
124128

125129
static vsort_runtime_t *vsort_runtime(void);
126130
static void vsort_runtime_initialize(void);
@@ -438,9 +442,26 @@ static void vsort_runtime_initialize(void)
438442
rt->thresholds.radix_threshold,
439443
rt->thresholds.cache_optimal_elements);
440444

445+
#if !defined(_WIN32) && !defined(_MSC_VER)
441446
atomic_store(&g_runtime_ready, true);
447+
#endif
442448
}
443449

450+
#if defined(_WIN32) || defined(_MSC_VER)
451+
static BOOL CALLBACK vsort_runtime_once_callback(PINIT_ONCE once, PVOID param, PVOID *context)
452+
{
453+
VSORT_UNUSED(once);
454+
VSORT_UNUSED(param);
455+
VSORT_UNUSED(context);
456+
vsort_runtime_initialize();
457+
return TRUE;
458+
}
459+
460+
VSORT_API void vsort_init(void)
461+
{
462+
InitOnceExecuteOnce(&g_runtime_once, vsort_runtime_once_callback, NULL, NULL);
463+
}
464+
#else
444465
VSORT_API void vsort_init(void)
445466
{
446467
bool expected = false;
@@ -452,14 +473,11 @@ VSORT_API void vsort_init(void)
452473
{
453474
while (!atomic_load(&g_runtime_ready))
454475
{
455-
#if defined(_WIN32) || defined(_MSC_VER)
456-
Sleep(0);
457-
#else
458476
sched_yield();
459-
#endif
460477
}
461478
}
462479
}
480+
#endif
463481

464482
// -----------------------------------------------------------------------------
465483
// Memory helpers

0 commit comments

Comments
 (0)