mirror of
https://github.com/nim-lang/Nim.git
synced 2026-02-15 15:44:14 +00:00
preparations for 0.8.12
This commit is contained in:
@@ -65,9 +65,12 @@ elif defined(windows):
|
||||
PAGE_READWRITE)
|
||||
if result == nil: raiseOutOfMem()
|
||||
|
||||
proc osDeallocPages(p: pointer, size: int) {.inline.} =
|
||||
# according to Microsoft, 0 is the only correct value here:
|
||||
when reallyOsDealloc: VirtualFree(p, 0, MEM_RELEASE)
|
||||
proc osDeallocPages(p: pointer, size: int) {.inline.} =
|
||||
# according to Microsoft, 0 is the only correct value for MEM_RELEASE:
|
||||
# This means that the OS has some different view over how big the block is
|
||||
# that we want to free! So, we cannot reliably release the memory back to
|
||||
# Windows :-(. We have to live with MEM_DECOMMIT instead.
|
||||
when reallyOsDealloc: VirtualFree(p, size, MEM_DECOMMIT)
|
||||
|
||||
else:
|
||||
{.error: "Port memory manager to your platform".}
|
||||
|
||||
3
lib/system/atomics.nim
Normal file → Executable file
3
lib/system/atomics.nim
Normal file → Executable file
@@ -9,7 +9,8 @@
|
||||
|
||||
## Atomic operations for Nimrod.
|
||||
|
||||
when (defined(gcc) or defined(llvm_gcc)) and hasThreadSupport:
|
||||
when (defined(gcc) or defined(llvm_gcc)) and hasThreadSupport and
|
||||
not defined(windows):
|
||||
proc sync_add_and_fetch(p: var int, val: int): int {.
|
||||
importc: "__sync_add_and_fetch", nodecl.}
|
||||
proc sync_sub_and_fetch(p: var int, val: int): int {.
|
||||
|
||||
36
lib/system/inboxes.nim
Normal file → Executable file
36
lib/system/inboxes.nim
Normal file → Executable file
@@ -7,8 +7,12 @@
|
||||
# distribution, for details about the copyright.
|
||||
#
|
||||
|
||||
## Message passing for threads. The current implementation is slow and does
|
||||
## not work with cyclic data structures. But hey, it's better than nothing.
|
||||
## Message passing for threads. **Note**: This is part of the system module.
|
||||
## Do not import it directly. To activate thread support you need to compile
|
||||
## with the ``--threads:on`` command line switch.
|
||||
##
|
||||
## **Note:** The current implementation of message passing is slow and does
|
||||
## not work with cyclic data structures.
|
||||
|
||||
type
|
||||
pbytes = ptr array[0.. 0xffff, byte]
|
||||
@@ -18,6 +22,7 @@ type
|
||||
lock: TSysLock
|
||||
cond: TSysCond
|
||||
elemType: PNimType
|
||||
ready: bool
|
||||
region: TMemRegion
|
||||
PInbox = ptr TInbox
|
||||
TLoadStoreMode = enum mStore, mLoad
|
||||
@@ -178,9 +183,7 @@ template lockInbox(q: expr, action: stmt) =
|
||||
action
|
||||
releaseSys(q.lock)
|
||||
|
||||
proc send*[TMsg](receiver: var TThread[TMsg], msg: TMsg) =
|
||||
## sends a message to a thread. `msg` is deeply copied.
|
||||
var q = cast[PInbox](getInBoxMem(receiver))
|
||||
template sendImpl(q: expr) =
|
||||
if q.mask == ThreadDeadMask:
|
||||
raise newException(EDeadThread, "cannot send message; thread died")
|
||||
acquireSys(q.lock)
|
||||
@@ -192,12 +195,24 @@ proc send*[TMsg](receiver: var TThread[TMsg], msg: TMsg) =
|
||||
releaseSys(q.lock)
|
||||
SignalSysCond(q.cond)
|
||||
|
||||
proc send*[TMsg](receiver: var TThread[TMsg], msg: TMsg) =
|
||||
## sends a message to a thread. `msg` is deeply copied.
|
||||
var q = cast[PInbox](getInBoxMem(receiver))
|
||||
sendImpl(q)
|
||||
|
||||
proc send*[TMsg](receiver: TThreadId[TMsg], msg: TMsg) =
|
||||
## sends a message to a thread. `msg` is deeply copied.
|
||||
var q = cast[PInbox](getInBoxMem(receiver[]))
|
||||
sendImpl(q)
|
||||
|
||||
proc llRecv(res: pointer, typ: PNimType) =
|
||||
# to save space, the generic is as small as possible
|
||||
var q = cast[PInbox](getInBoxMem())
|
||||
acquireSys(q.lock)
|
||||
q.ready = true
|
||||
while q.count <= 0:
|
||||
WaitSysCond(q.cond, q.lock)
|
||||
q.ready = false
|
||||
if typ != q.elemType:
|
||||
releaseSys(q.lock)
|
||||
raise newException(EInvalidValue, "cannot receive message of wrong type")
|
||||
@@ -215,4 +230,15 @@ proc peek*(): int =
|
||||
lockInbox(q):
|
||||
result = q.count
|
||||
|
||||
proc peek*[TMsg](t: var TThread[TMsg]): int =
|
||||
## returns the current number of messages in the inbox of thread `t`.
|
||||
var q = cast[PInbox](getInBoxMem(t))
|
||||
if q.mask != ThreadDeadMask:
|
||||
lockInbox(q):
|
||||
result = q.count
|
||||
|
||||
proc ready*[TMsg](t: var TThread[TMsg]): bool =
|
||||
## returns true iff the thread `t` is waiting on ``recv`` for new messages.
|
||||
var q = cast[PInbox](getInBoxMem(t))
|
||||
result = q.ready
|
||||
|
||||
|
||||
2
lib/system/syslocks.nim
Normal file → Executable file
2
lib/system/syslocks.nim
Normal file → Executable file
@@ -47,7 +47,7 @@ when defined(Windows):
|
||||
proc CreateEvent(lpEventAttributes: pointer,
|
||||
bManualReset, bInitialState: int32,
|
||||
lpName: cstring): TSysCond {.stdcall, noSideEffect,
|
||||
dynlib: "kernel32", importc: "CreateEvent".}
|
||||
dynlib: "kernel32", importc: "CreateEventA".}
|
||||
|
||||
proc CloseHandle(hObject: THandle) {.stdcall, noSideEffect,
|
||||
dynlib: "kernel32", importc: "CloseHandle".}
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
#
|
||||
|
||||
## Thread support for Nimrod. **Note**: This is part of the system module.
|
||||
## Do not import it directly. To active thread support you need to compile
|
||||
## Do not import it directly. To activate thread support you need to compile
|
||||
## with the ``--threads:on`` command line switch.
|
||||
##
|
||||
## Nimrod's memory model for threads is quite different from other common
|
||||
@@ -39,8 +39,8 @@
|
||||
|
||||
const
|
||||
maxRegisters = 256 # don't think there is an arch with more registers
|
||||
maxLocksPerThread* = 10 ## max number of locks a thread can hold
|
||||
## at the same time
|
||||
maxLocksPerThread = 10 ## max number of locks a thread can hold
|
||||
## at the same time
|
||||
useStackMaskHack = false ## use the stack mask hack for better performance
|
||||
StackGuardSize = 4096
|
||||
ThreadStackMask = 1024*256*sizeof(int)-1
|
||||
@@ -167,14 +167,17 @@ type
|
||||
PGcThread = ptr TGcThread
|
||||
TGcThread {.pure.} = object
|
||||
sys: TSysThread
|
||||
next, prev: PGcThread
|
||||
stackBottom, stackTop: pointer
|
||||
stackSize: int
|
||||
inbox: TThreadLocalStorage
|
||||
when emulatedThreadVars and not useStackMaskHack:
|
||||
tls: TThreadLocalStorage
|
||||
else:
|
||||
nil
|
||||
when hasSharedHeap:
|
||||
next, prev: PGcThread
|
||||
stackBottom, stackTop: pointer
|
||||
stackSize: int
|
||||
else:
|
||||
nil
|
||||
|
||||
# XXX it'd be more efficient to not use a global variable for the
|
||||
# thread storage slot, but to rely on the implementation to assign slot 0
|
||||
@@ -201,44 +204,45 @@ when not defined(useNimRtl):
|
||||
ThreadVarSetValue(globalsSlot, addr(mainThread))
|
||||
initStackBottom()
|
||||
initGC()
|
||||
|
||||
var heapLock: TSysLock
|
||||
InitSysLock(HeapLock)
|
||||
|
||||
when emulatedThreadVars:
|
||||
if NimThreadVarsSize() > sizeof(TThreadLocalStorage):
|
||||
echo "too large thread local storage size requested"
|
||||
quit 1
|
||||
|
||||
var
|
||||
threadList: PGcThread
|
||||
|
||||
proc registerThread(t: PGcThread) =
|
||||
# we need to use the GC global lock here!
|
||||
AcquireSys(HeapLock)
|
||||
t.prev = nil
|
||||
t.next = threadList
|
||||
if threadList != nil:
|
||||
sysAssert(threadList.prev == nil)
|
||||
threadList.prev = t
|
||||
threadList = t
|
||||
ReleaseSys(HeapLock)
|
||||
|
||||
proc unregisterThread(t: PGcThread) =
|
||||
# we need to use the GC global lock here!
|
||||
AcquireSys(HeapLock)
|
||||
if t == threadList: threadList = t.next
|
||||
if t.next != nil: t.next.prev = t.prev
|
||||
if t.prev != nil: t.prev.next = t.next
|
||||
# so that a thread can be unregistered twice which might happen if the
|
||||
# code executes `destroyThread`:
|
||||
t.next = nil
|
||||
t.prev = nil
|
||||
ReleaseSys(HeapLock)
|
||||
when hasSharedHeap:
|
||||
var heapLock: TSysLock
|
||||
InitSysLock(HeapLock)
|
||||
|
||||
var
|
||||
threadList: PGcThread
|
||||
|
||||
proc registerThread(t: PGcThread) =
|
||||
# we need to use the GC global lock here!
|
||||
AcquireSys(HeapLock)
|
||||
t.prev = nil
|
||||
t.next = threadList
|
||||
if threadList != nil:
|
||||
sysAssert(threadList.prev == nil)
|
||||
threadList.prev = t
|
||||
threadList = t
|
||||
ReleaseSys(HeapLock)
|
||||
|
||||
# on UNIX, the GC uses ``SIGFREEZE`` to tell every thread to stop so that
|
||||
# the GC can examine the stacks?
|
||||
proc stopTheWord() = nil
|
||||
proc unregisterThread(t: PGcThread) =
|
||||
# we need to use the GC global lock here!
|
||||
AcquireSys(HeapLock)
|
||||
if t == threadList: threadList = t.next
|
||||
if t.next != nil: t.next.prev = t.prev
|
||||
if t.prev != nil: t.prev.next = t.next
|
||||
# so that a thread can be unregistered twice which might happen if the
|
||||
# code executes `destroyThread`:
|
||||
t.next = nil
|
||||
t.prev = nil
|
||||
ReleaseSys(HeapLock)
|
||||
|
||||
# on UNIX, the GC uses ``SIGFREEZE`` to tell every thread to stop so that
|
||||
# the GC can examine the stacks?
|
||||
proc stopTheWord() = nil
|
||||
|
||||
# We jump through some hops here to ensure that Nimrod thread procs can have
|
||||
# the Nimrod calling convention. This is needed because thread procs are
|
||||
@@ -248,10 +252,15 @@ when not defined(useNimRtl):
|
||||
# GC'ed closures in Nimrod.
|
||||
|
||||
type
|
||||
TThread* {.pure, final.}[TParam] = object of TGcThread ## Nimrod thread.
|
||||
TThread* {.pure, final.}[TMsg] =
|
||||
object of TGcThread ## Nimrod thread. A thread is a heavy object (~14K)
|
||||
## that should not be part of a message! Use
|
||||
## a ``TThreadId`` for that.
|
||||
emptyFn: proc ()
|
||||
dataFn: proc (p: TParam)
|
||||
data: TParam
|
||||
dataFn: proc (p: TMsg)
|
||||
data: TMsg
|
||||
TThreadId*[TMsg] = ptr TThread[TMsg] ## the current implementation uses
|
||||
## a pointer as a thread ID.
|
||||
|
||||
proc initInbox(p: pointer)
|
||||
proc freeInbox(p: pointer)
|
||||
@@ -260,47 +269,47 @@ when not defined(boehmgc) and not hasSharedHeap:
|
||||
|
||||
template ThreadProcWrapperBody(closure: expr) =
|
||||
ThreadVarSetValue(globalsSlot, closure)
|
||||
var t = cast[ptr TThread[TParam]](closure)
|
||||
var t = cast[ptr TThread[TMsg]](closure)
|
||||
when useStackMaskHack:
|
||||
var tls: TThreadLocalStorage
|
||||
when not defined(boehmgc) and not hasSharedHeap:
|
||||
# init the GC for this thread:
|
||||
setStackBottom(addr(t))
|
||||
initGC()
|
||||
t.stackBottom = addr(t)
|
||||
registerThread(t)
|
||||
try:
|
||||
when false:
|
||||
var a = addr(tls)
|
||||
var b = MaskStackPointer(1293920-372736-303104-36864)
|
||||
c_fprintf(c_stdout, "TLS: %p\nmasked: %p\ndiff: %ld\n",
|
||||
a, b, cast[int](a) - cast[int](b))
|
||||
if t.emptyFn == nil: t.dataFn(t.data)
|
||||
else: t.emptyFn()
|
||||
finally:
|
||||
# XXX shut-down is not executed when the thread is forced down!
|
||||
freeInbox(addr(t.inbox))
|
||||
unregisterThread(t)
|
||||
when defined(deallocOsPages): deallocOsPages()
|
||||
when hasSharedHeap:
|
||||
t.stackBottom = addr(t)
|
||||
registerThread(t)
|
||||
if t.emptyFn == nil: t.dataFn(t.data)
|
||||
else: t.emptyFn()
|
||||
#finally:
|
||||
# XXX shut-down is not executed when the thread is forced down!
|
||||
freeInbox(addr(t.inbox))
|
||||
when hasSharedHeap: unregisterThread(t)
|
||||
when defined(deallocOsPages): deallocOsPages()
|
||||
# Since an unhandled exception terminates the whole process (!), there is
|
||||
# no need for a ``try finally`` here, nor would it be correct: The current
|
||||
# exception is tried to be re-raised by the code-gen after the ``finally``!
|
||||
# However this is doomed to fail, because we already unmapped every heap
|
||||
# page!
|
||||
|
||||
{.push stack_trace:off.}
|
||||
when defined(windows):
|
||||
proc threadProcWrapper[TParam](closure: pointer): int32 {.stdcall.} =
|
||||
proc threadProcWrapper[TMsg](closure: pointer): int32 {.stdcall.} =
|
||||
ThreadProcWrapperBody(closure)
|
||||
# implicitely return 0
|
||||
else:
|
||||
proc threadProcWrapper[TParam](closure: pointer) {.noconv.} =
|
||||
proc threadProcWrapper[TMsg](closure: pointer) {.noconv.} =
|
||||
ThreadProcWrapperBody(closure)
|
||||
{.pop.}
|
||||
|
||||
proc joinThread*[TParam](t: TThread[TParam]) {.inline.} =
|
||||
proc joinThread*[TMsg](t: TThread[TMsg]) {.inline.} =
|
||||
## waits for the thread `t` to finish.
|
||||
when hostOS == "windows":
|
||||
discard WaitForSingleObject(t.sys, -1'i32)
|
||||
else:
|
||||
discard pthread_join(t.sys, nil)
|
||||
|
||||
proc joinThreads*[TParam](t: openArray[TThread[TParam]]) =
|
||||
proc joinThreads*[TMsg](t: openArray[TThread[TMsg]]) =
|
||||
## waits for every thread in `t` to finish.
|
||||
when hostOS == "windows":
|
||||
var a: array[0..255, TSysThread]
|
||||
@@ -312,7 +321,7 @@ proc joinThreads*[TParam](t: openArray[TThread[TParam]]) =
|
||||
|
||||
when false:
|
||||
# XXX a thread should really release its heap here somehow:
|
||||
proc destroyThread*[TParam](t: var TThread[TParam]) =
|
||||
proc destroyThread*[TMsg](t: var TThread[TMsg]) =
|
||||
## forces the thread `t` to terminate. This is potentially dangerous if
|
||||
## you don't have full control over `t` and its acquired resources.
|
||||
when hostOS == "windows":
|
||||
@@ -321,18 +330,18 @@ when false:
|
||||
discard pthread_cancel(t.sys)
|
||||
unregisterThread(addr(t))
|
||||
|
||||
proc createThread*[TParam](t: var TThread[TParam],
|
||||
tp: proc (param: TParam) {.thread.},
|
||||
param: TParam) =
|
||||
proc createThread*[TMsg](t: var TThread[TMsg],
|
||||
tp: proc (msg: TMsg) {.thread.},
|
||||
param: TMsg) =
|
||||
## creates a new thread `t` and starts its execution. Entry point is the
|
||||
## proc `tp`. `param` is passed to `tp`.
|
||||
t.data = param
|
||||
t.dataFn = tp
|
||||
t.stackSize = ThreadStackSize
|
||||
when hasSharedHeap: t.stackSize = ThreadStackSize
|
||||
initInbox(addr(t.inbox))
|
||||
when hostOS == "windows":
|
||||
var dummyThreadId: int32
|
||||
t.sys = CreateThread(nil, ThreadStackSize, threadProcWrapper[TParam],
|
||||
t.sys = CreateThread(nil, ThreadStackSize, threadProcWrapper[TMsg],
|
||||
addr(t), 0'i32, dummyThreadId)
|
||||
if t.sys <= 0:
|
||||
raise newException(EResourceExhausted, "cannot create thread")
|
||||
@@ -340,18 +349,18 @@ proc createThread*[TParam](t: var TThread[TParam],
|
||||
var a: Tpthread_attr
|
||||
pthread_attr_init(a)
|
||||
pthread_attr_setstacksize(a, ThreadStackSize)
|
||||
if pthread_create(t.sys, a, threadProcWrapper[TParam], addr(t)) != 0:
|
||||
if pthread_create(t.sys, a, threadProcWrapper[TMsg], addr(t)) != 0:
|
||||
raise newException(EResourceExhausted, "cannot create thread")
|
||||
|
||||
proc createThread*[TParam](t: var TThread[TParam], tp: proc () {.thread.}) =
|
||||
proc createThread*[TMsg](t: var TThread[TMsg], tp: proc () {.thread.}) =
|
||||
## creates a new thread `t` and starts its execution. Entry point is the
|
||||
## proc `tp`.
|
||||
t.emptyFn = tp
|
||||
t.stackSize = ThreadStackSize
|
||||
when hasSharedHeap: t.stackSize = ThreadStackSize
|
||||
initInbox(addr(t.inbox))
|
||||
when hostOS == "windows":
|
||||
var dummyThreadId: int32
|
||||
t.sys = CreateThread(nil, ThreadStackSize, threadProcWrapper[TParam],
|
||||
t.sys = CreateThread(nil, ThreadStackSize, threadProcWrapper[TMsg],
|
||||
addr(t), 0'i32, dummyThreadId)
|
||||
if t.sys <= 0:
|
||||
raise newException(EResourceExhausted, "cannot create thread")
|
||||
@@ -359,9 +368,17 @@ proc createThread*[TParam](t: var TThread[TParam], tp: proc () {.thread.}) =
|
||||
var a: Tpthread_attr
|
||||
pthread_attr_init(a)
|
||||
pthread_attr_setstacksize(a, ThreadStackSize)
|
||||
if pthread_create(t.sys, a, threadProcWrapper[TParam], addr(t)) != 0:
|
||||
if pthread_create(t.sys, a, threadProcWrapper[TMsg], addr(t)) != 0:
|
||||
raise newException(EResourceExhausted, "cannot create thread")
|
||||
|
||||
proc threadId*[TMsg](t: var TThread[TMsg]): TThreadId[TMsg] {.inline.} =
|
||||
## returns the thread ID of `t`.
|
||||
result = addr(t)
|
||||
|
||||
proc myThreadId*[TMsg](): TThreadId[TMsg] =
|
||||
## returns the thread ID of the thread that calls this proc.
|
||||
result = cast[TThreadId[TMsg]](ThreadVarGetValue(globalsSlot))
|
||||
|
||||
when useStackMaskHack:
|
||||
proc runMain(tp: proc () {.thread.}) {.compilerproc.} =
|
||||
var mainThread: TThread[pointer]
|
||||
@@ -371,7 +388,7 @@ when useStackMaskHack:
|
||||
# --------------------------- lock handling ----------------------------------
|
||||
|
||||
type
|
||||
TLock* = TSysLock ## Nimrod lock
|
||||
TLock* = TSysLock ## Nimrod lock; not re-entrant!
|
||||
|
||||
const
|
||||
noDeadlocks = false # compileOption("deadlockPrevention")
|
||||
|
||||
Reference in New Issue
Block a user