bugfixes for semantic checking; thread implementation pushed the compiler

This commit is contained in:
Araq 2011-06-04 02:56:55 +02:00
commit 5008b44467
8 changed files with 153 additions and 125 deletions

View file

@ -20,9 +20,9 @@ type
withinBind, withinTypeDesc withinBind, withinTypeDesc
TSemGenericFlags = set[TSemGenericFlag] TSemGenericFlags = set[TSemGenericFlag]
proc semGenericStmt(c: PContext, n: PNode, flags: TSemGenericFlags = {}): PNode proc semGenericStmt(c: PContext, n: PNode, flags: TSemGenericFlags): PNode
proc semGenericStmtScope(c: PContext, n: PNode, proc semGenericStmtScope(c: PContext, n: PNode,
flags: TSemGenericFlags = {}): PNode = flags: TSemGenericFlags): PNode =
openScope(c.tab) openScope(c.tab)
result = semGenericStmt(c, n, flags) result = semGenericStmt(c, n, flags)
closeScope(c.tab) closeScope(c.tab)
@ -60,7 +60,7 @@ proc getIdentNode(n: PNode): PNode =
result = n result = n
proc semGenericStmt(c: PContext, n: PNode, proc semGenericStmt(c: PContext, n: PNode,
flags: TSemGenericFlags = {}): PNode = flags: TSemGenericFlags): PNode =
result = n result = n
if gCmd == cmdIdeTools: suggestStmt(c, n) if gCmd == cmdIdeTools: suggestStmt(c, n)
case n.kind case n.kind
@ -76,76 +76,88 @@ proc semGenericStmt(c: PContext, n: PNode,
of nkDotExpr: of nkDotExpr:
var s = QualifiedLookUp(c, n, {}) var s = QualifiedLookUp(c, n, {})
if s != nil: result = semGenericStmtSymbol(c, n, s) if s != nil: result = semGenericStmtSymbol(c, n, s)
# XXX for example: ``result.add`` -- ``add`` needs to be looked up here...
of nkEmpty, nkSym..nkNilLit: of nkEmpty, nkSym..nkNilLit:
nil nil
of nkBind: of nkBind:
result = semGenericStmt(c, n.sons[0], {withinBind}) result = semGenericStmt(c, n.sons[0], flags+{withinBind})
of nkCall, nkHiddenCallConv, nkInfix, nkPrefix, nkCommand, nkCallStrLit: of nkCall, nkHiddenCallConv, nkInfix, nkPrefix, nkCommand, nkCallStrLit:
# check if it is an expression macro: # check if it is an expression macro:
checkMinSonsLen(n, 1) checkMinSonsLen(n, 1)
var s = qualifiedLookup(c, n.sons[0], {}) var s = qualifiedLookup(c, n.sons[0], {})
var first = 0
if s != nil: if s != nil:
incl(s.flags, sfUsed) incl(s.flags, sfUsed)
case s.kind case s.kind
of skMacro: of skMacro:
return semMacroExpr(c, n, s, false) result = semMacroExpr(c, n, s, false)
of skTemplate: of skTemplate:
return semTemplateExpr(c, n, s, false) result = semTemplateExpr(c, n, s, false)
# BUGFIX: we must not return here, we need to do first phase of
# symbol lookup ...
of skUnknown, skParam: of skUnknown, skParam:
# Leave it as an identifier. # Leave it as an identifier.
of skProc, skMethod, skIterator, skConverter: of skProc, skMethod, skIterator, skConverter:
n.sons[0] = symChoice(c, n.sons[0], s) result.sons[0] = symChoice(c, n.sons[0], s)
first = 1
of skGenericParam: of skGenericParam:
n.sons[0] = newSymNode(s, n.sons[0].info) result.sons[0] = newSymNode(s, n.sons[0].info)
first = 1
of skType: of skType:
# bad hack for generics: # bad hack for generics:
if (s.typ != nil) and (s.typ.kind != tyGenericParam): if (s.typ != nil) and (s.typ.kind != tyGenericParam):
n.sons[0] = newSymNode(s, n.sons[0].info) result.sons[0] = newSymNode(s, n.sons[0].info)
else: n.sons[0] = newSymNode(s, n.sons[0].info) first = 1
for i in countup(1, sonsLen(n) - 1): else:
n.sons[i] = semGenericStmt(c, n.sons[i], flags) result.sons[0] = newSymNode(s, n.sons[0].info)
first = 1
for i in countup(first, sonsLen(result) - 1):
result.sons[i] = semGenericStmt(c, result.sons[i], flags)
of nkMacroStmt: of nkMacroStmt:
result = semMacroStmt(c, n, false) result = semMacroStmt(c, n, false)
for i in countup(0, sonsLen(result)-1):
result.sons[i] = semGenericStmt(c, result.sons[i], flags)
of nkIfStmt: of nkIfStmt:
for i in countup(0, sonsLen(n)-1): for i in countup(0, sonsLen(n)-1):
n.sons[i] = semGenericStmtScope(c, n.sons[i]) n.sons[i] = semGenericStmtScope(c, n.sons[i], flags)
of nkWhileStmt: of nkWhileStmt:
openScope(c.tab) openScope(c.tab)
for i in countup(0, sonsLen(n)-1): n.sons[i] = semGenericStmt(c, n.sons[i]) for i in countup(0, sonsLen(n)-1):
n.sons[i] = semGenericStmt(c, n.sons[i], flags)
closeScope(c.tab) closeScope(c.tab)
of nkCaseStmt: of nkCaseStmt:
openScope(c.tab) openScope(c.tab)
n.sons[0] = semGenericStmt(c, n.sons[0]) n.sons[0] = semGenericStmt(c, n.sons[0], flags)
for i in countup(1, sonsLen(n)-1): for i in countup(1, sonsLen(n)-1):
var a = n.sons[i] var a = n.sons[i]
checkMinSonsLen(a, 1) checkMinSonsLen(a, 1)
var L = sonsLen(a) var L = sonsLen(a)
for j in countup(0, L - 2): a.sons[j] = semGenericStmt(c, a.sons[j]) for j in countup(0, L-2): a.sons[j] = semGenericStmt(c, a.sons[j], flags)
a.sons[L - 1] = semGenericStmtScope(c, a.sons[L - 1]) a.sons[L - 1] = semGenericStmtScope(c, a.sons[L-1], flags)
closeScope(c.tab) closeScope(c.tab)
of nkForStmt: of nkForStmt:
var L = sonsLen(n) var L = sonsLen(n)
openScope(c.tab) openScope(c.tab)
n.sons[L - 2] = semGenericStmt(c, n.sons[L - 2]) n.sons[L - 2] = semGenericStmt(c, n.sons[L-2], flags)
for i in countup(0, L - 3): addDecl(c, newSymS(skUnknown, n.sons[i], c)) for i in countup(0, L - 3): addDecl(c, newSymS(skUnknown, n.sons[i], c))
n.sons[L - 1] = semGenericStmt(c, n.sons[L - 1]) n.sons[L - 1] = semGenericStmt(c, n.sons[L-1], flags)
closeScope(c.tab) closeScope(c.tab)
of nkBlockStmt, nkBlockExpr, nkBlockType: of nkBlockStmt, nkBlockExpr, nkBlockType:
checkSonsLen(n, 2) checkSonsLen(n, 2)
openScope(c.tab) openScope(c.tab)
if n.sons[0].kind != nkEmpty: addDecl(c, newSymS(skUnknown, n.sons[0], c)) if n.sons[0].kind != nkEmpty: addDecl(c, newSymS(skUnknown, n.sons[0], c))
n.sons[1] = semGenericStmt(c, n.sons[1]) n.sons[1] = semGenericStmt(c, n.sons[1], flags)
closeScope(c.tab) closeScope(c.tab)
of nkTryStmt: of nkTryStmt:
checkMinSonsLen(n, 2) checkMinSonsLen(n, 2)
n.sons[0] = semGenericStmtScope(c, n.sons[0]) n.sons[0] = semGenericStmtScope(c, n.sons[0], flags)
for i in countup(1, sonsLen(n) - 1): for i in countup(1, sonsLen(n)-1):
var a = n.sons[i] var a = n.sons[i]
checkMinSonsLen(a, 1) checkMinSonsLen(a, 1)
var L = sonsLen(a) var L = sonsLen(a)
for j in countup(0, L - 2): for j in countup(0, L-2):
a.sons[j] = semGenericStmt(c, a.sons[j], {withinTypeDesc}) a.sons[j] = semGenericStmt(c, a.sons[j], flags+{withinTypeDesc})
a.sons[L - 1] = semGenericStmtScope(c, a.sons[L - 1]) a.sons[L-1] = semGenericStmtScope(c, a.sons[L-1], flags)
of nkVarSection: of nkVarSection:
for i in countup(0, sonsLen(n) - 1): for i in countup(0, sonsLen(n) - 1):
var a = n.sons[i] var a = n.sons[i]
@ -153,8 +165,8 @@ proc semGenericStmt(c: PContext, n: PNode,
if (a.kind != nkIdentDefs) and (a.kind != nkVarTuple): IllFormedAst(a) if (a.kind != nkIdentDefs) and (a.kind != nkVarTuple): IllFormedAst(a)
checkMinSonsLen(a, 3) checkMinSonsLen(a, 3)
var L = sonsLen(a) var L = sonsLen(a)
a.sons[L-2] = semGenericStmt(c, a.sons[L-2], {withinTypeDesc}) a.sons[L-2] = semGenericStmt(c, a.sons[L-2], flags+{withinTypeDesc})
a.sons[L-1] = semGenericStmt(c, a.sons[L-1]) a.sons[L-1] = semGenericStmt(c, a.sons[L-1], flags)
for j in countup(0, L-3): for j in countup(0, L-3):
addDecl(c, newSymS(skUnknown, getIdentNode(a.sons[j]), c)) addDecl(c, newSymS(skUnknown, getIdentNode(a.sons[j]), c))
of nkGenericParams: of nkGenericParams:
@ -163,7 +175,7 @@ proc semGenericStmt(c: PContext, n: PNode,
if (a.kind != nkIdentDefs): IllFormedAst(a) if (a.kind != nkIdentDefs): IllFormedAst(a)
checkMinSonsLen(a, 3) checkMinSonsLen(a, 3)
var L = sonsLen(a) var L = sonsLen(a)
a.sons[L-2] = semGenericStmt(c, a.sons[L-2], {withinTypeDesc}) a.sons[L-2] = semGenericStmt(c, a.sons[L-2], flags+{withinTypeDesc})
# do not perform symbol lookup for default expressions # do not perform symbol lookup for default expressions
for j in countup(0, L-3): for j in countup(0, L-3):
addDecl(c, newSymS(skUnknown, getIdentNode(a.sons[j]), c)) addDecl(c, newSymS(skUnknown, getIdentNode(a.sons[j]), c))
@ -174,8 +186,8 @@ proc semGenericStmt(c: PContext, n: PNode,
if (a.kind != nkConstDef): IllFormedAst(a) if (a.kind != nkConstDef): IllFormedAst(a)
checkSonsLen(a, 3) checkSonsLen(a, 3)
addDecl(c, newSymS(skUnknown, getIdentNode(a.sons[0]), c)) addDecl(c, newSymS(skUnknown, getIdentNode(a.sons[0]), c))
a.sons[1] = semGenericStmt(c, a.sons[1], {withinTypeDesc}) a.sons[1] = semGenericStmt(c, a.sons[1], flags+{withinTypeDesc})
a.sons[2] = semGenericStmt(c, a.sons[2]) a.sons[2] = semGenericStmt(c, a.sons[2], flags)
of nkTypeSection: of nkTypeSection:
for i in countup(0, sonsLen(n) - 1): for i in countup(0, sonsLen(n) - 1):
var a = n.sons[i] var a = n.sons[i]
@ -190,15 +202,15 @@ proc semGenericStmt(c: PContext, n: PNode,
checkSonsLen(a, 3) checkSonsLen(a, 3)
if a.sons[1].kind != nkEmpty: if a.sons[1].kind != nkEmpty:
openScope(c.tab) openScope(c.tab)
a.sons[1] = semGenericStmt(c, a.sons[1]) a.sons[1] = semGenericStmt(c, a.sons[1], flags)
a.sons[2] = semGenericStmt(c, a.sons[2], {withinTypeDesc}) a.sons[2] = semGenericStmt(c, a.sons[2], flags+{withinTypeDesc})
closeScope(c.tab) closeScope(c.tab)
else: else:
a.sons[2] = semGenericStmt(c, a.sons[2], {withinTypeDesc}) a.sons[2] = semGenericStmt(c, a.sons[2], flags+{withinTypeDesc})
of nkEnumTy: of nkEnumTy:
checkMinSonsLen(n, 1) checkMinSonsLen(n, 1)
if n.sons[0].kind != nkEmpty: if n.sons[0].kind != nkEmpty:
n.sons[0] = semGenericStmt(c, n.sons[0], {withinTypeDesc}) n.sons[0] = semGenericStmt(c, n.sons[0], flags+{withinTypeDesc})
for i in countup(1, sonsLen(n) - 1): for i in countup(1, sonsLen(n) - 1):
var a: PNode var a: PNode
case n.sons[i].kind case n.sons[i].kind
@ -211,14 +223,14 @@ proc semGenericStmt(c: PContext, n: PNode,
of nkFormalParams: of nkFormalParams:
checkMinSonsLen(n, 1) checkMinSonsLen(n, 1)
if n.sons[0].kind != nkEmpty: if n.sons[0].kind != nkEmpty:
n.sons[0] = semGenericStmt(c, n.sons[0], {withinTypeDesc}) n.sons[0] = semGenericStmt(c, n.sons[0], flags+{withinTypeDesc})
for i in countup(1, sonsLen(n) - 1): for i in countup(1, sonsLen(n) - 1):
var a = n.sons[i] var a = n.sons[i]
if (a.kind != nkIdentDefs): IllFormedAst(a) if (a.kind != nkIdentDefs): IllFormedAst(a)
checkMinSonsLen(a, 3) checkMinSonsLen(a, 3)
var L = sonsLen(a) var L = sonsLen(a)
a.sons[L-2] = semGenericStmt(c, a.sons[L-2], {withinTypeDesc}) a.sons[L-2] = semGenericStmt(c, a.sons[L-2], flags+{withinTypeDesc})
a.sons[L-1] = semGenericStmt(c, a.sons[L-1]) a.sons[L-1] = semGenericStmt(c, a.sons[L-1], flags)
for j in countup(0, L-3): for j in countup(0, L-3):
addDecl(c, newSymS(skUnknown, getIdentNode(a.sons[j]), c)) addDecl(c, newSymS(skUnknown, getIdentNode(a.sons[j]), c))
of nkProcDef, nkMethodDef, nkConverterDef, nkMacroDef, nkTemplateDef, of nkProcDef, nkMethodDef, nkConverterDef, nkMacroDef, nkTemplateDef,
@ -226,13 +238,13 @@ proc semGenericStmt(c: PContext, n: PNode,
checkSonsLen(n, codePos + 1) checkSonsLen(n, codePos + 1)
addDecl(c, newSymS(skUnknown, getIdentNode(n.sons[0]), c)) addDecl(c, newSymS(skUnknown, getIdentNode(n.sons[0]), c))
openScope(c.tab) openScope(c.tab)
n.sons[genericParamsPos] = semGenericStmt(c, n.sons[genericParamsPos]) n.sons[genericParamsPos] = semGenericStmt(c, n.sons[genericParamsPos], flags)
if n.sons[paramsPos].kind != nkEmpty: if n.sons[paramsPos].kind != nkEmpty:
if n.sons[paramsPos].sons[0].kind != nkEmpty: if n.sons[paramsPos].sons[0].kind != nkEmpty:
addDecl(c, newSym(skUnknown, getIdent("result"), nil)) addDecl(c, newSym(skUnknown, getIdent("result"), nil))
n.sons[paramsPos] = semGenericStmt(c, n.sons[paramsPos]) n.sons[paramsPos] = semGenericStmt(c, n.sons[paramsPos], flags)
n.sons[pragmasPos] = semGenericStmt(c, n.sons[pragmasPos]) n.sons[pragmasPos] = semGenericStmt(c, n.sons[pragmasPos], flags)
n.sons[codePos] = semGenericStmtScope(c, n.sons[codePos]) n.sons[codePos] = semGenericStmtScope(c, n.sons[codePos], flags)
closeScope(c.tab) closeScope(c.tab)
else: else:
for i in countup(0, sonsLen(n) - 1): for i in countup(0, sonsLen(n) - 1):

View file

@ -748,7 +748,7 @@ proc semProcAux(c: PContext, n: PNode, kind: TSymKind,
else: else:
if s.typ.sons[0] != nil and kind != skIterator: if s.typ.sons[0] != nil and kind != skIterator:
addDecl(c, newSym(skUnknown, getIdent("result"), nil)) addDecl(c, newSym(skUnknown, getIdent("result"), nil))
n.sons[codePos] = semGenericStmtScope(c, n.sons[codePos]) n.sons[codePos] = semGenericStmtScope(c, n.sons[codePos], {})
if sfImportc in s.flags: if sfImportc in s.flags:
# so we just ignore the body after semantic checking for importc: # so we just ignore the body after semantic checking for importc:
n.sons[codePos] = ast.emptyNode n.sons[codePos] = ast.emptyNode

View file

@ -462,6 +462,12 @@ proc repeatChar*(count: int, c: Char = ' '): string {.noSideEffect,
result = newString(count) result = newString(count)
for i in 0..count-1: result[i] = c for i in 0..count-1: result[i] = c
proc repeatStr*(count: int, s: string): string {.noSideEffect,
rtl, extern: "nsuRepeatStr".} =
## Returns `s` concatenated `count` times.
result = newStringOfCap(count*s.len)
for i in 0..count-1: result.add(s)
proc align*(s: string, count: int): string {. proc align*(s: string, count: int): string {.
noSideEffect, rtl, extern: "nsuAlignString".} = noSideEffect, rtl, extern: "nsuAlignString".} =
## Aligns a string `s` with spaces, so that is of length `count`. Spaces are ## Aligns a string `s` with spaces, so that is of length `count`. Spaces are

View file

@ -532,7 +532,7 @@ when not defined(useNimRtl):
assert(not isAllocatedPtr(allocator, x)) assert(not isAllocatedPtr(allocator, x))
proc alloc(size: int): pointer = proc alloc(size: int): pointer =
when hasThreadSupport and hasSharedHeap: AquireSys(HeapLock) when hasThreadSupport and hasSharedHeap: AcquireSys(HeapLock)
result = unlockedAlloc(size) result = unlockedAlloc(size)
when hasThreadSupport and hasSharedHeap: ReleaseSys(HeapLock) when hasThreadSupport and hasSharedHeap: ReleaseSys(HeapLock)
@ -541,7 +541,7 @@ when not defined(useNimRtl):
zeroMem(result, size) zeroMem(result, size)
proc dealloc(p: pointer) = proc dealloc(p: pointer) =
when hasThreadSupport and hasSharedHeap: AquireSys(HeapLock) when hasThreadSupport and hasSharedHeap: AcquireSys(HeapLock)
unlockedDealloc(p) unlockedDealloc(p)
when hasThreadSupport and hasSharedHeap: ReleaseSys(HeapLock) when hasThreadSupport and hasSharedHeap: ReleaseSys(HeapLock)

View file

@ -57,26 +57,20 @@ type
decStack: TCellSeq # cells in the stack that are to decref again decStack: TCellSeq # cells in the stack that are to decref again
cycleRoots: TCellSet cycleRoots: TCellSet
tempStack: TCellSeq # temporary stack for recursion elimination tempStack: TCellSeq # temporary stack for recursion elimination
recGcLock: int # prevent recursion via finalizers; no thread lock
stat: TGcStat stat: TGcStat
var var
stackBottom {.rtlThreadVar.}: pointer stackBottom {.rtlThreadVar.}: pointer
gch {.rtlThreadVar.}: TGcHeap gch {.rtlThreadVar.}: TGcHeap
cycleThreshold {.rtlThreadVar.}: int = InitialCycleThreshold cycleThreshold {.rtlThreadVar.}: int = InitialCycleThreshold
recGcLock {.rtlThreadVar.}: int = 0
# we use a lock to prevent the garbage collector to be triggered in a
# finalizer; the collector should not call itself this way! Thus every
# object allocated by a finalizer will not trigger a garbage collection.
# This is wasteful but safe and won't ever be a problem for sane
# finalizers. This is a lock against recursive garbage collection, not a
# lock for threads!
proc aquire(gch: var TGcHeap) {.inline.} = proc acquire(gch: var TGcHeap) {.inline.} =
when hasThreadSupport: when hasThreadSupport and hasSharedHeap:
AquireSys(HeapLock) AcquireSys(HeapLock)
proc release(gch: var TGcHeap) {.inline.} = proc release(gch: var TGcHeap) {.inline.} =
when hasThreadSupport: when hasThreadSupport and hasSharedHeap:
releaseSys(HeapLock) releaseSys(HeapLock)
proc addZCT(s: var TCellSeq, c: PCell) {.noinline.} = proc addZCT(s: var TCellSeq, c: PCell) {.noinline.} =
@ -198,14 +192,14 @@ proc prepareDealloc(cell: PCell) =
# collection. Since we are already collecting we # collection. Since we are already collecting we
# prevend recursive entering here by a lock. # prevend recursive entering here by a lock.
# XXX: we should set the cell's children to nil! # XXX: we should set the cell's children to nil!
inc(recGcLock) inc(gch.recGcLock)
(cast[TFinalizer](cell.typ.finalizer))(cellToUsr(cell)) (cast[TFinalizer](cell.typ.finalizer))(cellToUsr(cell))
dec(recGcLock) dec(gch.recGcLock)
proc rtlAddCycleRoot(c: PCell) {.rtl, inl.} = proc rtlAddCycleRoot(c: PCell) {.rtl, inl.} =
# we MUST access gch as a global here, because this crosses DLL boundaries! # we MUST access gch as a global here, because this crosses DLL boundaries!
when hasThreadSupport: when hasThreadSupport:
AquireSys(HeapLock) AcquireSys(HeapLock)
incl(gch.cycleRoots, c) incl(gch.cycleRoots, c)
when hasThreadSupport: when hasThreadSupport:
ReleaseSys(HeapLock) ReleaseSys(HeapLock)
@ -213,7 +207,7 @@ proc rtlAddCycleRoot(c: PCell) {.rtl, inl.} =
proc rtlAddZCT(c: PCell) {.rtl, inl.} = proc rtlAddZCT(c: PCell) {.rtl, inl.} =
# we MUST access gch as a global here, because this crosses DLL boundaries! # we MUST access gch as a global here, because this crosses DLL boundaries!
when hasThreadSupport: when hasThreadSupport:
AquireSys(HeapLock) AcquireSys(HeapLock)
addZCT(gch.zct, c) addZCT(gch.zct, c)
when hasThreadSupport: when hasThreadSupport:
ReleaseSys(HeapLock) ReleaseSys(HeapLock)
@ -329,7 +323,7 @@ proc forAllChildren(cell: PCell, op: TWalkOp) =
proc checkCollection {.inline.} = proc checkCollection {.inline.} =
# checks if a collection should be done # checks if a collection should be done
if recGcLock == 0: if gch.recGcLock == 0:
collectCT(gch) collectCT(gch)
proc addNewObjToZCT(res: PCell) {.inline.} = proc addNewObjToZCT(res: PCell) {.inline.} =
@ -378,7 +372,7 @@ proc addNewObjToZCT(res: PCell) {.inline.} =
proc newObj(typ: PNimType, size: int): pointer {.compilerRtl.} = proc newObj(typ: PNimType, size: int): pointer {.compilerRtl.} =
# generates a new object and sets its reference counter to 0 # generates a new object and sets its reference counter to 0
aquire(gch) acquire(gch)
assert(typ.kind in {tyRef, tyString, tySequence}) assert(typ.kind in {tyRef, tyString, tySequence})
checkCollection() checkCollection()
var res = cast[PCell](rawAlloc(allocator, size + sizeof(TCell))) var res = cast[PCell](rawAlloc(allocator, size + sizeof(TCell)))
@ -406,7 +400,7 @@ proc newSeq(typ: PNimType, len: int): pointer {.compilerRtl.} =
cast[PGenericSeq](result).space = len cast[PGenericSeq](result).space = len
proc growObj(old: pointer, newsize: int): pointer {.rtl.} = proc growObj(old: pointer, newsize: int): pointer {.rtl.} =
aquire(gch) acquire(gch)
checkCollection() checkCollection()
var ol = usrToCell(old) var ol = usrToCell(old)
assert(ol.typ != nil) assert(ol.typ != nil)
@ -521,13 +515,15 @@ proc gcMark(p: pointer) {.inline.} =
add(gch.decStack, cell) add(gch.decStack, cell)
proc markThreadStacks(gch: var TGcHeap) = proc markThreadStacks(gch: var TGcHeap) =
when hasThreadSupport: when hasThreadSupport and hasSharedHeap:
{.error: "not fully implemented".}
var it = threadList var it = threadList
while it != nil: while it != nil:
# mark registers: # mark registers:
for i in 0 .. high(it.registers): gcMark(it.registers[i]) for i in 0 .. high(it.registers): gcMark(it.registers[i])
var sp = cast[TAddress](it.stackBottom) var sp = cast[TAddress](it.stackBottom)
var max = cast[TAddress](it.stackTop) var max = cast[TAddress](it.stackTop)
# XXX stack direction?
# XXX unroll this loop: # XXX unroll this loop:
while sp <=% max: while sp <=% max:
gcMark(cast[ppointer](sp)[]) gcMark(cast[ppointer](sp)[])
@ -696,7 +692,7 @@ proc unmarkStackAndRegisters(gch: var TGcHeap) =
var d = gch.decStack.d var d = gch.decStack.d
for i in 0..gch.decStack.len-1: for i in 0..gch.decStack.len-1:
assert isAllocatedPtr(allocator, d[i]) assert isAllocatedPtr(allocator, d[i])
# decRef(d[i]) inlined: cannot create a cycle and must not aquire lock # decRef(d[i]) inlined: cannot create a cycle and must not acquire lock
var c = d[i] var c = d[i]
# XXX no need for an atomic dec here: # XXX no need for an atomic dec here:
if --c.refcount: if --c.refcount:
@ -725,9 +721,17 @@ proc collectCT(gch: var TGcHeap) =
unmarkStackAndRegisters(gch) unmarkStackAndRegisters(gch)
when not defined(useNimRtl): when not defined(useNimRtl):
proc GC_disable() = discard atomicInc(recGcLock, 1) proc GC_disable() =
when hasThreadSupport and hasSharedHeap:
discard atomicInc(gch.recGcLock, 1)
else:
inc(gch.recGcLock)
proc GC_enable() = proc GC_enable() =
if recGcLock > 0: discard atomicDec(recGcLock, 1) if gch.recGcLock > 0:
when hasThreadSupport and hasSharedHeap:
discard atomicDec(gch.recGcLock, 1)
else:
dec(gch.recGcLock)
proc GC_setStrategy(strategy: TGC_Strategy) = proc GC_setStrategy(strategy: TGC_Strategy) =
case strategy case strategy
@ -744,7 +748,7 @@ when not defined(useNimRtl):
# set to the max value to suppress the cycle detector # set to the max value to suppress the cycle detector
proc GC_fullCollect() = proc GC_fullCollect() =
aquire(gch) acquire(gch)
var oldThreshold = cycleThreshold var oldThreshold = cycleThreshold
cycleThreshold = 0 # forces cycle collection cycleThreshold = 0 # forces cycle collection
collectCT(gch) collectCT(gch)

View file

@ -118,14 +118,16 @@ when not defined(useNimRtl):
proc initReprClosure(cl: var TReprClosure) = proc initReprClosure(cl: var TReprClosure) =
# Important: cellsets does not lock the heap when doing allocations! We # Important: cellsets does not lock the heap when doing allocations! We
# have to do it here ... # have to do it here ...
when hasThreadSupport and defined(heapLock): AquireSys(HeapLock) when hasThreadSupport and hasSharedHeap and defined(heapLock):
AcquireSys(HeapLock)
Init(cl.marked) Init(cl.marked)
cl.recdepth = -1 # default is to display everything! cl.recdepth = -1 # default is to display everything!
cl.indent = 0 cl.indent = 0
proc deinitReprClosure(cl: var TReprClosure) = proc deinitReprClosure(cl: var TReprClosure) =
Deinit(cl.marked) Deinit(cl.marked)
when hasThreadSupport and defined(heapLock): ReleaseSys(HeapLock) when hasThreadSupport and hasSharedHeap and defined(heapLock):
ReleaseSys(HeapLock)
proc reprBreak(result: var string, cl: TReprClosure) = proc reprBreak(result: var string, cl: TReprClosure) =
add result, "\n" add result, "\n"

View file

@ -27,7 +27,7 @@
## ##
## proc threadFunc(interval: tuple[a,b: int]) {.procvar.} = ## proc threadFunc(interval: tuple[a,b: int]) {.procvar.} =
## for i in interval.a..interval.b: ## for i in interval.a..interval.b:
## Aquire(L) # lock stdout ## Acquire(L) # lock stdout
## echo i ## echo i
## Release(L) ## Release(L)
## ##
@ -56,16 +56,16 @@ when defined(Windows):
dynlib: "kernel32", importc: "InitializeCriticalSection".} dynlib: "kernel32", importc: "InitializeCriticalSection".}
## Initializes the lock `L`. ## Initializes the lock `L`.
proc TryAquireSysAux(L: var TSysLock): int32 {.stdcall, proc TryAcquireSysAux(L: var TSysLock): int32 {.stdcall,
dynlib: "kernel32", importc: "TryEnterCriticalSection".} dynlib: "kernel32", importc: "TryEnterCriticalSection".}
## Tries to aquire the lock `L`. ## Tries to acquire the lock `L`.
proc TryAquireSys(L: var TSysLock): bool {.inline.} = proc TryAcquireSys(L: var TSysLock): bool {.inline.} =
result = TryAquireSysAux(L) != 0'i32 result = TryAcquireSysAux(L) != 0'i32
proc AquireSys(L: var TSysLock) {.stdcall, proc AcquireSys(L: var TSysLock) {.stdcall,
dynlib: "kernel32", importc: "EnterCriticalSection".} dynlib: "kernel32", importc: "EnterCriticalSection".}
## Aquires the lock `L`. ## Acquires the lock `L`.
proc ReleaseSys(L: var TSysLock) {.stdcall, proc ReleaseSys(L: var TSysLock) {.stdcall,
dynlib: "kernel32", importc: "LeaveCriticalSection".} dynlib: "kernel32", importc: "LeaveCriticalSection".}
@ -131,13 +131,13 @@ else:
proc InitSysLock(L: var TSysLock, attr: pointer = nil) {. proc InitSysLock(L: var TSysLock, attr: pointer = nil) {.
importc: "pthread_mutex_init", header: "<pthread.h>".} importc: "pthread_mutex_init", header: "<pthread.h>".}
proc AquireSys(L: var TSysLock) {. proc AcquireSys(L: var TSysLock) {.
importc: "pthread_mutex_lock", header: "<pthread.h>".} importc: "pthread_mutex_lock", header: "<pthread.h>".}
proc TryAquireSysAux(L: var TSysLock): cint {. proc TryAcquireSysAux(L: var TSysLock): cint {.
importc: "pthread_mutex_trylock", header: "<pthread.h>".} importc: "pthread_mutex_trylock", header: "<pthread.h>".}
proc TryAquireSys(L: var TSysLock): bool {.inline.} = proc TryAcquireSys(L: var TSysLock): bool {.inline.} =
result = TryAquireSysAux(L) == 0'i32 result = TryAcquireSysAux(L) == 0'i32
proc ReleaseSys(L: var TSysLock) {. proc ReleaseSys(L: var TSysLock) {.
importc: "pthread_mutex_unlock", header: "<pthread.h>".} importc: "pthread_mutex_unlock", header: "<pthread.h>".}
@ -168,14 +168,14 @@ else:
proc pthread_cancel(a1: TSysThread): cint {. proc pthread_cancel(a1: TSysThread): cint {.
importc: "pthread_cancel", header: "<pthread.h>".} importc: "pthread_cancel", header: "<pthread.h>".}
proc AquireSysTimeoutAux(L: var TSysLock, timeout: var Ttimespec): cint {. proc AcquireSysTimeoutAux(L: var TSysLock, timeout: var Ttimespec): cint {.
importc: "pthread_mutex_timedlock", header: "<time.h>".} importc: "pthread_mutex_timedlock", header: "<time.h>".}
proc AquireSysTimeout(L: var TSysLock, msTimeout: int) {.inline.} = proc AcquireSysTimeout(L: var TSysLock, msTimeout: int) {.inline.} =
var a: Ttimespec var a: Ttimespec
a.tv_sec = msTimeout div 1000 a.tv_sec = msTimeout div 1000
a.tv_nsec = (msTimeout mod 1000) * 1000 a.tv_nsec = (msTimeout mod 1000) * 1000
var res = AquireSysTimeoutAux(L, a) var res = AcquireSysTimeoutAux(L, a)
if res != 0'i32: raise newException(EResourceExhausted, $strerror(res)) if res != 0'i32: raise newException(EResourceExhausted, $strerror(res))
type type
@ -257,7 +257,7 @@ when not defined(useNimRtl):
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!
AquireSys(HeapLock) AcquireSys(HeapLock)
t.prev = nil t.prev = nil
t.next = threadList t.next = threadList
if threadList != nil: if threadList != nil:
@ -268,7 +268,7 @@ when not defined(useNimRtl):
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!
AquireSys(HeapLock) AcquireSys(HeapLock)
if t == threadList: threadList = t.next if t == threadList: threadList = t.next
if t.next != nil: t.next.prev = t.prev if t.next != nil: t.next.prev = t.prev
if t.prev != nil: t.prev.next = t.next if t.prev != nil: t.prev.next = t.next
@ -297,10 +297,12 @@ type
data: TParam data: TParam
template ThreadProcWrapperBody(closure: expr) = template ThreadProcWrapperBody(closure: expr) =
when not hasSharedHeap: initGC() # init the GC for this thread
ThreadVarSetValue(globalsSlot, closure) ThreadVarSetValue(globalsSlot, closure)
var t = cast[ptr TThread[TParam]](closure) var t = cast[ptr TThread[TParam]](closure)
when not hasSharedHeap: stackBottom = addr(t) when not hasSharedHeap:
# init the GC for this thread:
setStackBottom(addr(t))
initGC()
t.stackBottom = addr(t) t.stackBottom = addr(t)
registerThread(t) registerThread(t)
try: try:
@ -337,7 +339,7 @@ proc joinThreads*[TParam](t: openArray[TThread[TParam]]) =
proc destroyThread*[TParam](t: var TThread[TParam]) {.inline.} = proc destroyThread*[TParam](t: var TThread[TParam]) {.inline.} =
## forces the thread `t` to terminate. This is potentially dangerous if ## forces the thread `t` to terminate. This is potentially dangerous if
## you don't have full control over `t` and its aquired resources. ## you don't have full control over `t` and its acquired resources.
when hostOS == "windows": when hostOS == "windows":
discard TerminateThread(t.sys, 1'i32) discard TerminateThread(t.sys, 1'i32)
else: else:
@ -357,12 +359,14 @@ proc createThread*[TParam](t: var TThread[TParam],
var dummyThreadId: int32 var dummyThreadId: int32
t.sys = CreateThread(nil, stackSize, threadProcWrapper[TParam], t.sys = CreateThread(nil, stackSize, threadProcWrapper[TParam],
addr(t), 0'i32, dummyThreadId) addr(t), 0'i32, dummyThreadId)
if t.sys <= 0:
raise newException(EResourceExhausted, "cannot create thread")
else: else:
var a: Tpthread_attr var a: Tpthread_attr
pthread_attr_init(a) pthread_attr_init(a)
pthread_attr_setstacksize(a, stackSize) pthread_attr_setstacksize(a, stackSize)
if pthread_create(t.sys, a, threadProcWrapper[TParam], addr(t)) != 0: if pthread_create(t.sys, a, threadProcWrapper[TParam], addr(t)) != 0:
raise newException(EIO, "cannot create thread") raise newException(EResourceExhausted, "cannot create thread")
# --------------------------- lock handling ---------------------------------- # --------------------------- lock handling ----------------------------------
@ -386,17 +390,17 @@ proc OrderedLocks(g: PGcThread): bool =
if g.locks[i] >= g.locks[i+1]: return false if g.locks[i] >= g.locks[i+1]: return false
result = true result = true
proc TryAquire*(lock: var TLock): bool {.inline.} = proc TryAcquire*(lock: var TLock): bool {.inline.} =
## Try to aquires the lock `lock`. Returns `true` on success. ## Try to acquires the lock `lock`. Returns `true` on success.
result = TryAcquireSys(lock)
when noDeadlocks: when noDeadlocks:
result = TryAquireSys(lock)
if not result: return if not result: return
# we have to add it to the ordered list. Oh, and we might fail if # we have to add it to the ordered list. Oh, and we might fail if
# there is no space in the array left ... # there is no space in the array left ...
var g = ThisThread() var g = ThisThread()
if g.locksLen >= len(g.locks): if g.locksLen >= len(g.locks):
ReleaseSys(lock) ReleaseSys(lock)
raise newException(EResourceExhausted, "cannot aquire additional lock") raise newException(EResourceExhausted, "cannot acquire additional lock")
# find the position to add: # find the position to add:
var p = addr(lock) var p = addr(lock)
var L = g.locksLen-1 var L = g.locksLen-1
@ -418,11 +422,9 @@ proc TryAquire*(lock: var TLock): bool {.inline.} =
g.locks[g.locksLen] = p g.locks[g.locksLen] = p
inc(g.locksLen) inc(g.locksLen)
assert OrderedLocks(g) assert OrderedLocks(g)
else:
result = TryAquireSys(lock)
proc Aquire*(lock: var TLock) = proc Acquire*(lock: var TLock) =
## Aquires the lock `lock`. ## Acquires the lock `lock`.
when nodeadlocks: when nodeadlocks:
var g = ThisThread() var g = ThisThread()
var p = addr(lock) var p = addr(lock)
@ -435,20 +437,21 @@ proc Aquire*(lock: var TLock) =
else: else:
# do the crazy stuff here: # do the crazy stuff here:
if g.locksLen >= len(g.locks): if g.locksLen >= len(g.locks):
raise newException(EResourceExhausted, "cannot aquire additional lock") raise newException(EResourceExhausted,
"cannot acquire additional lock")
while L >= i: while L >= i:
ReleaseSys(cast[ptr TSysLock](g.locks[L])[]) ReleaseSys(cast[ptr TSysLock](g.locks[L])[])
g.locks[L+1] = g.locks[L] g.locks[L+1] = g.locks[L]
dec L dec L
# aquire the current lock: # acquire the current lock:
AquireSys(lock) AcquireSys(lock)
g.locks[i] = p g.locks[i] = p
inc(g.locksLen) inc(g.locksLen)
# aquire old locks in proper order again: # acquire old locks in proper order again:
L = g.locksLen-1 L = g.locksLen-1
inc i inc i
while i <= L: while i <= L:
AquireSys(cast[ptr TSysLock](g.locks[i])[]) AcquireSys(cast[ptr TSysLock](g.locks[i])[])
inc(i) inc(i)
# DANGER: We can only modify this global var if we gained every lock! # DANGER: We can only modify this global var if we gained every lock!
# NO! We need an atomic increment. Crap. # NO! We need an atomic increment. Crap.
@ -458,13 +461,13 @@ proc Aquire*(lock: var TLock) =
# simply add to the end: # simply add to the end:
if g.locksLen >= len(g.locks): if g.locksLen >= len(g.locks):
raise newException(EResourceExhausted, "cannot aquire additional lock") raise newException(EResourceExhausted, "cannot acquire additional lock")
AquireSys(lock) AcquireSys(lock)
g.locks[g.locksLen] = p g.locks[g.locksLen] = p
inc(g.locksLen) inc(g.locksLen)
assert OrderedLocks(g) assert OrderedLocks(g)
else: else:
AquireSys(lock) AcquireSys(lock)
proc Release*(lock: var TLock) = proc Release*(lock: var TLock) =
## Releases the lock `lock`. ## Releases the lock `lock`.

View file

@ -1,5 +1,6 @@
discard """ discard """
outputsub: "101" outputsub: "101"
cmd: "nimrod cc --hints:on --threads:on $# $#"
""" """
import os import os
@ -19,33 +20,33 @@ proc threadFunc(interval: tuple[a, b: int]) {.procvar.} =
when nodeadlocks: when nodeadlocks:
case i mod 6 case i mod 6
of 0: of 0:
Aquire(L) # lock stdout Acquire(L) # lock stdout
Aquire(M) Acquire(M)
Aquire(N) Acquire(N)
of 1: of 1:
Aquire(L) Acquire(L)
Aquire(N) # lock stdout Acquire(N) # lock stdout
Aquire(M) Acquire(M)
of 2: of 2:
Aquire(M) Acquire(M)
Aquire(L) Acquire(L)
Aquire(N) Acquire(N)
of 3: of 3:
Aquire(M) Acquire(M)
Aquire(N) Acquire(N)
Aquire(L) Acquire(L)
of 4: of 4:
Aquire(N) Acquire(N)
Aquire(M) Acquire(M)
Aquire(L) Acquire(L)
of 5: of 5:
Aquire(N) Acquire(N)
Aquire(L) Acquire(L)
Aquire(M) Acquire(M)
else: assert false else: assert false
else: else:
Aquire(L) # lock stdout Acquire(L) # lock stdout
Aquire(M) Acquire(M)
echo i echo i
os.sleep(10) os.sleep(10)