refactoring: orc can use getThreadId() (#19123)
* refactoring: orc can use getThreadId() * progress
This commit is contained in:
parent
7313b70a6a
commit
cde83d90aa
4 changed files with 110 additions and 96 deletions
|
|
@ -79,10 +79,12 @@ proc trace(s: Cell; desc: PNimTypeV2; j: var GcEnv) {.inline.} =
|
||||||
var p = s +! sizeof(RefHeader)
|
var p = s +! sizeof(RefHeader)
|
||||||
cast[TraceProc](desc.traceImpl)(p, addr(j))
|
cast[TraceProc](desc.traceImpl)(p, addr(j))
|
||||||
|
|
||||||
|
include threadids
|
||||||
|
|
||||||
when logOrc:
|
when logOrc:
|
||||||
proc writeCell(msg: cstring; s: Cell; desc: PNimTypeV2) =
|
proc writeCell(msg: cstring; s: Cell; desc: PNimTypeV2) =
|
||||||
cfprintf(cstderr, "%s %s %ld root index: %ld; RC: %ld; color: %ld\n",
|
cfprintf(cstderr, "%s %s %ld root index: %ld; RC: %ld; color: %ld; thread: %ld\n",
|
||||||
msg, desc.name, s.refId, s.rootIdx, s.rc shr rcShift, s.color)
|
msg, desc.name, s.refId, s.rootIdx, s.rc shr rcShift, s.color, getThreadId())
|
||||||
|
|
||||||
proc free(s: Cell; desc: PNimTypeV2) {.inline.} =
|
proc free(s: Cell; desc: PNimTypeV2) {.inline.} =
|
||||||
when traceCollector:
|
when traceCollector:
|
||||||
|
|
@ -419,6 +421,7 @@ proc registerCycle(s: Cell; desc: PNimTypeV2) =
|
||||||
proc GC_runOrc* =
|
proc GC_runOrc* =
|
||||||
## Forces a cycle collection pass.
|
## Forces a cycle collection pass.
|
||||||
collectCycles()
|
collectCycles()
|
||||||
|
orcAssert roots.len == 0, "roots not empty!"
|
||||||
|
|
||||||
proc GC_enableOrc*() =
|
proc GC_enableOrc*() =
|
||||||
## Enables the cycle collector subsystem of `--gc:orc`. This is a `--gc:orc`
|
## Enables the cycle collector subsystem of `--gc:orc`. This is a `--gc:orc`
|
||||||
|
|
|
||||||
103
lib/system/threadids.nim
Normal file
103
lib/system/threadids.nim
Normal file
|
|
@ -0,0 +1,103 @@
|
||||||
|
#
|
||||||
|
#
|
||||||
|
# Nim's Runtime Library
|
||||||
|
# (c) Copyright 2020 Andreas Rumpf
|
||||||
|
#
|
||||||
|
# See the file "copying.txt", included in this
|
||||||
|
# distribution, for details about the copyright.
|
||||||
|
#
|
||||||
|
|
||||||
|
# we need to cache current threadId to not perform syscall all the time
|
||||||
|
var threadId {.threadvar.}: int
|
||||||
|
|
||||||
|
when defined(windows):
|
||||||
|
proc getCurrentThreadId(): int32 {.
|
||||||
|
stdcall, dynlib: "kernel32", importc: "GetCurrentThreadId".}
|
||||||
|
|
||||||
|
proc getThreadId*(): int =
|
||||||
|
## Gets the ID of the currently running thread.
|
||||||
|
if threadId == 0:
|
||||||
|
threadId = int(getCurrentThreadId())
|
||||||
|
result = threadId
|
||||||
|
|
||||||
|
elif defined(linux):
|
||||||
|
proc syscall(arg: clong): clong {.varargs, importc: "syscall", header: "<unistd.h>".}
|
||||||
|
when defined(amd64):
|
||||||
|
const NR_gettid = clong(186)
|
||||||
|
else:
|
||||||
|
var NR_gettid {.importc: "__NR_gettid", header: "<sys/syscall.h>".}: clong
|
||||||
|
|
||||||
|
proc getThreadId*(): int =
|
||||||
|
## Gets the ID of the currently running thread.
|
||||||
|
if threadId == 0:
|
||||||
|
threadId = int(syscall(NR_gettid))
|
||||||
|
result = threadId
|
||||||
|
|
||||||
|
elif defined(dragonfly):
|
||||||
|
proc lwp_gettid(): int32 {.importc, header: "unistd.h".}
|
||||||
|
|
||||||
|
proc getThreadId*(): int =
|
||||||
|
## Gets the ID of the currently running thread.
|
||||||
|
if threadId == 0:
|
||||||
|
threadId = int(lwp_gettid())
|
||||||
|
result = threadId
|
||||||
|
|
||||||
|
elif defined(openbsd):
|
||||||
|
proc getthrid(): int32 {.importc: "getthrid", header: "<unistd.h>".}
|
||||||
|
|
||||||
|
proc getThreadId*(): int =
|
||||||
|
## Gets the ID of the currently running thread.
|
||||||
|
if threadId == 0:
|
||||||
|
threadId = int(getthrid())
|
||||||
|
result = threadId
|
||||||
|
|
||||||
|
elif defined(netbsd):
|
||||||
|
proc lwp_self(): int32 {.importc: "_lwp_self", header: "<lwp.h>".}
|
||||||
|
|
||||||
|
proc getThreadId*(): int =
|
||||||
|
## Gets the ID of the currently running thread.
|
||||||
|
if threadId == 0:
|
||||||
|
threadId = int(lwp_self())
|
||||||
|
result = threadId
|
||||||
|
|
||||||
|
elif defined(freebsd):
|
||||||
|
proc syscall(arg: cint, arg0: ptr cint): cint {.varargs, importc: "syscall", header: "<unistd.h>".}
|
||||||
|
var SYS_thr_self {.importc:"SYS_thr_self", header:"<sys/syscall.h>".}: cint
|
||||||
|
|
||||||
|
proc getThreadId*(): int =
|
||||||
|
## Gets the ID of the currently running thread.
|
||||||
|
var tid = 0.cint
|
||||||
|
if threadId == 0:
|
||||||
|
discard syscall(SYS_thr_self, addr tid)
|
||||||
|
threadId = tid
|
||||||
|
result = threadId
|
||||||
|
|
||||||
|
elif defined(macosx):
|
||||||
|
proc syscall(arg: cint): cint {.varargs, importc: "syscall", header: "<unistd.h>".}
|
||||||
|
var SYS_thread_selfid {.importc:"SYS_thread_selfid", header:"<sys/syscall.h>".}: cint
|
||||||
|
|
||||||
|
proc getThreadId*(): int =
|
||||||
|
## Gets the ID of the currently running thread.
|
||||||
|
if threadId == 0:
|
||||||
|
threadId = int(syscall(SYS_thread_selfid))
|
||||||
|
result = threadId
|
||||||
|
|
||||||
|
elif defined(solaris):
|
||||||
|
type thread_t {.importc: "thread_t", header: "<thread.h>".} = distinct int
|
||||||
|
proc thr_self(): thread_t {.importc, header: "<thread.h>".}
|
||||||
|
|
||||||
|
proc getThreadId*(): int =
|
||||||
|
## Gets the ID of the currently running thread.
|
||||||
|
if threadId == 0:
|
||||||
|
threadId = int(thr_self())
|
||||||
|
result = threadId
|
||||||
|
|
||||||
|
elif defined(haiku):
|
||||||
|
type thr_id {.importc: "thread_id", header: "<OS.h>".} = distinct int32
|
||||||
|
proc find_thread(name: cstring): thr_id {.importc, header: "<OS.h>".}
|
||||||
|
|
||||||
|
proc getThreadId*(): int =
|
||||||
|
## Gets the ID of the currently running thread.
|
||||||
|
if threadId == 0:
|
||||||
|
threadId = int(find_thread(nil))
|
||||||
|
result = threadId
|
||||||
|
|
@ -29,9 +29,6 @@ when defined(windows):
|
||||||
proc terminateThread(hThread: SysThread, dwExitCode: int32): int32 {.
|
proc terminateThread(hThread: SysThread, dwExitCode: int32): int32 {.
|
||||||
stdcall, dynlib: "kernel32", importc: "TerminateThread".}
|
stdcall, dynlib: "kernel32", importc: "TerminateThread".}
|
||||||
|
|
||||||
proc getCurrentThreadId(): int32 {.
|
|
||||||
stdcall, dynlib: "kernel32", importc: "GetCurrentThreadId".}
|
|
||||||
|
|
||||||
type
|
type
|
||||||
ThreadVarSlot = distinct int32
|
ThreadVarSlot = distinct int32
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -343,94 +343,5 @@ else:
|
||||||
proc createThread*(t: var Thread[void], tp: proc () {.thread, nimcall.}) =
|
proc createThread*(t: var Thread[void], tp: proc () {.thread, nimcall.}) =
|
||||||
createThread[void](t, tp)
|
createThread[void](t, tp)
|
||||||
|
|
||||||
# we need to cache current threadId to not perform syscall all the time
|
when not defined(gcOrc):
|
||||||
var threadId {.threadvar.}: int
|
include threadids
|
||||||
|
|
||||||
when defined(windows):
|
|
||||||
proc getThreadId*(): int =
|
|
||||||
## Gets the ID of the currently running thread.
|
|
||||||
if threadId == 0:
|
|
||||||
threadId = int(getCurrentThreadId())
|
|
||||||
result = threadId
|
|
||||||
|
|
||||||
elif defined(linux):
|
|
||||||
proc syscall(arg: clong): clong {.varargs, importc: "syscall", header: "<unistd.h>".}
|
|
||||||
when defined(amd64):
|
|
||||||
const NR_gettid = clong(186)
|
|
||||||
else:
|
|
||||||
var NR_gettid {.importc: "__NR_gettid", header: "<sys/syscall.h>".}: clong
|
|
||||||
|
|
||||||
proc getThreadId*(): int =
|
|
||||||
## Gets the ID of the currently running thread.
|
|
||||||
if threadId == 0:
|
|
||||||
threadId = int(syscall(NR_gettid))
|
|
||||||
result = threadId
|
|
||||||
|
|
||||||
elif defined(dragonfly):
|
|
||||||
proc lwp_gettid(): int32 {.importc, header: "unistd.h".}
|
|
||||||
|
|
||||||
proc getThreadId*(): int =
|
|
||||||
## Gets the ID of the currently running thread.
|
|
||||||
if threadId == 0:
|
|
||||||
threadId = int(lwp_gettid())
|
|
||||||
result = threadId
|
|
||||||
|
|
||||||
elif defined(openbsd):
|
|
||||||
proc getthrid(): int32 {.importc: "getthrid", header: "<unistd.h>".}
|
|
||||||
|
|
||||||
proc getThreadId*(): int =
|
|
||||||
## get the ID of the currently running thread.
|
|
||||||
if threadId == 0:
|
|
||||||
threadId = int(getthrid())
|
|
||||||
result = threadId
|
|
||||||
|
|
||||||
elif defined(netbsd):
|
|
||||||
proc lwp_self(): int32 {.importc: "_lwp_self", header: "<lwp.h>".}
|
|
||||||
|
|
||||||
proc getThreadId*(): int =
|
|
||||||
## Gets the ID of the currently running thread.
|
|
||||||
if threadId == 0:
|
|
||||||
threadId = int(lwp_self())
|
|
||||||
result = threadId
|
|
||||||
|
|
||||||
elif defined(freebsd):
|
|
||||||
proc syscall(arg: cint, arg0: ptr cint): cint {.varargs, importc: "syscall", header: "<unistd.h>".}
|
|
||||||
var SYS_thr_self {.importc:"SYS_thr_self", header:"<sys/syscall.h>".}: cint
|
|
||||||
|
|
||||||
proc getThreadId*(): int =
|
|
||||||
## Gets the ID of the currently running thread.
|
|
||||||
var tid = 0.cint
|
|
||||||
if threadId == 0:
|
|
||||||
discard syscall(SYS_thr_self, addr tid)
|
|
||||||
threadId = tid
|
|
||||||
result = threadId
|
|
||||||
|
|
||||||
elif defined(macosx):
|
|
||||||
proc syscall(arg: cint): cint {.varargs, importc: "syscall", header: "<unistd.h>".}
|
|
||||||
var SYS_thread_selfid {.importc:"SYS_thread_selfid", header:"<sys/syscall.h>".}: cint
|
|
||||||
|
|
||||||
proc getThreadId*(): int =
|
|
||||||
## Gets the ID of the currently running thread.
|
|
||||||
if threadId == 0:
|
|
||||||
threadId = int(syscall(SYS_thread_selfid))
|
|
||||||
result = threadId
|
|
||||||
|
|
||||||
elif defined(solaris):
|
|
||||||
type thread_t {.importc: "thread_t", header: "<thread.h>".} = distinct int
|
|
||||||
proc thr_self(): thread_t {.importc, header: "<thread.h>".}
|
|
||||||
|
|
||||||
proc getThreadId*(): int =
|
|
||||||
## Gets the ID of the currently running thread.
|
|
||||||
if threadId == 0:
|
|
||||||
threadId = int(thr_self())
|
|
||||||
result = threadId
|
|
||||||
|
|
||||||
elif defined(haiku):
|
|
||||||
type thr_id {.importc: "thread_id", header: "<OS.h>".} = distinct int32
|
|
||||||
proc find_thread(name: cstring): thr_id {.importc, header: "<OS.h>".}
|
|
||||||
|
|
||||||
proc getThreadId*(): int =
|
|
||||||
## Gets the ID of the currently running thread.
|
|
||||||
if threadId == 0:
|
|
||||||
threadId = int(find_thread(nil))
|
|
||||||
result = threadId
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue