summary refs log tree commit diff stats
path: root/lib/system/threads.nim
diff options
context:
space:
mode:
Diffstat (limited to 'lib/system/threads.nim')
-rw-r--r--lib/system/threads.nim32
1 files changed, 31 insertions, 1 deletions
diff --git a/lib/system/threads.nim b/lib/system/threads.nim
index f72395fd2..7886fdcdf 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: "<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")