Skip to content

Commit ffb9cc2

Browse files
committed
Cleanup membarrier portability
Fixes #111776
1 parent a9d67ec commit ffb9cc2

File tree

6 files changed

+41
-44
lines changed

6 files changed

+41
-44
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: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
#include <sys/swap.h>
3030
#endif
3131

32+
#if HAVE_SYS_MEMBARRIER_H
33+
#include <sys/membarrier.h>
34+
#endif
35+
3236
#include <sys/resource.h>
3337

3438
#undef min
@@ -95,7 +99,9 @@ extern "C"
9599
#endif // __HAIKU__
96100

97101
#ifdef __linux__
98-
#include <sys/syscall.h> // __NR_membarrier
102+
// Helper membarrier function
103+
#include <sys/syscall.h>
104+
#define membarrier(...) syscall(__NR_membarrier, __VA_ARGS__)
99105
#endif
100106

101107
#if HAVE_PTHREAD_NP_H
@@ -132,31 +138,15 @@ typedef cpuset_t cpu_set_t;
132138
// The cached total number of CPUs that can be used in the OS.
133139
static uint32_t g_totalCpuCount = 0;
134140

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-
156141
bool CanFlushUsingMembarrier()
157142
{
158143

159144
#ifdef TARGET_ANDROID
145+
146+
#ifndef __NR_membarrier
147+
#error Android!
148+
#endif
149+
160150
// Avoid calling membarrier on older Android versions where membarrier
161151
// may be barred by seccomp causing the process to be killed.
162152
int apiLevel = android_get_device_api_level();
@@ -166,18 +156,20 @@ bool CanFlushUsingMembarrier()
166156
}
167157
#endif
168158

159+
#if HAVE_SYS_MEMBARRIER_H
169160
// Starting with Linux kernel 4.14, process memory barriers can be generated
170161
// using MEMBARRIER_CMD_PRIVATE_EXPEDITED.
171162

172-
int mask = membarrier(MEMBARRIER_CMD_QUERY, 0);
163+
int mask = membarrier(MEMBARRIER_CMD_QUERY, 0, 0);
173164

174165
if (mask >= 0 &&
175166
mask & MEMBARRIER_CMD_PRIVATE_EXPEDITED &&
176167
// Register intent to use the private expedited command.
177-
membarrier(MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED, 0) == 0)
168+
membarrier(MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED, 0, 0) == 0)
178169
{
179170
return true;
180171
}
172+
#endif
181173

182174
return false;
183175
}
@@ -423,12 +415,15 @@ bool GCToOSInterface::CanGetCurrentProcessorNumber()
423415
// Flush write buffers of processors that are executing threads of the current process
424416
void GCToOSInterface::FlushProcessWriteBuffers()
425417
{
418+
#if HAVE_SYS_MEMBARRIER_H
426419
if (s_flushUsingMemBarrier)
427420
{
428-
int status = membarrier(MEMBARRIER_CMD_PRIVATE_EXPEDITED, 0);
421+
int status = membarrier(MEMBARRIER_CMD_PRIVATE_EXPEDITED, 0, 0);
429422
assert(status == 0 && "Failed to flush using membarrier");
430423
}
431-
else if (g_helperPage != 0)
424+
else
425+
#endif
426+
if (g_helperPage != 0)
432427
{
433428
int status = pthread_mutex_lock(&g_flushProcessWriteBuffersMutex);
434429
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: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,14 @@ SET_DEFAULT_DEBUG_CHANNEL(PROCESS); // some headers have code with asserts, so d
6363
#include <limits.h>
6464
#include <vector>
6565

66+
#if HAVE_SYS_MEMBARRIER_H
67+
#include <sys/membarrier.h>
68+
#endif
69+
6670
#ifdef __linux__
67-
#include <sys/syscall.h> // __NR_membarrier
71+
// Helper membarrier function
72+
#include <sys/syscall.h>
73+
#define membarrier(...) syscall(__NR_membarrier, __VA_ARGS__)
6874
#endif
6975

7076
#ifdef __APPLE__
@@ -134,18 +140,6 @@ CObjectType CorUnix::otProcess(
134140
# define membarrier(...) -ENOSYS
135141
#endif
136142

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-
149143
//
150144
// Tracks if the OS supports FlushProcessWriteBuffers using membarrier
151145
//
@@ -2581,19 +2575,21 @@ InitializeFlushProcessWriteBuffers()
25812575
_ASSERTE(s_helperPage == 0);
25822576
_ASSERTE(s_flushUsingMemBarrier == 0);
25832577

2578+
#if HAVE_SYS_MEMBARRIER_H
25842579
// Starting with Linux kernel 4.14, process memory barriers can be generated
25852580
// using MEMBARRIER_CMD_PRIVATE_EXPEDITED.
2586-
int mask = membarrier(MEMBARRIER_CMD_QUERY, 0);
2581+
int mask = membarrier(MEMBARRIER_CMD_QUERY, 0, 0);
25872582
if (mask >= 0 &&
25882583
mask & MEMBARRIER_CMD_PRIVATE_EXPEDITED)
25892584
{
25902585
// Register intent to use the private expedited command.
2591-
if (membarrier(MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED, 0) == 0)
2586+
if (membarrier(MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED, 0, 0) == 0)
25922587
{
25932588
s_flushUsingMemBarrier = TRUE;
25942589
return TRUE;
25952590
}
25962591
}
2592+
#endif
25972593

25982594
#ifdef TARGET_APPLE
25992595
return TRUE;
@@ -2649,12 +2645,15 @@ VOID
26492645
PALAPI
26502646
FlushProcessWriteBuffers()
26512647
{
2648+
#if HAVE_SYS_MEMBARRIER_H
26522649
if (s_flushUsingMemBarrier)
26532650
{
2654-
int status = membarrier(MEMBARRIER_CMD_PRIVATE_EXPEDITED, 0);
2651+
int status = membarrier(MEMBARRIER_CMD_PRIVATE_EXPEDITED, 0, 0);
26552652
FATAL_ASSERT(status == 0, "Failed to flush using membarrier");
26562653
}
2657-
else if (s_helperPage != 0)
2654+
else
2655+
#endif
2656+
if (s_helperPage != 0)
26582657
{
26592658
int status = pthread_mutex_lock(&flushProcessWriteBuffersMutex);
26602659
FATAL_ASSERT(status == 0, "Failed to lock the flushProcessWriteBuffersMutex lock");

0 commit comments

Comments
 (0)