Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Thread cleanup attempt #0
  • Loading branch information
SirOlaf committed Jul 22, 2024
commit 61c9726a05e603b31ae4374469f043a8c982a6b5
12 changes: 12 additions & 0 deletions lib/system/alloc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ type
MemRegion = object
when not defined(gcDestructors):
minLargeObj, maxLargeObj: int
activeChunks: int
freeSmallChunks: array[0..max(1, SmallChunkSize div MemAlign-1), PSmallChunk]
when defined(gcDestructors):
sharedFreeLists: array[0..max(1, SmallChunkSize div MemAlign-1), ptr FreeCell]
Expand Down Expand Up @@ -626,8 +627,10 @@ proc freeBigChunk(a: var MemRegion, c: PBigChunk) =
let rest = splitChunk2(a, c, MaxBigChunkSize)
addChunkToMatrix(a, rest)
addChunkToMatrix(a, c)
dec a.activeChunks

proc getBigChunk(a: var MemRegion, size: int): PBigChunk =
inc a.activeChunks
sysAssert(size > 0, "getBigChunk 2")
var size = size # roundup(size, PageSize)
var fl = 0
Expand Down Expand Up @@ -667,6 +670,7 @@ proc getBigChunk(a: var MemRegion, size: int): PBigChunk =
releaseSys a.lock

proc getHugeChunk(a: var MemRegion; size: int): PBigChunk =
inc a.activeChunks
result = cast[PBigChunk](allocPages(a, size))
when RegionHasLock:
if not a.lockActive:
Expand All @@ -693,6 +697,7 @@ proc freeHugeChunk(a: var MemRegion; c: PBigChunk) =
excl(a.chunkStarts, pageIndex(c))
decCurrMem(a, size)
osDeallocPages(c, size)
dec a.activeChunks

proc getSmallChunk(a: var MemRegion): PSmallChunk =
var res = getBigChunk(a, PageSize)
Expand Down Expand Up @@ -1129,6 +1134,11 @@ when defined(nimTypeNames):
proc getMemCounters(a: MemRegion): (int, int) {.inline.} =
(a.allocCounter, a.deallocCounter)

proc abandonAllocator(a: var MemRegion) =
if a.activeChunks == 0:
deallocOsPages(a)
# if there's still active chunks, we cannot clean up

# ---------------------- thread memory region -------------------------------

template instantiateForRegion(allocator: untyped) {.dirty.} =
Expand All @@ -1144,6 +1154,8 @@ template instantiateForRegion(allocator: untyped) {.dirty.} =

proc deallocOsPages = deallocOsPages(allocator)

proc abandonAllocator = abandonAllocator(allocator)

proc allocImpl(size: Natural): pointer =
result = alloc(allocator, size)

Expand Down
2 changes: 2 additions & 0 deletions lib/system/threadimpl.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var
nimThreadDestructionHandlers* {.rtlThreadVar.}: seq[proc () {.closure, gcsafe, raises: [].}]
when not defined(boehmgc) and not hasSharedHeap and not defined(gogc) and not defined(gcRegions):
proc deallocOsPages() {.rtl, raises: [].}
proc abandonAllocator() {.rtl, raises: [].}
proc threadTrouble() {.raises: [], gcsafe.}
# create for the main thread. Note: do not insert this data into the list
# of all threads; it's not to be stopped etc.
Expand Down Expand Up @@ -93,6 +94,7 @@ proc threadProcWrapStackFrame[TArg](thrd: ptr Thread[TArg]) {.raises: [].} =
when declared(deallocOsPages): deallocOsPages()
else:
threadProcWrapDispatch(thrd)
abandonAllocator()

template nimThreadProcWrapperBody*(closure: untyped): untyped =
var thrd = cast[ptr Thread[TArg]](closure)
Expand Down