bugfixes for semantic checking; thread implementation pushed the compiler

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

View File

@@ -20,9 +20,9 @@ type
withinBind, withinTypeDesc
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,
flags: TSemGenericFlags = {}): PNode =
flags: TSemGenericFlags): PNode =
openScope(c.tab)
result = semGenericStmt(c, n, flags)
closeScope(c.tab)
@@ -60,7 +60,7 @@ proc getIdentNode(n: PNode): PNode =
result = n
proc semGenericStmt(c: PContext, n: PNode,
flags: TSemGenericFlags = {}): PNode =
flags: TSemGenericFlags): PNode =
result = n
if gCmd == cmdIdeTools: suggestStmt(c, n)
case n.kind
@@ -76,76 +76,88 @@ proc semGenericStmt(c: PContext, n: PNode,
of nkDotExpr:
var s = QualifiedLookUp(c, n, {})
if s != nil: result = semGenericStmtSymbol(c, n, s)
# XXX for example: ``result.add`` -- ``add`` needs to be looked up here...
of nkEmpty, nkSym..nkNilLit:
nil
of nkBind:
result = semGenericStmt(c, n.sons[0], {withinBind})
result = semGenericStmt(c, n.sons[0], flags+{withinBind})
of nkCall, nkHiddenCallConv, nkInfix, nkPrefix, nkCommand, nkCallStrLit:
# check if it is an expression macro:
checkMinSonsLen(n, 1)
var s = qualifiedLookup(c, n.sons[0], {})
var first = 0
if s != nil:
incl(s.flags, sfUsed)
case s.kind
of skMacro:
return semMacroExpr(c, n, s, false)
result = semMacroExpr(c, n, s, false)
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:
# Leave it as an identifier.
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:
n.sons[0] = newSymNode(s, n.sons[0].info)
result.sons[0] = newSymNode(s, n.sons[0].info)
first = 1
of skType:
# bad hack for generics:
if (s.typ != nil) and (s.typ.kind != tyGenericParam):
n.sons[0] = newSymNode(s, n.sons[0].info)
else: n.sons[0] = newSymNode(s, n.sons[0].info)
for i in countup(1, sonsLen(n) - 1):
n.sons[i] = semGenericStmt(c, n.sons[i], flags)
result.sons[0] = newSymNode(s, n.sons[0].info)
first = 1
else:
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:
result = semMacroStmt(c, n, false)
for i in countup(0, sonsLen(result)-1):
result.sons[i] = semGenericStmt(c, result.sons[i], flags)
of nkIfStmt:
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:
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)
of nkCaseStmt:
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):
var a = n.sons[i]
checkMinSonsLen(a, 1)
var L = sonsLen(a)
for j in countup(0, L - 2): a.sons[j] = semGenericStmt(c, a.sons[j])
a.sons[L - 1] = semGenericStmtScope(c, a.sons[L - 1])
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], flags)
closeScope(c.tab)
of nkForStmt:
var L = sonsLen(n)
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))
n.sons[L - 1] = semGenericStmt(c, n.sons[L - 1])
n.sons[L - 1] = semGenericStmt(c, n.sons[L-1], flags)
closeScope(c.tab)
of nkBlockStmt, nkBlockExpr, nkBlockType:
checkSonsLen(n, 2)
openScope(c.tab)
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)
of nkTryStmt:
checkMinSonsLen(n, 2)
n.sons[0] = semGenericStmtScope(c, n.sons[0])
for i in countup(1, sonsLen(n) - 1):
n.sons[0] = semGenericStmtScope(c, n.sons[0], flags)
for i in countup(1, sonsLen(n)-1):
var a = n.sons[i]
checkMinSonsLen(a, 1)
var L = sonsLen(a)
for j in countup(0, L - 2):
a.sons[j] = semGenericStmt(c, a.sons[j], {withinTypeDesc})
a.sons[L - 1] = semGenericStmtScope(c, a.sons[L - 1])
for j in countup(0, L-2):
a.sons[j] = semGenericStmt(c, a.sons[j], flags+{withinTypeDesc})
a.sons[L-1] = semGenericStmtScope(c, a.sons[L-1], flags)
of nkVarSection:
for i in countup(0, sonsLen(n) - 1):
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)
checkMinSonsLen(a, 3)
var L = sonsLen(a)
a.sons[L-2] = semGenericStmt(c, a.sons[L-2], {withinTypeDesc})
a.sons[L-1] = semGenericStmt(c, a.sons[L-1])
a.sons[L-2] = semGenericStmt(c, a.sons[L-2], flags+{withinTypeDesc})
a.sons[L-1] = semGenericStmt(c, a.sons[L-1], flags)
for j in countup(0, L-3):
addDecl(c, newSymS(skUnknown, getIdentNode(a.sons[j]), c))
of nkGenericParams:
@@ -163,7 +175,7 @@ proc semGenericStmt(c: PContext, n: PNode,
if (a.kind != nkIdentDefs): IllFormedAst(a)
checkMinSonsLen(a, 3)
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
for j in countup(0, L-3):
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)
checkSonsLen(a, 3)
addDecl(c, newSymS(skUnknown, getIdentNode(a.sons[0]), c))
a.sons[1] = semGenericStmt(c, a.sons[1], {withinTypeDesc})
a.sons[2] = semGenericStmt(c, a.sons[2])
a.sons[1] = semGenericStmt(c, a.sons[1], flags+{withinTypeDesc})
a.sons[2] = semGenericStmt(c, a.sons[2], flags)
of nkTypeSection:
for i in countup(0, sonsLen(n) - 1):
var a = n.sons[i]
@@ -190,15 +202,15 @@ proc semGenericStmt(c: PContext, n: PNode,
checkSonsLen(a, 3)
if a.sons[1].kind != nkEmpty:
openScope(c.tab)
a.sons[1] = semGenericStmt(c, a.sons[1])
a.sons[2] = semGenericStmt(c, a.sons[2], {withinTypeDesc})
a.sons[1] = semGenericStmt(c, a.sons[1], flags)
a.sons[2] = semGenericStmt(c, a.sons[2], flags+{withinTypeDesc})
closeScope(c.tab)
else:
a.sons[2] = semGenericStmt(c, a.sons[2], {withinTypeDesc})
a.sons[2] = semGenericStmt(c, a.sons[2], flags+{withinTypeDesc})
of nkEnumTy:
checkMinSonsLen(n, 1)
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):
var a: PNode
case n.sons[i].kind
@@ -211,14 +223,14 @@ proc semGenericStmt(c: PContext, n: PNode,
of nkFormalParams:
checkMinSonsLen(n, 1)
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):
var a = n.sons[i]
if (a.kind != nkIdentDefs): IllFormedAst(a)
checkMinSonsLen(a, 3)
var L = sonsLen(a)
a.sons[L-2] = semGenericStmt(c, a.sons[L-2], {withinTypeDesc})
a.sons[L-1] = semGenericStmt(c, a.sons[L-1])
a.sons[L-2] = semGenericStmt(c, a.sons[L-2], flags+{withinTypeDesc})
a.sons[L-1] = semGenericStmt(c, a.sons[L-1], flags)
for j in countup(0, L-3):
addDecl(c, newSymS(skUnknown, getIdentNode(a.sons[j]), c))
of nkProcDef, nkMethodDef, nkConverterDef, nkMacroDef, nkTemplateDef,
@@ -226,13 +238,13 @@ proc semGenericStmt(c: PContext, n: PNode,
checkSonsLen(n, codePos + 1)
addDecl(c, newSymS(skUnknown, getIdentNode(n.sons[0]), c))
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].sons[0].kind != nkEmpty:
addDecl(c, newSym(skUnknown, getIdent("result"), nil))
n.sons[paramsPos] = semGenericStmt(c, n.sons[paramsPos])
n.sons[pragmasPos] = semGenericStmt(c, n.sons[pragmasPos])
n.sons[codePos] = semGenericStmtScope(c, n.sons[codePos])
n.sons[paramsPos] = semGenericStmt(c, n.sons[paramsPos], flags)
n.sons[pragmasPos] = semGenericStmt(c, n.sons[pragmasPos], flags)
n.sons[codePos] = semGenericStmtScope(c, n.sons[codePos], flags)
closeScope(c.tab)
else:
for i in countup(0, sonsLen(n) - 1):

View File

@@ -748,7 +748,7 @@ proc semProcAux(c: PContext, n: PNode, kind: TSymKind,
else:
if s.typ.sons[0] != nil and kind != skIterator:
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:
# so we just ignore the body after semantic checking for importc:
n.sons[codePos] = ast.emptyNode

View File

@@ -461,6 +461,12 @@ proc repeatChar*(count: int, c: Char = ' '): string {.noSideEffect,
## the character `c`.
result = newString(count)
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 {.
noSideEffect, rtl, extern: "nsuAlignString".} =

View File

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

View File

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

View File

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

View File

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

View File

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