Skip to content

Commit

Permalink
bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
nikandfor committed Jan 6, 2024
1 parent c4a7e8b commit 034eb06
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
25 changes: 18 additions & 7 deletions batch3.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ type (

cnt int // number of goroutines in a critical zone

res Res
err error
ready bool
res Res
err error

in, out bool
}

Batch[Res any] struct {
Expand Down Expand Up @@ -116,7 +117,7 @@ func (c *Controller[Res]) exit() int {
if c.cnt == 0 {
var zero Res
c.res, c.err = zero, nil
c.ready = false
c.in, c.out = false, false
}

c.mu.Unlock()
Expand All @@ -133,19 +134,22 @@ func (c *Controller[Res]) commit(ctx context.Context, err error) (Res, error) {
again:
if err != nil || atomic.LoadInt32(&c.queue) == 0 {
c.cnt = -c.cnt
c.ready = true
c.in = true

if ep, ok := err.(PanicError); ok {
c.out = true
c.err = err
panic(ep.Panic)
} else if err != nil {
c.out = true
c.err = err
} else {
func() {
var res Res
var err error

defer func() {
c.out = true
c.res, c.err = res, err

if p := recover(); p != nil {
Expand All @@ -162,7 +166,7 @@ again:
} else {
c.cond.Wait()

if !c.ready {
if !c.in {
goto again
}
}
Expand Down Expand Up @@ -260,8 +264,15 @@ func (b *Batch[Res]) Rollback(ctx context.Context, err error) (Res, error) {
return b.c.commit(ctx, err)
}

func AsPanicError(err error) (PanicError, bool) {
var pe PanicError

return pe, errors.As(err, &pe)
}

func (e PanicError) Error() string {
return fmt.Sprintf("panic: %v", e.Panic)
}

func (noCopy) Lock() {}
func (*noCopy) Lock() {}
func (*noCopy) Unlock() {}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module nikand.dev/go/batch

go 1.18

retract v0.4.0 // has a bug
5 changes: 4 additions & 1 deletion multi.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ func (c *Multi[Res]) Enter(blocking bool) (b Batch[Res], coach, index int) {
for coach := range c.cs {
b, idx := c.cs[coach].Enter(false)
if idx >= 0 {
return b, coach, idx
return Batch[Res]{
c: b.c,
state: b.state,
}, coach, idx
}
}

Expand Down

0 comments on commit 034eb06

Please sign in to comment.