Commit 41502c6aa5

David CARLIER <devnexen@gmail.com>
2023-05-25 21:32:17
std.Thread: refining stack size from platform minimum, changes more targetted towards platform like Linux/musl (#15791)
1 parent 230ea41
lib/std/c/darwin.zig
@@ -4155,3 +4155,9 @@ pub const vm_extmod_statistics_t = *vm_extmod_statistics;
 pub const vm_extmod_statistics_data_t = vm_extmod_statistics;
 
 pub extern "c" fn vm_stats(info: ?*anyopaque, count: *c_uint) kern_return_t;
+
+/// TODO refines if necessary
+pub const PTHREAD_STACK_MIN = switch (builtin.cpu.arch) {
+    .arm, .aarch64 => 16 * 1024,
+    else => 8 * 1024,
+};
lib/std/c/dragonfly.zig
@@ -1160,3 +1160,5 @@ pub const sigevent = extern struct {
     sigev_value: sigval,
     sigev_notify_function: ?*const fn (sigval) callconv(.C) void,
 };
+
+pub const PTHREAD_STACK_MIN = 16 * 1024;
lib/std/c/emscripten.zig
@@ -9,3 +9,5 @@ pub const pthread_rwlock_t = extern struct {
 };
 const __SIZEOF_PTHREAD_COND_T = 48;
 const __SIZEOF_PTHREAD_MUTEX_T = 28;
+
+pub const PTHREAD_STACK_MIN = 2048;
lib/std/c/freebsd.zig
@@ -2818,3 +2818,9 @@ pub const ptrace_cs_remote = extern struct {
 };
 
 pub extern "c" fn ptrace(request: c_int, pid: pid_t, addr: [*:0]u8, data: c_int) c_int;
+
+/// TODO refines if necessary
+pub const PTHREAD_STACK_MIN = switch (builtin.cpu.arch) {
+    .x86, .powerpc => 4 * 512,
+    else => 4 * 1024,
+};
lib/std/c/haiku.zig
@@ -1068,3 +1068,6 @@ pub const sigevent = extern struct {
     sigev_notify_function: ?*const fn (sigval) callconv(.C) void,
     sigev_notify_attributes: ?*pthread_attr_t,
 };
+
+/// TODO refines if necessary
+pub const PTHREAD_STACK_MIN = 2 * 4096;
lib/std/c/linux.zig
@@ -350,6 +350,18 @@ const __SIZEOF_PTHREAD_MUTEX_T = switch (native_abi) {
 };
 const __SIZEOF_SEM_T = 4 * @sizeOf(usize);
 
+/// TODO refines if necessary
+pub const PTHREAD_STACK_MIN = switch (native_abi) {
+    .musl, .musleabi, .musleabihf => 2048,
+    .gnu, .gnuabin32, .gnuabi64, .gnueabi, .gnueabihf, .gnux32 => switch (native_arch) {
+        .aarch64, .arm, .armeb, .powerpc, .powerpc64, .powerpc64le, .loongarch32, .loongarch64 => 131072,
+        .sparc64 => 24576,
+        else => 16 * 1024,
+    },
+    .android => if (@sizeOf(usize) == 8) 16 * 1024 else 8 * 1024,
+    else => 16 * 1024,
+};
+
 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;
 
lib/std/c/minix.zig
@@ -16,3 +16,5 @@ const __SIZEOF_PTHREAD_MUTEX_T = switch (builtin.abi) {
     },
     else => unreachable,
 };
+
+pub const PTHREAD_STACK_MIN = 16 * 1024;
lib/std/c/netbsd.zig
@@ -1720,3 +1720,6 @@ pub const PIOD = struct {
 };
 
 pub extern "c" fn ptrace(request: c_int, pid: pid_t, addr: ?*anyopaque, data: c_int) c_int;
+
+/// TODO refines if necessary
+pub const PTHREAD_STACK_MIN = 16 * 1024;
lib/std/c/openbsd.zig
@@ -1631,3 +1631,10 @@ pub const HW_ALLOWPOWERDOWN = 22;
 pub const HW_PERFPOLICY = 23;
 pub const HW_SMT = 24;
 pub const HW_NCPUONLINE = 25;
+
+/// TODO refines if necessary
+pub const PTHREAD_STACK_MIN = switch (builtin.cpu.arch) {
+    .sparc64 => 1 << 13,
+    .mips64 => 1 << 14,
+    else => 1 << 12,
+};
lib/std/c/solaris.zig
@@ -1946,3 +1946,5 @@ pub const sigevent = extern struct {
     sigev_notify_function: ?*const fn (sigval) callconv(.C) void,
     sigev_notify_attributes: ?*pthread_attr_t,
 };
+
+pub const PTHREAD_STACK_MIN = if (@sizeOf(usize) == 8) 8 * 1024 else 4 * 1024;
lib/std/c/wasi.zig
@@ -119,3 +119,5 @@ pub const POLL = struct {
     pub const HUP = 0x2000;
     pub const NVAL = 0x4000;
 };
+
+pub const PTHREAD_STACK_MIN = 16 * 1024;
lib/std/Thread.zig
@@ -690,7 +690,7 @@ const PosixThreadImpl = struct {
         defer assert(c.pthread_attr_destroy(&attr) == .SUCCESS);
 
         // Use the same set of parameters used by the libc-less impl.
-        const stack_size = std.math.max(config.stack_size, 16 * 1024);
+        const stack_size = std.math.max(config.stack_size, c.PTHREAD_STACK_MIN);
         assert(c.pthread_attr_setstacksize(&attr, stack_size) == .SUCCESS);
         assert(c.pthread_attr_setguardsize(&attr, std.mem.page_size) == .SUCCESS);