authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-02-29 22:13:09-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-02-29 22:13:09-05:00
log6fcf6716be3021d99d4972e8ca9b2139e1f8f40b
treece59f492173f2bf1a8803701fa7a26a83ec1dca3
parentd951e0402a3567dff4f139b3cb4ad404374622cd
signaturelock-open Commit is signed but in an unrecognized format.

std.Thread.cpuCount on Windows uses the PEB

rather than calling GetSystemInfo from kernel32.dll. Also remove OutOfMemory from the error set.

1 files changed, 14 insertions(+), 17 deletions(-)

lib/std/thread.zig+14-17
...@@ -1,5 +1,5 @@...@@ -1,5 +1,5 @@
1const builtin = @import("builtin");
2const std = @import("std.zig");1const std = @import("std.zig");
2const builtin = std.builtin;
3const os = std.os;3const os = std.os;
4const mem = std.mem;4const mem = std.mem;
5const windows = std.os.windows;5const windows = std.os.windows;
...@@ -9,14 +9,14 @@ const assert = std.debug.assert;...@@ -9,14 +9,14 @@ const assert = std.debug.assert;
9pub const Thread = struct {9pub const Thread = struct {
10 data: Data,10 data: Data,
1111
12 pub const use_pthreads = builtin.os.tag != .windows and builtin.link_libc;12 pub const use_pthreads = std.Target.current.os.tag != .windows and builtin.link_libc;
1313
14 /// Represents a kernel thread handle.14 /// Represents a kernel thread handle.
15 /// May be an integer or a pointer depending on the platform.15 /// May be an integer or a pointer depending on the platform.
16 /// On Linux and POSIX, this is the same as Id.16 /// On Linux and POSIX, this is the same as Id.
17 pub const Handle = if (use_pthreads)17 pub const Handle = if (use_pthreads)
18 c.pthread_t18 c.pthread_t
19 else switch (builtin.os.tag) {19 else switch (std.Target.current.os.tag) {
20 .linux => i32,20 .linux => i32,
21 .windows => windows.HANDLE,21 .windows => windows.HANDLE,
22 else => void,22 else => void,
...@@ -25,7 +25,7 @@ pub const Thread = struct {...@@ -25,7 +25,7 @@ pub const Thread = struct {
25 /// Represents a unique ID per thread.25 /// Represents a unique ID per thread.
26 /// May be an integer or pointer depending on the platform.26 /// May be an integer or pointer depending on the platform.
27 /// On Linux and POSIX, this is the same as Handle.27 /// On Linux and POSIX, this is the same as Handle.
28 pub const Id = switch (builtin.os.tag) {28 pub const Id = switch (std.Target.current.os.tag) {
29 .windows => windows.DWORD,29 .windows => windows.DWORD,
30 else => Handle,30 else => Handle,
31 };31 };
...@@ -35,7 +35,7 @@ pub const Thread = struct {...@@ -35,7 +35,7 @@ pub const Thread = struct {
35 handle: Thread.Handle,35 handle: Thread.Handle,
36 memory: []align(mem.page_size) u8,36 memory: []align(mem.page_size) u8,
37 }37 }
38 else switch (builtin.os.tag) {38 else switch (std.Target.current.os.tag) {
39 .linux => struct {39 .linux => struct {
40 handle: Thread.Handle,40 handle: Thread.Handle,
41 memory: []align(mem.page_size) u8,41 memory: []align(mem.page_size) u8,
...@@ -55,7 +55,7 @@ pub const Thread = struct {...@@ -55,7 +55,7 @@ pub const Thread = struct {
55 if (use_pthreads) {55 if (use_pthreads) {
56 return c.pthread_self();56 return c.pthread_self();
57 } else57 } else
58 return switch (builtin.os.tag) {58 return switch (std.Target.current.os.tag) {
59 .linux => os.linux.gettid(),59 .linux => os.linux.gettid(),
60 .windows => windows.kernel32.GetCurrentThreadId(),60 .windows => windows.kernel32.GetCurrentThreadId(),
61 else => @compileError("Unsupported OS"),61 else => @compileError("Unsupported OS"),
...@@ -83,7 +83,7 @@ pub const Thread = struct {...@@ -83,7 +83,7 @@ pub const Thread = struct {
83 else => unreachable,83 else => unreachable,
84 }84 }
85 os.munmap(self.data.memory);85 os.munmap(self.data.memory);
86 } else switch (builtin.os.tag) {86 } else switch (std.Target.current.os.tag) {
87 .linux => {87 .linux => {
88 while (true) {88 while (true) {
89 const pid_value = @atomicLoad(i32, &self.data.handle, .SeqCst);89 const pid_value = @atomicLoad(i32, &self.data.handle, .SeqCst);
...@@ -150,7 +150,7 @@ pub const Thread = struct {...@@ -150,7 +150,7 @@ pub const Thread = struct {
150 const Context = @TypeOf(context);150 const Context = @TypeOf(context);
151 comptime assert(@typeInfo(@TypeOf(startFn)).Fn.args[0].arg_type.? == Context);151 comptime assert(@typeInfo(@TypeOf(startFn)).Fn.args[0].arg_type.? == Context);
152152
153 if (builtin.os.tag == .windows) {153 if (std.Target.current.os.tag == .windows) {
154 const WinThread = struct {154 const WinThread = struct {
155 const OuterContext = struct {155 const OuterContext = struct {
156 thread: Thread,156 thread: Thread,
...@@ -309,16 +309,16 @@ pub const Thread = struct {...@@ -309,16 +309,16 @@ pub const Thread = struct {
309 os.EINVAL => unreachable,309 os.EINVAL => unreachable,
310 else => return os.unexpectedErrno(@intCast(usize, err)),310 else => return os.unexpectedErrno(@intCast(usize, err)),
311 }311 }
312 } else if (builtin.os.tag == .linux) {312 } else if (std.Target.current.os.tag == .linux) {
313 var flags: u32 = os.CLONE_VM | os.CLONE_FS | os.CLONE_FILES | os.CLONE_SIGHAND |313 var flags: u32 = os.CLONE_VM | os.CLONE_FS | os.CLONE_FILES | os.CLONE_SIGHAND |
314 os.CLONE_THREAD | os.CLONE_SYSVSEM | os.CLONE_PARENT_SETTID | os.CLONE_CHILD_CLEARTID |314 os.CLONE_THREAD | os.CLONE_SYSVSEM | os.CLONE_PARENT_SETTID | os.CLONE_CHILD_CLEARTID |
315 os.CLONE_DETACHED;315 os.CLONE_DETACHED;
316 var newtls: usize = undefined;316 var newtls: usize = undefined;
317 // This structure is only needed when targeting i386317 // This structure is only needed when targeting i386
318 var user_desc: if (builtin.arch == .i386) os.linux.user_desc else void = undefined;318 var user_desc: if (std.Target.current.cpu.arch == .i386) os.linux.user_desc else void = undefined;
319319
320 if (os.linux.tls.tls_image) |tls_img| {320 if (os.linux.tls.tls_image) |tls_img| {
321 if (builtin.arch == .i386) {321 if (std.Target.current.cpu.arch == .i386) {
322 user_desc = os.linux.user_desc{322 user_desc = os.linux.user_desc{
323 .entry_number = tls_img.gdt_entry_number,323 .entry_number = tls_img.gdt_entry_number,
324 .base_addr = os.linux.tls.copyTLS(mmap_addr + tls_start_offset),324 .base_addr = os.linux.tls.copyTLS(mmap_addr + tls_start_offset),
...@@ -362,21 +362,18 @@ pub const Thread = struct {...@@ -362,21 +362,18 @@ pub const Thread = struct {
362 }362 }
363363
364 pub const CpuCountError = error{364 pub const CpuCountError = error{
365 OutOfMemory,
366 PermissionDenied,365 PermissionDenied,
367 SystemResources,366 SystemResources,
368 Unexpected,367 Unexpected,
369 };368 };
370369
371 pub fn cpuCount() CpuCountError!usize {370 pub fn cpuCount() CpuCountError!usize {
372 if (builtin.os.tag == .linux) {371 if (std.Target.current.os.tag == .linux) {
373 const cpu_set = try os.sched_getaffinity(0);372 const cpu_set = try os.sched_getaffinity(0);
374 return @as(usize, os.CPU_COUNT(cpu_set)); // TODO should not need this usize cast373 return @as(usize, os.CPU_COUNT(cpu_set)); // TODO should not need this usize cast
375 }374 }
376 if (builtin.os.tag == .windows) {375 if (std.Target.current.os.tag == .windows) {
377 var system_info: windows.SYSTEM_INFO = undefined;376 return os.windows.peb().NumberOfProcessors;
378 windows.kernel32.GetSystemInfo(&system_info);
379 return @intCast(usize, system_info.dwNumberOfProcessors);
380 }377 }
381 var count: c_int = undefined;378 var count: c_int = undefined;
382 var count_len: usize = @sizeOf(c_int);379 var count_len: usize = @sizeOf(c_int);