threadtypes.nim 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. include system/inclrtl
  2. const hasSharedHeap* = defined(boehmgc) or defined(gogc) # don't share heaps; every thread has its own
  3. when defined(windows):
  4. type
  5. Handle* = int
  6. SysThread* = Handle
  7. WinThreadProc* = proc (x: pointer): int32 {.stdcall.}
  8. proc createThread*(lpThreadAttributes: pointer, dwStackSize: int32,
  9. lpStartAddress: WinThreadProc,
  10. lpParameter: pointer,
  11. dwCreationFlags: int32,
  12. lpThreadId: var int32): SysThread {.
  13. stdcall, dynlib: "kernel32", importc: "CreateThread".}
  14. proc winSuspendThread*(hThread: SysThread): int32 {.
  15. stdcall, dynlib: "kernel32", importc: "SuspendThread".}
  16. proc winResumeThread*(hThread: SysThread): int32 {.
  17. stdcall, dynlib: "kernel32", importc: "ResumeThread".}
  18. proc waitForSingleObject*(hHandle: SysThread, dwMilliseconds: int32): int32 {.
  19. stdcall, dynlib: "kernel32", importc: "WaitForSingleObject".}
  20. proc waitForMultipleObjects*(nCount: int32,
  21. lpHandles: ptr SysThread,
  22. bWaitAll: int32,
  23. dwMilliseconds: int32): int32 {.
  24. stdcall, dynlib: "kernel32", importc: "WaitForMultipleObjects".}
  25. proc terminateThread*(hThread: SysThread, dwExitCode: int32): int32 {.
  26. stdcall, dynlib: "kernel32", importc: "TerminateThread".}
  27. proc setThreadAffinityMask*(hThread: SysThread, dwThreadAffinityMask: uint) {.
  28. importc: "SetThreadAffinityMask", stdcall, header: "<windows.h>".}
  29. elif defined(genode):
  30. const
  31. GenodeHeader* = "genode_cpp/threads.h"
  32. type
  33. SysThread* {.importcpp: "Nim::SysThread",
  34. header: GenodeHeader, final, pure.} = object
  35. GenodeThreadProc* = proc (x: pointer) {.noconv.}
  36. proc initThread*(s: var SysThread,
  37. env: GenodeEnv,
  38. stackSize: culonglong,
  39. entry: GenodeThreadProc,
  40. arg: pointer,
  41. affinity: cuint) {.
  42. importcpp: "#.initThread(@)".}
  43. else:
  44. when not (defined(macosx) or defined(haiku)):
  45. {.passl: "-pthread".}
  46. when not defined(haiku):
  47. {.passc: "-pthread".}
  48. const
  49. schedh = "#define _GNU_SOURCE\n#include <sched.h>"
  50. pthreadh* = "#define _GNU_SOURCE\n#include <pthread.h>"
  51. when not declared(Time):
  52. when defined(linux):
  53. type Time = clong
  54. else:
  55. type Time = int
  56. when (defined(linux) or defined(nintendoswitch)) and defined(amd64):
  57. type
  58. SysThread* {.importc: "pthread_t",
  59. header: "<sys/types.h>" .} = distinct culong
  60. Pthread_attr* {.importc: "pthread_attr_t",
  61. header: "<sys/types.h>".} = object
  62. abi: array[56 div sizeof(clong), clong]
  63. elif defined(openbsd) and defined(amd64):
  64. type
  65. SysThread* {.importc: "pthread_t", header: "<pthread.h>".} = object
  66. Pthread_attr* {.importc: "pthread_attr_t",
  67. header: "<pthread.h>".} = object
  68. else:
  69. type
  70. SysThread* {.importc: "pthread_t", header: "<sys/types.h>".} = int
  71. Pthread_attr* {.importc: "pthread_attr_t",
  72. header: "<sys/types.h>".} = object
  73. type
  74. Timespec* {.importc: "struct timespec", header: "<time.h>".} = object
  75. tv_sec*: Time
  76. tv_nsec*: clong
  77. proc pthread_attr_init*(a1: var Pthread_attr): cint {.
  78. importc, header: pthreadh.}
  79. proc pthread_attr_setstack*(a1: ptr Pthread_attr, a2: pointer, a3: int): cint {.
  80. importc, header: pthreadh.}
  81. proc pthread_attr_setstacksize*(a1: var Pthread_attr, a2: int): cint {.
  82. importc, header: pthreadh.}
  83. proc pthread_attr_destroy*(a1: var Pthread_attr): cint {.
  84. importc, header: pthreadh.}
  85. proc pthread_create*(a1: var SysThread, a2: var Pthread_attr,
  86. a3: proc (x: pointer): pointer {.noconv.},
  87. a4: pointer): cint {.importc: "pthread_create",
  88. header: pthreadh.}
  89. proc pthread_join*(a1: SysThread, a2: ptr pointer): cint {.
  90. importc, header: pthreadh.}
  91. proc pthread_cancel*(a1: SysThread): cint {.
  92. importc: "pthread_cancel", header: pthreadh.}
  93. type CpuSet* {.importc: "cpu_set_t", header: schedh.} = object
  94. when defined(linux) and defined(amd64):
  95. abi: array[1024 div (8 * sizeof(culong)), culong]
  96. proc cpusetZero*(s: var CpuSet) {.importc: "CPU_ZERO", header: schedh.}
  97. proc cpusetIncl*(cpu: cint; s: var CpuSet) {.
  98. importc: "CPU_SET", header: schedh.}
  99. when defined(android):
  100. # libc of android doesn't implement pthread_setaffinity_np,
  101. # it exposes pthread_gettid_np though, so we can use that in combination
  102. # with sched_setaffinity to set the thread affinity.
  103. type Pid* {.importc: "pid_t", header: "<sys/types.h>".} = int32 # From posix_other.nim
  104. proc setAffinityTID*(tid: Pid; setsize: csize_t; s: var CpuSet) {.
  105. importc: "sched_setaffinity", header: schedh.}
  106. proc pthread_gettid_np*(thread: SysThread): Pid {.
  107. importc: "pthread_gettid_np", header: pthreadh.}
  108. proc setAffinity*(thread: SysThread; setsize: csize_t; s: var CpuSet) =
  109. setAffinityTID(pthread_gettid_np(thread), setsize, s)
  110. else:
  111. proc setAffinity*(thread: SysThread; setsize: csize_t; s: var CpuSet) {.
  112. importc: "pthread_setaffinity_np", header: pthreadh.}
  113. const
  114. emulatedThreadVars* = compileOption("tlsEmulation")
  115. # we preallocate a fixed size for thread local storage, so that no heap
  116. # allocations are needed. Currently less than 16K are used on a 64bit machine.
  117. # We use `float` for proper alignment:
  118. const nimTlsSize {.intdefine.} = 16000
  119. type
  120. ThreadLocalStorage* = array[0..(nimTlsSize div sizeof(float)), float]
  121. PGcThread* = ptr GcThread
  122. GcThread* {.pure, inheritable.} = object
  123. when emulatedThreadVars:
  124. tls*: ThreadLocalStorage
  125. else:
  126. nil
  127. when hasSharedHeap:
  128. next*, prev*: PGcThread
  129. stackBottom*, stackTop*: pointer
  130. stackSize*: int
  131. else:
  132. nil
  133. const hasAllocStack* = defined(zephyr) # maybe freertos too?
  134. type
  135. Thread*[TArg] = object
  136. core*: PGcThread
  137. sys*: SysThread
  138. when TArg is void:
  139. dataFn*: proc () {.nimcall, gcsafe.}
  140. else:
  141. dataFn*: proc (m: TArg) {.nimcall, gcsafe.}
  142. data*: TArg
  143. when hasAllocStack:
  144. rawStack*: pointer
  145. proc `=copy`*[TArg](x: var Thread[TArg], y: Thread[TArg]) {.error.}