Merge branch 'devel' into faster-nimsuggest

This commit is contained in:
Andreas Rumpf
2017-02-17 10:48:01 +01:00
52 changed files with 1048 additions and 281 deletions

View File

@@ -128,13 +128,7 @@ iterator items(stack: ptr GcStack): ptr GcStack =
yield s
s = s.next
# There will be problems with GC in foreign threads if `threads` option is off or TLS emulation is enabled
const allowForeignThreadGc = compileOption("threads") and not compileOption("tlsEmulation")
when allowForeignThreadGc:
var
localGcInitialized {.rtlThreadVar.}: bool
when declared(threadType):
proc setupForeignThreadGc*() {.gcsafe.} =
## Call this if you registered a callback that will be run from a thread not
## under your control. This has a cheap thread-local guard, so the GC for
@@ -143,16 +137,33 @@ when allowForeignThreadGc:
##
## This function is available only when ``--threads:on`` and ``--tlsEmulation:off``
## switches are used
if not localGcInitialized:
localGcInitialized = true
if threadType == ThreadType.None:
initAllocator()
var stackTop {.volatile.}: pointer
setStackBottom(addr(stackTop))
initGC()
threadType = ThreadType.ForeignThread
proc tearDownForeignThreadGc*() {.gcsafe.} =
## Call this to tear down the GC, previously initialized by ``setupForeignThreadGc``.
## If GC has not been previously initialized, or has already been torn down, the
## call does nothing.
##
## This function is available only when ``--threads:on`` and ``--tlsEmulation:off``
## switches are used
if threadType != ThreadType.ForeignThread:
return
when declared(deallocOsPages): deallocOsPages()
threadType = ThreadType.None
when declared(gch): zeroMem(addr gch, sizeof(gch))
else:
template setupForeignThreadGc*() =
{.error: "setupForeignThreadGc is available only when ``--threads:on`` and ``--tlsEmulation:off`` are used".}
template tearDownForeignThreadGc*() =
{.error: "tearDownForeignThreadGc is available only when ``--threads:on`` and ``--tlsEmulation:off`` are used".}
# ----------------- stack management --------------------------------------
# inspired from Smart Eiffel

View File

@@ -67,7 +67,7 @@ proc checkErr(f: File) =
{.push stackTrace:off, profiler:off.}
proc readBuffer(f: File, buffer: pointer, len: Natural): int =
result = c_fread(buffer, 1, len, f)
checkErr(f)
if result != len: checkErr(f)
proc readBytes(f: File, a: var openArray[int8|uint8], start, len: Natural): int =
result = readBuffer(f, addr(a[start]), len)
@@ -118,8 +118,9 @@ const
proc close*(f: File) = discard c_fclose(f)
proc readChar(f: File): char =
let x = c_fgetc(f)
if x == -1: raiseEOF()
checkErr(f)
if x < 0:
checkErr(f)
raiseEOF()
result = char(x)
proc flushFile*(f: File) = discard c_fflush(f)
@@ -140,7 +141,7 @@ proc readLine(f: File, line: var TaintedString): bool =
# fgets doesn't append an \L
c_memset(addr line.string[pos], '\L'.ord, sp)
var fgetsSuccess = c_fgets(addr line.string[pos], sp, f) != nil
checkErr(f)
if not fgetsSuccess: checkErr(f)
let m = c_memchr(addr line.string[pos], '\L'.ord, sp)
if m != nil:
# \l found: Could be our own or the one by fgets, in any case, we're done
@@ -170,21 +171,23 @@ proc readLine(f: File): TaintedString =
proc write(f: File, i: int) =
when sizeof(int) == 8:
c_fprintf(f, "%lld", i)
if c_fprintf(f, "%lld", i) < 0: checkErr(f)
else:
c_fprintf(f, "%ld", i)
if c_fprintf(f, "%ld", i) < 0: checkErr(f)
proc write(f: File, i: BiggestInt) =
when sizeof(BiggestInt) == 8:
c_fprintf(f, "%lld", i)
if c_fprintf(f, "%lld", i) < 0: checkErr(f)
else:
c_fprintf(f, "%ld", i)
if c_fprintf(f, "%ld", i) < 0: checkErr(f)
proc write(f: File, b: bool) =
if b: write(f, "true")
else: write(f, "false")
proc write(f: File, r: float32) = c_fprintf(f, "%g", r)
proc write(f: File, r: BiggestFloat) = c_fprintf(f, "%g", r)
proc write(f: File, r: float32) =
if c_fprintf(f, "%g", r) < 0: checkErr(f)
proc write(f: File, r: BiggestFloat) =
if c_fprintf(f, "%g", r) < 0: checkErr(f)
proc write(f: File, c: char) = discard c_putc(ord(c), f)
proc write(f: File, a: varargs[string, `$`]) =
@@ -212,7 +215,10 @@ proc rawFileSize(file: File): int =
discard c_fseek(file, clong(oldPos), 0)
proc endOfFile(f: File): bool =
result = c_feof(f) != 0
var c = c_fgetc(f)
discard c_ungetc(c, f)
return c < 0'i32
#result = c_feof(f) != 0
proc readAllFile(file: File, len: int): string =
# We acquire the filesize beforehand and hope it doesn't change.

View File

@@ -285,7 +285,19 @@ when useStackMaskHack:
when not defined(useNimRtl):
when not useStackMaskHack:
#when not defined(createNimRtl): initStackBottom()
when declared(initGC): initGC()
when declared(initGC):
initGC()
when not emulatedThreadVars:
type ThreadType {.pure.} = enum
None = 0,
NimThread = 1,
ForeignThread = 2
var
threadType {.rtlThreadVar.}: ThreadType
threadType = ThreadType.NimThread
when emulatedThreadVars:
if nimThreadVarsSize() > sizeof(ThreadLocalStorage):
@@ -442,6 +454,8 @@ proc threadProcWrapStackFrame[TArg](thrd: ptr Thread[TArg]) =
# init the GC for refc/markandsweep
setStackBottom(addr(p))
initGC()
when declared(threadType):
threadType = ThreadType.NimThread
when declared(registerThread):
thrd.stackBottom = addr(thrd)
registerThread(thrd)