summaryrefslogtreecommitdiff
path: root/vendor/github.com/pion/turn/v2/internal/client/trylock.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/pion/turn/v2/internal/client/trylock.go')
-rw-r--r--vendor/github.com/pion/turn/v2/internal/client/trylock.go24
1 files changed, 24 insertions, 0 deletions
diff --git a/vendor/github.com/pion/turn/v2/internal/client/trylock.go b/vendor/github.com/pion/turn/v2/internal/client/trylock.go
new file mode 100644
index 0000000..48e25a0
--- /dev/null
+++ b/vendor/github.com/pion/turn/v2/internal/client/trylock.go
@@ -0,0 +1,24 @@
+package client
+
+import (
+ "sync/atomic"
+)
+
+// TryLock implement the classic "try-lock" operation.
+type TryLock struct {
+ n int32
+}
+
+// Lock tries to lock the try-lock. If successful, it returns true.
+// Otherwise, it returns false immedidately.
+func (c *TryLock) Lock() error {
+ if !atomic.CompareAndSwapInt32(&c.n, 0, 1) {
+ return errDoubleLock
+ }
+ return nil
+}
+
+// Unlock unlocks the try-lock.
+func (c *TryLock) Unlock() {
+ atomic.StoreInt32(&c.n, 0)
+}