Implement threads on Zephyr (#19156)
* pthreads setup for zephyr - enable tweak stack size - update lib/system/threads.nim - Fix int/uint in casting pointer. * add documentation and tweak flag names * add documentation and tweak flag names * fix configuration flag names * fix configuration flag names * cleanup Co-authored-by: Jaremy Creechley <jaremy.creechley@panthalassa.com>
This commit is contained in:
parent
1b143f5e79
commit
7772ca303c
3 changed files with 36 additions and 9 deletions
|
|
@ -681,6 +681,15 @@ nimMemAlignTiny
|
||||||
Sets `MemAlign` to `4` bytes which reduces the memory alignment
|
Sets `MemAlign` to `4` bytes which reduces the memory alignment
|
||||||
to better match some embedded devices.
|
to better match some embedded devices.
|
||||||
|
|
||||||
|
Thread stack size
|
||||||
|
=================
|
||||||
|
|
||||||
|
Nim's thread API provides a simple wrapper around more advanced
|
||||||
|
RTOS task features. Customizing the stack size and stack guard size can
|
||||||
|
be done by setting `-d:nimThreadStackSize=16384` or `-d:nimThreadStackGuard=32`.
|
||||||
|
|
||||||
|
Currently only Zephyr and FreeRTOS support these configurations.
|
||||||
|
|
||||||
Nim for realtime systems
|
Nim for realtime systems
|
||||||
========================
|
========================
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -153,6 +153,8 @@ else:
|
||||||
|
|
||||||
proc pthread_attr_init(a1: var Pthread_attr): cint {.
|
proc pthread_attr_init(a1: var Pthread_attr): cint {.
|
||||||
importc, header: pthreadh.}
|
importc, header: pthreadh.}
|
||||||
|
proc pthread_attr_setstack*(a1: ptr Pthread_attr, a2: pointer, a3: int): cint {.
|
||||||
|
importc, header: pthreadh.}
|
||||||
proc pthread_attr_setstacksize(a1: var Pthread_attr, a2: int): cint {.
|
proc pthread_attr_setstacksize(a1: var Pthread_attr, a2: int): cint {.
|
||||||
importc, header: pthreadh.}
|
importc, header: pthreadh.}
|
||||||
proc pthread_attr_destroy(a1: var Pthread_attr): cint {.
|
proc pthread_attr_destroy(a1: var Pthread_attr): cint {.
|
||||||
|
|
|
||||||
|
|
@ -47,14 +47,23 @@
|
||||||
when not declared(ThisIsSystem):
|
when not declared(ThisIsSystem):
|
||||||
{.error: "You must not import this module explicitly".}
|
{.error: "You must not import this module explicitly".}
|
||||||
|
|
||||||
const
|
when defined(zephyr) or defined(freertos):
|
||||||
StackGuardSize = 4096
|
const
|
||||||
ThreadStackMask =
|
nimThreadStackSize {.intdefine.} = 8192
|
||||||
when defined(genode):
|
nimThreadStackGuard {.intdefine.} = 128
|
||||||
1024*64*sizeof(int)-1
|
|
||||||
else:
|
StackGuardSize = nimThreadStackGuard
|
||||||
1024*256*sizeof(int)-1
|
ThreadStackSize = nimThreadStackSize - nimThreadStackGuard
|
||||||
ThreadStackSize = ThreadStackMask+1 - StackGuardSize
|
else:
|
||||||
|
const
|
||||||
|
StackGuardSize = 4096
|
||||||
|
ThreadStackMask =
|
||||||
|
when defined(genode):
|
||||||
|
1024*64*sizeof(int)-1
|
||||||
|
else:
|
||||||
|
1024*256*sizeof(int)-1
|
||||||
|
|
||||||
|
ThreadStackSize = ThreadStackMask+1 - StackGuardSize
|
||||||
|
|
||||||
#const globalsSlot = ThreadVarSlot(0)
|
#const globalsSlot = ThreadVarSlot(0)
|
||||||
#sysAssert checkSlot.int == globalsSlot.int
|
#sysAssert checkSlot.int == globalsSlot.int
|
||||||
|
|
@ -321,7 +330,14 @@ 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
|
||||||
let setstacksizeResult = pthread_attr_setstacksize(a, ThreadStackSize)
|
when defined(zephyr):
|
||||||
|
var
|
||||||
|
rawstk = allocShared0(ThreadStackSize + StackGuardSize)
|
||||||
|
stk = cast[pointer](cast[uint](rawstk) + StackGuardSize)
|
||||||
|
let setstacksizeResult = pthread_attr_setstack(addr a, stk, ThreadStackSize)
|
||||||
|
else:
|
||||||
|
let setstacksizeResult = pthread_attr_setstacksize(a, ThreadStackSize)
|
||||||
|
|
||||||
when not defined(ios):
|
when not defined(ios):
|
||||||
# This fails on iOS
|
# This fails on iOS
|
||||||
doAssert(setstacksizeResult == 0)
|
doAssert(setstacksizeResult == 0)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue