summaryrefslogtreecommitdiffhomepage
path: root/pkg/amutex
diff options
context:
space:
mode:
authorgVisor bot <gvisor-bot@google.com>2020-05-12 19:30:34 +0000
committergVisor bot <gvisor-bot@google.com>2020-05-12 19:30:34 +0000
commit71ad9a410874528f79c4cd04d7d4fc2d17107991 (patch)
tree72cd29ac358c62f4d5a156a547a1fbe4bee2b5f6 /pkg/amutex
parent8c219c77bd0b8a1460a99d7ac004b400a053b7c9 (diff)
parent8dd1d5b75a95100e747b1a88e9e557d5d2c30b64 (diff)
Merge release-20200422.0-72-g8dd1d5b (automated)
Diffstat (limited to 'pkg/amutex')
-rw-r--r--pkg/amutex/amutex.go17
1 files changed, 17 insertions, 0 deletions
diff --git a/pkg/amutex/amutex.go b/pkg/amutex/amutex.go
index 1c4fd1784..a078a31db 100644
--- a/pkg/amutex/amutex.go
+++ b/pkg/amutex/amutex.go
@@ -18,6 +18,8 @@ package amutex
import (
"sync/atomic"
+
+ "gvisor.dev/gvisor/pkg/syserror"
)
// Sleeper must be implemented by users of the abortable mutex to allow for
@@ -53,6 +55,21 @@ func (NoopSleeper) SleepFinish(success bool) {}
// Interrupted implements Sleeper.Interrupted.
func (NoopSleeper) Interrupted() bool { return false }
+// Block blocks until either receiving from ch succeeds (in which case it
+// returns nil) or sleeper is interrupted (in which case it returns
+// syserror.ErrInterrupted).
+func Block(sleeper Sleeper, ch <-chan struct{}) error {
+ cancel := sleeper.SleepStart()
+ select {
+ case <-ch:
+ sleeper.SleepFinish(true)
+ return nil
+ case <-cancel:
+ sleeper.SleepFinish(false)
+ return syserror.ErrInterrupted
+ }
+}
+
// AbortableMutex is an abortable mutex. It allows Lock() to be aborted while it
// waits to acquire the mutex.
type AbortableMutex struct {