From b1734995434ef468478801607d51acf3f1990f23 Mon Sep 17 00:00:00 2001 From: Araq Date: Tue, 31 Jan 2017 13:06:19 +0100 Subject: [PATCH] added system.getThreadId for various OSes (untested) --- lib/system/threads.nim | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/lib/system/threads.nim b/lib/system/threads.nim index f72395fd25..7886fdcdf1 100644 --- a/lib/system/threads.nim +++ b/lib/system/threads.nim @@ -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: "".} + 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: "".} = distinct int + proc gettid(): Pid {.importc, header: "".} + + 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: "".} = distinct int + proc thr_self(): thread_t {.importc, header: "".} + + proc getThreadId*(): int = + ## get the ID of the currently running thread. + result = int(thr_self()) + + const emulatedThreadVars = compileOption("tlsEmulation")