Skip to content

Commit 1118c2e

Browse files
Ericson2314tstellar
authored andcommitted
[libcxx][libcxxabi] Fix build for OpenBSD (#92186)
- No indirect syscalls on OpenBSD. Instead there is a `futex` function which issues a direct syscall. - Monotonic clock is available despite the full POSIX suite of timers not being available in its entirety. See https://lists.boost.org/boost-bugs/2015/07/41690.php and boostorg/log@c98b1f4 for a description of an analogous problem and fix for Boost. (cherry picked from commit af7467c)
1 parent 48c1364 commit 1118c2e

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

libcxx/src/atomic.cpp

+14-2
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,28 @@
2525
# if !defined(SYS_futex) && defined(SYS_futex_time64)
2626
# define SYS_futex SYS_futex_time64
2727
# endif
28+
# define _LIBCPP_FUTEX(...) syscall(SYS_futex, __VA_ARGS__)
2829

2930
#elif defined(__FreeBSD__)
3031

3132
# include <sys/types.h>
3233
# include <sys/umtx.h>
3334

35+
# define _LIBCPP_FUTEX(...) syscall(SYS_futex, __VA_ARGS__)
36+
37+
#elif defined(__OpenBSD__)
38+
39+
# include <sys/futex.h>
40+
41+
// OpenBSD has no indirect syscalls
42+
# define _LIBCPP_FUTEX(...) futex(__VA_ARGS__)
43+
3444
#else // <- Add other operating systems here
3545

3646
// Baseline needs no new headers
3747

48+
# define _LIBCPP_FUTEX(...) syscall(SYS_futex, __VA_ARGS__)
49+
3850
#endif
3951

4052
_LIBCPP_BEGIN_NAMESPACE_STD
@@ -44,11 +56,11 @@ _LIBCPP_BEGIN_NAMESPACE_STD
4456
static void
4557
__libcpp_platform_wait_on_address(__cxx_atomic_contention_t const volatile* __ptr, __cxx_contention_t __val) {
4658
static constexpr timespec __timeout = {2, 0};
47-
syscall(SYS_futex, __ptr, FUTEX_WAIT_PRIVATE, __val, &__timeout, 0, 0);
59+
_LIBCPP_FUTEX(__ptr, FUTEX_WAIT_PRIVATE, __val, &__timeout, 0, 0);
4860
}
4961

5062
static void __libcpp_platform_wake_by_address(__cxx_atomic_contention_t const volatile* __ptr, bool __notify_one) {
51-
syscall(SYS_futex, __ptr, FUTEX_WAKE_PRIVATE, __notify_one ? 1 : INT_MAX, 0, 0, 0);
63+
_LIBCPP_FUTEX(__ptr, FUTEX_WAKE_PRIVATE, __notify_one ? 1 : INT_MAX, 0, 0, 0);
5264
}
5365

5466
#elif defined(__APPLE__) && defined(_LIBCPP_USE_ULOCK)

libcxx/src/chrono.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
# include <sys/time.h> // for gettimeofday and timeval
3232
#endif
3333

34-
#if defined(__APPLE__) || defined(__gnu_hurd__) || (defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0)
34+
// OpenBSD does not have a fully conformant suite of POSIX timers, but
35+
// it does have clock_gettime and CLOCK_MONOTONIC which is all we need.
36+
#if defined(__APPLE__) || defined(__gnu_hurd__) || defined(__OpenBSD__) || (defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0)
3537
# define _LIBCPP_HAS_CLOCK_GETTIME
3638
#endif
3739

libcxxabi/src/cxa_guard_impl.h

+15-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@
4747
#include "__cxxabi_config.h"
4848
#include "include/atomic_support.h" // from libc++
4949
#if defined(__has_include)
50+
# if __has_include(<sys/futex.h>)
51+
# include <sys/futex.h>
52+
# endif
5053
# if __has_include(<sys/syscall.h>)
5154
# include <sys/syscall.h>
5255
# endif
@@ -411,7 +414,18 @@ struct InitByteGlobalMutex {
411414
// Futex Implementation
412415
//===----------------------------------------------------------------------===//
413416

414-
#if defined(SYS_futex)
417+
#if defined(__OpenBSD__)
418+
void PlatformFutexWait(int* addr, int expect) {
419+
constexpr int WAIT = 0;
420+
futex(reinterpret_cast<volatile uint32_t*>(addr), WAIT, expect, NULL, NULL);
421+
__tsan_acquire(addr);
422+
}
423+
void PlatformFutexWake(int* addr) {
424+
constexpr int WAKE = 1;
425+
__tsan_release(addr);
426+
futex(reinterpret_cast<volatile uint32_t*>(addr), WAKE, INT_MAX, NULL, NULL);
427+
}
428+
#elif defined(SYS_futex)
415429
void PlatformFutexWait(int* addr, int expect) {
416430
constexpr int WAIT = 0;
417431
syscall(SYS_futex, addr, WAIT, expect, 0);

0 commit comments

Comments
 (0)