improved actors.sync

This commit is contained in:
Araq
2011-12-31 11:18:18 +01:00
parent 743182afd7
commit 92395568bb
2 changed files with 78 additions and 2 deletions

View File

@@ -119,15 +119,25 @@ proc createActorPool*[TIn, TOut](a: var TActorPool[TIn, TOut], poolSize = 4) =
proc sync*[TIn, TOut](a: var TActorPool[TIn, TOut], polling=50) =
## waits for every actor of `a` to finish with its work. Currently this is
## implemented as polling every `polling` ms. This will change in a later
## implemented as polling every `polling` ms and has a slight chance
## of failing since we check for every actor to be in `ready` state and not
## for messages still in ether. This will change in a later
## version, however.
var allReadyCount = 0
while true:
var wait = false
for i in 0..high(a.actors):
if not a.actors[i].i.ready:
wait = true
allReadyCount = 0
break
if not wait: break
if not wait:
# it's possible that some actor sent a message to some other actor but
# both appeared to be non-working as the message takes some time to
# arrive. We assume that this won't take longer than `polling` and
# simply attempt a second time and declare victory then. ;-)
inc allReadyCount
if allReadyCount > 1: break
sleep(polling)
proc terminate*[TIn, TOut](a: var TActorPool[TIn, TOut]) =

66
tests/compile/talias.nim Normal file
View File

@@ -0,0 +1,66 @@
# Test the alias analysis
type
TAnalysisResult* = enum
arNo, arMaybe, arYes
proc isPartOf*[S, T](a: S, b: T): TAnalysisResult {.
magic: "IsPartOf", noSideEffect.}
## not yet exported properly.
template compileTimeAssert(cond: expr) =
when not cond:
{.compile: "is false: " & astToStr(cond).}
template `<|` (a, b: expr) =
compileTimeAssert isPartOf(a, b) == arYes
template `!<|` (a, b: expr) =
compileTimeAssert isPartOf(a, b) == arNo
template `?<|` (a, b: expr) =
compileTimeAssert isPartOf(a, b) == arMaybe
type
TA = object
TC = object of TA
arr: array[0..3, int]
le, ri: ref TC
f: string
c: char
se: seq[TA]
proc p(param1, param2: TC): TC =
var
local: TC
plocal: ptr TC
plocal2: ptr TA
local.arr <| local
local.arr[0] <| local
local.arr[2] !<| local.arr[1]
plocal2[] ?<| local
param1 ?<| param2
local.arr[0] !<| param1
local.arr !<| param1
local.le[] ?<| param1
param1 !<| local.arr[0]
param1 !<| local.arr
param1 ?<| local.le[]
result !<| local
result <| result
var
a, b: int
x: TC
a <| a
a !<| b
discard p(x, x)