system: thread: stack dealloction on Zephyr (#19633) [backport:1.6]

Try to free the stack allocation when a thread exits. Possibly works for FreeRTOS as well.
This commit is contained in:
Jaremy Creechley 2022-03-22 12:08:31 -07:00 • committed by GitHub
commit 4c8934305c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -47,7 +47,10 @@
when not declared(ThisIsSystem): when not declared(ThisIsSystem):
{.error: "You must not import this module explicitly".} {.error: "You must not import this module explicitly".}
when defined(zephyr) or defined(freertos): const
hasAllocStack = defined(zephyr) # maybe freertos too?
when hasAllocStack or defined(zephyr) or defined(freertos):
const const
nimThreadStackSize {.intdefine.} = 8192 nimThreadStackSize {.intdefine.} = 8192
nimThreadStackGuard {.intdefine.} = 128 nimThreadStackGuard {.intdefine.} = 128
@ -68,6 +71,12 @@ else:
#const globalsSlot = ThreadVarSlot(0) #const globalsSlot = ThreadVarSlot(0)
#sysAssert checkSlot.int == globalsSlot.int #sysAssert checkSlot.int == globalsSlot.int
# Zephyr doesn't include this properly without some help
when defined(zephyr):
{.emit: """/*INCLUDESECTION*/
#include <pthread.h>
""".}
# create for the main thread. Note: do not insert this data into the list # create for the main thread. Note: do not insert this data into the list
# of all threads; it's not to be stopped etc. # of all threads; it's not to be stopped etc.
when not defined(useNimRtl): when not defined(useNimRtl):
@ -98,6 +107,8 @@ type
else: else:
dataFn: proc (m: TArg) {.nimcall, gcsafe.} dataFn: proc (m: TArg) {.nimcall, gcsafe.}
data: TArg data: TArg
when hasAllocStack:
rawStack: pointer
proc `=copy`*[TArg](x: var Thread[TArg], y: Thread[TArg]) {.error.} proc `=copy`*[TArg](x: var Thread[TArg], y: Thread[TArg]) {.error.}
@ -161,6 +172,8 @@ else:
threadTrouble() threadTrouble()
finally: finally:
afterThreadRuns() afterThreadRuns()
when hasAllocStack:
deallocShared(thrd.rawStack)
proc threadProcWrapStackFrame[TArg](thrd: ptr Thread[TArg]) {.raises: [].} = proc threadProcWrapStackFrame[TArg](thrd: ptr Thread[TArg]) {.raises: [].} =
when defined(boehmgc): when defined(boehmgc):
@ -330,11 +343,12 @@ else:
when hasSharedHeap: t.core.stackSize = ThreadStackSize when hasSharedHeap: t.core.stackSize = ThreadStackSize
var a {.noinit.}: Pthread_attr var a {.noinit.}: Pthread_attr
doAssert pthread_attr_init(a) == 0 doAssert pthread_attr_init(a) == 0
when defined(zephyr): when hasAllocStack:
var var
rawstk = allocShared0(ThreadStackSize + StackGuardSize) rawstk = allocShared0(ThreadStackSize + StackGuardSize)
stk = cast[pointer](cast[uint](rawstk) + StackGuardSize) stk = cast[pointer](cast[uint](rawstk) + StackGuardSize)
let setstacksizeResult = pthread_attr_setstack(addr a, stk, ThreadStackSize) let setstacksizeResult = pthread_attr_setstack(addr a, stk, ThreadStackSize)
t.rawStack = rawstk
else: else:
let setstacksizeResult = pthread_attr_setstacksize(a, ThreadStackSize) let setstacksizeResult = pthread_attr_setstacksize(a, ThreadStackSize)