Skip to content

Commit

Permalink
Avoid the closure for now, until coroc can handle generics
Browse files Browse the repository at this point in the history
  • Loading branch information
chriso committed Nov 8, 2023
1 parent 2577572 commit 1de6a91
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions coroutine_durable.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ const Durable = true
//
//go:noinline
func New[R, S any](f func()) Coroutine[R, S] {
return NewWithReturn[R, S](func() (_ R) {
f()
return
})
// The function has the go:noinline tag because we want to ensure that the
// context will be allocated on the heap. If the context remains allocated
// on the stack it might escape when returned by a call to LoadContext that
// the compiler cannot track.
return Coroutine[R, S]{
ctx: &Context[R, S]{
context: context[R]{entry: f},
},
}
}

// New creates a new coroutine which executes f as entry point.
Expand All @@ -34,7 +39,7 @@ func NewWithReturn[R, S any](f func() R) Coroutine[R, S] {
// the compiler cannot track.
return Coroutine[R, S]{
ctx: &Context[R, S]{
context: context[R]{entry: f},
context: context[R]{entryR: f},
},
}
}
Expand Down Expand Up @@ -85,7 +90,8 @@ func (s *Stack) isTop() bool {
}

type serializedCoroutine[R any] struct {
entry func() R
entry func()
entryR func() R
stack Stack
resume bool
}
Expand Down Expand Up @@ -167,7 +173,11 @@ func (c Coroutine[R, S]) Next() (hasNext bool) {
}()

c.ctx.Stack.FP = -1
c.ctx.result = c.ctx.entry()
if c.ctx.entry != nil {
c.ctx.entry()
} else {
c.ctx.result = c.ctx.entryR()
}
})

return hasNext
Expand All @@ -177,7 +187,8 @@ type context[R any] struct {
// Entry point of the coroutine, this is captured so the associated
// generator can call into the coroutine to start or resume it at the
// last yield point.
entry func() R
entry func()
entryR func() R
Stack
}

Expand Down

0 comments on commit 1de6a91

Please sign in to comment.