Skip to content

Commit 1b71136

Browse files
authored
Cleanup membarrier portability (#111943)
Fixes #111776
1 parent 29d286b commit 1b71136

File tree

6 files changed

+35
-56
lines changed

6 files changed

+35
-56
lines changed

src/coreclr/gc/unix/config.gc.h.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#cmakedefine01 HAVE_SYS_TIME_H
88
#cmakedefine01 HAVE_SYS_MMAN_H
9+
#cmakedefine01 HAVE_SYS_MEMBARRIER_H
910
#cmakedefine01 HAVE_PTHREAD_THREADID_NP
1011
#cmakedefine01 HAVE_PTHREAD_GETTHREADID_NP
1112
#cmakedefine01 HAVE_VM_FLAGS_SUPERPAGE_SIZE_ANY

src/coreclr/gc/unix/configure.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ include(CheckLibraryExists)
1111
check_include_files(sys/time.h HAVE_SYS_TIME_H)
1212
check_include_files(sys/mman.h HAVE_SYS_MMAN_H)
1313
check_include_files(pthread_np.h HAVE_PTHREAD_NP_H)
14+
check_include_files(sys/membarrier.h HAVE_SYS_MEMBARRIER_H)
1415

1516
check_function_exists(vm_allocate HAVE_VM_ALLOCATE)
1617
check_function_exists(sysctlbyname HAVE_SYSCTLBYNAME)

src/coreclr/gc/unix/gcenv.unix.cpp

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@
2929
#include <sys/swap.h>
3030
#endif
3131

32+
#ifdef __linux__
33+
#include <linux/membarrier.h>
34+
#include <sys/syscall.h>
35+
#define membarrier(...) syscall(__NR_membarrier, __VA_ARGS__)
36+
#elif HAVE_SYS_MEMBARRIER_H
37+
#include <sys/membarrier.h>
38+
#endif
39+
3240
#include <sys/resource.h>
3341

3442
#undef min
@@ -94,10 +102,6 @@ extern "C"
94102
#include <OS.h>
95103
#endif // __HAIKU__
96104

97-
#ifdef __linux__
98-
#include <sys/syscall.h> // __NR_membarrier
99-
#endif
100-
101105
#if HAVE_PTHREAD_NP_H
102106
#include <pthread_np.h>
103107
#endif
@@ -132,29 +136,9 @@ typedef cpuset_t cpu_set_t;
132136
// The cached total number of CPUs that can be used in the OS.
133137
static uint32_t g_totalCpuCount = 0;
134138

135-
//
136-
// Helper membarrier function
137-
//
138-
#ifdef __NR_membarrier
139-
# define membarrier(...) syscall(__NR_membarrier, __VA_ARGS__)
140-
#else
141-
# define membarrier(...) -ENOSYS
142-
#endif
143-
144-
enum membarrier_cmd
145-
{
146-
MEMBARRIER_CMD_QUERY = 0,
147-
MEMBARRIER_CMD_GLOBAL = (1 << 0),
148-
MEMBARRIER_CMD_GLOBAL_EXPEDITED = (1 << 1),
149-
MEMBARRIER_CMD_REGISTER_GLOBAL_EXPEDITED = (1 << 2),
150-
MEMBARRIER_CMD_PRIVATE_EXPEDITED = (1 << 3),
151-
MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED = (1 << 4),
152-
MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE = (1 << 5),
153-
MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_SYNC_CORE = (1 << 6)
154-
};
155-
156139
bool CanFlushUsingMembarrier()
157140
{
141+
#if defined(__linux__) || HAVE_SYS_MEMBARRIER_H
158142

159143
#ifdef TARGET_ANDROID
160144
// Avoid calling membarrier on older Android versions where membarrier
@@ -169,15 +153,16 @@ bool CanFlushUsingMembarrier()
169153
// Starting with Linux kernel 4.14, process memory barriers can be generated
170154
// using MEMBARRIER_CMD_PRIVATE_EXPEDITED.
171155

172-
int mask = membarrier(MEMBARRIER_CMD_QUERY, 0);
156+
int mask = membarrier(MEMBARRIER_CMD_QUERY, 0, 0);
173157

174158
if (mask >= 0 &&
175159
mask & MEMBARRIER_CMD_PRIVATE_EXPEDITED &&
176160
// Register intent to use the private expedited command.
177-
membarrier(MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED, 0) == 0)
161+
membarrier(MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED, 0, 0) == 0)
178162
{
179163
return true;
180164
}
165+
#endif
181166

182167
return false;
183168
}
@@ -423,12 +408,15 @@ bool GCToOSInterface::CanGetCurrentProcessorNumber()
423408
// Flush write buffers of processors that are executing threads of the current process
424409
void GCToOSInterface::FlushProcessWriteBuffers()
425410
{
411+
#if defined(__linux__) || HAVE_SYS_MEMBARRIER_H
426412
if (s_flushUsingMemBarrier)
427413
{
428-
int status = membarrier(MEMBARRIER_CMD_PRIVATE_EXPEDITED, 0);
414+
int status = membarrier(MEMBARRIER_CMD_PRIVATE_EXPEDITED, 0, 0);
429415
assert(status == 0 && "Failed to flush using membarrier");
430416
}
431-
else if (g_helperPage != 0)
417+
else
418+
#endif
419+
if (g_helperPage != 0)
432420
{
433421
int status = pthread_mutex_lock(&g_flushProcessWriteBuffersMutex);
434422
assert(status == 0 && "Failed to lock the flushProcessWriteBuffersMutex lock");

src/coreclr/pal/src/config.h.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
#cmakedefine01 HAVE_CRT_EXTERNS_H
1414
#cmakedefine01 HAVE_SYS_TIME_H
1515
#cmakedefine01 HAVE_PTHREAD_NP_H
16+
#cmakedefine01 HAVE_SYS_MEMBARRIER_H
1617
#cmakedefine01 HAVE_SYS_LWP_H
1718
#cmakedefine01 HAVE_LWP_H
1819
#cmakedefine01 HAVE_RUNETYPE_H
1920
#cmakedefine01 HAVE_GNU_LIBNAMES_H
2021
#cmakedefine01 HAVE_PRCTL_H
21-
#cmakedefine01 HAVE_PTHREAD_NP_H
2222
#cmakedefine01 HAVE_AUXV_HWCAP_H
2323
#cmakedefine01 HAVE_SYS_PTRACE_H
2424
#cmakedefine01 HAVE_SYS_UCONTEXT_H

src/coreclr/pal/src/configure.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ check_include_files(procfs.h HAVE_PROCFS_H)
4040
check_include_files(crt_externs.h HAVE_CRT_EXTERNS_H)
4141
check_include_files(sys/time.h HAVE_SYS_TIME_H)
4242
check_include_files(pthread_np.h HAVE_PTHREAD_NP_H)
43+
check_include_files(sys/membarrier.h HAVE_SYS_MEMBARRIER_H)
4344
check_include_files(sys/lwp.h HAVE_SYS_LWP_H)
4445
check_include_files(lwp.h HAVE_LWP_H)
4546
check_include_files(runetype.h HAVE_RUNETYPE_H)

src/coreclr/pal/src/thread/process.cpp

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ SET_DEFAULT_DEBUG_CHANNEL(PROCESS); // some headers have code with asserts, so d
6464
#include <vector>
6565

6666
#ifdef __linux__
67-
#include <sys/syscall.h> // __NR_membarrier
67+
#include <linux/membarrier.h>
68+
#include <sys/syscall.h>
69+
#define membarrier(...) syscall(__NR_membarrier, __VA_ARGS__)
70+
#elif HAVE_SYS_MEMBARRIER_H
71+
#include <sys/membarrier.h>
6872
#endif
6973

7074
#ifdef __APPLE__
@@ -125,27 +129,6 @@ CObjectType CorUnix::otProcess(
125129
CObjectType::NoOwner
126130
);
127131

128-
//
129-
// Helper membarrier function
130-
//
131-
#ifdef __NR_membarrier
132-
# define membarrier(...) syscall(__NR_membarrier, __VA_ARGS__)
133-
#else
134-
# define membarrier(...) -ENOSYS
135-
#endif
136-
137-
enum membarrier_cmd
138-
{
139-
MEMBARRIER_CMD_QUERY = 0,
140-
MEMBARRIER_CMD_GLOBAL = (1 << 0),
141-
MEMBARRIER_CMD_GLOBAL_EXPEDITED = (1 << 1),
142-
MEMBARRIER_CMD_REGISTER_GLOBAL_EXPEDITED = (1 << 2),
143-
MEMBARRIER_CMD_PRIVATE_EXPEDITED = (1 << 3),
144-
MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED = (1 << 4),
145-
MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE = (1 << 5),
146-
MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_SYNC_CORE = (1 << 6)
147-
};
148-
149132
//
150133
// Tracks if the OS supports FlushProcessWriteBuffers using membarrier
151134
//
@@ -2581,19 +2564,21 @@ InitializeFlushProcessWriteBuffers()
25812564
_ASSERTE(s_helperPage == 0);
25822565
_ASSERTE(s_flushUsingMemBarrier == 0);
25832566

2567+
#if defined(__linux__) || HAVE_SYS_MEMBARRIER_H
25842568
// Starting with Linux kernel 4.14, process memory barriers can be generated
25852569
// using MEMBARRIER_CMD_PRIVATE_EXPEDITED.
2586-
int mask = membarrier(MEMBARRIER_CMD_QUERY, 0);
2570+
int mask = membarrier(MEMBARRIER_CMD_QUERY, 0, 0);
25872571
if (mask >= 0 &&
25882572
mask & MEMBARRIER_CMD_PRIVATE_EXPEDITED)
25892573
{
25902574
// Register intent to use the private expedited command.
2591-
if (membarrier(MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED, 0) == 0)
2575+
if (membarrier(MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED, 0, 0) == 0)
25922576
{
25932577
s_flushUsingMemBarrier = TRUE;
25942578
return TRUE;
25952579
}
25962580
}
2581+
#endif
25972582

25982583
#ifdef TARGET_APPLE
25992584
return TRUE;
@@ -2649,12 +2634,15 @@ VOID
26492634
PALAPI
26502635
FlushProcessWriteBuffers()
26512636
{
2637+
#if defined(__linux__) || HAVE_SYS_MEMBARRIER_H
26522638
if (s_flushUsingMemBarrier)
26532639
{
2654-
int status = membarrier(MEMBARRIER_CMD_PRIVATE_EXPEDITED, 0);
2640+
int status = membarrier(MEMBARRIER_CMD_PRIVATE_EXPEDITED, 0, 0);
26552641
FATAL_ASSERT(status == 0, "Failed to flush using membarrier");
26562642
}
2657-
else if (s_helperPage != 0)
2643+
else
2644+
#endif
2645+
if (s_helperPage != 0)
26582646
{
26592647
int status = pthread_mutex_lock(&flushProcessWriteBuffersMutex);
26602648
FATAL_ASSERT(status == 0, "Failed to lock the flushProcessWriteBuffersMutex lock");

0 commit comments

Comments
 (0)