-
Notifications
You must be signed in to change notification settings - Fork 4
/
jobs_windows.odin
70 lines (57 loc) · 1.98 KB
/
jobs_windows.odin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// +build windows
package jobs
import "core:os"
import "core:runtime"
import "core:sys/windows"
foreign import kernel32 "system:Kernel32.lib"
SYSTEM_INFO :: struct {
using _: struct #raw_union {
dwOemId: windows.DWORD,
using _: struct #raw_union {
wProcessorArchitecture: windows.WORD,
wReserved: windows.WORD,
},
},
dwPageSize: windows.DWORD,
lpMinimumApplicationAddress: windows.LPVOID,
lpMaximumApplicationAddress: windows.LPVOID,
dwActiveProcessorMask: windows.DWORD_PTR,
dwNumberOfProcessors: windows.DWORD,
dwProcessorType: windows.DWORD,
dwAllocationGranularity: windows.DWORD,
wProcessorLevel: windows.WORD,
wProcessorRevision: windows.WORD,
}
@(default_calling_convention = "stdcall")
foreign kernel32 {
GetNativeSystemInfo :: proc(lpSystemInfo: ^SYSTEM_INFO) ---
SetThreadAffinityMask :: proc(hThread: windows.HANDLE, dwThreadAffinityMask: windows.DWORD_PTR) -> windows.DWORD_PTR ---
}
_Thread :: windows.HANDLE
_get_num_hardware_threads :: proc() -> int {
info: SYSTEM_INFO
GetNativeSystemInfo(&info)
return int(info.dwNumberOfProcessors)
}
_create_worker_thread :: proc() -> _Thread {
handle := windows.CreateThread(nil, 0, _thread_start_routine, nil, 0, nil)
if handle == nil {
panic("Failed to create thread.")
}
return handle
_thread_start_routine :: proc "stdcall" (param: windows.LPVOID) -> windows.DWORD {
// HACK
context = runtime.default_context()
run_worker_thread()
return 0
}
}
_current_thread_id :: proc() -> u64 {
return u64(windows.GetCurrentThreadId())
}
_wait_for_threads_to_finish :: proc(threads: []Thread) {
if windows.WaitForMultipleObjects(windows.DWORD(len(threads)), &threads[0], true, windows.INFINITE) ==
windows.WAIT_FAILED {
panic("Failed to wait for threads to finish.")
}
}