mirror of
https://github.com/nim-lang/Nim.git
synced 2026-07-19 15:31:28 +00:00
added system.getThreadId for various OSes (untested)
This commit is contained in:
@@ -53,7 +53,7 @@ when defined(windows):
|
||||
type
|
||||
SysThread* = Handle
|
||||
WinThreadProc = proc (x: pointer): int32 {.stdcall.}
|
||||
{.deprecated: [TSysThread: SysThread, TWinThreadProc: WinThreadProc].}
|
||||
{.deprecated: [TSysThread: SysThread].}
|
||||
|
||||
proc createThread(lpThreadAttributes: pointer, dwStackSize: int32,
|
||||
lpStartAddress: WinThreadProc,
|
||||
@@ -77,6 +77,9 @@ when defined(windows):
|
||||
proc terminateThread(hThread: SysThread, dwExitCode: int32): int32 {.
|
||||
stdcall, dynlib: "kernel32", importc: "TerminateThread".}
|
||||
|
||||
proc getCurrentThreadId(): int32 {.
|
||||
stdcall, dynlib: "kernel32", importc: "GetCurrentThreadId".}
|
||||
|
||||
type
|
||||
ThreadVarSlot = distinct int32
|
||||
|
||||
@@ -108,6 +111,10 @@ when defined(windows):
|
||||
proc setThreadAffinityMask(hThread: SysThread, dwThreadAffinityMask: uint) {.
|
||||
importc: "SetThreadAffinityMask", stdcall, header: "<windows.h>".}
|
||||
|
||||
proc getThreadId*(): int =
|
||||
## get the ID of the currently running thread.
|
||||
result = int(getCurrentThreadId())
|
||||
|
||||
else:
|
||||
when not defined(macosx):
|
||||
{.passL: "-pthread".}
|
||||
@@ -187,6 +194,29 @@ else:
|
||||
proc setAffinity(thread: SysThread; setsize: csize; s: var CpuSet) {.
|
||||
importc: "pthread_setaffinity_np", header: pthreadh.}
|
||||
|
||||
when defined(linux):
|
||||
type Pid {.importc: "pid_t", header: "<sys/types.h>".} = distinct int
|
||||
proc gettid(): Pid {.importc, header: "<sys/types.h>".}
|
||||
|
||||
proc getThreadId*(): int =
|
||||
## get the ID of the currently running thread.
|
||||
result = int(gettid())
|
||||
elif defined(macosx) or defined(bsd):
|
||||
proc pthread_main_np(): cint {.importc, header: "pthread.h".}
|
||||
|
||||
proc getThreadId*(): int =
|
||||
## get the ID of the currently running thread.
|
||||
result = int(pthread_main_np())
|
||||
elif defined(solaris):
|
||||
# just a guess really:
|
||||
type thread_t {.importc: "thread_t", header: "<thread.h>".} = distinct int
|
||||
proc thr_self(): thread_t {.importc, header: "<thread.h>".}
|
||||
|
||||
proc getThreadId*(): int =
|
||||
## get the ID of the currently running thread.
|
||||
result = int(thr_self())
|
||||
|
||||
|
||||
const
|
||||
emulatedThreadVars = compileOption("tlsEmulation")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user