mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-29 18:53:58 +00:00
support for the Genode OS framework (#5560)
This commit is contained in:
committed by
Andreas Rumpf
parent
57246cbcec
commit
7e351fc7fa
@@ -146,6 +146,17 @@ elif defined(windows) or defined(dos):
|
||||
if result != nil: return
|
||||
procAddrError(name)
|
||||
|
||||
elif defined(genode):
|
||||
|
||||
proc nimUnloadLibrary(lib: LibHandle) {.
|
||||
error: "nimUnloadLibrary not implemented".}
|
||||
|
||||
proc nimLoadLibrary(path: string): LibHandle {.
|
||||
error: "nimLoadLibrary not implemented".}
|
||||
|
||||
proc nimGetProcAddr(lib: LibHandle, name: cstring): ProcAddr {.
|
||||
error: "nimGetProcAddr not implemented".}
|
||||
|
||||
else:
|
||||
{.error: "no implementation for dyncalls".}
|
||||
|
||||
|
||||
@@ -77,6 +77,19 @@ when defined(emscripten):
|
||||
var mmapDescr = cast[EmscriptenMMapBlock](mmapDescrPos)
|
||||
munmap(mmapDescr.realPointer, mmapDescr.realSize)
|
||||
|
||||
elif defined(genode):
|
||||
|
||||
proc osAllocPages(size: int): pointer {.
|
||||
importcpp: "genodeEnv->rm().attach(genodeEnv->ram().alloc(@))".}
|
||||
|
||||
proc osTryAllocPages(size: int): pointer =
|
||||
{.emit: """try {""".}
|
||||
result = osAllocPages size
|
||||
{.emit: """} catch (...) { }""".}
|
||||
|
||||
proc osDeallocPages(p: pointer, size: int) {.
|
||||
importcpp: "genodeEnv->rm().detach(#)".}
|
||||
|
||||
elif defined(posix):
|
||||
const
|
||||
PROT_READ = 1 # page can be read
|
||||
|
||||
@@ -75,6 +75,28 @@ when defined(Windows):
|
||||
proc waitSysCondWindows(cond: var SysCond) =
|
||||
discard waitForSingleObject(cond, -1'i32)
|
||||
|
||||
elif defined(genode):
|
||||
const
|
||||
Header = "genode_cpp/syslocks.h"
|
||||
type
|
||||
SysLock {.importcpp: "Nim::SysLock", pure, final,
|
||||
header: Header.} = object
|
||||
SysCond {.importcpp: "Nim::SysCond", pure, final,
|
||||
header: Header.} = object
|
||||
|
||||
proc initSysLock(L: var SysLock) = discard
|
||||
proc deinitSys(L: var SysLock) = discard
|
||||
proc acquireSys(L: var SysLock) {.noSideEffect, importcpp.}
|
||||
proc tryAcquireSys(L: var SysLock): bool {.noSideEffect, importcpp.}
|
||||
proc releaseSys(L: var SysLock) {.noSideEffect, importcpp.}
|
||||
|
||||
proc initSysCond(L: var SysCond) = discard
|
||||
proc deinitSysCond(L: var SysCond) = discard
|
||||
proc waitSysCond(cond: var SysCond, lock: var SysLock) {.
|
||||
noSideEffect, importcpp.}
|
||||
proc signalSysCond(cond: var SysCond) {.
|
||||
noSideEffect, importcpp.}
|
||||
|
||||
else:
|
||||
type
|
||||
SysLock {.importc: "pthread_mutex_t", pure, final,
|
||||
|
||||
@@ -46,7 +46,11 @@ const
|
||||
maxRegisters = 256 # don't think there is an arch with more registers
|
||||
useStackMaskHack = false ## use the stack mask hack for better performance
|
||||
StackGuardSize = 4096
|
||||
ThreadStackMask = 1024*256*sizeof(int)-1
|
||||
ThreadStackMask =
|
||||
when defined(genode):
|
||||
1024*64*sizeof(int)-1
|
||||
else:
|
||||
1024*256*sizeof(int)-1
|
||||
ThreadStackSize = ThreadStackMask+1 - StackGuardSize
|
||||
|
||||
when defined(windows):
|
||||
@@ -115,6 +119,49 @@ when defined(windows):
|
||||
## get the ID of the currently running thread.
|
||||
result = int(getCurrentThreadId())
|
||||
|
||||
elif defined(genode):
|
||||
const
|
||||
GenodeHeader = "genode_cpp/threads.h"
|
||||
type
|
||||
SysThread* {.importcpp: "Nim::SysThread",
|
||||
header: GenodeHeader, final, pure.} = object
|
||||
GenodeThreadProc = proc (x: pointer) {.noconv.}
|
||||
ThreadVarSlot = int
|
||||
|
||||
proc initThread(s: var SysThread,
|
||||
stackSize: culonglong,
|
||||
entry: GenodeThreadProc,
|
||||
arg: pointer) {.
|
||||
importcpp: "#.initThread(genodeEnv, @)".}
|
||||
|
||||
proc threadVarAlloc(): ThreadVarSlot = 0
|
||||
|
||||
proc offMainThread(): bool {.
|
||||
importcpp: "Nim::SysThread::offMainThread",
|
||||
header: GenodeHeader.}
|
||||
|
||||
proc threadVarSetValue(value: pointer) {.
|
||||
importcpp: "Nim::SysThread::threadVarSetValue(@)",
|
||||
header: GenodeHeader.}
|
||||
|
||||
proc threadVarGetValue(): pointer {.
|
||||
importcpp: "Nim::SysThread::threadVarGetValue()",
|
||||
header: GenodeHeader.}
|
||||
|
||||
var mainTls: pointer
|
||||
|
||||
proc threadVarSetValue(s: ThreadVarSlot, value: pointer) {.inline.} =
|
||||
if offMainThread():
|
||||
threadVarSetValue(value);
|
||||
else:
|
||||
mainTls = value
|
||||
|
||||
proc threadVarGetValue(s: ThreadVarSlot): pointer {.inline.} =
|
||||
if offMainThread():
|
||||
threadVarGetValue();
|
||||
else:
|
||||
mainTls
|
||||
|
||||
else:
|
||||
when not defined(macosx):
|
||||
{.passL: "-pthread".}
|
||||
@@ -451,6 +498,9 @@ when defined(windows):
|
||||
proc threadProcWrapper[TArg](closure: pointer): int32 {.stdcall.} =
|
||||
threadProcWrapperBody(closure)
|
||||
# implicitly return 0
|
||||
elif defined(genode):
|
||||
proc threadProcWrapper[TArg](closure: pointer) {.noconv.} =
|
||||
threadProcWrapperBody(closure)
|
||||
else:
|
||||
proc threadProcWrapper[TArg](closure: pointer): pointer {.noconv.} =
|
||||
threadProcWrapperBody(closure)
|
||||
@@ -482,6 +532,14 @@ when hostOS == "windows":
|
||||
cast[ptr SysThread](addr(a)), 1, -1)
|
||||
inc(k, MAXIMUM_WAIT_OBJECTS)
|
||||
|
||||
elif defined(genode):
|
||||
proc joinThread*[TArg](t: Thread[TArg]) {.importcpp.}
|
||||
## waits for the thread `t` to finish.
|
||||
|
||||
proc joinThreads*[TArg](t: varargs[Thread[TArg]]) =
|
||||
## waits for every thread in `t` to finish.
|
||||
for i in 0..t.high: joinThread(t[i])
|
||||
|
||||
else:
|
||||
proc joinThread*[TArg](t: Thread[TArg]) {.inline.} =
|
||||
## waits for the thread `t` to finish.
|
||||
@@ -531,6 +589,21 @@ when hostOS == "windows":
|
||||
## shouldn't use this proc.
|
||||
setThreadAffinityMask(t.sys, uint(1 shl cpu))
|
||||
|
||||
elif defined(genode):
|
||||
proc createThread*[TArg](t: var Thread[TArg],
|
||||
tp: proc (arg: TArg) {.thread, nimcall.},
|
||||
param: TArg) =
|
||||
when TArg isnot void: t.data = param
|
||||
t.dataFn = tp
|
||||
when hasSharedHeap: t.stackSize = ThreadStackSize
|
||||
t.sys.initThread(
|
||||
ThreadStackSize.culonglong,
|
||||
threadProcWrapper[TArg], addr(t))
|
||||
|
||||
proc pinToCpu*[Arg](t: var Thread[Arg]; cpu: Natural) =
|
||||
{.hint: "cannot change Genode thread CPU affinity after initialization".}
|
||||
discard
|
||||
|
||||
else:
|
||||
proc createThread*[TArg](t: var Thread[TArg],
|
||||
tp: proc (arg: TArg) {.thread, nimcall.},
|
||||
|
||||
Reference in New Issue
Block a user