bugfix: regionized pointers in a generic context; renamed 'Future' to 'Promise'
This commit is contained in:
parent
d2dbcf2fa4
commit
030eac86c0
7 changed files with 137 additions and 108 deletions
|
|
@ -885,6 +885,8 @@ const
|
||||||
|
|
||||||
nkCallKinds* = {nkCall, nkInfix, nkPrefix, nkPostfix,
|
nkCallKinds* = {nkCall, nkInfix, nkPrefix, nkPostfix,
|
||||||
nkCommand, nkCallStrLit, nkHiddenCallConv}
|
nkCommand, nkCallStrLit, nkHiddenCallConv}
|
||||||
|
nkIdentKinds* = {nkIdent, nkSym, nkAccQuoted, nkOpenSymChoice,
|
||||||
|
nkClosedSymChoice}
|
||||||
|
|
||||||
nkLiterals* = {nkCharLit..nkTripleStrLit}
|
nkLiterals* = {nkCharLit..nkTripleStrLit}
|
||||||
nkLambdaKinds* = {nkLambda, nkDo}
|
nkLambdaKinds* = {nkLambda, nkDo}
|
||||||
|
|
|
||||||
|
|
@ -134,26 +134,26 @@ proc callCodegenProc*(name: string, arg1: PNode;
|
||||||
|
|
||||||
# we have 4 cases to consider:
|
# we have 4 cases to consider:
|
||||||
# - a void proc --> nothing to do
|
# - a void proc --> nothing to do
|
||||||
# - a proc returning GC'ed memory --> requires a future
|
# - a proc returning GC'ed memory --> requires a promise
|
||||||
# - a proc returning non GC'ed memory --> pass as hidden 'var' parameter
|
# - a proc returning non GC'ed memory --> pass as hidden 'var' parameter
|
||||||
# - not in a parallel environment --> requires a future for memory safety
|
# - not in a parallel environment --> requires a promise for memory safety
|
||||||
type
|
type
|
||||||
TSpawnResult = enum
|
TSpawnResult = enum
|
||||||
srVoid, srFuture, srByVar
|
srVoid, srPromise, srByVar
|
||||||
TFutureKind = enum
|
TPromiseKind = enum
|
||||||
futInvalid # invalid type T for 'Future[T]'
|
promInvalid # invalid type T for 'Promise[T]'
|
||||||
futGC # Future of a GC'ed type
|
promGC # Promise of a GC'ed type
|
||||||
futBlob # Future of a blob type
|
promBlob # Promise of a blob type
|
||||||
|
|
||||||
proc spawnResult(t: PType; inParallel: bool): TSpawnResult =
|
proc spawnResult(t: PType; inParallel: bool): TSpawnResult =
|
||||||
if t.isEmptyType: srVoid
|
if t.isEmptyType: srVoid
|
||||||
elif inParallel and not containsGarbageCollectedRef(t): srByVar
|
elif inParallel and not containsGarbageCollectedRef(t): srByVar
|
||||||
else: srFuture
|
else: srPromise
|
||||||
|
|
||||||
proc futureKind(t: PType): TFutureKind =
|
proc promiseKind(t: PType): TPromiseKind =
|
||||||
if t.skipTypes(abstractInst).kind in {tyRef, tyString, tySequence}: futGC
|
if t.skipTypes(abstractInst).kind in {tyRef, tyString, tySequence}: promGC
|
||||||
elif containsGarbageCollectedRef(t): futInvalid
|
elif containsGarbageCollectedRef(t): promInvalid
|
||||||
else: futBlob
|
else: promBlob
|
||||||
|
|
||||||
discard """
|
discard """
|
||||||
We generate roughly this:
|
We generate roughly this:
|
||||||
|
|
@ -164,12 +164,12 @@ proc f_wrapper(args) =
|
||||||
# the 'parallel' statement
|
# the 'parallel' statement
|
||||||
var b = args.b
|
var b = args.b
|
||||||
|
|
||||||
args.fut = nimCreateFuture(thread, sizeof(T)) # optional
|
args.prom = nimCreatePromise(thread, sizeof(T)) # optional
|
||||||
nimFutureCreateCondVar(args.fut) # optional
|
nimPromiseCreateCondVar(args.prom) # optional
|
||||||
nimArgsPassingDone() # signal parent that the work is done
|
nimArgsPassingDone() # signal parent that the work is done
|
||||||
#
|
#
|
||||||
args.fut.blob = f(a, b, ...)
|
args.prom.blob = f(a, b, ...)
|
||||||
nimFutureSignal(args.fut)
|
nimPromiseSignal(args.prom)
|
||||||
|
|
||||||
# - or -
|
# - or -
|
||||||
f(a, b, ...)
|
f(a, b, ...)
|
||||||
|
|
@ -181,42 +181,42 @@ stmtList:
|
||||||
scratchObj.b = b
|
scratchObj.b = b
|
||||||
|
|
||||||
nimSpawn(f_wrapper, addr scratchObj)
|
nimSpawn(f_wrapper, addr scratchObj)
|
||||||
scratchObj.fut # optional
|
scratchObj.prom # optional
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
proc createNimCreateFutureCall(fut, threadParam: PNode): PNode =
|
proc createNimCreatePromiseCall(prom, threadParam: PNode): PNode =
|
||||||
let size = newNodeIT(nkCall, fut.info, getSysType(tyInt))
|
let size = newNodeIT(nkCall, prom.info, getSysType(tyInt))
|
||||||
size.add newSymNode(createMagic("sizeof", mSizeOf))
|
size.add newSymNode(createMagic("sizeof", mSizeOf))
|
||||||
assert fut.typ.kind == tyGenericInst
|
assert prom.typ.kind == tyGenericInst
|
||||||
size.add newNodeIT(nkType, fut.info, fut.typ.sons[1])
|
size.add newNodeIT(nkType, prom.info, prom.typ.sons[1])
|
||||||
|
|
||||||
let castExpr = newNodeIT(nkCast, fut.info, fut.typ)
|
let castExpr = newNodeIT(nkCast, prom.info, prom.typ)
|
||||||
castExpr.add emptyNode
|
castExpr.add emptyNode
|
||||||
castExpr.add callCodeGenProc("nimCreateFuture", threadParam, size)
|
castExpr.add callCodeGenProc("nimCreatePromise", threadParam, size)
|
||||||
result = newFastAsgnStmt(fut, castExpr)
|
result = newFastAsgnStmt(prom, castExpr)
|
||||||
|
|
||||||
proc createWrapperProc(f: PNode; threadParam, argsParam: PSym;
|
proc createWrapperProc(f: PNode; threadParam, argsParam: PSym;
|
||||||
varSection, call, barrier, fut: PNode): PSym =
|
varSection, call, barrier, prom: PNode): PSym =
|
||||||
var body = newNodeI(nkStmtList, f.info)
|
var body = newNodeI(nkStmtList, f.info)
|
||||||
body.add varSection
|
body.add varSection
|
||||||
if barrier != nil:
|
if barrier != nil:
|
||||||
body.add callCodeGenProc("barrierEnter", barrier)
|
body.add callCodeGenProc("barrierEnter", barrier)
|
||||||
if fut != nil:
|
if prom != nil:
|
||||||
body.add createNimCreateFutureCall(fut, threadParam.newSymNode)
|
body.add createNimCreatePromiseCall(prom, threadParam.newSymNode)
|
||||||
if barrier == nil:
|
if barrier == nil:
|
||||||
body.add callCodeGenProc("nimFutureCreateCondVar", fut)
|
body.add callCodeGenProc("nimPromiseCreateCondVar", prom)
|
||||||
|
|
||||||
body.add callCodeGenProc("nimArgsPassingDone", threadParam.newSymNode)
|
body.add callCodeGenProc("nimArgsPassingDone", threadParam.newSymNode)
|
||||||
if fut != nil:
|
if prom != nil:
|
||||||
let fk = fut.typ.sons[1].futureKind
|
let fk = prom.typ.sons[1].promiseKind
|
||||||
if fk == futInvalid:
|
if fk == promInvalid:
|
||||||
localError(f.info, "cannot create a future of type: " &
|
localError(f.info, "cannot create a promise of type: " &
|
||||||
typeToString(fut.typ.sons[1]))
|
typeToString(prom.typ.sons[1]))
|
||||||
body.add newAsgnStmt(indirectAccess(fut,
|
body.add newAsgnStmt(indirectAccess(prom,
|
||||||
if fk == futGC: "data" else: "blob", fut.info), call)
|
if fk == promGC: "data" else: "blob", prom.info), call)
|
||||||
if barrier == nil:
|
if barrier == nil:
|
||||||
body.add callCodeGenProc("nimFutureSignal", fut)
|
body.add callCodeGenProc("nimPromiseSignal", prom)
|
||||||
else:
|
else:
|
||||||
body.add call
|
body.add call
|
||||||
if barrier != nil:
|
if barrier != nil:
|
||||||
|
|
@ -381,7 +381,7 @@ proc wrapProcForSpawn*(owner: PSym; n: PNode; retType: PType;
|
||||||
of srVoid:
|
of srVoid:
|
||||||
internalAssert dest == nil
|
internalAssert dest == nil
|
||||||
result = newNodeI(nkStmtList, n.info)
|
result = newNodeI(nkStmtList, n.info)
|
||||||
of srFuture:
|
of srPromise:
|
||||||
internalAssert dest == nil
|
internalAssert dest == nil
|
||||||
result = newNodeIT(nkStmtListExpr, n.info, retType)
|
result = newNodeIT(nkStmtListExpr, n.info, retType)
|
||||||
of srByVar:
|
of srByVar:
|
||||||
|
|
@ -450,17 +450,17 @@ proc wrapProcForSpawn*(owner: PSym; n: PNode; retType: PType;
|
||||||
result.add newFastAsgnStmt(newDotExpr(scratchObj, field), barrier)
|
result.add newFastAsgnStmt(newDotExpr(scratchObj, field), barrier)
|
||||||
barrierAsExpr = indirectAccess(castExpr, field, n.info)
|
barrierAsExpr = indirectAccess(castExpr, field, n.info)
|
||||||
|
|
||||||
var futField, futAsExpr: PNode = nil
|
var promField, promAsExpr: PNode = nil
|
||||||
if spawnKind == srFuture:
|
if spawnKind == srPromise:
|
||||||
var field = newSym(skField, getIdent"fut", owner, n.info)
|
var field = newSym(skField, getIdent"prom", owner, n.info)
|
||||||
field.typ = retType
|
field.typ = retType
|
||||||
objType.addField(field)
|
objType.addField(field)
|
||||||
futField = newDotExpr(scratchObj, field)
|
promField = newDotExpr(scratchObj, field)
|
||||||
futAsExpr = indirectAccess(castExpr, field, n.info)
|
promAsExpr = indirectAccess(castExpr, field, n.info)
|
||||||
|
|
||||||
let wrapper = createWrapperProc(fn, threadParam, argsParam, varSection, call,
|
let wrapper = createWrapperProc(fn, threadParam, argsParam, varSection, call,
|
||||||
barrierAsExpr, futAsExpr)
|
barrierAsExpr, promAsExpr)
|
||||||
result.add callCodeGenProc("nimSpawn", wrapper.newSymNode,
|
result.add callCodeGenProc("nimSpawn", wrapper.newSymNode,
|
||||||
genAddrOf(scratchObj.newSymNode))
|
genAddrOf(scratchObj.newSymNode))
|
||||||
|
|
||||||
if spawnKind == srFuture: result.add futField
|
if spawnKind == srPromise: result.add promField
|
||||||
|
|
|
||||||
|
|
@ -1579,9 +1579,9 @@ proc semShallowCopy(c: PContext, n: PNode, flags: TExprFlags): PNode =
|
||||||
else:
|
else:
|
||||||
result = semDirectOp(c, n, flags)
|
result = semDirectOp(c, n, flags)
|
||||||
|
|
||||||
proc createFuture(c: PContext; t: PType; info: TLineInfo): PType =
|
proc createPromise(c: PContext; t: PType; info: TLineInfo): PType =
|
||||||
result = newType(tyGenericInvokation, c.module)
|
result = newType(tyGenericInvokation, c.module)
|
||||||
addSonSkipIntLit(result, magicsys.getCompilerProc("Future").typ)
|
addSonSkipIntLit(result, magicsys.getCompilerProc("Promise").typ)
|
||||||
addSonSkipIntLit(result, t)
|
addSonSkipIntLit(result, t)
|
||||||
result = instGenericContainer(c, info, result, allowMetaTypes = false)
|
result = instGenericContainer(c, info, result, allowMetaTypes = false)
|
||||||
|
|
||||||
|
|
@ -1619,9 +1619,9 @@ proc semMagic(c: PContext, n: PNode, s: PSym, flags: TExprFlags): PNode =
|
||||||
of mSpawn:
|
of mSpawn:
|
||||||
result = setMs(n, s)
|
result = setMs(n, s)
|
||||||
result.sons[1] = semExpr(c, n.sons[1])
|
result.sons[1] = semExpr(c, n.sons[1])
|
||||||
# later passes may transform the type 'Future[T]' back into 'T'
|
# later passes may transform the type 'Promise[T]' back into 'T'
|
||||||
if not result[1].typ.isEmptyType:
|
if not result[1].typ.isEmptyType:
|
||||||
result.typ = createFuture(c, result[1].typ, n.info)
|
result.typ = createPromise(c, result[1].typ, n.info)
|
||||||
else: result = semDirectOp(c, n, flags)
|
else: result = semDirectOp(c, n, flags)
|
||||||
|
|
||||||
proc semWhen(c: PContext, n: PNode, semCheck = true): PNode =
|
proc semWhen(c: PContext, n: PNode, semCheck = true): PNode =
|
||||||
|
|
|
||||||
|
|
@ -1084,8 +1084,10 @@ proc semTypeNode(c: PContext, n: PNode, prev: PType): PType =
|
||||||
of nkCallKinds:
|
of nkCallKinds:
|
||||||
if isRange(n):
|
if isRange(n):
|
||||||
result = semRangeAux(c, n, prev)
|
result = semRangeAux(c, n, prev)
|
||||||
elif n[0].kind == nkIdent:
|
elif n[0].kind notin nkIdentKinds:
|
||||||
let op = n.sons[0].ident
|
result = semTypeExpr(c, n)
|
||||||
|
else:
|
||||||
|
let op = considerAcc(n.sons[0])
|
||||||
if op.id in {ord(wAnd), ord(wOr)} or op.s == "|":
|
if op.id in {ord(wAnd), ord(wOr)} or op.s == "|":
|
||||||
checkSonsLen(n, 3)
|
checkSonsLen(n, 3)
|
||||||
var
|
var
|
||||||
|
|
@ -1120,8 +1122,6 @@ proc semTypeNode(c: PContext, n: PNode, prev: PType): PType =
|
||||||
result = semAnyRef(c, n, tyRef, prev)
|
result = semAnyRef(c, n, tyRef, prev)
|
||||||
else:
|
else:
|
||||||
result = semTypeExpr(c, n)
|
result = semTypeExpr(c, n)
|
||||||
else:
|
|
||||||
result = semTypeExpr(c, n)
|
|
||||||
of nkWhenStmt:
|
of nkWhenStmt:
|
||||||
var whenResult = semWhen(c, n, false)
|
var whenResult = semWhen(c, n, false)
|
||||||
if whenResult.kind == nkStmtList: whenResult.kind = nkStmtListType
|
if whenResult.kind == nkStmtList: whenResult.kind = nkStmtListType
|
||||||
|
|
|
||||||
|
|
@ -65,12 +65,14 @@ proc closeBarrier*(b: ptr Barrier) {.compilerProc.} =
|
||||||
# ----------------------------------------------------------------------------
|
# ----------------------------------------------------------------------------
|
||||||
|
|
||||||
type
|
type
|
||||||
|
foreign* = object ## a region that indicates the pointer comes from a
|
||||||
|
## foreign thread heap.
|
||||||
AwaitInfo = object
|
AwaitInfo = object
|
||||||
cv: CondVar
|
cv: CondVar
|
||||||
idx: int
|
idx: int
|
||||||
|
|
||||||
RawFuture* = ptr RawFutureObj ## untyped base class for 'Future[T]'
|
RawPromise* = ptr RawPromiseObj ## untyped base class for 'Promise[T]'
|
||||||
RawFutureObj {.inheritable.} = object # \
|
RawPromiseObj {.inheritable.} = object # \
|
||||||
# we allocate this with the thread local allocator; this
|
# we allocate this with the thread local allocator; this
|
||||||
# is possible since we already need to do the GC_unref
|
# is possible since we already need to do the GC_unref
|
||||||
# on the owning thread
|
# on the owning thread
|
||||||
|
|
@ -81,10 +83,10 @@ type
|
||||||
idx: int
|
idx: int
|
||||||
data: PObject # we incRef and unref it to keep it alive
|
data: PObject # we incRef and unref it to keep it alive
|
||||||
owner: ptr Worker
|
owner: ptr Worker
|
||||||
next: RawFuture
|
next: RawPromise
|
||||||
align: float64 # a float for proper alignment
|
align: float64 # a float for proper alignment
|
||||||
|
|
||||||
Future* {.compilerProc.} [T] = ptr object of RawFutureObj
|
Promise* {.compilerProc.} [T] = ptr object of RawPromiseObj
|
||||||
blob: T ## the underlying value, if available. Note that usually
|
blob: T ## the underlying value, if available. Note that usually
|
||||||
## you should not access this field directly! However it can
|
## you should not access this field directly! However it can
|
||||||
## sometimes be more efficient than getting the value via ``^``.
|
## sometimes be more efficient than getting the value via ``^``.
|
||||||
|
|
@ -99,24 +101,24 @@ type
|
||||||
ready: bool # put it here for correct alignment!
|
ready: bool # put it here for correct alignment!
|
||||||
initialized: bool # whether it has even been initialized
|
initialized: bool # whether it has even been initialized
|
||||||
shutdown: bool # the pool requests to shut down this worker thread
|
shutdown: bool # the pool requests to shut down this worker thread
|
||||||
futureLock: TLock
|
promiseLock: TLock
|
||||||
head: RawFuture
|
head: RawPromise
|
||||||
|
|
||||||
proc finished*(fut: RawFuture) =
|
proc finished*(prom: RawPromise) =
|
||||||
## This MUST be called for every created future to free its associated
|
## This MUST be called for every created promise to free its associated
|
||||||
## resources. Note that the default reading operation ``^`` is destructive
|
## resources. Note that the default reading operation ``^`` is destructive
|
||||||
## and calls ``finished``.
|
## and calls ``finished``.
|
||||||
doAssert fut.ai.isNil, "future is still attached to an 'awaitAny'"
|
doAssert prom.ai.isNil, "promise is still attached to an 'awaitAny'"
|
||||||
assert fut.next == nil
|
assert prom.next == nil
|
||||||
let w = fut.owner
|
let w = prom.owner
|
||||||
acquire(w.futureLock)
|
acquire(w.promiseLock)
|
||||||
fut.next = w.head
|
prom.next = w.head
|
||||||
w.head = fut
|
w.head = prom
|
||||||
release(w.futureLock)
|
release(w.promiseLock)
|
||||||
|
|
||||||
proc cleanFutures(w: ptr Worker) =
|
proc cleanPromises(w: ptr Worker) =
|
||||||
var it = w.head
|
var it = w.head
|
||||||
acquire(w.futureLock)
|
acquire(w.promiseLock)
|
||||||
while it != nil:
|
while it != nil:
|
||||||
let nxt = it.next
|
let nxt = it.next
|
||||||
if it.usesCondVar: destroyCondVar(it.cv)
|
if it.usesCondVar: destroyCondVar(it.cv)
|
||||||
|
|
@ -124,62 +126,84 @@ proc cleanFutures(w: ptr Worker) =
|
||||||
dealloc(it)
|
dealloc(it)
|
||||||
it = nxt
|
it = nxt
|
||||||
w.head = nil
|
w.head = nil
|
||||||
release(w.futureLock)
|
release(w.promiseLock)
|
||||||
|
|
||||||
proc nimCreateFuture(owner: pointer; blobSize: int): RawFuture {.
|
proc nimCreatePromise(owner: pointer; blobSize: int): RawPromise {.
|
||||||
compilerProc.} =
|
compilerProc.} =
|
||||||
result = cast[RawFuture](alloc0(RawFutureObj.sizeof + blobSize))
|
result = cast[RawPromise](alloc0(RawPromiseObj.sizeof + blobSize))
|
||||||
result.owner = cast[ptr Worker](owner)
|
result.owner = cast[ptr Worker](owner)
|
||||||
|
|
||||||
proc nimFutureCreateCondVar(fut: RawFuture) {.compilerProc.} =
|
proc nimPromiseCreateCondVar(prom: RawPromise) {.compilerProc.} =
|
||||||
fut.cv = createCondVar()
|
prom.cv = createCondVar()
|
||||||
fut.usesCondVar = true
|
prom.usesCondVar = true
|
||||||
|
|
||||||
proc nimFutureSignal(fut: RawFuture) {.compilerProc.} =
|
proc nimPromiseSignal(prom: RawPromise) {.compilerProc.} =
|
||||||
if fut.ai != nil:
|
if prom.ai != nil:
|
||||||
acquire(fut.ai.cv.L)
|
acquire(prom.ai.cv.L)
|
||||||
fut.ai.idx = fut.idx
|
prom.ai.idx = prom.idx
|
||||||
inc fut.ai.cv.counter
|
inc prom.ai.cv.counter
|
||||||
release(fut.ai.cv.L)
|
release(prom.ai.cv.L)
|
||||||
signal(fut.ai.cv.c)
|
signal(prom.ai.cv.c)
|
||||||
if fut.usesCondVar: signal(fut.cv)
|
if prom.usesCondVar: signal(prom.cv)
|
||||||
|
|
||||||
proc await*[T](fut: Future[T]) =
|
proc await*[T](prom: Promise[T]) =
|
||||||
## waits until the value for the future arrives.
|
## waits until the value for the promise arrives.
|
||||||
if fut.usesCondVar: await(fut.cv)
|
if prom.usesCondVar: await(prom.cv)
|
||||||
|
|
||||||
proc `^`*[T](fut: Future[T]): T =
|
proc awaitAndThen*[T](prom: Promise[T]; action: proc (x: T) {.closure.}) =
|
||||||
|
## blocks until the value is available and then passes this value
|
||||||
|
## to ``action``. Note that due to Nimrod's parameter passing semantics this
|
||||||
|
## means that ``T`` doesn't need to be copied and so ``awaitAndThen`` can
|
||||||
|
## sometimes be more efficient than ``^``.
|
||||||
|
if prom.usesCondVar: await(prom)
|
||||||
|
when T is string or T is seq:
|
||||||
|
action(cast[T](prom.data))
|
||||||
|
elif T is ref:
|
||||||
|
{.error: "'awaitAndThen' not available for Promise[ref]".}
|
||||||
|
else:
|
||||||
|
action(prom.blob)
|
||||||
|
finished(prom)
|
||||||
|
|
||||||
|
proc `^`*[T](prom: Promise[ref T]): foreign ptr T =
|
||||||
## blocks until the value is available and then returns this value. Note
|
## blocks until the value is available and then returns this value. Note
|
||||||
## this reading is destructive for reasons of efficiency and convenience.
|
## this reading is destructive for reasons of efficiency and convenience.
|
||||||
## This calls ``finished(fut)``.
|
## This calls ``finished(prom)``.
|
||||||
if fut.usesCondVar: await(fut)
|
if prom.usesCondVar: await(prom)
|
||||||
when T is string or T is seq or T is ref:
|
result = cast[foreign ptr T](prom.data)
|
||||||
result = cast[T](fut.data)
|
finished(prom)
|
||||||
else:
|
|
||||||
result = fut.blob
|
|
||||||
finished(fut)
|
|
||||||
|
|
||||||
proc awaitAny*(futures: openArray[RawFuture]): int =
|
proc `^`*[T](prom: Promise[T]): T =
|
||||||
# awaits any of the given futures. Returns the index of one future for which
|
## blocks until the value is available and then returns this value. Note
|
||||||
## a value arrived. A future only supports one call to 'awaitAny' at the
|
## this reading is destructive for reasons of efficiency and convenience.
|
||||||
|
## This calls ``finished(prom)``.
|
||||||
|
if prom.usesCondVar: await(prom)
|
||||||
|
when T is string or T is seq:
|
||||||
|
result = cast[T](prom.data)
|
||||||
|
else:
|
||||||
|
result = prom.blob
|
||||||
|
finished(prom)
|
||||||
|
|
||||||
|
proc awaitAny*(promises: openArray[RawPromise]): int =
|
||||||
|
# awaits any of the given promises. Returns the index of one promise for which
|
||||||
|
## a value arrived. A promise only supports one call to 'awaitAny' at the
|
||||||
## same time. That means if you await([a,b]) and await([b,c]) the second
|
## same time. That means if you await([a,b]) and await([b,c]) the second
|
||||||
## call will only await 'c'. If there is no future left to be able to wait
|
## call will only await 'c'. If there is no promise left to be able to wait
|
||||||
## on, -1 is returned.
|
## on, -1 is returned.
|
||||||
## **Note**: This results in non-deterministic behaviour and so should be
|
## **Note**: This results in non-deterministic behaviour and so should be
|
||||||
## avoided.
|
## avoided.
|
||||||
var ai: AwaitInfo
|
var ai: AwaitInfo
|
||||||
ai.cv = createCondVar()
|
ai.cv = createCondVar()
|
||||||
var conflicts = 0
|
var conflicts = 0
|
||||||
for i in 0 .. futures.high:
|
for i in 0 .. promises.high:
|
||||||
if cas(addr futures[i].ai, nil, addr ai):
|
if cas(addr promises[i].ai, nil, addr ai):
|
||||||
futures[i].idx = i
|
promises[i].idx = i
|
||||||
else:
|
else:
|
||||||
inc conflicts
|
inc conflicts
|
||||||
if conflicts < futures.len:
|
if conflicts < promises.len:
|
||||||
await(ai.cv)
|
await(ai.cv)
|
||||||
result = ai.idx
|
result = ai.idx
|
||||||
for i in 0 .. futures.high:
|
for i in 0 .. promises.high:
|
||||||
discard cas(addr futures[i].ai, addr ai, nil)
|
discard cas(addr promises[i].ai, addr ai, nil)
|
||||||
else:
|
else:
|
||||||
result = -1
|
result = -1
|
||||||
destroyCondVar(ai.cv)
|
destroyCondVar(ai.cv)
|
||||||
|
|
@ -207,7 +231,7 @@ proc slave(w: ptr Worker) {.thread.} =
|
||||||
await(w.taskArrived)
|
await(w.taskArrived)
|
||||||
assert(not w.ready)
|
assert(not w.ready)
|
||||||
w.f(w, w.data)
|
w.f(w, w.data)
|
||||||
if w.head != nil: w.cleanFutures
|
if w.head != nil: w.cleanPromises
|
||||||
if w.shutdown:
|
if w.shutdown:
|
||||||
w.shutdown = false
|
w.shutdown = false
|
||||||
atomicDec currentPoolSize
|
atomicDec currentPoolSize
|
||||||
|
|
@ -228,7 +252,7 @@ var
|
||||||
proc activateThread(i: int) {.noinline.} =
|
proc activateThread(i: int) {.noinline.} =
|
||||||
workersData[i].taskArrived = createCondVar()
|
workersData[i].taskArrived = createCondVar()
|
||||||
workersData[i].taskStarted = createCondVar()
|
workersData[i].taskStarted = createCondVar()
|
||||||
initLock workersData[i].futureLock
|
initLock workersData[i].promiseLock
|
||||||
workersData[i].initialized = true
|
workersData[i].initialized = true
|
||||||
createThread(workers[i], slave, addr(workersData[i]))
|
createThread(workers[i], slave, addr(workersData[i]))
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -42,7 +42,6 @@ type
|
||||||
cstring* {.magic: Cstring.} ## built-in cstring (*compatible string*) type
|
cstring* {.magic: Cstring.} ## built-in cstring (*compatible string*) type
|
||||||
pointer* {.magic: Pointer.} ## built-in pointer type, use the ``addr``
|
pointer* {.magic: Pointer.} ## built-in pointer type, use the ``addr``
|
||||||
## operator to get a pointer to a variable
|
## operator to get a pointer to a variable
|
||||||
|
|
||||||
const
|
const
|
||||||
on* = true ## alias for ``true``
|
on* = true ## alias for ``true``
|
||||||
off* = false ## alias for ``false``
|
off* = false ## alias for ``false``
|
||||||
|
|
@ -51,6 +50,9 @@ const
|
||||||
|
|
||||||
type
|
type
|
||||||
Ordinal* {.magic: Ordinal.}[T]
|
Ordinal* {.magic: Ordinal.}[T]
|
||||||
|
`ptr`* {.magic: Pointer.}[T] ## built-in generic untraced pointer type
|
||||||
|
`ref`* {.magic: Pointer.}[T] ## built-in generic traced pointer type
|
||||||
|
|
||||||
`nil` {.magic: "Nil".}
|
`nil` {.magic: "Nil".}
|
||||||
expr* {.magic: Expr.} ## meta type to denote an expression (for templates)
|
expr* {.magic: Expr.} ## meta type to denote an expression (for templates)
|
||||||
stmt* {.magic: Stmt.} ## meta type to denote a statement (for templates)
|
stmt* {.magic: Stmt.} ## meta type to denote a statement (for templates)
|
||||||
|
|
|
||||||
|
|
@ -179,7 +179,8 @@ when not defined(nimmixin):
|
||||||
# internal proc used for destroying sequences and arrays
|
# internal proc used for destroying sequences and arrays
|
||||||
for i in countup(0, r.len - 1): destroy(r[i])
|
for i in countup(0, r.len - 1): destroy(r[i])
|
||||||
else:
|
else:
|
||||||
# XXX Why is this exported and no compilerproc?
|
# XXX Why is this exported and no compilerproc? -> compilerprocs cannot be
|
||||||
|
# generic for now
|
||||||
proc nimDestroyRange*[T](r: T) =
|
proc nimDestroyRange*[T](r: T) =
|
||||||
# internal proc used for destroying sequences and arrays
|
# internal proc used for destroying sequences and arrays
|
||||||
mixin destroy
|
mixin destroy
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue