std.Thread.Futex improvements (#11464)

* atomic: cache_line

* Thread: Futex rewrite + more native platform support

* Futex: tests compile

* Futex: compiles and runs test

* Futex: broadcast test

* Futex: fix PosixImpl for tests

* Futex: fix compile errors for bsd platforms

* Futex: review changes + fix timeout=0 + more comments
This commit is contained in:
protty 2022-04-19 19:42:15 -05:00 committed by GitHub
parent 2fa7f6e502
commit e3cbea934e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 882 additions and 404 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,5 @@
const std = @import("std.zig");
const target = @import("builtin").target;
const builtin = @import("builtin");
pub const Ordering = std.builtin.AtomicOrder;
@ -40,7 +40,7 @@ test "fence/compilerFence" {
/// Signals to the processor that the caller is inside a busy-wait spin-loop.
pub inline fn spinLoopHint() void {
switch (target.cpu.arch) {
switch (builtin.target.cpu.arch) {
// No-op instruction that can hint to save (or share with a hardware-thread)
// pipelining/power resources
// https://software.intel.com/content/www/us/en/develop/articles/benefitting-power-and-performance-sleep-loops.html
@ -59,7 +59,7 @@ pub inline fn spinLoopHint() void {
// `yield` was introduced in v6k but is also available on v6m.
// https://www.keil.com/support/man/docs/armasm/armasm_dom1361289926796.htm
.arm, .armeb, .thumb, .thumbeb => {
const can_yield = comptime std.Target.arm.featureSetHasAny(target.cpu.features, .{
const can_yield = comptime std.Target.arm.featureSetHasAny(builtin.target.cpu.features, .{
.has_v6k, .has_v6m,
});
if (can_yield) {
@ -80,3 +80,41 @@ test "spinLoopHint" {
spinLoopHint();
}
}
/// The estimated size of the CPU's cache line when atomically updating memory.
/// Add this much padding or align to this boundary to avoid atomically-updated
/// memory from forcing cache invalidations on near, but non-atomic, memory.
///
// https://en.wikipedia.org/wiki/False_sharing
// https://github.com/golang/go/search?q=CacheLinePadSize
pub const cache_line = switch (builtin.cpu.arch) {
// x86_64: Starting from Intel's Sandy Bridge, the spatial prefetcher pulls in pairs of 64-byte cache lines at a time.
// - https://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-optimization-manual.pdf
// - https://github.com/facebook/folly/blob/1b5288e6eea6df074758f877c849b6e73bbb9fbb/folly/lang/Align.h#L107
//
// aarch64: Some big.LITTLE ARM archs have "big" cores with 128-byte cache lines:
// - https://www.mono-project.com/news/2016/09/12/arm64-icache/
// - https://cpufun.substack.com/p/more-m1-fun-hardware-information
//
// powerpc64: PPC has 128-byte cache lines
// - https://github.com/golang/go/blob/3dd58676054223962cd915bb0934d1f9f489d4d2/src/internal/cpu/cpu_ppc64x.go#L9
.x86_64, .aarch64, .powerpc64 => 128,
// These platforms reportedly have 32-byte cache lines
// - https://github.com/golang/go/blob/3dd58676054223962cd915bb0934d1f9f489d4d2/src/internal/cpu/cpu_arm.go#L7
// - https://github.com/golang/go/blob/3dd58676054223962cd915bb0934d1f9f489d4d2/src/internal/cpu/cpu_mips.go#L7
// - https://github.com/golang/go/blob/3dd58676054223962cd915bb0934d1f9f489d4d2/src/internal/cpu/cpu_mipsle.go#L7
// - https://github.com/golang/go/blob/3dd58676054223962cd915bb0934d1f9f489d4d2/src/internal/cpu/cpu_mips64x.go#L9
// - https://github.com/golang/go/blob/3dd58676054223962cd915bb0934d1f9f489d4d2/src/internal/cpu/cpu_riscv64.go#L7
.arm, .mips, .mips64, .riscv64 => 32,
// This platform reportedly has 256-byte cache lines
// - https://github.com/golang/go/blob/3dd58676054223962cd915bb0934d1f9f489d4d2/src/internal/cpu/cpu_s390x.go#L7
.s390x => 256,
// Other x86 and WASM platforms have 64-byte cache lines.
// The rest of the architectures are assumed to be similar.
// - https://github.com/golang/go/blob/dda2991c2ea0c5914714469c4defc2562a907230/src/internal/cpu/cpu_x86.go#L9
// - https://github.com/golang/go/blob/3dd58676054223962cd915bb0934d1f9f489d4d2/src/internal/cpu/cpu_wasm.go#L7
else => 64,
};

View File

@ -36,6 +36,9 @@ pub const sem_t = ?*opaque {};
pub extern "c" fn pthread_setname_np(thread: std.c.pthread_t, name: [*:0]const u8) E;
pub extern "c" fn pthread_getname_np(thread: std.c.pthread_t, name: [*:0]u8, len: usize) E;
pub extern "c" fn umtx_sleep(ptr: *const volatile c_int, value: c_int, timeout: c_int) c_int;
pub extern "c" fn umtx_wakeup(ptr: *const volatile c_int, count: c_int) c_int;
// See:
// - https://gitweb.dragonflybsd.org/dragonfly.git/blob/HEAD:/include/unistd.h
// - https://gitweb.dragonflybsd.org/dragonfly.git/blob/HEAD:/sys/sys/types.h

View File

@ -62,6 +62,46 @@ pub const sem_t = extern struct {
_padding: u32,
};
// https://github.com/freebsd/freebsd-src/blob/main/sys/sys/umtx.h
pub const UMTX_OP = enum(c_int) {
LOCK = 0,
UNLOCK = 1,
WAIT = 2,
WAKE = 3,
MUTEX_TRYLOCK = 4,
MUTEX_LOCK = 5,
MUTEX_UNLOCK = 6,
SET_CEILING = 7,
CV_WAIT = 8,
CV_SIGNAL = 9,
CV_BROADCAST = 10,
WAIT_UINT = 11,
RW_RDLOCK = 12,
RW_WRLOCK = 13,
RW_UNLOCK = 14,
WAIT_UINT_PRIVATE = 15,
WAKE_PRIVATE = 16,
MUTEX_WAIT = 17,
MUTEX_WAKE = 18, // deprecated
SEM_WAIT = 19, // deprecated
SEM_WAKE = 20, // deprecated
NWAKE_PRIVATE = 31,
MUTEX_WAKE2 = 22,
SEM2_WAIT = 23,
SEM2_WAKE = 24,
SHM = 25,
ROBUST_LISTS = 26,
};
pub const UMTX_ABSTIME = 0x01;
pub const _umtx_time = extern struct {
_timeout: timespec,
_flags: u32,
_clockid: u32,
};
pub extern "c" fn _umtx_op(obj: usize, op: c_int, val: c_ulong, uaddr: usize, uaddr2: usize) c_int;
pub const EAI = enum(c_int) {
/// address family for hostname not supported
ADDRFAMILY = 1,

View File

@ -45,6 +45,13 @@ pub extern "c" fn unveil(path: ?[*:0]const u8, permissions: ?[*:0]const u8) c_in
pub extern "c" fn pthread_set_name_np(thread: std.c.pthread_t, name: [*:0]const u8) void;
pub extern "c" fn pthread_get_name_np(thread: std.c.pthread_t, name: [*:0]u8, len: usize) void;
// https://github.com/openbsd/src/blob/2207c4325726fdc5c4bcd0011af0fdf7d3dab137/sys/sys/futex.h
pub const FUTEX_WAIT = 1;
pub const FUTEX_WAKE = 2;
pub const FUTEX_REQUEUE = 3;
pub const FUTEX_PRIVATE_FLAG = 128;
pub extern "c" fn futex(uaddr: ?*const volatile u32, op: c_int, val: c_int, timeout: ?*const timespec, uaddr2: ?*const volatile u32) c_int;
pub const login_cap_t = extern struct {
lc_class: ?[*:0]const u8,
lc_cap: ?[*:0]const u8,