Documentation
¶
Overview ¶
Package go-lock is a Golang library implementing an effcient read-write lock with the following built-in mechanism:
1) Mutex with timeout mechanism
2) Trylock
3) No-starve read-write solution
Index ¶
- type CASMutex
- func (m *CASMutex) Lock()
- func (m *CASMutex) RLock()
- func (m *CASMutex) RLocker() sync.Locker
- func (m *CASMutex) RTryLock() bool
- func (m *CASMutex) RTryLockWithContext(ctx context.Context) bool
- func (m *CASMutex) RTryLockWithTimeout(duration time.Duration) bool
- func (m *CASMutex) RUnlock()
- func (m *CASMutex) TryLock() bool
- func (m *CASMutex) TryLockWithContext(ctx context.Context) bool
- func (m *CASMutex) TryLockWithTimeout(duration time.Duration) bool
- func (m *CASMutex) Unlock()
- type ChanMutex
- type Mutex
- type RWMutex
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CASMutex ¶
type CASMutex struct {
// contains filtered or unexported fields
}
CASMutex is the struct implementing RWMutex with CAS mechanism.
Example ¶
// set RWMutex with CAS mechanism (CASMutex). var rwMut lock.RWMutex = lock.NewCASMutex() // set default value count := int32(0) // block here rwMut.Lock() go func() { time.Sleep(50 * time.Millisecond) fmt.Println("Now is", atomic.AddInt32(&count, 1)) // Now is 1 rwMut.Unlock() }() // waiting for previous goroutine releasing the lock, and locking it again rwMut.Lock() fmt.Println("Now is", atomic.AddInt32(&count, 2)) // Now is 3 // TryLock without blocking // Return false, because the lock is not released. fmt.Println("Return", rwMut.TryLock()) // RTryLockWithTimeout without blocking // Return false, because the lock is not released. fmt.Println("Return", rwMut.RTryLockWithTimeout(50*time.Millisecond)) // TryLockWithContext without blocking ctx, cancel := context.WithTimeout(context.TODO(), 50*time.Millisecond) defer cancel() // Return false, because the lock is not released. fmt.Println("Return", rwMut.TryLockWithContext(ctx)) // release the lock in the end. rwMut.Unlock()
Output: Now is 1 Now is 3 Return false Return false Return false
func (*CASMutex) Lock ¶
func (m *CASMutex) Lock()
Lock acquires the lock. If it is currently held by others, Lock will wait until it has a chance to acquire it.
func (*CASMutex) RLock ¶
func (m *CASMutex) RLock()
RLock acquires the read lock. If it is currently held by others writing, RLock will wait until it has a chance to acquire it.
func (*CASMutex) RLocker ¶ added in v1.1.0
RLocker returns a Locker interface that implements the Lock and Unlock methods by calling CASMutex.RLock and CASMutex.RUnlock.
func (*CASMutex) RTryLock ¶
RTryLock attempts to acquire the read lock without blocking. Return false if someone is writing it now.
func (*CASMutex) RTryLockWithContext ¶ added in v1.0.1
RTryLockWithContext attempts to acquire the read lock, blocking until resources are available or ctx is done (timeout or cancellation).
func (*CASMutex) RTryLockWithTimeout ¶
RTryLockWithTimeout attempts to acquire the read lock within a period of time. Return false if spending time is more than duration and no chance to acquire it.
func (*CASMutex) TryLock ¶
TryLock attempts to acquire the lock without blocking. Return false if someone is holding it now.
func (*CASMutex) TryLockWithContext ¶ added in v1.0.1
TryLockWithContext attempts to acquire the lock, blocking until resources are available or ctx is done (timeout or cancellation).
func (*CASMutex) TryLockWithTimeout ¶
TryLockWithTimeout attempts to acquire the lock within a period of time. Return false if spending time is more than duration and no chance to acquire it.
type ChanMutex ¶
type ChanMutex struct {
// contains filtered or unexported fields
}
ChanMutex is the struct implementing Mutex by channel.
func (*ChanMutex) Lock ¶
func (m *ChanMutex) Lock()
Lock acquires the lock. If it is currently held by others, Lock will wait until it has a chance to acquire it.
func (*ChanMutex) TryLock ¶
TryLock attempts to acquire the lock without blocking. Return false if someone is holding it now.
func (*ChanMutex) TryLockWithContext ¶ added in v1.0.1
TryLockWithContext attempts to acquire the lock, blocking until resources are available or ctx is done (timeout or cancellation).
func (*ChanMutex) TryLockWithTimeout ¶
TryLockWithTimeout attempts to acquire the lock within a period of time. Return false if spending time is more than duration and no chance to acquire it.
type Mutex ¶ added in v1.1.0
type Mutex interface { // Lock acquires the lock. // If it is currently held by others, Lock will wait until it has a chance to acquire it. Lock() // TryLock attempts to acquire the lock without blocking. // Return false if someone is holding it now. TryLock() bool // TryLockWithTimeout attempts to acquire the lock within a period of time. // Return false if spending time is more than duration and no chance to acquire it. TryLockWithTimeout(time.Duration) bool // TryLockWithContext attempts to acquire the lock, blocking until resources // are available or ctx is done (timeout or cancellation). TryLockWithContext(ctx context.Context) bool // Unlock releases the lock. Unlock() }
Mutex is a mutual exclusion lock, and must not be copied after first use.
type RWMutex ¶ added in v1.1.0
type RWMutex interface { Mutex // RLock acquires the read lock. // If it is currently held by others writing, RLock will wait until it has a chance to acquire it. RLock() // RTryLock attempts to acquire the read lock without blocking. // Return false if someone is writing it now. RTryLock() bool // RTryLockWithTimeout attempts to acquire the read lock within a period of time. // Return false if spending time is more than duration and no chance to acquire it. RTryLockWithTimeout(time.Duration) bool // RTryLockWithContext attempts to acquire the read lock, blocking until resources // are available or ctx is done (timeout or cancellation). RTryLockWithContext(ctx context.Context) bool // RUnlock releases the read lock. RUnlock() // RLocker returns a Locker interface that implements the Lock and Unlock methods // by calling rw.RLock and rw.RUnlock. RLocker() sync.Locker }
RWMutex is a reader/writer mutual exclusion lock. The lock can be held by an arbitrary number of readers or a single writer. It must not be copied after first use.