File tree Expand file tree Collapse file tree 3 files changed +36
-9
lines changed
Expand file tree Collapse file tree 3 files changed +36
-9
lines changed Original file line number Diff line number Diff line change @@ -681,6 +681,15 @@ nimMemAlignTiny
681681Sets `MemAlign ` to `4 ` bytes which reduces the memory alignment
682682to better match some embedded devices.
683683
684+ Thread stack size
685+ =================
686+
687+ Nim's thread API provides a simple wrapper around more advanced
688+ RTOS task features. Customizing the stack size and stack guard size can
689+ be done by setting `-d:nimThreadStackSize=16384 ` or `-d:nimThreadStackGuard=32 `.
690+
691+ Currently only Zephyr and FreeRTOS support these configurations.
692+
684693Nim for realtime systems
685694========================
686695
Original file line number Diff line number Diff line change @@ -153,6 +153,8 @@ else:
153153
154154 proc pthread_attr_init (a1: var Pthread_attr): cint {.
155155 importc , header : pthreadh.}
156+ proc pthread_attr_setstack * (a1: ptr Pthread_attr, a2: pointer , a3: int ): cint {.
157+ importc , header : pthreadh.}
156158 proc pthread_attr_setstacksize (a1: var Pthread_attr, a2: int ): cint {.
157159 importc , header : pthreadh.}
158160 proc pthread_attr_destroy (a1: var Pthread_attr): cint {.
Original file line number Diff line number Diff line change 4747when not declared (ThisIsSystem ):
4848 {.error : " You must not import this module explicitly" .}
4949
50- const
51- StackGuardSize = 4096
52- ThreadStackMask =
53- when defined (genode):
54- 1024 * 64 * sizeof (int )- 1
55- else :
56- 1024 * 256 * sizeof (int )- 1
57- ThreadStackSize = ThreadStackMask + 1 - StackGuardSize
50+ when defined (zephyr) or defined (freertos):
51+ const
52+ nimThreadStackSize {.intdefine .} = 8192
53+ nimThreadStackGuard {.intdefine .} = 128
54+
55+ StackGuardSize = nimThreadStackGuard
56+ ThreadStackSize = nimThreadStackSize - nimThreadStackGuard
57+ else :
58+ const
59+ StackGuardSize = 4096
60+ ThreadStackMask =
61+ when defined (genode):
62+ 1024 * 64 * sizeof (int )- 1
63+ else :
64+ 1024 * 256 * sizeof (int )- 1
65+
66+ ThreadStackSize = ThreadStackMask + 1 - StackGuardSize
5867
5968# const globalsSlot = ThreadVarSlot(0)
6069# sysAssert checkSlot.int == globalsSlot.int
@@ -321,7 +330,14 @@ else:
321330 when hasSharedHeap: t.core.stackSize = ThreadStackSize
322331 var a {.noinit .}: Pthread_attr
323332 doAssert pthread_attr_init (a) == 0
324- let setstacksizeResult = pthread_attr_setstacksize (a, ThreadStackSize )
333+ when defined (zephyr):
334+ var
335+ rawstk = allocShared0 (ThreadStackSize + StackGuardSize )
336+ stk = cast [pointer ](cast [uint ](rawstk) + StackGuardSize )
337+ let setstacksizeResult = pthread_attr_setstack (addr a, stk, ThreadStackSize )
338+ else :
339+ let setstacksizeResult = pthread_attr_setstacksize (a, ThreadStackSize )
340+
325341 when not defined (ios):
326342 # This fails on iOS
327343 doAssert (setstacksizeResult == 0 )
You can’t perform that action at this time.
0 commit comments