diff options
author | EuAndreh <eu@euandre.org> | 2025-01-22 12:31:30 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2025-01-22 12:31:30 -0300 |
commit | 59d879ef4e654ce53c2450e000ffa435f06c2f0e (patch) | |
tree | 05ae996bf799b1e51f891a5586b3b72fa9bdfe3f /stmutil/context.go | |
parent | Setup Makefile build skeleton (diff) | |
download | stm-59d879ef4e654ce53c2450e000ffa435f06c2f0e.tar.gz stm-59d879ef4e654ce53c2450e000ffa435f06c2f0e.tar.xz |
Unify code into default repo format
Diffstat (limited to 'stmutil/context.go')
-rw-r--r-- | stmutil/context.go | 39 |
1 files changed, 0 insertions, 39 deletions
diff --git a/stmutil/context.go b/stmutil/context.go deleted file mode 100644 index 6f8ba9b..0000000 --- a/stmutil/context.go +++ /dev/null @@ -1,39 +0,0 @@ -package stmutil - -import ( - "context" - "sync" - - "github.com/anacrolix/stm" -) - -var ( - mu sync.Mutex - ctxVars = map[context.Context]*stm.Var[bool]{} -) - -// Returns an STM var that contains a bool equal to `ctx.Err != nil`, and a cancel function to be -// called when the user is no longer interested in the var. -func ContextDoneVar(ctx context.Context) (*stm.Var[bool], func()) { - mu.Lock() - defer mu.Unlock() - if v, ok := ctxVars[ctx]; ok { - return v, func() {} - } - if ctx.Err() != nil { - // TODO: What if we had read-only Vars? Then we could have a global one for this that we - // just reuse. - v := stm.NewBuiltinEqVar(true) - return v, func() {} - } - v := stm.NewVar(false) - go func() { - <-ctx.Done() - stm.AtomicSet(v, true) - mu.Lock() - delete(ctxVars, ctx) - mu.Unlock() - }() - ctxVars[ctx] = v - return v, func() {} -} |