File: trylock.go

package info (click to toggle)
golang-github-pion-turn.v2 2.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-backports, forky, sid, trixie
  • size: 716 kB
  • sloc: makefile: 4
file content (24 lines) | stat: -rw-r--r-- 456 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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 immediately.
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)
}