aboutsummaryrefslogtreecommitdiff
path: root/stmutil/context.go
blob: 49f211c6dcd571afbfb0110643bb31b47a8d5c06 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package stmutil

import (
	"context"

	"github.com/lukechampine/stm"
)

func ContextDoneVar(ctx context.Context) (*stm.Var, func()) {
	ctx, cancel := context.WithCancel(ctx)
	_var := stm.NewVar(false)
	go func() {
		<-ctx.Done()
		stm.AtomicSet(_var, true)
	}()
	return _var, cancel
}