summaryrefslogtreecommitdiff
path: root/typedsync/cond.go
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2023-02-05 13:02:57 -0700
committerLuke Shumaker <lukeshu@lukeshu.com>2023-02-05 13:33:02 -0700
commit5a1a904b4264c6ee323c9bd433f9ee4da93c984d (patch)
tree035414c8e8f3b94e24b482e5096a3b4e3688e257 /typedsync/cond.go
parent2d939c9c6e62395ed924fe7c5cd4c4b294e391a9 (diff)
typedsync: Bring up to being a mostly-drop-in replacement for Go 1.20 sync
Diffstat (limited to 'typedsync/cond.go')
-rw-r--r--typedsync/cond.go38
1 files changed, 38 insertions, 0 deletions
diff --git a/typedsync/cond.go b/typedsync/cond.go
new file mode 100644
index 0000000..00d3164
--- /dev/null
+++ b/typedsync/cond.go
@@ -0,0 +1,38 @@
+// Copyright (C) 2023 Luke Shumaker <lukeshu@lukeshu.com>
+//
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+package typedsync
+
+import (
+ "sync"
+)
+
+// Cond is a type-safe equivalent of the standard library's sync.Cond.
+//
+// See the [sync.Cond documentation] for full details.
+//
+// [sync.Cond documentation]: https://pkg.go.dev/sync#Cond
+type Cond[T Locker] struct {
+ L T
+ inner sync.Cond
+}
+
+func NewCond[T Locker](l T) *Cond[T] {
+ return &Cond[T]{L: l}
+}
+
+func (c *Cond[T]) Broadcast() {
+ c.inner.L = c.L
+ c.inner.Broadcast()
+}
+
+func (c *Cond[T]) Signal() {
+ c.inner.L = c.L
+ c.inner.Signal()
+}
+
+func (c *Cond[T]) Wait() {
+ c.inner.L = c.L
+ c.inner.Wait()
+}