-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathrawthreads.nim
More file actions
287 lines (234 loc) · 9.72 KB
/
Copy pathrawthreads.nim
File metadata and controls
287 lines (234 loc) · 9.72 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# Thread module for Nimony
{.feature: "lenientnils".}
import std/oserrors
when defined(windows):
import windows/winlean
type
SysThread = Handle
WinThreadProc = proc (x: pointer): uint32 {.stdcall.}
proc createThread(lpThreadAttributes: pointer, dwStackSize: uint,
lpStartAddress: WinThreadProc,
lpParameter: pointer,
dwCreationFlags: uint32,
lpThreadId: var uint32): SysThread {.
stdcall, importc: "CreateThread", dynlib: "kernel32".}
proc winSuspendThread(hThread: SysThread): int32 {.
stdcall, dynlib: "kernel32", importc: "SuspendThread".}
proc winResumeThread(hThread: SysThread): int32 {.
stdcall, dynlib: "kernel32", importc: "ResumeThread".}
proc waitForSingleObject(hHandle: SysThread, dwMilliseconds: int32): int32 {.
stdcall, dynlib: "kernel32", importc: "WaitForSingleObject".}
proc waitForMultipleObjects*(nCount: int32,
lpHandles: ptr SysThread,
bWaitAll: int32,
dwMilliseconds: int32): int32 {.
stdcall, dynlib: "kernel32", importc: "WaitForMultipleObjects".}
proc terminateThread*(hThread: SysThread, dwExitCode: int32): int32 {.
stdcall, dynlib: "kernel32", importc: "TerminateThread".}
proc setThreadAffinityMask*(hThread: SysThread, dwThreadAffinityMask: uint): uint {.
importc: "SetThreadAffinityMask", stdcall, dynlib: "kernel32".}
elif defined(genode):
const
GenodeHeader = "genode_cpp/threads.h"
type
SysThread {.importcpp: "Nim::SysThread",
header: GenodeHeader, final, pure.} = object
GenodeThreadProc = proc (x: pointer) {.noconv.}
proc initThread(s: var SysThread,
env: GenodeEnv,
stackSize: culonglong,
entry: GenodeThreadProc,
arg: pointer,
affinity: cuint) {.
importcpp: "#.initThread(@)".}
else:
when not (defined(macosx) or defined(haiku)):
{.passL: "-pthread".}
when not defined(haiku):
{.passC: "-pthread".}
type
SysThread = distinct culong ## pthread_t: unsigned long on glibc/musl,
## a pointer on Darwin — same width either way
Pthread_attr = object ## pthread_attr_t as an opaque, oversized blob:
## 56 B on glibc 64-bit, 36 B on i386, 64 B on
## Darwin. int64 elements give both ABIs' alignment.
abi: array[8, int64]
proc pthread_attr_init(a1: var Pthread_attr): cint {.
importc: "pthread_attr_init".}
proc pthread_attr_setstack(a1: ptr Pthread_attr, a2: pointer, a3: int): cint {.
importc: "pthread_attr_setstack".}
proc pthread_attr_setstacksize(a1: var Pthread_attr, a2: int): cint {.
importc: "pthread_attr_setstacksize".}
proc pthread_attr_destroy(a1: var Pthread_attr): cint {.
importc: "pthread_attr_destroy".}
proc pthread_create(a1: var SysThread, a2: var Pthread_attr,
a3: proc (x: pointer): pointer {.noconv.},
a4: pointer): cint {.importc: "pthread_create".}
proc pthread_join(a1: SysThread, a2: ptr pointer): cint {.
importc: "pthread_join".}
proc pthread_cancel(a1: SysThread): cint {.
importc: "pthread_cancel".}
when defined(posix) and not defined(macosx):
type CpuSet = object ## cpu_set_t (glibc: 1024-bit mask)
abi: array[16, uint64]
func cpusetZero(s: var CpuSet) =
for i in 0 ..< s.abi.len: s.abi[i] = 0'u64
func cpusetIncl(cpu: cint; s: var CpuSet) =
# CPU_SET is a header macro over the bit array; reimplemented natively.
if cpu >= 0 and int(cpu) < s.abi.len * 64:
s.abi[int(cpu) shr 6] = s.abi[int(cpu) shr 6] or (1'u64 shl (int(cpu) and 63))
proc setAffinity(thread: SysThread; setsize: csize_t; s: var CpuSet) {.
importc: "pthread_setaffinity_np".}
type
RawThread* = object ## OS thread wrapper holding the system handle and entry closure.
sys*: SysThread
dataFn: proc (arg: pointer) {.nimcall.}
data: pointer
proc `=copy`(dest: var RawThread; src: RawThread) {.error.}
template nimThreadProcWrapperBody(closure: pointer) =
let t = cast[ptr RawThread](closure)
t.dataFn(t.data)
when defined(windows):
proc threadProcWrapper(closure: pointer): uint32 {.stdcall.} =
result = 0'u32
nimThreadProcWrapperBody(closure)
# implicitly return 0
elif defined(genode):
proc threadProcWrapper(closure: pointer) {.noconv.} =
nimThreadProcWrapperBody(closure)
else:
proc threadProcWrapper(closure: pointer): pointer {.noconv.} =
result = nil
nimThreadProcWrapperBody(closure)
when defined(genode):
var affinityOffset: cuint = 1
## CPU affinity offset for next thread, safe to roll-over.
proc create*(t {.noinit.}: out RawThread; fn: proc (arg: pointer) {.nimcall.}; arg: pointer;
stackSize = 0; pinnedToCpu = -1) {.raises.} =
## Spawns a thread running `fn(arg)`. `stackSize` selects a custom stack (0 → OS default).
## `pinnedToCpu` requests CPU affinity where supported (`-1` leaves scheduling to the OS).
t.dataFn = fn
t.data = arg
when defined(windows):
var dummyThreadId: uint32 = 0'u32
t.sys = createThread(nil, uint(stackSize), threadProcWrapper, addr(t), 0'u32, dummyThreadId)
if t.sys.int <= 0:
raiseOSError(osLastError())
elif pinnedToCpu >= 0:
# we cannot undo the thread creation so we cannot raise an error if this fails here:
discard setThreadAffinityMask(t.sys, uint(1 shl pinnedToCpu))
elif defined(genode):
t.sys.initThread(runtimeEnv, stackSize.culonglong,
threadProcWrapper, addr(t), if pinnedToCpu >= 0: pinnedToCpu else: affinityOffset)
inc affinityOffset
else:
var a {.noinit.}: Pthread_attr
if pthread_attr_init(a) != 0:
raiseOSError(osLastError())
if stackSize > 0:
discard pthread_attr_setstacksize(a, stackSize)
if pthread_create(t.sys, a, threadProcWrapper, addr(t)) != 0:
raiseOSError(osLastError())
discard pthread_attr_destroy(a)
when not defined(macosx):
if pinnedToCpu >= 0:
var s {.noinit.}: CpuSet
cpusetZero(s)
cpusetIncl(pinnedToCpu.cint, s)
setAffinity(t.sys, csize_t(sizeof(s)), s)
proc join*(t: var RawThread) =
## Waits for the thread `t` to finish.
when defined(windows):
discard waitForSingleObject(t.sys, -1'i32)
elif defined(genode):
joinThread(t.sys)
else:
discard pthread_join(t.sys, nil)
## ------------- Thread ID retrieval ----------------------------
# we need to cache current threadId to not perform syscall all the time
var threadId {.threadvar.}: int
when defined(windows):
proc getCurrentThreadId(): int32 {.
stdcall, dynlib: "kernel32", importc: "GetCurrentThreadId".}
proc getThreadId*(): int =
## Gets the ID of the currently running thread.
if threadId == 0:
threadId = int(getCurrentThreadId())
result = threadId
elif defined(linux):
proc syscall(arg: clong): clong {.varargs, importc: "syscall".}
const NR_gettid = (
when defined(amd64): clong(186)
elif defined(i386): clong(224)
else: clong(178)) # arm64 (and every asm-generic unistd arch)
proc getThreadId*(): int =
## Gets the ID of the currently running thread.
if threadId == 0:
threadId = int(syscall(NR_gettid))
result = threadId
elif defined(dragonfly):
proc lwp_gettid(): int32 {.importc, header: "unistd.h".}
proc getThreadId*(): int =
## Gets the ID of the currently running thread.
if threadId == 0:
threadId = int(lwp_gettid())
result = threadId
elif defined(openbsd):
proc getthrid(): int32 {.importc: "getthrid", header: "<unistd.h>".}
proc getThreadId*(): int =
## Gets the ID of the currently running thread.
if threadId == 0:
threadId = int(getthrid())
result = threadId
elif defined(netbsd):
proc lwp_self(): int32 {.importc: "_lwp_self", header: "<lwp.h>".}
proc getThreadId*(): int =
## Gets the ID of the currently running thread.
if threadId == 0:
threadId = int(lwp_self())
result = threadId
elif defined(freebsd):
when defined(amd64) or defined(i386):
const SYS_thr_self = 432
else:
var SYS_thr_self {.importc:"SYS_thr_self", header:"<sys/syscall.h>".}: cint
when defined(cpu64):
type
Off {.importc: "off_t", header: "<sys/types.h>".} = int64
Quad {.importc: "quad_t", header: "<sys/types.h>".} = int64
proc syscall(arg: Quad): Off {.varargs, importc: "__syscall", header: "<unistd.h>".}
else:
proc syscall(arg: cint): cint {.varargs, importc: "syscall", header: "<unistd.h>".}
proc getThreadId*(): int =
## Gets the ID of the currently running thread.
var tid = when defined(cpu64): Off(0) else: cint(0)
if threadId == 0:
discard syscall(SYS_thr_self, addr tid)
threadId = int(tid)
result = threadId
elif defined(macosx):
proc pthread_threadid_np(thread: SysThread, thread_id: ptr uint64): cint {.
importc: "pthread_threadid_np".}
proc getThreadId*(): int =
## Gets the ID of the currently running thread.
if threadId == 0:
var tid = 0'u64
discard pthread_threadid_np(SysThread(0), addr tid)
threadId = int(tid)
result = threadId
elif defined(solaris):
type thread_t {.importc: "thread_t", header: "<thread.h>".} = distinct int
proc thr_self(): thread_t {.importc, header: "<thread.h>".}
proc getThreadId*(): int =
## Gets the ID of the currently running thread.
if threadId == 0:
threadId = int(thr_self())
result = threadId
elif defined(haiku):
type thr_id {.importc: "thread_id", header: "<OS.h>".} = distinct int32
proc find_thread(name: cstring): thr_id {.importc, header: "<OS.h>".}
proc getThreadId*(): int =
## Gets the ID of the currently running thread.
if threadId == 0:
threadId = int(find_thread(nil))
result = threadId