123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391 |
- #
- #
- # Nim's Runtime Library
- # (c) Copyright 2012 Andreas Rumpf
- #
- # See the file "copying.txt", included in this
- # distribution, for details about the copyright.
- #
- ## Thread support for Nim.
- ##
- ## **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`:option: command line switch.
- ##
- ## Nim's memory model for threads is quite different from other common
- ## programming languages (C, Pascal): Each thread has its own
- ## (garbage collected) heap and sharing of memory is restricted. This helps
- ## to prevent race conditions and improves efficiency. See `the manual for
- ## details of this memory model <manual.html#threads>`_.
- ##
- ## Examples
- ## ========
- ##
- ## .. code-block:: Nim
- ##
- ## import std/locks
- ##
- ## var
- ## thr: array[0..4, Thread[tuple[a,b: int]]]
- ## L: Lock
- ##
- ## proc threadFunc(interval: tuple[a,b: int]) {.thread.} =
- ## for i in interval.a..interval.b:
- ## acquire(L) # lock stdout
- ## echo i
- ## release(L)
- ##
- ## initLock(L)
- ##
- ## for i in 0..high(thr):
- ## createThread(thr[i], threadFunc, (i*10, i*10+5))
- ## joinThreads(thr)
- ##
- ## deinitLock(L)
- when not declared(ThisIsSystem):
- {.error: "You must not import this module explicitly".}
- when defined(nimPreviewSlimSystem):
- import std/assertions
- const
- hasAllocStack = defined(zephyr) # maybe freertos too?
- when defined(gcDestructors):
- proc allocThreadStorage(size: int): pointer =
- result = c_malloc(csize_t size)
- zeroMem(result, size)
- proc deallocThreadStorage(p: pointer) = c_free(p)
- else:
- template allocThreadStorage(size: untyped): untyped = allocShared0(size)
- template deallocThreadStorage(p: pointer) = deallocShared(p)
- when hasAllocStack or defined(zephyr) or defined(freertos):
- const
- nimThreadStackSize {.intdefine.} = 8192
- nimThreadStackGuard {.intdefine.} = 128
- StackGuardSize = nimThreadStackGuard
- ThreadStackSize = nimThreadStackSize - nimThreadStackGuard
- 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
- # Zephyr doesn't include this properly without some help
- when defined(zephyr):
- {.emit: """/*INCLUDESECTION*/
- #include <pthread.h>
- """.}
- # create for the main thread. Note: do not insert this data into the list
- # of all threads; it's not to be stopped etc.
- when not defined(useNimRtl):
- #when not defined(createNimRtl): initStackBottom()
- when declared(initGC):
- initGC()
- when not emulatedThreadVars:
- type ThreadType {.pure.} = enum
- None = 0,
- NimThread = 1,
- ForeignThread = 2
- var
- threadType {.rtlThreadVar.}: ThreadType
- threadType = ThreadType.NimThread
- # We jump through some hops here to ensure that Nim thread procs can have
- # the Nim calling convention. This is needed because thread procs are
- # ``stdcall`` on Windows and ``noconv`` on UNIX. Alternative would be to just
- # use ``stdcall`` since it is mapped to ``noconv`` on UNIX anyway.
- type
- Thread*[TArg] = object
- core: PGcThread
- sys: SysThread
- when TArg is void:
- dataFn: proc () {.nimcall, gcsafe.}
- else:
- dataFn: proc (m: TArg) {.nimcall, gcsafe.}
- data: TArg
- when hasAllocStack:
- rawStack: pointer
- proc `=copy`*[TArg](x: var Thread[TArg], y: Thread[TArg]) {.error.}
- var
- threadDestructionHandlers {.rtlThreadVar.}: seq[proc () {.closure, gcsafe, raises: [].}]
- proc onThreadDestruction*(handler: proc () {.closure, gcsafe, raises: [].}) =
- ## Registers a *thread local* handler that is called at the thread's
- ## destruction.
- ##
- ## A thread is destructed when the `.thread` proc returns
- ## normally or when it raises an exception. Note that unhandled exceptions
- ## in a thread nevertheless cause the whole process to die.
- threadDestructionHandlers.add handler
- template afterThreadRuns() =
- for i in countdown(threadDestructionHandlers.len-1, 0):
- threadDestructionHandlers[i]()
- when not defined(boehmgc) and not hasSharedHeap and not defined(gogc) and not defined(gcRegions):
- proc deallocOsPages() {.rtl, raises: [].}
- proc threadTrouble() {.raises: [], gcsafe.}
- ## defined in system/excpt.nim
- when defined(boehmgc):
- type GCStackBaseProc = proc(sb: pointer, t: pointer) {.noconv.}
- proc boehmGC_call_with_stack_base(sbp: GCStackBaseProc, p: pointer)
- {.importc: "GC_call_with_stack_base", boehmGC.}
- proc boehmGC_register_my_thread(sb: pointer)
- {.importc: "GC_register_my_thread", boehmGC.}
- proc boehmGC_unregister_my_thread()
- {.importc: "GC_unregister_my_thread", boehmGC.}
- proc threadProcWrapDispatch[TArg](sb: pointer, thrd: pointer) {.noconv, raises: [].} =
- boehmGC_register_my_thread(sb)
- try:
- let thrd = cast[ptr Thread[TArg]](thrd)
- when TArg is void:
- thrd.dataFn()
- else:
- thrd.dataFn(thrd.data)
- except:
- threadTrouble()
- finally:
- afterThreadRuns()
- boehmGC_unregister_my_thread()
- else:
- proc threadProcWrapDispatch[TArg](thrd: ptr Thread[TArg]) {.raises: [].} =
- try:
- when TArg is void:
- thrd.dataFn()
- else:
- when defined(nimV2):
- thrd.dataFn(thrd.data)
- else:
- var x: TArg
- deepCopy(x, thrd.data)
- thrd.dataFn(x)
- except:
- threadTrouble()
- finally:
- afterThreadRuns()
- when hasAllocStack:
- deallocThreadStorage(thrd.rawStack)
- proc threadProcWrapStackFrame[TArg](thrd: ptr Thread[TArg]) {.raises: [].} =
- when defined(boehmgc):
- boehmGC_call_with_stack_base(threadProcWrapDispatch[TArg], thrd)
- elif not defined(nogc) and not defined(gogc) and not defined(gcRegions) and not usesDestructors:
- var p {.volatile.}: pointer
- # init the GC for refc/markandsweep
- nimGC_setStackBottom(addr(p))
- initGC()
- when declared(threadType):
- threadType = ThreadType.NimThread
- threadProcWrapDispatch[TArg](thrd)
- when declared(deallocOsPages): deallocOsPages()
- else:
- threadProcWrapDispatch(thrd)
- template threadProcWrapperBody(closure: untyped): untyped =
- var thrd = cast[ptr Thread[TArg]](closure)
- var core = thrd.core
- when declared(globalsSlot): threadVarSetValue(globalsSlot, thrd.core)
- threadProcWrapStackFrame(thrd)
- # 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!
- # mark as not running anymore:
- thrd.core = nil
- thrd.dataFn = nil
- deallocThreadStorage(cast[pointer](core))
- {.push stack_trace:off.}
- 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)
- {.pop.}
- proc running*[TArg](t: Thread[TArg]): bool {.inline.} =
- ## Returns true if `t` is running.
- result = t.dataFn != nil
- proc handle*[TArg](t: Thread[TArg]): SysThread {.inline.} =
- ## Returns the thread handle of `t`.
- result = t.sys
- when hostOS == "windows":
- const MAXIMUM_WAIT_OBJECTS = 64
- proc joinThread*[TArg](t: Thread[TArg]) {.inline.} =
- ## Waits for the thread `t` to finish.
- discard waitForSingleObject(t.sys, -1'i32)
- proc joinThreads*[TArg](t: varargs[Thread[TArg]]) =
- ## Waits for every thread in `t` to finish.
- var a: array[MAXIMUM_WAIT_OBJECTS, SysThread]
- var k = 0
- while k < len(t):
- var count = min(len(t) - k, MAXIMUM_WAIT_OBJECTS)
- for i in 0..(count - 1): a[i] = t[i + k].sys
- discard waitForMultipleObjects(int32(count),
- 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.
- discard pthread_join(t.sys, nil)
- proc joinThreads*[TArg](t: varargs[Thread[TArg]]) =
- ## Waits for every thread in `t` to finish.
- for i in 0..t.high: joinThread(t[i])
- when false:
- # XXX a thread should really release its heap here somehow:
- proc destroyThread*[TArg](t: var Thread[TArg]) =
- ## 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":
- discard TerminateThread(t.sys, 1'i32)
- else:
- discard pthread_cancel(t.sys)
- when declared(registerThread): unregisterThread(addr(t))
- t.dataFn = nil
- ## if thread `t` already exited, `t.core` will be `null`.
- if not isNil(t.core):
- deallocThreadStorage(t.core)
- t.core = nil
- when hostOS == "windows":
- proc createThread*[TArg](t: var Thread[TArg],
- tp: proc (arg: TArg) {.thread, nimcall.},
- param: TArg) =
- ## Creates a new thread `t` and starts its execution.
- ##
- ## Entry point is the proc `tp`.
- ## `param` is passed to `tp`. `TArg` can be `void` if you
- ## don't need to pass any data to the thread.
- t.core = cast[PGcThread](allocThreadStorage(sizeof(GcThread)))
- when TArg isnot void: t.data = param
- t.dataFn = tp
- when hasSharedHeap: t.core.stackSize = ThreadStackSize
- var dummyThreadId: int32
- t.sys = createThread(nil, ThreadStackSize, threadProcWrapper[TArg],
- addr(t), 0'i32, dummyThreadId)
- if t.sys <= 0:
- raise newException(ResourceExhaustedError, "cannot create thread")
- proc pinToCpu*[Arg](t: var Thread[Arg]; cpu: Natural) =
- ## Pins a thread to a `CPU`:idx:.
- ##
- ## In other words sets a thread's `affinity`:idx:.
- ## If you don't know what this means, you shouldn't use this proc.
- setThreadAffinityMask(t.sys, uint(1 shl cpu))
- elif defined(genode):
- var affinityOffset: cuint = 1
- ## CPU affinity offset for next thread, safe to roll-over.
- proc createThread*[TArg](t: var Thread[TArg],
- tp: proc (arg: TArg) {.thread, nimcall.},
- param: TArg) =
- t.core = cast[PGcThread](allocThreadStorage(sizeof(GcThread)))
- when TArg isnot void: t.data = param
- t.dataFn = tp
- when hasSharedHeap: t.stackSize = ThreadStackSize
- t.sys.initThread(
- runtimeEnv,
- ThreadStackSize.culonglong,
- threadProcWrapper[TArg], addr(t), affinityOffset)
- inc affinityOffset
- 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.},
- param: TArg) =
- ## Creates a new thread `t` and starts its execution.
- ##
- ## Entry point is the proc `tp`. `param` is passed to `tp`.
- ## `TArg` can be `void` if you
- ## don't need to pass any data to the thread.
- t.core = cast[PGcThread](allocThreadStorage(sizeof(GcThread)))
- when TArg isnot void: t.data = param
- t.dataFn = tp
- when hasSharedHeap: t.core.stackSize = ThreadStackSize
- var a {.noinit.}: Pthread_attr
- doAssert pthread_attr_init(a) == 0
- when hasAllocStack:
- var
- rawstk = allocThreadStorage(ThreadStackSize + StackGuardSize)
- stk = cast[pointer](cast[uint](rawstk) + StackGuardSize)
- let setstacksizeResult = pthread_attr_setstack(addr a, stk, ThreadStackSize)
- t.rawStack = rawstk
- else:
- let setstacksizeResult = pthread_attr_setstacksize(a, ThreadStackSize)
- when not defined(ios):
- # This fails on iOS
- doAssert(setstacksizeResult == 0)
- if pthread_create(t.sys, a, threadProcWrapper[TArg], addr(t)) != 0:
- raise newException(ResourceExhaustedError, "cannot create thread")
- doAssert pthread_attr_destroy(a) == 0
- proc pinToCpu*[Arg](t: var Thread[Arg]; cpu: Natural) =
- ## Pins a thread to a `CPU`:idx:.
- ##
- ## In other words sets a thread's `affinity`:idx:.
- ## If you don't know what this means, you shouldn't use this proc.
- when not defined(macosx):
- var s {.noinit.}: CpuSet
- cpusetZero(s)
- cpusetIncl(cpu.cint, s)
- setAffinity(t.sys, csize_t(sizeof(s)), s)
- proc createThread*(t: var Thread[void], tp: proc () {.thread, nimcall.}) =
- createThread[void](t, tp)
- when not defined(gcOrc):
- include threadids
|