Use constant nimCoroutines instead of defined(nimCoroutines)

Variable
This commit is contained in:
Rokas Kupstys 2017-02-18 17:17:44 +02:00
commit f80ddbbcc6
4 changed files with 32 additions and 12 deletions

View file

@ -16,7 +16,10 @@
## -d:nimCoroutinesSetjmp Use setjmp backend. ## -d:nimCoroutinesSetjmp Use setjmp backend.
## -d:nimCoroutinesSetjmpBundled Use bundled setjmp implementation. ## -d:nimCoroutinesSetjmpBundled Use bundled setjmp implementation.
when not defined(nimCoroutines) and not defined(nimdoc): when not nimCoroutines and not defined(nimdoc):
when defined(noNimCoroutines):
{.error: "Coroutines can not be used with -d:noNimCoroutines"}
else:
{.error: "Coroutines require -d:nimCoroutines".} {.error: "Coroutines require -d:nimCoroutines".}
import os import os

View file

@ -2462,6 +2462,23 @@ template accumulateResult*(iter: untyped) =
# we have to compute this here before turning it off in except.nim anyway ... # we have to compute this here before turning it off in except.nim anyway ...
const NimStackTrace = compileOption("stacktrace") const NimStackTrace = compileOption("stacktrace")
template coroutinesSupportedPlatform(): bool =
when defined(sparc) or defined(ELATE) or compileOption("gc", "v2") or
defined(boehmgc) or defined(gogc) or defined(nogc) or defined(gcStack) or
defined(gcMarkAndSweep):
false
else:
true
when defined(nimCoroutines):
when not coroutinesSupportedPlatform():
{.error: "Coroutines are not supported on this architecture and/or garbage collector.".}
const nimCoroutines* = true
elif defined(noNimCoroutines):
const nimCoroutines* = false
else:
const nimCoroutines* = false
{.push checks: off.} {.push checks: off.}
# obviously we cannot generate checking operations here :-) # obviously we cannot generate checking operations here :-)
# because it would yield into an endless recursion # because it would yield into an endless recursion

View file

@ -64,14 +64,14 @@ type
maxPause: int64 # max measured GC pause in nanoseconds maxPause: int64 # max measured GC pause in nanoseconds
GcStack {.final, pure.} = object GcStack {.final, pure.} = object
when defined(nimCoroutines): when nimCoroutines:
prev: ptr GcStack prev: ptr GcStack
next: ptr GcStack next: ptr GcStack
maxStackSize: int # Used to track statistics because we can not use maxStackSize: int # Used to track statistics because we can not use
# GcStat.maxStackSize when multiple stacks exist. # GcStat.maxStackSize when multiple stacks exist.
bottom: pointer bottom: pointer
when withRealTime or defined(nimCoroutines): when withRealTime or nimCoroutines:
pos: pointer # Used with `withRealTime` only for code clarity, see GC_Step(). pos: pointer # Used with `withRealTime` only for code clarity, see GC_Step().
when withRealTime: when withRealTime:
bottomSaved: pointer bottomSaved: pointer
@ -79,7 +79,7 @@ type
GcHeap {.final, pure.} = object # this contains the zero count and GcHeap {.final, pure.} = object # this contains the zero count and
# non-zero count table # non-zero count table
stack: GcStack stack: GcStack
when defined(nimCoroutines): when nimCoroutines:
activeStack: ptr GcStack # current executing coroutine stack. activeStack: ptr GcStack # current executing coroutine stack.
cycleThreshold: int cycleThreshold: int
when useCellIds: when useCellIds:
@ -827,7 +827,7 @@ proc collectCTBody(gch: var GcHeap) =
let t0 = getticks() let t0 = getticks()
sysAssert(allocInv(gch.region), "collectCT: begin") sysAssert(allocInv(gch.region), "collectCT: begin")
when defined(nimCoroutines): when nimCoroutines:
for stack in gch.stack.items(): for stack in gch.stack.items():
gch.stat.maxStackSize = max(gch.stat.maxStackSize, stack.stackSize()) gch.stat.maxStackSize = max(gch.stat.maxStackSize, stack.stackSize())
else: else:
@ -948,7 +948,7 @@ when not defined(useNimRtl):
"[GC] zct capacity: " & $gch.zct.cap & "\n" & "[GC] zct capacity: " & $gch.zct.cap & "\n" &
"[GC] max cycle table size: " & $gch.stat.cycleTableSize & "\n" & "[GC] max cycle table size: " & $gch.stat.cycleTableSize & "\n" &
"[GC] max pause time [ms]: " & $(gch.stat.maxPause div 1000_000) & "\n" "[GC] max pause time [ms]: " & $(gch.stat.maxPause div 1000_000) & "\n"
when defined(nimCoroutines): when nimCoroutines:
result = result & "[GC] number of stacks: " & $gch.stack.len & "\n" result = result & "[GC] number of stacks: " & $gch.stack.len & "\n"
for stack in items(gch.stack): for stack in items(gch.stack):
result = result & "[GC] stack " & stack.bottom.repr & "[GC] max stack size " & cast[pointer](stack.maxStackSize).repr & "\n" result = result & "[GC] stack " & stack.bottom.repr & "[GC] max stack size " & cast[pointer](stack.maxStackSize).repr & "\n"

View file

@ -109,7 +109,7 @@ else:
proc len(stack: var GcStack): int = 1 proc len(stack: var GcStack): int = 1
proc stackSize(stack: ptr GcStack): int {.noinline.} = proc stackSize(stack: ptr GcStack): int {.noinline.} =
when defined(nimCoroutines): when nimCoroutines:
var pos = stack.pos var pos = stack.pos
else: else:
var pos {.volatile.}: pointer var pos {.volatile.}: pointer
@ -127,7 +127,7 @@ proc stackSize(): int {.noinline.} =
for stack in gch.stack.items(): for stack in gch.stack.items():
result = result + stack.stackSize() result = result + stack.stackSize()
when defined(nimCoroutines): when nimCoroutines:
proc setPosition(stack: ptr GcStack, position: pointer) = proc setPosition(stack: ptr GcStack, position: pointer) =
stack.pos = position stack.pos = position
stack.maxStackSize = max(stack.maxStackSize, stack.stackSize()) stack.maxStackSize = max(stack.maxStackSize, stack.stackSize())
@ -198,7 +198,7 @@ else:
const stackIncreases = false const stackIncreases = false
{.push stack_trace: off.} {.push stack_trace: off.}
when defined(nimCoroutines): when nimCoroutines:
proc GC_addStack(bottom: pointer) {.cdecl, exportc.} = proc GC_addStack(bottom: pointer) {.cdecl, exportc.} =
# c_fprintf(stdout, "GC_addStack: %p;\n", bottom) # c_fprintf(stdout, "GC_addStack: %p;\n", bottom)
var stack = gch.stack.append() var stack = gch.stack.append()
@ -219,7 +219,7 @@ when defined(nimCoroutines):
when not defined(useNimRtl): when not defined(useNimRtl):
proc setStackBottom(theStackBottom: pointer) = proc setStackBottom(theStackBottom: pointer) =
# Initializes main stack of the thread. # Initializes main stack of the thread.
when defined(nimCoroutines): when nimCoroutines:
if gch.stack.next == nil: if gch.stack.next == nil:
# Main stack was not initialized yet # Main stack was not initialized yet
gch.stack.next = addr(gch.stack) gch.stack.next = addr(gch.stack)
@ -257,7 +257,7 @@ proc isOnStack(p: pointer): bool =
result = a <=% x and x <=% b result = a <=% x and x <=% b
when defined(sparc): # For SPARC architecture. when defined(sparc): # For SPARC architecture.
when defined(nimCoroutines): when nimCoroutines:
{.error: "Nim coroutines are not supported on this platform."} {.error: "Nim coroutines are not supported on this platform."}
template forEachStackSlot(gch, gcMark: untyped) {.dirty.} = template forEachStackSlot(gch, gcMark: untyped) {.dirty.} =