Skip to content

Commit 6fcf671

Browse files
committed
std.Thread.cpuCount on Windows uses the PEB
rather than calling GetSystemInfo from kernel32.dll. Also remove OutOfMemory from the error set.
1 parent d951e04 commit 6fcf671

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

lib/std/thread.zig

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const builtin = @import("builtin");
21
const std = @import("std.zig");
2+
const builtin = std.builtin;
33
const os = std.os;
44
const mem = std.mem;
55
const windows = std.os.windows;
@@ -9,14 +9,14 @@ const assert = std.debug.assert;
99
pub const Thread = struct {
1010
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

1414
/// Represents a kernel thread handle.
1515
/// May be an integer or a pointer depending on the platform.
1616
/// On Linux and POSIX, this is the same as Id.
1717
pub const Handle = if (use_pthreads)
1818
c.pthread_t
19-
else switch (builtin.os.tag) {
19+
else switch (std.Target.current.os.tag) {
2020
.linux => i32,
2121
.windows => windows.HANDLE,
2222
else => void,
@@ -25,7 +25,7 @@ pub const Thread = struct {
2525
/// Represents a unique ID per thread.
2626
/// May be an integer or pointer depending on the platform.
2727
/// 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) {
2929
.windows => windows.DWORD,
3030
else => Handle,
3131
};
@@ -35,7 +35,7 @@ pub const Thread = struct {
3535
handle: Thread.Handle,
3636
memory: []align(mem.page_size) u8,
3737
}
38-
else switch (builtin.os.tag) {
38+
else switch (std.Target.current.os.tag) {
3939
.linux => struct {
4040
handle: Thread.Handle,
4141
memory: []align(mem.page_size) u8,
@@ -55,7 +55,7 @@ pub const Thread = struct {
5555
if (use_pthreads) {
5656
return c.pthread_self();
5757
} else
58-
return switch (builtin.os.tag) {
58+
return switch (std.Target.current.os.tag) {
5959
.linux => os.linux.gettid(),
6060
.windows => windows.kernel32.GetCurrentThreadId(),
6161
else => @compileError("Unsupported OS"),
@@ -83,7 +83,7 @@ pub const Thread = struct {
8383
else => unreachable,
8484
}
8585
os.munmap(self.data.memory);
86-
} else switch (builtin.os.tag) {
86+
} else switch (std.Target.current.os.tag) {
8787
.linux => {
8888
while (true) {
8989
const pid_value = @atomicLoad(i32, &self.data.handle, .SeqCst);
@@ -150,7 +150,7 @@ pub const Thread = struct {
150150
const Context = @TypeOf(context);
151151
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) {
154154
const WinThread = struct {
155155
const OuterContext = struct {
156156
thread: Thread,
@@ -309,16 +309,16 @@ pub const Thread = struct {
309309
os.EINVAL => unreachable,
310310
else => return os.unexpectedErrno(@intCast(usize, err)),
311311
}
312-
} else if (builtin.os.tag == .linux) {
312+
} else if (std.Target.current.os.tag == .linux) {
313313
var flags: u32 = os.CLONE_VM | os.CLONE_FS | os.CLONE_FILES | os.CLONE_SIGHAND |
314314
os.CLONE_THREAD | os.CLONE_SYSVSEM | os.CLONE_PARENT_SETTID | os.CLONE_CHILD_CLEARTID |
315315
os.CLONE_DETACHED;
316316
var newtls: usize = undefined;
317317
// 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

320320
if (os.linux.tls.tls_image) |tls_img| {
321-
if (builtin.arch == .i386) {
321+
if (std.Target.current.cpu.arch == .i386) {
322322
user_desc = os.linux.user_desc{
323323
.entry_number = tls_img.gdt_entry_number,
324324
.base_addr = os.linux.tls.copyTLS(mmap_addr + tls_start_offset),
@@ -362,21 +362,18 @@ pub const Thread = struct {
362362
}
363363

364364
pub const CpuCountError = error{
365-
OutOfMemory,
366365
PermissionDenied,
367366
SystemResources,
368367
Unexpected,
369368
};
370369

371370
pub fn cpuCount() CpuCountError!usize {
372-
if (builtin.os.tag == .linux) {
371+
if (std.Target.current.os.tag == .linux) {
373372
const cpu_set = try os.sched_getaffinity(0);
374373
return @as(usize, os.CPU_COUNT(cpu_set)); // TODO should not need this usize cast
375374
}
376-
if (builtin.os.tag == .windows) {
377-
var system_info: windows.SYSTEM_INFO = undefined;
378-
windows.kernel32.GetSystemInfo(&system_info);
379-
return @intCast(usize, system_info.dwNumberOfProcessors);
375+
if (std.Target.current.os.tag == .windows) {
376+
return os.windows.peb().NumberOfProcessors;
380377
}
381378
var count: c_int = undefined;
382379
var count_len: usize = @sizeOf(c_int);

0 commit comments

Comments
 (0)