From 52e150e44234b631f8a9b2de11862434046b4992 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Fri, 1 Nov 2019 12:23:59 +1100 Subject: Add stmutil containers and ContextDoneVar --- stmutil/context.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 stmutil/context.go (limited to 'stmutil/context.go') diff --git a/stmutil/context.go b/stmutil/context.go new file mode 100644 index 0000000..49f211c --- /dev/null +++ b/stmutil/context.go @@ -0,0 +1,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 +} -- cgit v1.2.3