authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-07-09 13:19:11-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-07-09 13:19:11-04:00
logcaa008505729f9511f6f0b070636013e9597b3f7
tree976dcc5d0c2c5bb4a51bbf5636f1ef489dbe5219
parenta0c564d7621c1bf3f83be59d6f91056b0cfe1e16

implement std.os.cpuCount for windows


4 files changed, 31 insertions(+), 2 deletions(-)

std/event.zig+3
...@@ -142,6 +142,9 @@ pub const Loop = struct {...@@ -142,6 +142,9 @@ pub const Loop = struct {
142 epoll_op: u32,142 epoll_op: u32,
143 eventfd: i32,143 eventfd: i32,
144 },144 },
145 builtin.Os.windows => struct {
146 base: ResumeNode,
147 },
145 else => @compileError("unsupported OS"),148 else => @compileError("unsupported OS"),
146 };149 };
147150
std/heap.zig+2-2
...@@ -96,12 +96,12 @@ pub const DirectAllocator = struct {...@@ -96,12 +96,12 @@ pub const DirectAllocator = struct {
96 },96 },
97 Os.windows => {97 Os.windows => {
98 const amt = n + alignment + @sizeOf(usize);98 const amt = n + alignment + @sizeOf(usize);
99 const optional_heap_handle = @atomicLoad(?HeapHandle, ?self.heap_handle, builtin.AtomicOrder.SeqCst);99 const optional_heap_handle = @atomicLoad(?HeapHandle, &self.heap_handle, builtin.AtomicOrder.SeqCst);
100 const heap_handle = optional_heap_handle orelse blk: {100 const heap_handle = optional_heap_handle orelse blk: {
101 const hh = os.windows.HeapCreate(os.windows.HEAP_NO_SERIALIZE, amt, 0) orelse return error.OutOfMemory;101 const hh = os.windows.HeapCreate(os.windows.HEAP_NO_SERIALIZE, amt, 0) orelse return error.OutOfMemory;
102 const other_hh = @cmpxchgStrong(?HeapHandle, &self.heap_handle, null, hh, builtin.AtomicOrder.SeqCst, builtin.AtomicOrder.SeqCst) orelse break :blk hh;102 const other_hh = @cmpxchgStrong(?HeapHandle, &self.heap_handle, null, hh, builtin.AtomicOrder.SeqCst, builtin.AtomicOrder.SeqCst) orelse break :blk hh;
103 _ = os.windows.HeapDestroy(hh);103 _ = os.windows.HeapDestroy(hh);
104 break :blk other_hh;104 break :blk other_hh.?; // can't be null because of the cmpxchg
105 };105 };
106 const ptr = os.windows.HeapAlloc(heap_handle, 0, amt) orelse return error.OutOfMemory;106 const ptr = os.windows.HeapAlloc(heap_handle, 0, amt) orelse return error.OutOfMemory;
107 const root_addr = @ptrToInt(ptr);107 const root_addr = @ptrToInt(ptr);
std/os/index.zig+5
...@@ -2806,6 +2806,11 @@ pub fn cpuCount(fallback_allocator: *mem.Allocator) CpuCountError!usize {...@@ -2806,6 +2806,11 @@ pub fn cpuCount(fallback_allocator: *mem.Allocator) CpuCountError!usize {
2806 }2806 }
2807 }2807 }
2808 },2808 },
2809 builtin.Os.windows => {
2810 var system_info: windows.SYSTEM_INFO = undefined;
2811 windows.GetSystemInfo(&system_info);
2812 return @intCast(usize, system_info.dwNumberOfProcessors);
2813 },
2809 else => @compileError("unsupported OS"),2814 else => @compileError("unsupported OS"),
2810 }2815 }
2811}2816}
std/os/windows/index.zig+21
...@@ -107,6 +107,7 @@ pub extern "kernel32" stdcallcc fn GetFinalPathNameByHandleA(...@@ -107,6 +107,7 @@ pub extern "kernel32" stdcallcc fn GetFinalPathNameByHandleA(
107107
108pub extern "kernel32" stdcallcc fn GetProcessHeap() ?HANDLE;108pub extern "kernel32" stdcallcc fn GetProcessHeap() ?HANDLE;
109109
110pub extern "kernel32" stdcallcc fn GetSystemInfo(lpSystemInfo: *SYSTEM_INFO) void;
110pub extern "kernel32" stdcallcc fn GetSystemTimeAsFileTime(*FILETIME) void;111pub extern "kernel32" stdcallcc fn GetSystemTimeAsFileTime(*FILETIME) void;
111112
112pub extern "kernel32" stdcallcc fn HeapCreate(flOptions: DWORD, dwInitialSize: SIZE_T, dwMaximumSize: SIZE_T) ?HANDLE;113pub extern "kernel32" stdcallcc fn HeapCreate(flOptions: DWORD, dwInitialSize: SIZE_T, dwMaximumSize: SIZE_T) ?HANDLE;
...@@ -204,6 +205,7 @@ pub const SIZE_T = usize;...@@ -204,6 +205,7 @@ pub const SIZE_T = usize;
204pub const TCHAR = if (UNICODE) WCHAR else u8;205pub const TCHAR = if (UNICODE) WCHAR else u8;
205pub const UINT = c_uint;206pub const UINT = c_uint;
206pub const ULONG_PTR = usize;207pub const ULONG_PTR = usize;
208pub const DWORD_PTR = ULONG_PTR;
207pub const UNICODE = false;209pub const UNICODE = false;
208pub const WCHAR = u16;210pub const WCHAR = u16;
209pub const WORD = u16;211pub const WORD = u16;
...@@ -413,3 +415,22 @@ pub const FILETIME = extern struct {...@@ -413,3 +415,22 @@ pub const FILETIME = extern struct {
413 dwLowDateTime: DWORD,415 dwLowDateTime: DWORD,
414 dwHighDateTime: DWORD,416 dwHighDateTime: DWORD,
415};417};
418
419pub const SYSTEM_INFO = extern struct {
420 anon1: extern union {
421 dwOemId: DWORD,
422 anon2: extern struct {
423 wProcessorArchitecture: WORD,
424 wReserved: WORD,
425 },
426 },
427 dwPageSize: DWORD,
428 lpMinimumApplicationAddress: LPVOID,
429 lpMaximumApplicationAddress: LPVOID,
430 dwActiveProcessorMask: DWORD_PTR,
431 dwNumberOfProcessors: DWORD,
432 dwProcessorType: DWORD,
433 dwAllocationGranularity: DWORD,
434 wProcessorLevel: WORD,
435 wProcessorRevision: WORD,
436};