diff options
author | Araq <rumpf_a@web.de> | 2011-09-20 00:56:48 +0200 |
---|---|---|
committer | Araq <rumpf_a@web.de> | 2011-09-20 00:56:48 +0200 |
commit | fd62116f6eb80d1dd3d6cc745d80629ad32dca1a (patch) | |
tree | ac5cbd102ffa580e322eda22deeef9298babae4a /lib/pure | |
parent | dc3ace4f379931f2af4dd4a3cd2a0984a94865af (diff) | |
download | Nim-fd62116f6eb80d1dd3d6cc745d80629ad32dca1a.tar.gz |
bugfixes for generics; new threads implementation still broken
Diffstat (limited to 'lib/pure')
-rw-r--r-- | lib/pure/actors.nim | 45 | ||||
-rwxr-xr-x | lib/pure/parsecfg.nim | 1 |
2 files changed, 24 insertions, 22 deletions
diff --git a/lib/pure/actors.nim b/lib/pure/actors.nim index 285e3241d..4576cb602 100644 --- a/lib/pure/actors.nim +++ b/lib/pure/actors.nim @@ -30,7 +30,7 @@ proc spawn*[TIn, TOut](action: proc( self: PActor[TIn, TOut]){.thread.}): PActor[TIn, TOut] = ## creates an actor; that is a thread with an inbox. The caller MUST call ## ``join`` because that also frees the associated resources with the actor. - result = allocShared0(sizeof(result[])) + result = cast[PActor[TIn, TOut]](allocShared0(sizeof(result[]))) open(result.i) createThread(result.t, action, result) @@ -52,8 +52,8 @@ proc recv*[TIn, TOut](a: PActor[TIn, TOut]): TTask[TIn, TOut] = ## receives a task from `a`'s inbox. result = recv(a.i) -proc send*[TIn, TOut, X, Y](sender: PActor[X, Z], - receiver: PActor[TIn, TOut], msg: TIn) = +proc send*[TIn, TOut, X, Y](receiver: PActor[TIn, TOut], msg: TIn, + sender: PActor[X, Y]) = ## sends a message to `a`'s inbox. var t: TTask[TIn, TOut] t.receiver = addr(sender.i) @@ -99,9 +99,9 @@ proc poolWorker[TIn, TOut](self: PActor[TIn, TOut]) {.thread.} = var m = self.recv if m.shutDown: break when TOut is void: - action(m.data) + m.action(m.data) else: - self.repy(action(m.data)) + self.repy(m.action(m.data)) proc createActorPool*[TIn, TOut](a: var TActorPool[TIn, TOut], poolSize = 4) = ## creates an actor pool. @@ -109,21 +109,20 @@ proc createActorPool*[TIn, TOut](a: var TActorPool[TIn, TOut], poolSize = 4) = when TOut isnot void: open(a.outputs) for i in 0 .. < a.actors.len: - a.actors[i] = spawn(poolWorker) + a.actors[i] = spawn(poolWorker[TIn, TOut]) proc join*[TIn, TOut](a: var TActorPool[TIn, TOut]) = ## waits for each actor in the actor pool `a` to finish and frees the ## resources attached to `a`. var t: TTask[TIn, TOut] t.shutdown = true - for i in 0 .. < a.actors.len: send(a.actors[i], t) + for i in 0 .. < a.actors.len: send(a.actors[i].i, t) for i in 0 .. < a.actors.len: join(a.actors[i]) when TOut isnot void: close(a.outputs) a.actors = nil template setupTask = - var t: TTask[TIn, TOut] t.action = action shallowCopy(t.data, input) @@ -132,7 +131,7 @@ template schedule = # it remains 'hot' ;-). Round-robin hurts for keeping threads hot. for i in 0..high(a.actors): if a.actors[i].i.ready: - a.actors[i].send(t) + a.actors[i].i.send(t) return # no thread ready :-( --> send message to the thread which has the least # messages pending: @@ -142,27 +141,29 @@ template schedule = var curr = a.actors[i].i.peek if curr == 0: # ok, is ready now: - a.actors[i].send(t) + a.actors[i].i.send(t) return if curr < minVal: minVal = curr minIdx = i - a.actors[minIdx].send(t) + a.actors[minIdx].i.send(t) -proc spawn*[TIn, TOut](p: var TActorPool[TIn, TOut], - action: proc (input: TIn): TOut {.thread.}, - input: TIn): ptr TChannel[TOut] = - ## uses the actor pool to run `action` concurrently. `spawn` is guaranteed - ## to not block. +proc spawn*[TIn, TOut](p: var TActorPool[TIn, TOut], input: TIn, + action: proc (input: TIn): TOut {.thread.} + ): ptr TChannel[TOut] = + ## uses the actor pool to run ``action(input)`` concurrently. + ## `spawn` is guaranteed to not block. + var t: TTask[TIn, TOut] setupTask() result = addr(p.outputs) + t.receiver = result schedule() -proc spawn*[TIn](p: var TActorPool[TIn, void], - action: proc (input: TIn) {.thread.}, - input: TIn) = - ## uses the actor pool to run `action` concurrently. `spawn` is guaranteed - ## to not block. +proc spawn*[TIn](p: var TActorPool[TIn, void], input: TIn, + action: proc (input: TIn) {.thread.}) = + ## uses the actor pool to run ``action(input)`` concurrently. + ## `spawn` is guaranteed to not block. + var t: TTask[TIn, void] setupTask() schedule() @@ -171,7 +172,7 @@ when isMainModule: a: TActorPool[int, void] createActorPool(a) for i in 0 .. < 300: - a.spawn(proc (x: int) {.thread.} = echo x) + a.spawn(i, proc (x: int) {.thread.} = echo x) when false: proc treeDepth(n: PNode): int {.thread.} = diff --git a/lib/pure/parsecfg.nim b/lib/pure/parsecfg.nim index 67644e156..3e85a9ee6 100755 --- a/lib/pure/parsecfg.nim +++ b/lib/pure/parsecfg.nim @@ -323,6 +323,7 @@ proc getKeyValPair(c: var TCfgParser, kind: TCfgEventKind): TCfgEvent = if c.tok.kind == tkSymbol: result.value = c.tok.literal else: + reset result result.kind = cfgError result.msg = errorStr(c, "symbol expected, but found: " & c.tok.literal) rawGetTok(c, c.tok) |