Trim trailling spaces
This commit is contained in:
parent
afad61c220
commit
f9d8d6ce09
3 changed files with 43 additions and 45 deletions
|
|
@ -8,8 +8,8 @@
|
||||||
#
|
#
|
||||||
|
|
||||||
## This module implements the ability to access symbols from shared
|
## This module implements the ability to access symbols from shared
|
||||||
## libraries. On POSIX this uses the ``dlsym`` mechanism, on
|
## libraries. On POSIX this uses the ``dlsym`` mechanism, on
|
||||||
## Windows ``LoadLibrary``.
|
## Windows ``LoadLibrary``.
|
||||||
|
|
||||||
type
|
type
|
||||||
LibHandle* = pointer ## a handle to a dynamically loaded library
|
LibHandle* = pointer ## a handle to a dynamically loaded library
|
||||||
|
|
@ -17,11 +17,11 @@ type
|
||||||
{.deprecated: [TLibHandle: LibHandle].}
|
{.deprecated: [TLibHandle: LibHandle].}
|
||||||
|
|
||||||
proc loadLib*(path: string, global_symbols=false): LibHandle
|
proc loadLib*(path: string, global_symbols=false): LibHandle
|
||||||
## loads a library from `path`. Returns nil if the library could not
|
## loads a library from `path`. Returns nil if the library could not
|
||||||
## be loaded.
|
## be loaded.
|
||||||
|
|
||||||
proc loadLib*(): LibHandle
|
proc loadLib*(): LibHandle
|
||||||
## gets the handle from the current executable. Returns nil if the
|
## gets the handle from the current executable. Returns nil if the
|
||||||
## library could not be loaded.
|
## library could not be loaded.
|
||||||
|
|
||||||
proc unloadLib*(lib: LibHandle)
|
proc unloadLib*(lib: LibHandle)
|
||||||
|
|
@ -63,13 +63,13 @@ when defined(posix):
|
||||||
proc dlsym(lib: LibHandle, name: cstring): pointer {.
|
proc dlsym(lib: LibHandle, name: cstring): pointer {.
|
||||||
importc, header: "<dlfcn.h>".}
|
importc, header: "<dlfcn.h>".}
|
||||||
|
|
||||||
proc loadLib(path: string, global_symbols=false): LibHandle =
|
proc loadLib(path: string, global_symbols=false): LibHandle =
|
||||||
var flags = RTLD_NOW
|
var flags = RTLD_NOW
|
||||||
if global_symbols: flags = flags or RTLD_GLOBAL
|
if global_symbols: flags = flags or RTLD_GLOBAL
|
||||||
return dlopen(path, flags)
|
return dlopen(path, flags)
|
||||||
proc loadLib(): LibHandle = return dlopen(nil, RTLD_NOW)
|
proc loadLib(): LibHandle = return dlopen(nil, RTLD_NOW)
|
||||||
proc unloadLib(lib: LibHandle) = dlclose(lib)
|
proc unloadLib(lib: LibHandle) = dlclose(lib)
|
||||||
proc symAddr(lib: LibHandle, name: cstring): pointer =
|
proc symAddr(lib: LibHandle, name: cstring): pointer =
|
||||||
return dlsym(lib, name)
|
return dlsym(lib, name)
|
||||||
|
|
||||||
elif defined(windows) or defined(dos):
|
elif defined(windows) or defined(dos):
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@
|
||||||
const
|
const
|
||||||
NilLibHandle: LibHandle = nil
|
NilLibHandle: LibHandle = nil
|
||||||
|
|
||||||
proc rawWrite(f: File, s: string) =
|
proc rawWrite(f: File, s: string) =
|
||||||
# we cannot throw an exception here!
|
# we cannot throw an exception here!
|
||||||
discard writeBuffer(f, cstring(s), s.len)
|
discard writeBuffer(f, cstring(s), s.len)
|
||||||
|
|
||||||
|
|
@ -145,6 +145,5 @@ elif defined(mac):
|
||||||
|
|
||||||
else:
|
else:
|
||||||
{.error: "no implementation for dyncalls".}
|
{.error: "no implementation for dyncalls".}
|
||||||
|
|
||||||
{.pop.}
|
|
||||||
|
|
||||||
|
{.pop.}
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@
|
||||||
## Do not import it directly. To activate 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.
|
## with the ``--threads:on`` command line switch.
|
||||||
##
|
##
|
||||||
## Nim's memory model for threads is quite different from other common
|
## Nim's memory model for threads is quite different from other common
|
||||||
## programming languages (C, Pascal): Each thread has its own
|
## programming languages (C, Pascal): Each thread has its own
|
||||||
## (garbage collected) heap and sharing of memory is restricted. This helps
|
## (garbage collected) heap and sharing of memory is restricted. This helps
|
||||||
## to prevent race conditions and improves efficiency. See `the manual for
|
## to prevent race conditions and improves efficiency. See `the manual for
|
||||||
|
|
@ -26,7 +26,7 @@
|
||||||
## var
|
## var
|
||||||
## thr: array [0..4, Thread[tuple[a,b: int]]]
|
## thr: array [0..4, Thread[tuple[a,b: int]]]
|
||||||
## L: Lock
|
## L: Lock
|
||||||
##
|
##
|
||||||
## proc threadFunc(interval: tuple[a,b: int]) {.thread.} =
|
## proc threadFunc(interval: tuple[a,b: int]) {.thread.} =
|
||||||
## for i in interval.a..interval.b:
|
## for i in interval.a..interval.b:
|
||||||
## acquire(L) # lock stdout
|
## acquire(L) # lock stdout
|
||||||
|
|
@ -38,8 +38,8 @@
|
||||||
## for i in 0..high(thr):
|
## for i in 0..high(thr):
|
||||||
## createThread(thr[i], threadFunc, (i*10, i*10+5))
|
## createThread(thr[i], threadFunc, (i*10, i*10+5))
|
||||||
## joinThreads(thr)
|
## joinThreads(thr)
|
||||||
|
|
||||||
when not declared(NimString):
|
when not declared(NimString):
|
||||||
{.error: "You must not import this module explicitly".}
|
{.error: "You must not import this module explicitly".}
|
||||||
|
|
||||||
const
|
const
|
||||||
|
|
@ -56,15 +56,15 @@ when defined(windows):
|
||||||
{.deprecated: [TSysThread: SysThread, TWinThreadProc: WinThreadProc].}
|
{.deprecated: [TSysThread: SysThread, TWinThreadProc: WinThreadProc].}
|
||||||
|
|
||||||
proc createThread(lpThreadAttributes: pointer, dwStackSize: int32,
|
proc createThread(lpThreadAttributes: pointer, dwStackSize: int32,
|
||||||
lpStartAddress: WinThreadProc,
|
lpStartAddress: WinThreadProc,
|
||||||
lpParameter: pointer,
|
lpParameter: pointer,
|
||||||
dwCreationFlags: int32,
|
dwCreationFlags: int32,
|
||||||
lpThreadId: var int32): SysThread {.
|
lpThreadId: var int32): SysThread {.
|
||||||
stdcall, dynlib: "kernel32", importc: "CreateThread".}
|
stdcall, dynlib: "kernel32", importc: "CreateThread".}
|
||||||
|
|
||||||
proc winSuspendThread(hThread: SysThread): int32 {.
|
proc winSuspendThread(hThread: SysThread): int32 {.
|
||||||
stdcall, dynlib: "kernel32", importc: "SuspendThread".}
|
stdcall, dynlib: "kernel32", importc: "SuspendThread".}
|
||||||
|
|
||||||
proc winResumeThread(hThread: SysThread): int32 {.
|
proc winResumeThread(hThread: SysThread): int32 {.
|
||||||
stdcall, dynlib: "kernel32", importc: "ResumeThread".}
|
stdcall, dynlib: "kernel32", importc: "ResumeThread".}
|
||||||
|
|
||||||
|
|
@ -76,7 +76,7 @@ when defined(windows):
|
||||||
|
|
||||||
proc terminateThread(hThread: SysThread, dwExitCode: int32): int32 {.
|
proc terminateThread(hThread: SysThread, dwExitCode: int32): int32 {.
|
||||||
stdcall, dynlib: "kernel32", importc: "TerminateThread".}
|
stdcall, dynlib: "kernel32", importc: "TerminateThread".}
|
||||||
|
|
||||||
type
|
type
|
||||||
ThreadVarSlot = distinct int32
|
ThreadVarSlot = distinct int32
|
||||||
|
|
||||||
|
|
@ -104,7 +104,7 @@ when defined(windows):
|
||||||
importc: "TlsSetValue", stdcall, dynlib: "kernel32".}
|
importc: "TlsSetValue", stdcall, dynlib: "kernel32".}
|
||||||
proc threadVarGetValue(dwTlsIndex: ThreadVarSlot): pointer {.
|
proc threadVarGetValue(dwTlsIndex: ThreadVarSlot): pointer {.
|
||||||
importc: "TlsGetValue", stdcall, dynlib: "kernel32".}
|
importc: "TlsGetValue", stdcall, dynlib: "kernel32".}
|
||||||
|
|
||||||
else:
|
else:
|
||||||
when not defined(macosx):
|
when not defined(macosx):
|
||||||
{.passL: "-pthread".}
|
{.passL: "-pthread".}
|
||||||
|
|
@ -116,7 +116,7 @@ else:
|
||||||
final, pure.} = object
|
final, pure.} = object
|
||||||
Pthread_attr {.importc: "pthread_attr_t",
|
Pthread_attr {.importc: "pthread_attr_t",
|
||||||
header: "<sys/types.h>", final, pure.} = object
|
header: "<sys/types.h>", final, pure.} = object
|
||||||
|
|
||||||
Timespec {.importc: "struct timespec",
|
Timespec {.importc: "struct timespec",
|
||||||
header: "<time.h>", final, pure.} = object
|
header: "<time.h>", final, pure.} = object
|
||||||
tv_sec: int
|
tv_sec: int
|
||||||
|
|
@ -130,8 +130,8 @@ else:
|
||||||
importc, header: "<pthread.h>".}
|
importc, header: "<pthread.h>".}
|
||||||
|
|
||||||
proc pthread_create(a1: var SysThread, a2: var PthreadAttr,
|
proc pthread_create(a1: var SysThread, a2: var PthreadAttr,
|
||||||
a3: proc (x: pointer): pointer {.noconv.},
|
a3: proc (x: pointer): pointer {.noconv.},
|
||||||
a4: pointer): cint {.importc: "pthread_create",
|
a4: pointer): cint {.importc: "pthread_create",
|
||||||
header: "<pthread.h>".}
|
header: "<pthread.h>".}
|
||||||
proc pthread_join(a1: SysThread, a2: ptr pointer): cint {.
|
proc pthread_join(a1: SysThread, a2: ptr pointer): cint {.
|
||||||
importc, header: "<pthread.h>".}
|
importc, header: "<pthread.h>".}
|
||||||
|
|
@ -146,7 +146,7 @@ else:
|
||||||
|
|
||||||
proc pthread_getspecific(a1: ThreadVarSlot): pointer {.
|
proc pthread_getspecific(a1: ThreadVarSlot): pointer {.
|
||||||
importc: "pthread_getspecific", header: "<pthread.h>".}
|
importc: "pthread_getspecific", header: "<pthread.h>".}
|
||||||
proc pthread_key_create(a1: ptr ThreadVarSlot,
|
proc pthread_key_create(a1: ptr ThreadVarSlot,
|
||||||
destruct: proc (x: pointer) {.noconv.}): int32 {.
|
destruct: proc (x: pointer) {.noconv.}): int32 {.
|
||||||
importc: "pthread_key_create", header: "<pthread.h>".}
|
importc: "pthread_key_create", header: "<pthread.h>".}
|
||||||
proc pthread_key_delete(a1: ThreadVarSlot): int32 {.
|
proc pthread_key_delete(a1: ThreadVarSlot): int32 {.
|
||||||
|
|
@ -154,7 +154,7 @@ else:
|
||||||
|
|
||||||
proc pthread_setspecific(a1: ThreadVarSlot, a2: pointer): int32 {.
|
proc pthread_setspecific(a1: ThreadVarSlot, a2: pointer): int32 {.
|
||||||
importc: "pthread_setspecific", header: "<pthread.h>".}
|
importc: "pthread_setspecific", header: "<pthread.h>".}
|
||||||
|
|
||||||
proc threadVarAlloc(): ThreadVarSlot {.inline.} =
|
proc threadVarAlloc(): ThreadVarSlot {.inline.} =
|
||||||
discard pthread_key_create(addr(result), nil)
|
discard pthread_key_create(addr(result), nil)
|
||||||
proc threadVarSetValue(s: ThreadVarSlot, value: pointer) {.inline.} =
|
proc threadVarSetValue(s: ThreadVarSlot, value: pointer) {.inline.} =
|
||||||
|
|
@ -196,7 +196,7 @@ type
|
||||||
nil
|
nil
|
||||||
{.deprecated: [TThreadLocalStorage: ThreadLocalStorage, TGcThread: GcThread].}
|
{.deprecated: [TThreadLocalStorage: ThreadLocalStorage, TGcThread: GcThread].}
|
||||||
|
|
||||||
# XXX it'd be more efficient to not use a global variable for the
|
# 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 X
|
# thread storage slot, but to rely on the implementation to assign slot X
|
||||||
# for us... ;-)
|
# for us... ;-)
|
||||||
var globalsSlot: ThreadVarSlot
|
var globalsSlot: ThreadVarSlot
|
||||||
|
|
@ -222,7 +222,7 @@ when useStackMaskHack:
|
||||||
proc maskStackPointer(offset: int): pointer {.compilerRtl, inl.} =
|
proc maskStackPointer(offset: int): pointer {.compilerRtl, inl.} =
|
||||||
var x {.volatile.}: pointer
|
var x {.volatile.}: pointer
|
||||||
x = addr(x)
|
x = addr(x)
|
||||||
result = cast[pointer]((cast[int](x) and not ThreadStackMask) +%
|
result = cast[pointer]((cast[int](x) and not ThreadStackMask) +%
|
||||||
(0) +% offset)
|
(0) +% offset)
|
||||||
|
|
||||||
# create for the main thread. Note: do not insert this data into the list
|
# create for the main thread. Note: do not insert this data into the list
|
||||||
|
|
@ -231,27 +231,27 @@ when not defined(useNimRtl):
|
||||||
when not useStackMaskHack:
|
when not useStackMaskHack:
|
||||||
#when not defined(createNimRtl): initStackBottom()
|
#when not defined(createNimRtl): initStackBottom()
|
||||||
initGC()
|
initGC()
|
||||||
|
|
||||||
when emulatedThreadVars:
|
when emulatedThreadVars:
|
||||||
if nimThreadVarsSize() > sizeof(ThreadLocalStorage):
|
if nimThreadVarsSize() > sizeof(ThreadLocalStorage):
|
||||||
echo "too large thread local storage size requested"
|
echo "too large thread local storage size requested"
|
||||||
quit 1
|
quit 1
|
||||||
|
|
||||||
when hasSharedHeap and not defined(boehmgc) and not defined(gogc) and not defined(nogc):
|
when hasSharedHeap and not defined(boehmgc) and not defined(gogc) and not defined(nogc):
|
||||||
var
|
var
|
||||||
threadList: PGcThread
|
threadList: PGcThread
|
||||||
|
|
||||||
proc registerThread(t: PGcThread) =
|
proc registerThread(t: PGcThread) =
|
||||||
# we need to use the GC global lock here!
|
# we need to use the GC global lock here!
|
||||||
acquireSys(HeapLock)
|
acquireSys(HeapLock)
|
||||||
t.prev = nil
|
t.prev = nil
|
||||||
t.next = threadList
|
t.next = threadList
|
||||||
if threadList != nil:
|
if threadList != nil:
|
||||||
sysAssert(threadList.prev == nil, "threadList.prev == nil")
|
sysAssert(threadList.prev == nil, "threadList.prev == nil")
|
||||||
threadList.prev = t
|
threadList.prev = t
|
||||||
threadList = t
|
threadList = t
|
||||||
releaseSys(HeapLock)
|
releaseSys(HeapLock)
|
||||||
|
|
||||||
proc unregisterThread(t: PGcThread) =
|
proc unregisterThread(t: PGcThread) =
|
||||||
# we need to use the GC global lock here!
|
# we need to use the GC global lock here!
|
||||||
acquireSys(HeapLock)
|
acquireSys(HeapLock)
|
||||||
|
|
@ -263,13 +263,13 @@ when not defined(useNimRtl):
|
||||||
t.next = nil
|
t.next = nil
|
||||||
t.prev = nil
|
t.prev = nil
|
||||||
releaseSys(HeapLock)
|
releaseSys(HeapLock)
|
||||||
|
|
||||||
# on UNIX, the GC uses ``SIGFREEZE`` to tell every thread to stop so that
|
# on UNIX, the GC uses ``SIGFREEZE`` to tell every thread to stop so that
|
||||||
# the GC can examine the stacks?
|
# the GC can examine the stacks?
|
||||||
proc stopTheWord() = discard
|
proc stopTheWord() = discard
|
||||||
|
|
||||||
# We jump through some hops here to ensure that Nim thread procs can have
|
# 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
|
# the Nim calling convention. This is needed because thread procs are
|
||||||
# ``stdcall`` on Windows and ``noconv`` on UNIX. Alternative would be to just
|
# ``stdcall`` on Windows and ``noconv`` on UNIX. Alternative would be to just
|
||||||
# use ``stdcall`` since it is mapped to ``noconv`` on UNIX anyway.
|
# use ``stdcall`` since it is mapped to ``noconv`` on UNIX anyway.
|
||||||
|
|
||||||
|
|
@ -311,30 +311,30 @@ template threadProcWrapperBody(closure: expr) {.immediate.} =
|
||||||
# exception is tried to be re-raised by the code-gen after the ``finally``!
|
# 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
|
# However this is doomed to fail, because we already unmapped every heap
|
||||||
# page!
|
# page!
|
||||||
|
|
||||||
# mark as not running anymore:
|
# mark as not running anymore:
|
||||||
t.dataFn = nil
|
t.dataFn = nil
|
||||||
|
|
||||||
{.push stack_trace:off.}
|
{.push stack_trace:off.}
|
||||||
when defined(windows):
|
when defined(windows):
|
||||||
proc threadProcWrapper[TArg](closure: pointer): int32 {.stdcall.} =
|
proc threadProcWrapper[TArg](closure: pointer): int32 {.stdcall.} =
|
||||||
threadProcWrapperBody(closure)
|
threadProcWrapperBody(closure)
|
||||||
# implicitly return 0
|
# implicitly return 0
|
||||||
else:
|
else:
|
||||||
proc threadProcWrapper[TArg](closure: pointer): pointer {.noconv.} =
|
proc threadProcWrapper[TArg](closure: pointer): pointer {.noconv.} =
|
||||||
threadProcWrapperBody(closure)
|
threadProcWrapperBody(closure)
|
||||||
{.pop.}
|
{.pop.}
|
||||||
|
|
||||||
proc running*[TArg](t: Thread[TArg]): bool {.inline.} =
|
proc running*[TArg](t: Thread[TArg]): bool {.inline.} =
|
||||||
## returns true if `t` is running.
|
## returns true if `t` is running.
|
||||||
result = t.dataFn != nil
|
result = t.dataFn != nil
|
||||||
|
|
||||||
when hostOS == "windows":
|
when hostOS == "windows":
|
||||||
proc joinThread*[TArg](t: Thread[TArg]) {.inline.} =
|
proc joinThread*[TArg](t: Thread[TArg]) {.inline.} =
|
||||||
## waits for the thread `t` to finish.
|
## waits for the thread `t` to finish.
|
||||||
discard waitForSingleObject(t.sys, -1'i32)
|
discard waitForSingleObject(t.sys, -1'i32)
|
||||||
|
|
||||||
proc joinThreads*[TArg](t: varargs[Thread[TArg]]) =
|
proc joinThreads*[TArg](t: varargs[Thread[TArg]]) =
|
||||||
## waits for every thread in `t` to finish.
|
## waits for every thread in `t` to finish.
|
||||||
var a: array[0..255, SysThread]
|
var a: array[0..255, SysThread]
|
||||||
sysAssert a.len >= t.len, "a.len >= t.len"
|
sysAssert a.len >= t.len, "a.len >= t.len"
|
||||||
|
|
@ -365,7 +365,7 @@ when false:
|
||||||
|
|
||||||
when hostOS == "windows":
|
when hostOS == "windows":
|
||||||
proc createThread*[TArg](t: var Thread[TArg],
|
proc createThread*[TArg](t: var Thread[TArg],
|
||||||
tp: proc (arg: TArg) {.thread.},
|
tp: proc (arg: TArg) {.thread.},
|
||||||
param: TArg) =
|
param: TArg) =
|
||||||
## creates a new thread `t` and starts its execution. Entry point is the
|
## 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
|
## proc `tp`. `param` is passed to `tp`. `TArg` can be ``void`` if you
|
||||||
|
|
@ -379,8 +379,8 @@ when hostOS == "windows":
|
||||||
if t.sys <= 0:
|
if t.sys <= 0:
|
||||||
raise newException(ResourceExhaustedError, "cannot create thread")
|
raise newException(ResourceExhaustedError, "cannot create thread")
|
||||||
else:
|
else:
|
||||||
proc createThread*[TArg](t: var Thread[TArg],
|
proc createThread*[TArg](t: var Thread[TArg],
|
||||||
tp: proc (arg: TArg) {.thread.},
|
tp: proc (arg: TArg) {.thread.},
|
||||||
param: TArg) =
|
param: TArg) =
|
||||||
## creates a new thread `t` and starts its execution. Entry point is the
|
## 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
|
## proc `tp`. `param` is passed to `tp`. `TArg` can be ``void`` if you
|
||||||
|
|
@ -413,4 +413,3 @@ when useStackMaskHack:
|
||||||
var mainThread: Thread[pointer]
|
var mainThread: Thread[pointer]
|
||||||
createThread(mainThread, tp)
|
createThread(mainThread, tp)
|
||||||
joinThread(mainThread)
|
joinThread(mainThread)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue