threadids.nim 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2020 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. # we need to cache current threadId to not perform syscall all the time
  10. var threadId {.threadvar.}: int
  11. when defined(windows):
  12. proc getCurrentThreadId(): int32 {.
  13. stdcall, dynlib: "kernel32", importc: "GetCurrentThreadId".}
  14. proc getThreadId*(): int =
  15. ## Gets the ID of the currently running thread.
  16. if threadId == 0:
  17. threadId = int(getCurrentThreadId())
  18. result = threadId
  19. elif defined(linux):
  20. proc syscall(arg: clong): clong {.varargs, importc: "syscall", header: "<unistd.h>".}
  21. when defined(amd64):
  22. const NR_gettid = clong(186)
  23. else:
  24. var NR_gettid {.importc: "__NR_gettid", header: "<sys/syscall.h>".}: clong
  25. proc getThreadId*(): int =
  26. ## Gets the ID of the currently running thread.
  27. if threadId == 0:
  28. threadId = int(syscall(NR_gettid))
  29. result = threadId
  30. elif defined(dragonfly):
  31. proc lwp_gettid(): int32 {.importc, header: "unistd.h".}
  32. proc getThreadId*(): int =
  33. ## Gets the ID of the currently running thread.
  34. if threadId == 0:
  35. threadId = int(lwp_gettid())
  36. result = threadId
  37. elif defined(openbsd):
  38. proc getthrid(): int32 {.importc: "getthrid", header: "<unistd.h>".}
  39. proc getThreadId*(): int =
  40. ## Gets the ID of the currently running thread.
  41. if threadId == 0:
  42. threadId = int(getthrid())
  43. result = threadId
  44. elif defined(netbsd):
  45. proc lwp_self(): int32 {.importc: "_lwp_self", header: "<lwp.h>".}
  46. proc getThreadId*(): int =
  47. ## Gets the ID of the currently running thread.
  48. if threadId == 0:
  49. threadId = int(lwp_self())
  50. result = threadId
  51. elif defined(freebsd):
  52. proc syscall(arg: cint, arg0: ptr cint): cint {.varargs, importc: "syscall", header: "<unistd.h>".}
  53. var SYS_thr_self {.importc:"SYS_thr_self", header:"<sys/syscall.h>".}: cint
  54. proc getThreadId*(): int =
  55. ## Gets the ID of the currently running thread.
  56. var tid = 0.cint
  57. if threadId == 0:
  58. discard syscall(SYS_thr_self, addr tid)
  59. threadId = tid
  60. result = threadId
  61. elif defined(macosx):
  62. proc syscall(arg: cint): cint {.varargs, importc: "syscall", header: "<unistd.h>".}
  63. var SYS_thread_selfid {.importc:"SYS_thread_selfid", header:"<sys/syscall.h>".}: cint
  64. proc getThreadId*(): int =
  65. ## Gets the ID of the currently running thread.
  66. if threadId == 0:
  67. threadId = int(syscall(SYS_thread_selfid))
  68. result = threadId
  69. elif defined(solaris):
  70. type thread_t {.importc: "thread_t", header: "<thread.h>".} = distinct int
  71. proc thr_self(): thread_t {.importc, header: "<thread.h>".}
  72. proc getThreadId*(): int =
  73. ## Gets the ID of the currently running thread.
  74. if threadId == 0:
  75. threadId = int(thr_self())
  76. result = threadId
  77. elif defined(haiku):
  78. type thr_id {.importc: "thread_id", header: "<OS.h>".} = distinct int32
  79. proc find_thread(name: cstring): thr_id {.importc, header: "<OS.h>".}
  80. proc getThreadId*(): int =
  81. ## Gets the ID of the currently running thread.
  82. if threadId == 0:
  83. threadId = int(find_thread(nil))
  84. result = threadId