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