Promises are now refs
This commit is contained in:
parent
a4323b06b3
commit
2de99653d0
8 changed files with 132 additions and 87 deletions
|
|
@ -1636,7 +1636,7 @@ proc genMagicExpr(p: BProc, e: PNode, d: var TLoc, op: TMagic) =
|
||||||
of mSlurp..mQuoteAst:
|
of mSlurp..mQuoteAst:
|
||||||
localError(e.info, errXMustBeCompileTime, e.sons[0].sym.name.s)
|
localError(e.info, errXMustBeCompileTime, e.sons[0].sym.name.s)
|
||||||
of mSpawn:
|
of mSpawn:
|
||||||
let n = lowerings.wrapProcForSpawn(p.module.module, e[1], e.typ, nil, nil)
|
let n = lowerings.wrapProcForSpawn(p.module.module, e, e.typ, nil, nil)
|
||||||
expr(p, n, d)
|
expr(p, n, d)
|
||||||
of mParallel:
|
of mParallel:
|
||||||
let n = semparallel.liftParallel(p.module.module, e)
|
let n = semparallel.liftParallel(p.module.module, e)
|
||||||
|
|
|
||||||
|
|
@ -86,7 +86,7 @@ proc indirectAccess*(a: PNode, b: string, info: TLineInfo): PNode =
|
||||||
# returns a[].b as a node
|
# returns a[].b as a node
|
||||||
var deref = newNodeI(nkHiddenDeref, info)
|
var deref = newNodeI(nkHiddenDeref, info)
|
||||||
deref.typ = a.typ.skipTypes(abstractInst).sons[0]
|
deref.typ = a.typ.skipTypes(abstractInst).sons[0]
|
||||||
var t = deref.typ
|
var t = deref.typ.skipTypes(abstractInst)
|
||||||
var field: PSym
|
var field: PSym
|
||||||
while true:
|
while true:
|
||||||
assert t.kind == tyObject
|
assert t.kind == tyObject
|
||||||
|
|
@ -94,6 +94,7 @@ proc indirectAccess*(a: PNode, b: string, info: TLineInfo): PNode =
|
||||||
if field != nil: break
|
if field != nil: break
|
||||||
t = t.sons[0]
|
t = t.sons[0]
|
||||||
if t == nil: break
|
if t == nil: break
|
||||||
|
t = t.skipTypes(abstractInst)
|
||||||
assert field != nil, b
|
assert field != nil, b
|
||||||
addSon(deref, a)
|
addSon(deref, a)
|
||||||
result = newNodeI(nkDotExpr, info)
|
result = newNodeI(nkDotExpr, info)
|
||||||
|
|
@ -132,6 +133,11 @@ proc callCodegenProc*(name: string, arg1: PNode;
|
||||||
if arg3 != nil: result.add arg3
|
if arg3 != nil: result.add arg3
|
||||||
result.typ = sym.typ.sons[0]
|
result.typ = sym.typ.sons[0]
|
||||||
|
|
||||||
|
proc callProc(a: PNode): PNode =
|
||||||
|
result = newNodeI(nkCall, a.info)
|
||||||
|
result.add a
|
||||||
|
result.typ = a.typ.sons[0]
|
||||||
|
|
||||||
# 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 promise
|
# - a proc returning GC'ed memory --> requires a promise
|
||||||
|
|
@ -169,14 +175,14 @@ proc addLocalVar(varSection: PNode; owner: PSym; typ: PType; v: PNode): PSym =
|
||||||
discard """
|
discard """
|
||||||
We generate roughly this:
|
We generate roughly this:
|
||||||
|
|
||||||
proc f_wrapper(args) =
|
proc f_wrapper(thread, args) =
|
||||||
barrierEnter(args.barrier) # for parallel statement
|
barrierEnter(args.barrier) # for parallel statement
|
||||||
var a = args.a # thread transfer; deepCopy or shallowCopy or no copy
|
var a = args.a # thread transfer; deepCopy or shallowCopy or no copy
|
||||||
# depending on whether we're in a 'parallel' statement
|
# depending on whether we're in a 'parallel' statement
|
||||||
var b = args.b
|
var b = args.b
|
||||||
|
var prom = args.prom
|
||||||
|
|
||||||
args.prom = nimCreatePromise(thread, sizeof(T)) # optional
|
prom.owner = thread # optional
|
||||||
nimPromiseCreateCondVar(args.prom) # optional
|
|
||||||
nimArgsPassingDone() # signal parent that the work is done
|
nimArgsPassingDone() # signal parent that the work is done
|
||||||
#
|
#
|
||||||
args.prom.blob = f(a, b, ...)
|
args.prom.blob = f(a, b, ...)
|
||||||
|
|
@ -196,17 +202,6 @@ stmtList:
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
proc createNimCreatePromiseCall(prom, threadParam: PNode): PNode =
|
|
||||||
let size = newNodeIT(nkCall, prom.info, getSysType(tyInt))
|
|
||||||
size.add newSymNode(createMagic("sizeof", mSizeOf))
|
|
||||||
assert prom.typ.kind == tyGenericInst
|
|
||||||
size.add newNodeIT(nkType, prom.info, prom.typ.sons[1])
|
|
||||||
|
|
||||||
let castExpr = newNodeIT(nkCast, prom.info, prom.typ)
|
|
||||||
castExpr.add emptyNode
|
|
||||||
castExpr.add callCodeGenProc("nimCreatePromise", threadParam, size)
|
|
||||||
result = castExpr
|
|
||||||
|
|
||||||
proc createWrapperProc(f: PNode; threadParam, argsParam: PSym;
|
proc createWrapperProc(f: PNode; threadParam, argsParam: PSym;
|
||||||
varSection, call, barrier, prom: PNode;
|
varSection, call, barrier, prom: PNode;
|
||||||
spawnKind: TSpawnResult): PSym =
|
spawnKind: TSpawnResult): PSym =
|
||||||
|
|
@ -223,14 +218,14 @@ proc createWrapperProc(f: PNode; threadParam, argsParam: PSym;
|
||||||
threadLocalProm = addLocalVar(varSection, argsParam.owner, prom.typ, prom)
|
threadLocalProm = addLocalVar(varSection, argsParam.owner, prom.typ, prom)
|
||||||
elif prom != nil:
|
elif prom != nil:
|
||||||
internalAssert prom.typ.kind == tyGenericInst
|
internalAssert prom.typ.kind == tyGenericInst
|
||||||
threadLocalProm = addLocalVar(varSection, argsParam.owner, prom.typ,
|
threadLocalProm = addLocalVar(varSection, argsParam.owner, prom.typ, prom)
|
||||||
createNimCreatePromiseCall(prom, threadParam.newSymNode))
|
|
||||||
|
|
||||||
body.add varSection
|
body.add varSection
|
||||||
if prom != nil and spawnKind != srByVar:
|
if prom != nil and spawnKind != srByVar:
|
||||||
body.add newFastAsgnStmt(prom, threadLocalProm.newSymNode)
|
# generate:
|
||||||
if barrier == nil:
|
# prom.owner = threadParam
|
||||||
body.add callCodeGenProc("nimPromiseCreateCondVar", prom)
|
body.add newAsgnStmt(indirectAccess(threadLocalProm.newSymNode,
|
||||||
|
"owner", prom.info), threadParam.newSymNode)
|
||||||
|
|
||||||
body.add callCodeGenProc("nimArgsPassingDone", threadParam.newSymNode)
|
body.add callCodeGenProc("nimArgsPassingDone", threadParam.newSymNode)
|
||||||
if spawnKind == srByVar:
|
if spawnKind == srByVar:
|
||||||
|
|
@ -404,10 +399,11 @@ proc setupArgsForParallelism(n: PNode; objType: PType; scratchObj: PSym;
|
||||||
indirectAccess(castExpr, field, n.info))
|
indirectAccess(castExpr, field, n.info))
|
||||||
call.add(threadLocal.newSymNode)
|
call.add(threadLocal.newSymNode)
|
||||||
|
|
||||||
proc wrapProcForSpawn*(owner: PSym; n: PNode; retType: PType;
|
proc wrapProcForSpawn*(owner: PSym; spawnExpr: PNode; retType: PType;
|
||||||
barrier, dest: PNode = nil): PNode =
|
barrier, dest: PNode = nil): PNode =
|
||||||
# if 'barrier' != nil, then it is in a 'parallel' section and we
|
# if 'barrier' != nil, then it is in a 'parallel' section and we
|
||||||
# generate quite different code
|
# generate quite different code
|
||||||
|
let n = spawnExpr[1]
|
||||||
let spawnKind = spawnResult(retType, barrier!=nil)
|
let spawnKind = spawnResult(retType, barrier!=nil)
|
||||||
case spawnKind
|
case spawnKind
|
||||||
of srVoid:
|
of srVoid:
|
||||||
|
|
@ -489,6 +485,11 @@ proc wrapProcForSpawn*(owner: PSym; n: PNode; retType: PType;
|
||||||
objType.addField(field)
|
objType.addField(field)
|
||||||
promField = newDotExpr(scratchObj, field)
|
promField = newDotExpr(scratchObj, field)
|
||||||
promAsExpr = indirectAccess(castExpr, field, n.info)
|
promAsExpr = indirectAccess(castExpr, field, n.info)
|
||||||
|
# create promise:
|
||||||
|
result.add newFastAsgnStmt(promField, callProc(spawnExpr[2]))
|
||||||
|
if barrier == nil:
|
||||||
|
result.add callCodeGenProc("nimPromiseCreateCondVar", promField)
|
||||||
|
|
||||||
elif spawnKind == srByVar:
|
elif spawnKind == srByVar:
|
||||||
var field = newSym(skField, getIdent"prom", owner, n.info)
|
var field = newSym(skField, getIdent"prom", owner, n.info)
|
||||||
field.typ = newType(tyPtr, objType.owner)
|
field.typ = newType(tyPtr, objType.owner)
|
||||||
|
|
|
||||||
|
|
@ -646,10 +646,11 @@ proc singlePragma(c: PContext, sym: PSym, n: PNode, i: int,
|
||||||
processDynLib(c, it, sym)
|
processDynLib(c, it, sym)
|
||||||
of wCompilerproc:
|
of wCompilerproc:
|
||||||
noVal(it) # compilerproc may not get a string!
|
noVal(it) # compilerproc may not get a string!
|
||||||
makeExternExport(sym, "$1", it.info)
|
if sfFromGeneric notin sym.flags:
|
||||||
incl(sym.flags, sfCompilerProc)
|
makeExternExport(sym, "$1", it.info)
|
||||||
incl(sym.flags, sfUsed) # suppress all those stupid warnings
|
incl(sym.flags, sfCompilerProc)
|
||||||
registerCompilerProc(sym)
|
incl(sym.flags, sfUsed) # suppress all those stupid warnings
|
||||||
|
registerCompilerProc(sym)
|
||||||
of wProcVar:
|
of wProcVar:
|
||||||
noVal(it)
|
noVal(it)
|
||||||
incl(sym.flags, sfProcvar)
|
incl(sym.flags, sfProcvar)
|
||||||
|
|
|
||||||
|
|
@ -1585,6 +1585,16 @@ proc createPromise(c: PContext; t: PType; info: TLineInfo): PType =
|
||||||
addSonSkipIntLit(result, t)
|
addSonSkipIntLit(result, t)
|
||||||
result = instGenericContainer(c, info, result, allowMetaTypes = false)
|
result = instGenericContainer(c, info, result, allowMetaTypes = false)
|
||||||
|
|
||||||
|
proc instantiateCreatePromiseCall(c: PContext; t: PType;
|
||||||
|
info: TLineInfo): PSym =
|
||||||
|
let sym = magicsys.getCompilerProc("nimCreatePromise")
|
||||||
|
if sym == nil:
|
||||||
|
localError(info, errSystemNeeds, "nimCreatePromise")
|
||||||
|
var bindings: TIdTable
|
||||||
|
initIdTable(bindings)
|
||||||
|
bindings.idTablePut(sym.ast[genericParamsPos].sons[0].typ, t)
|
||||||
|
result = c.semGenerateInstance(c, sym, bindings, info)
|
||||||
|
|
||||||
proc setMs(n: PNode, s: PSym): PNode =
|
proc setMs(n: PNode, s: PSym): PNode =
|
||||||
result = n
|
result = n
|
||||||
n.sons[0] = newSymNode(s)
|
n.sons[0] = newSymNode(s)
|
||||||
|
|
@ -1626,6 +1636,7 @@ proc semMagic(c: PContext, n: PNode, s: PSym, flags: TExprFlags): PNode =
|
||||||
result.typ = result[1].typ
|
result.typ = result[1].typ
|
||||||
else:
|
else:
|
||||||
result.typ = createPromise(c, result[1].typ, n.info)
|
result.typ = createPromise(c, result[1].typ, n.info)
|
||||||
|
result.add instantiateCreatePromiseCall(c, result[1].typ, n.info).newSymNode
|
||||||
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 =
|
||||||
|
|
|
||||||
|
|
@ -406,19 +406,19 @@ proc transformSpawn(owner: PSym; n, barrier: PNode): PNode =
|
||||||
if result.isNil:
|
if result.isNil:
|
||||||
result = newNodeI(nkStmtList, n.info)
|
result = newNodeI(nkStmtList, n.info)
|
||||||
result.add n
|
result.add n
|
||||||
result.add wrapProcForSpawn(owner, m[1], b.typ, barrier, it[0])
|
result.add wrapProcForSpawn(owner, m, b.typ, barrier, it[0])
|
||||||
it.sons[it.len-1] = emptyNode
|
it.sons[it.len-1] = emptyNode
|
||||||
if result.isNil: result = n
|
if result.isNil: result = n
|
||||||
of nkAsgn, nkFastAsgn:
|
of nkAsgn, nkFastAsgn:
|
||||||
let b = n[1]
|
let b = n[1]
|
||||||
if getMagic(b) == mSpawn:
|
if getMagic(b) == mSpawn:
|
||||||
let m = transformSlices(b)
|
let m = transformSlices(b)
|
||||||
return wrapProcForSpawn(owner, m[1], b.typ, barrier, n[0])
|
return wrapProcForSpawn(owner, m, b.typ, barrier, n[0])
|
||||||
result = transformSpawnSons(owner, n, barrier)
|
result = transformSpawnSons(owner, n, barrier)
|
||||||
of nkCallKinds:
|
of nkCallKinds:
|
||||||
if getMagic(n) == mSpawn:
|
if getMagic(n) == mSpawn:
|
||||||
result = transformSlices(n)
|
result = transformSlices(n)
|
||||||
return wrapProcForSpawn(owner, result[1], n.typ, barrier, nil)
|
return wrapProcForSpawn(owner, result, n.typ, barrier, nil)
|
||||||
result = transformSpawnSons(owner, n, barrier)
|
result = transformSpawnSons(owner, n, barrier)
|
||||||
elif n.safeLen > 0:
|
elif n.safeLen > 0:
|
||||||
result = transformSpawnSons(owner, n, barrier)
|
result = transformSpawnSons(owner, n, barrier)
|
||||||
|
|
|
||||||
|
|
@ -85,25 +85,26 @@ type
|
||||||
cv: CondVar
|
cv: CondVar
|
||||||
idx: int
|
idx: int
|
||||||
|
|
||||||
RawPromise* = ptr RawPromiseObj ## untyped base class for 'Promise[T]'
|
RawPromise* = ref RawPromiseObj ## untyped base class for 'Promise[T]'
|
||||||
RawPromiseObj {.inheritable.} = object # \
|
RawPromiseObj = object of TObject
|
||||||
# we allocate this with the thread local allocator; this
|
|
||||||
# is possible since we already need to do the GC_unref
|
|
||||||
# on the owning thread
|
|
||||||
ready, usesCondVar: bool
|
ready, usesCondVar: bool
|
||||||
cv: CondVar #\
|
cv: CondVar #\
|
||||||
# for 'awaitAny' support
|
# for 'awaitAny' support
|
||||||
ai: ptr AwaitInfo
|
ai: ptr AwaitInfo
|
||||||
idx: int
|
idx: int
|
||||||
data: PObject # we incRef and unref it to keep it alive
|
data: pointer # we incRef and unref it to keep it alive
|
||||||
owner: ptr Worker
|
owner: pointer # ptr Worker
|
||||||
next: RawPromise
|
|
||||||
align: float64 # a float for proper alignment
|
|
||||||
|
|
||||||
Promise* {.compilerProc.} [T] = ptr object of RawPromiseObj
|
PromiseObj[T] = object of RawPromiseObj
|
||||||
blob: T ## the underlying value, if available. Note that usually
|
blob: T
|
||||||
## you should not access this field directly! However it can
|
|
||||||
## sometimes be more efficient than getting the value via ``^``.
|
Promise*{.compilerProc.}[T] = ref PromiseObj[T]
|
||||||
|
|
||||||
|
ToFreeQueue = object
|
||||||
|
len: int
|
||||||
|
lock: TLock
|
||||||
|
empty: TCond
|
||||||
|
data: array[512, pointer]
|
||||||
|
|
||||||
WorkerProc = proc (thread, args: pointer) {.nimcall, gcsafe.}
|
WorkerProc = proc (thread, args: pointer) {.nimcall, gcsafe.}
|
||||||
Worker = object
|
Worker = object
|
||||||
|
|
@ -115,37 +116,55 @@ 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
|
||||||
promiseLock: TLock
|
q: ToFreeQueue
|
||||||
head: RawPromise
|
|
||||||
|
|
||||||
proc finished*(prom: RawPromise) =
|
proc await*(prom: RawPromise) =
|
||||||
## This MUST be called for every created promise to free its associated
|
## waits until the value for the promise arrives. Usually it is not necessary
|
||||||
## resources. Note that the default reading operation ``^`` is destructive
|
## to call this explicitly.
|
||||||
## and calls ``finished``.
|
if prom.usesCondVar:
|
||||||
|
prom.usesCondVar = false
|
||||||
|
await(prom.cv)
|
||||||
|
destroyCondVar(prom.cv)
|
||||||
|
|
||||||
|
proc finished(prom: RawPromise) =
|
||||||
doAssert prom.ai.isNil, "promise is still attached to an 'awaitAny'"
|
doAssert prom.ai.isNil, "promise is still attached to an 'awaitAny'"
|
||||||
assert prom.next == nil
|
# we have to protect against the rare cases where the owner of the promise
|
||||||
let w = prom.owner
|
# simply disregards the promise and yet the "promiser" has not yet written
|
||||||
acquire(w.promiseLock)
|
# anything to it:
|
||||||
prom.next = w.head
|
await(prom)
|
||||||
w.head = prom
|
if prom.data.isNil: return
|
||||||
release(w.promiseLock)
|
let owner = cast[ptr Worker](prom.owner)
|
||||||
|
let q = addr(owner.q)
|
||||||
|
var waited = false
|
||||||
|
while true:
|
||||||
|
acquire(q.lock)
|
||||||
|
if q.len < q.data.len:
|
||||||
|
q.data[q.len] = prom.data
|
||||||
|
inc q.len
|
||||||
|
release(q.lock)
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
# the queue is exhausted! We block until it has been cleaned:
|
||||||
|
release(q.lock)
|
||||||
|
wait(q.empty, q.lock)
|
||||||
|
waited = true
|
||||||
|
prom.data = nil
|
||||||
|
# wakeup other potentially waiting threads:
|
||||||
|
if waited: signal(q.empty)
|
||||||
|
|
||||||
proc cleanPromises(w: ptr Worker) =
|
proc cleanPromises(w: ptr Worker) =
|
||||||
var it = w.head
|
let q = addr(w.q)
|
||||||
acquire(w.promiseLock)
|
acquire(q.lock)
|
||||||
while it != nil:
|
for i in 0 .. <q.len:
|
||||||
let nxt = it.next
|
GC_unref(cast[PObject](q.data[i]))
|
||||||
if it.usesCondVar: destroyCondVar(it.cv)
|
q.len = 0
|
||||||
if it.data != nil: GC_unref(it.data)
|
release(q.lock)
|
||||||
dealloc(it)
|
signal(q.empty)
|
||||||
it = nxt
|
|
||||||
w.head = nil
|
|
||||||
release(w.promiseLock)
|
|
||||||
|
|
||||||
proc nimCreatePromise(owner: pointer; blobSize: int): RawPromise {.
|
proc promFinalizer[T](prom: Promise[T]) = finished(prom)
|
||||||
compilerProc.} =
|
|
||||||
result = cast[RawPromise](alloc0(RawPromiseObj.sizeof + blobSize))
|
proc nimCreatePromise[T](): Promise[T] {.compilerProc.} =
|
||||||
result.owner = cast[ptr Worker](owner)
|
new(result, promFinalizer)
|
||||||
|
|
||||||
proc nimPromiseCreateCondVar(prom: RawPromise) {.compilerProc.} =
|
proc nimPromiseCreateCondVar(prom: RawPromise) {.compilerProc.} =
|
||||||
prom.cv = createCondVar()
|
prom.cv = createCondVar()
|
||||||
|
|
@ -160,16 +179,12 @@ proc nimPromiseSignal(prom: RawPromise) {.compilerProc.} =
|
||||||
signal(prom.ai.cv.c)
|
signal(prom.ai.cv.c)
|
||||||
if prom.usesCondVar: signal(prom.cv)
|
if prom.usesCondVar: signal(prom.cv)
|
||||||
|
|
||||||
proc await*[T](prom: Promise[T]) =
|
|
||||||
## waits until the value for the promise arrives.
|
|
||||||
if prom.usesCondVar: await(prom.cv)
|
|
||||||
|
|
||||||
proc awaitAndThen*[T](prom: Promise[T]; action: proc (x: T) {.closure.}) =
|
proc awaitAndThen*[T](prom: Promise[T]; action: proc (x: T) {.closure.}) =
|
||||||
## blocks until the ``prom`` is available and then passes its value
|
## blocks until the ``prom`` is available and then passes its value
|
||||||
## to ``action``. Note that due to Nimrod's parameter passing semantics this
|
## 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
|
## means that ``T`` doesn't need to be copied and so ``awaitAndThen`` can
|
||||||
## sometimes be more efficient than ``^``.
|
## sometimes be more efficient than ``^``.
|
||||||
if prom.usesCondVar: await(prom)
|
await(prom)
|
||||||
when T is string or T is seq:
|
when T is string or T is seq:
|
||||||
action(cast[T](prom.data))
|
action(cast[T](prom.data))
|
||||||
elif T is ref:
|
elif T is ref:
|
||||||
|
|
@ -179,23 +194,17 @@ proc awaitAndThen*[T](prom: Promise[T]; action: proc (x: T) {.closure.}) =
|
||||||
finished(prom)
|
finished(prom)
|
||||||
|
|
||||||
proc `^`*[T](prom: Promise[ref T]): foreign ptr T =
|
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.
|
||||||
## this reading is destructive for reasons of efficiency and convenience.
|
await(prom)
|
||||||
## This calls ``finished(prom)``.
|
|
||||||
if prom.usesCondVar: await(prom)
|
|
||||||
result = cast[foreign ptr T](prom.data)
|
result = cast[foreign ptr T](prom.data)
|
||||||
finished(prom)
|
|
||||||
|
|
||||||
proc `^`*[T](prom: Promise[T]): T =
|
proc `^`*[T](prom: Promise[T]): T =
|
||||||
## blocks until the value is available and then returns this value. Note
|
## blocks until the value is available and then returns this value.
|
||||||
## this reading is destructive for reasons of efficiency and convenience.
|
await(prom)
|
||||||
## This calls ``finished(prom)``.
|
|
||||||
if prom.usesCondVar: await(prom)
|
|
||||||
when T is string or T is seq:
|
when T is string or T is seq:
|
||||||
result = cast[T](prom.data)
|
result = cast[T](prom.data)
|
||||||
else:
|
else:
|
||||||
result = prom.blob
|
result = prom.blob
|
||||||
finished(prom)
|
|
||||||
|
|
||||||
proc awaitAny*(promises: openArray[RawPromise]): int =
|
proc awaitAny*(promises: openArray[RawPromise]): int =
|
||||||
# awaits any of the given promises. Returns the index of one promise for which
|
# awaits any of the given promises. Returns the index of one promise for which
|
||||||
|
|
@ -245,7 +254,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.cleanPromises
|
if w.q.len != 0: w.cleanPromises
|
||||||
if w.shutdown:
|
if w.shutdown:
|
||||||
w.shutdown = false
|
w.shutdown = false
|
||||||
atomicDec currentPoolSize
|
atomicDec currentPoolSize
|
||||||
|
|
@ -266,8 +275,9 @@ 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].promiseLock
|
|
||||||
workersData[i].initialized = true
|
workersData[i].initialized = true
|
||||||
|
initCond(workersData[i].q.empty)
|
||||||
|
initLock(workersData[i].q.lock)
|
||||||
createThread(workers[i], slave, addr(workersData[i]))
|
createThread(workers[i], slave, addr(workersData[i]))
|
||||||
|
|
||||||
proc setup() =
|
proc setup() =
|
||||||
|
|
|
||||||
|
|
@ -153,9 +153,11 @@ when (defined(gcc) or defined(llvm_gcc)) and hasThreadSupport:
|
||||||
## A value of 0 indicates typical alignment should be used. The compiler may also
|
## A value of 0 indicates typical alignment should be used. The compiler may also
|
||||||
## ignore this parameter.
|
## ignore this parameter.
|
||||||
|
|
||||||
|
template fence*() = atomicThreadFence(ATOMIC_SEQ_CST)
|
||||||
elif defined(vcc) and hasThreadSupport:
|
elif defined(vcc) and hasThreadSupport:
|
||||||
proc addAndFetch*(p: ptr int, val: int): int {.
|
proc addAndFetch*(p: ptr int, val: int): int {.
|
||||||
importc: "NimXadd", nodecl.}
|
importc: "NimXadd", nodecl.}
|
||||||
|
|
||||||
else:
|
else:
|
||||||
proc addAndFetch*(p: ptr int, val: int): int {.inline.} =
|
proc addAndFetch*(p: ptr int, val: int): int {.inline.} =
|
||||||
inc(p[], val)
|
inc(p[], val)
|
||||||
|
|
@ -231,4 +233,10 @@ elif false:
|
||||||
|
|
||||||
proc cpuRelax {.inline.} = os.sleep(1)
|
proc cpuRelax {.inline.} = os.sleep(1)
|
||||||
|
|
||||||
|
when not defined(fence) and hasThreadSupport:
|
||||||
|
# XXX fixme
|
||||||
|
proc fence*() {.inline.} =
|
||||||
|
var dummy: bool
|
||||||
|
discard cas(addr dummy, false, true)
|
||||||
|
|
||||||
{.pop.}
|
{.pop.}
|
||||||
|
|
|
||||||
14
todo.txt
14
todo.txt
|
|
@ -1,6 +1,20 @@
|
||||||
version 0.9.6
|
version 0.9.6
|
||||||
=============
|
=============
|
||||||
|
|
||||||
|
Concurrency
|
||||||
|
-----------
|
||||||
|
|
||||||
|
- document the new 'spawn' and 'parallel' statements
|
||||||
|
- implement 'deepCopy' builtin
|
||||||
|
- implement 'foo[1..4] = spawn(f[4..7])'
|
||||||
|
- the disjoint checker needs to deal with 'a = spawn f(); g = spawn f()'
|
||||||
|
- Minor: The copying of the 'ref Promise' into the thead local storage only
|
||||||
|
happens to work due to the write barrier's implementation
|
||||||
|
|
||||||
|
|
||||||
|
Misc
|
||||||
|
----
|
||||||
|
|
||||||
- fix the bug that keeps 'defer' template from working
|
- fix the bug that keeps 'defer' template from working
|
||||||
- make '--implicitStatic:on' the default
|
- make '--implicitStatic:on' the default
|
||||||
- fix the tuple unpacking in lambda bug
|
- fix the tuple unpacking in lambda bug
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue