Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move threads out of system #20674

Merged
merged 19 commits into from
Oct 29, 2022
3 changes: 1 addition & 2 deletions lib/core/locks.nim
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ when not compileOption("threads") and not defined(nimdoc):
when false: # fix #12330
{.error: "Locks requires --threads:on option.".}

const insideRLocksModule = false
include "system/syslocks"
import std/private/syslocks

type
Lock* = SysLock ## Nim lock; whether this is re-entrant
Expand Down
3 changes: 1 addition & 2 deletions lib/core/rlocks.nim
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ when not compileOption("threads") and not defined(nimdoc):
# so they can replace each other seamlessly.
{.error: "Rlocks requires --threads:on option.".}

const insideRLocksModule = true
include "system/syslocks"
import std/private/syslocks

type
RLock* = SysLock ## Nim lock, re-entrant
Expand Down
2 changes: 1 addition & 1 deletion lib/genode/alloc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ when not defined(genode):
{.error: "Genode only module".}

when not declared(GenodeEnv):
include genode/env
import genode/env

type RamDataspaceCapability {.
importcpp: "Genode::Ram_dataspace_capability", pure.} = object
Expand Down
8 changes: 4 additions & 4 deletions lib/genode/env.nim
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ when not defined(genode):
{.error: "Genode only include".}

type
GenodeEnvObj {.importcpp: "Genode::Env", header: "<base/env.h>", pure.} = object
GenodeEnvPtr = ptr GenodeEnvObj
GenodeEnvObj* {.importcpp: "Genode::Env", header: "<base/env.h>", pure.} = object
GenodeEnvPtr* = ptr GenodeEnvObj

const runtimeEnvSym = "nim_runtime_env"
const runtimeEnvSym* = "nim_runtime_env"

when not defined(nimscript):
var runtimeEnv {.importcpp: runtimeEnvSym.}: GenodeEnvPtr
var runtimeEnv* {.importcpp: runtimeEnvSym.}: GenodeEnvPtr
2 changes: 1 addition & 1 deletion lib/pure/concurrency/cpuinfo.nim
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ when defined(macosx) or defined(bsd):
importc: "sysctl", nodecl.}

when defined(genode):
include genode/env
import genode/env

proc affinitySpaceTotal(env: GenodeEnvPtr): cuint {.
importcpp: "@->cpu().affinity_space().total()".}
Expand Down
170 changes: 85 additions & 85 deletions lib/system/syslocks.nim → lib/std/private/syslocks.nim
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ when defined(windows):
type
Handle = int

SysLock {.importc: "CRITICAL_SECTION",
SysLock* {.importc: "CRITICAL_SECTION",
header: "<windows.h>", final, pure.} = object # CRITICAL_SECTION in WinApi
DebugInfo: pointer
LockCount: int32
Expand All @@ -24,29 +24,29 @@ when defined(windows):
LockSemaphore: int
SpinCount: int

SysCond {.importc: "RTL_CONDITION_VARIABLE", header: "<windows.h>".} = object
SysCond* {.importc: "RTL_CONDITION_VARIABLE", header: "<windows.h>".} = object
thePtr {.importc: "ptr".} : Handle

proc initSysLock(L: var SysLock) {.importc: "InitializeCriticalSection",
proc initSysLock*(L: var SysLock) {.importc: "InitializeCriticalSection",
header: "<windows.h>".}
## Initializes the lock `L`.

proc tryAcquireSysAux(L: var SysLock): int32 {.importc: "TryEnterCriticalSection",
header: "<windows.h>".}
## Tries to acquire the lock `L`.

proc tryAcquireSys(L: var SysLock): bool {.inline.} =
proc tryAcquireSys*(L: var SysLock): bool {.inline.} =
result = tryAcquireSysAux(L) != 0'i32

proc acquireSys(L: var SysLock) {.importc: "EnterCriticalSection",
proc acquireSys*(L: var SysLock) {.importc: "EnterCriticalSection",
header: "<windows.h>".}
## Acquires the lock `L`.

proc releaseSys(L: var SysLock) {.importc: "LeaveCriticalSection",
proc releaseSys*(L: var SysLock) {.importc: "LeaveCriticalSection",
header: "<windows.h>".}
## Releases the lock `L`.

proc deinitSys(L: var SysLock) {.importc: "DeleteCriticalSection",
proc deinitSys*(L: var SysLock) {.importc: "DeleteCriticalSection",
header: "<windows.h>".}

proc initializeConditionVariable(
Expand All @@ -60,41 +60,41 @@ when defined(windows):
): int32 {.stdcall, noSideEffect, dynlib: "kernel32", importc: "SleepConditionVariableCS".}


proc signalSysCond(hEvent: var SysCond) {.stdcall, noSideEffect,
proc signalSysCond*(hEvent: var SysCond) {.stdcall, noSideEffect,
dynlib: "kernel32", importc: "WakeConditionVariable".}

proc broadcastSysCond(hEvent: var SysCond) {.stdcall, noSideEffect,
proc broadcastSysCond*(hEvent: var SysCond) {.stdcall, noSideEffect,
dynlib: "kernel32", importc: "WakeAllConditionVariable".}

proc initSysCond(cond: var SysCond) {.inline.} =
proc initSysCond*(cond: var SysCond) {.inline.} =
initializeConditionVariable(cond)
proc deinitSysCond(cond: var SysCond) {.inline.} =
proc deinitSysCond*(cond: var SysCond) {.inline.} =
discard
proc waitSysCond(cond: var SysCond, lock: var SysLock) =
proc waitSysCond*(cond: var SysCond, lock: var SysLock) =
discard sleepConditionVariableCS(cond, lock, -1'i32)

elif defined(genode):
const
Header = "genode_cpp/syslocks.h"
type
SysLock {.importcpp: "Nim::SysLock", pure, final,
SysLock* {.importcpp: "Nim::SysLock", pure, final,
header: Header.} = object
SysCond {.importcpp: "Nim::SysCond", pure, final,
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 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) {.
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) {.
proc signalSysCond*(cond: var SysCond) {.
noSideEffect, importcpp.}
proc broadcastSysCond(cond: var SysCond) {.
proc broadcastSysCond*(cond: var SysCond) {.
noSideEffect, importcpp.}

else:
Expand All @@ -105,7 +105,7 @@ else:
when defined(linux) and defined(amd64):
abi: array[40 div sizeof(clong), clong]

SysLockAttr {.importc: "pthread_mutexattr_t", pure, final
SysLockAttr* {.importc: "pthread_mutexattr_t", pure, final
header: """#include <sys/types.h>
#include <pthread.h>""".} = object
when defined(linux) and defined(amd64):
Expand Down Expand Up @@ -143,92 +143,92 @@ else:
# to prevent this once and for all, we're doing an extra malloc when
# initializing the primitive.
type
SysLock = ptr SysLockObj
SysCond = ptr SysCondObj
SysLock* = ptr SysLockObj
SysCond* = ptr SysCondObj

when not declared(c_malloc):
proc c_malloc(size: csize_t): pointer {.
importc: "malloc", header: "<stdlib.h>".}
proc c_free(p: pointer) {.
importc: "free", header: "<stdlib.h>".}

proc initSysLock(L: var SysLock, attr: ptr SysLockAttr = nil) =
proc initSysLock*(L: var SysLock, attr: ptr SysLockAttr = nil) =
L = cast[SysLock](c_malloc(csize_t(sizeof(SysLockObj))))
initSysLockAux(L[], attr)

proc deinitSys(L: var SysLock) =
proc deinitSys*(L: var SysLock) =
deinitSysAux(L[])
c_free(L)

template acquireSys(L: var SysLock) =
template acquireSys*(L: var SysLock) =
acquireSysAux(L[])
template tryAcquireSys(L: var SysLock): bool =
template tryAcquireSys*(L: var SysLock): bool =
tryAcquireSysAux(L[]) == 0'i32
template releaseSys(L: var SysLock) =
template releaseSys*(L: var SysLock) =
releaseSysAux(L[])
else:
type
SysLock = SysLockObj
SysCond = SysCondObj
SysLock* = SysLockObj
SysCond* = SysCondObj

template initSysLock(L: var SysLock, attr: ptr SysLockAttr = nil) =
template initSysLock*(L: var SysLock, attr: ptr SysLockAttr = nil) =
initSysLockAux(L, attr)
template deinitSys(L: var SysLock) =
template deinitSys*(L: var SysLock) =
deinitSysAux(L)
template acquireSys(L: var SysLock) =
template acquireSys*(L: var SysLock) =
acquireSysAux(L)
template tryAcquireSys(L: var SysLock): bool =
template tryAcquireSys*(L: var SysLock): bool =
tryAcquireSysAux(L) == 0'i32
template releaseSys(L: var SysLock) =
template releaseSys*(L: var SysLock) =
releaseSysAux(L)

when insideRLocksModule:
let SysLockType_Reentrant {.importc: "PTHREAD_MUTEX_RECURSIVE",
header: "<pthread.h>".}: SysLockType
proc initSysLockAttr(a: var SysLockAttr) {.
importc: "pthread_mutexattr_init", header: "<pthread.h>", noSideEffect.}
proc setSysLockType(a: var SysLockAttr, t: SysLockType) {.
importc: "pthread_mutexattr_settype", header: "<pthread.h>", noSideEffect.}
# rlocks
var SysLockType_Reentrant* {.importc: "PTHREAD_MUTEX_RECURSIVE",
header: "<pthread.h>".}: SysLockType
proc initSysLockAttr*(a: var SysLockAttr) {.
importc: "pthread_mutexattr_init", header: "<pthread.h>", noSideEffect.}
proc setSysLockType*(a: var SysLockAttr, t: SysLockType) {.
importc: "pthread_mutexattr_settype", header: "<pthread.h>", noSideEffect.}

# locks
proc initSysCondAux(cond: var SysCondObj, cond_attr: ptr SysCondAttr = nil) {.
importc: "pthread_cond_init", header: "<pthread.h>", noSideEffect.}
proc deinitSysCondAux(cond: var SysCondObj) {.noSideEffect,
importc: "pthread_cond_destroy", header: "<pthread.h>".}

proc waitSysCondAux(cond: var SysCondObj, lock: var SysLockObj): cint {.
importc: "pthread_cond_wait", header: "<pthread.h>", noSideEffect.}
proc signalSysCondAux(cond: var SysCondObj) {.
importc: "pthread_cond_signal", header: "<pthread.h>", noSideEffect.}
proc broadcastSysCondAux(cond: var SysCondObj) {.
importc: "pthread_cond_broadcast", header: "<pthread.h>", noSideEffect.}

when defined(ios):
proc initSysCond*(cond: var SysCond, cond_attr: ptr SysCondAttr = nil) =
cond = cast[SysCond](c_malloc(csize_t(sizeof(SysCondObj))))
initSysCondAux(cond[], cond_attr)

proc deinitSysCond*(cond: var SysCond) =
deinitSysCondAux(cond[])
c_free(cond)

template waitSysCond*(cond: var SysCond, lock: var SysLock) =
discard waitSysCondAux(cond[], lock[])
template signalSysCond*(cond: var SysCond) =
signalSysCondAux(cond[])
template broadcastSysCond*(cond: var SysCond) =
broadcastSysCondAux(cond[])
else:
proc initSysCondAux(cond: var SysCondObj, cond_attr: ptr SysCondAttr = nil) {.
importc: "pthread_cond_init", header: "<pthread.h>", noSideEffect.}
proc deinitSysCondAux(cond: var SysCondObj) {.noSideEffect,
importc: "pthread_cond_destroy", header: "<pthread.h>".}

proc waitSysCondAux(cond: var SysCondObj, lock: var SysLockObj): cint {.
importc: "pthread_cond_wait", header: "<pthread.h>", noSideEffect.}
proc signalSysCondAux(cond: var SysCondObj) {.
importc: "pthread_cond_signal", header: "<pthread.h>", noSideEffect.}
proc broadcastSysCondAux(cond: var SysCondObj) {.
importc: "pthread_cond_broadcast", header: "<pthread.h>", noSideEffect.}

when defined(ios):
proc initSysCond(cond: var SysCond, cond_attr: ptr SysCondAttr = nil) =
cond = cast[SysCond](c_malloc(csize_t(sizeof(SysCondObj))))
initSysCondAux(cond[], cond_attr)

proc deinitSysCond(cond: var SysCond) =
deinitSysCondAux(cond[])
c_free(cond)

template waitSysCond(cond: var SysCond, lock: var SysLock) =
discard waitSysCondAux(cond[], lock[])
template signalSysCond(cond: var SysCond) =
signalSysCondAux(cond[])
template broadcastSysCond(cond: var SysCond) =
broadcastSysCondAux(cond[])
else:
template initSysCond(cond: var SysCond, cond_attr: ptr SysCondAttr = nil) =
initSysCondAux(cond, cond_attr)
template deinitSysCond(cond: var SysCond) =
deinitSysCondAux(cond)

template waitSysCond(cond: var SysCond, lock: var SysLock) =
discard waitSysCondAux(cond, lock)
template signalSysCond(cond: var SysCond) =
signalSysCondAux(cond)
template broadcastSysCond(cond: var SysCond) =
broadcastSysCondAux(cond)
template initSysCond*(cond: var SysCond, cond_attr: ptr SysCondAttr = nil) =
initSysCondAux(cond, cond_attr)
template deinitSysCond*(cond: var SysCond) =
deinitSysCondAux(cond)

template waitSysCond*(cond: var SysCond, lock: var SysLock) =
discard waitSysCondAux(cond, lock)
template signalSysCond*(cond: var SysCond) =
signalSysCondAux(cond)
template broadcastSysCond*(cond: var SysCond) =
broadcastSysCondAux(cond)

{.pop.}
Loading