Skip to content

Commit

Permalink
pthreads setup for zephyr
Browse files Browse the repository at this point in the history
- enable tweak stack size
- update lib/system/threads.nim
- Fix int/uint in casting pointer.
  • Loading branch information
Jaremy Creechley committed Nov 18, 2021
1 parent a8611c6 commit 9f1c81e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
2 changes: 2 additions & 0 deletions lib/system/threadlocalstorage.nim
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ else:

proc pthread_attr_init(a1: var Pthread_attr): cint {.
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 {.
importc, header: pthreadh.}
proc pthread_attr_destroy(a1: var Pthread_attr): cint {.
Expand Down
31 changes: 22 additions & 9 deletions lib/system/threads.nim
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,20 @@
when not declared(ThisIsSystem):
{.error: "You must not import this module explicitly".}

const
StackGuardSize = 4096
ThreadStackMask =
when defined(genode):
1024*64*sizeof(int)-1
else:
1024*256*sizeof(int)-1
ThreadStackSize = ThreadStackMask+1 - StackGuardSize
when defined(zephyr):
const
StackGuardSize {.intdefine.} = 128
ThreadStackSize {.intdefine.} = 8192 - 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)
#sysAssert checkSlot.int == globalsSlot.int
Expand Down Expand Up @@ -321,7 +327,14 @@ else:
when hasSharedHeap: t.core.stackSize = ThreadStackSize
var a {.noinit.}: Pthread_attr
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):
# This fails on iOS
doAssert(setstacksizeResult == 0)
Expand Down

0 comments on commit 9f1c81e

Please sign in to comment.