Documentation ¶
Overview ¶
Package xylock defines wrapper types of sync mutex, rwmutex, and semaphore.
Example ¶
package main import ( "fmt" "time" "github.com/xybor-x/xylock" ) func main() { var x int var lock = xylock.Lock{} for i := 0; i < 10000; i++ { go lock.LockFunc(func() { x = x + 1 }) } time.Sleep(100 * time.Millisecond) fmt.Println(lock.RLockFunc(func() any { return x })) }
Output: 10000
Index ¶
- type Lock
- type RWLock
- func (rw *RWLock) Lock()
- func (rw *RWLock) RLock()
- func (rw *RWLock) RLockFunc(f func() any) any
- func (rw *RWLock) RLocker() sync.Locker
- func (rw *RWLock) RUnlock()
- func (rw *RWLock) RWLockFunc(f func() any) any
- func (rw *RWLock) TryLock() bool
- func (rw *RWLock) TryRLock() bool
- func (rw *RWLock) Unlock()
- func (rw *RWLock) WLockFunc(f func())
- type Semaphore
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Lock ¶
type Lock struct {
// contains filtered or unexported fields
}
Lock is a wrapper struct of sync.Mutex. All methods of Lock support to do nothing if Lock pointer is nil.
func (*Lock) Lock ¶
func (l *Lock) Lock()
Lock locks l. If the lock is already in use, the calling goroutine blocks until the mutex is available.
func (*Lock) LockFunc ¶
func (l *Lock) LockFunc(f func())
LockFunc blocks to call a function until it completes.
func (*Lock) RLockFunc ¶
RLockFunc blocks to call a function until it completes, then returns the reading value.
type RWLock ¶
type RWLock struct {
// contains filtered or unexported fields
}
RWLock is a wrapper struct of sync.RWMutex. All methods of RWLock support to do nothing if RWLock pointer is nil.
func (*RWLock) Lock ¶
func (rw *RWLock) Lock()
Lock locks rw for writing. If the lock is already locked for reading or writing, Lock blocks until the lock is available.
func (*RWLock) RLock ¶
func (rw *RWLock) RLock()
RLock locks rw for reading.
It should not be used for recursive read locking; a blocked Lock call excludes new readers from acquiring the lock. See the documentation on the sync.RWMutex type.
func (*RWLock) RLockFunc ¶
RLockFunc blocks to call a function for reading until it completes, then returns reading value.
func (*RWLock) RLocker ¶
RLocker returns a Locker interface that implements the Lock and Unlock methods by calling rw.RLock and rw.RUnlock.
func (*RWLock) RUnlock ¶
func (rw *RWLock) RUnlock()
RUnlock undoes a single RLock call; it does not affect other simultaneous readers. It is a run-time error if rw is not locked for reading on entry to RUnlock.
func (*RWLock) RWLockFunc ¶
RWLockFunc blocks to call a function for reading and writing until it completes, then returns reading value.
func (*RWLock) TryLock ¶
TryLock tries to lock rw for writing and reports whether it succeeded.
Note that while correct uses of TryLock do exist, they are rare, and use of TryLock is often a sign of a deeper problem in a particular use of mutexes.
func (*RWLock) TryRLock ¶
TryRLock tries to lock rw for reading and reports whether it succeeded.
Note that while correct uses of TryRLock do exist, they are rare, and use of TryRLock is often a sign of a deeper problem in a particular use of mutexes.
func (*RWLock) Unlock ¶
func (rw *RWLock) Unlock()
Unlock unlocks rw for writing. It is a run-time error if rw is not locked for writing on entry to Unlock.
As with Mutexes, a locked RWMutex is not associated with a particular goroutine. One goroutine may RLock (Lock) a RWMutex and then arrange for another goroutine to RUnlock (Unlock) it.
type Semaphore ¶
type Semaphore struct {
// contains filtered or unexported fields
}
Semaphore is a wrapper of semaphore.Weighted. All methods of Semaphore support to do nothing if Semaphore pointer is nil.
func NewSemaphore ¶
NewSemaphore creates a new semaphore with the given maximum combined weight for concurrent access. All method of Semaphore support to do nothing if Semaphore pointer is nil.
func (*Semaphore) AcquireCtx ¶
AcquireCtx acquires the semaphore with a weight of n, blocking until resources are available or ctx is done. On success, returns nil. On failure, returns ctx.Err() and leaves the semaphore unchanged.
If ctx is already done, AcquireCtx may still succeed without blocking.
func (*Semaphore) AcquireFunc ¶
AcquireFunc acquires the semaphore with a weight of n and blocks to call a function until it completes, then releases with the same weight.
func (*Semaphore) RAcquireFunc ¶
RAcquireFunc acquires the semaphore with a weight of n and blocks to call a function for reading until it completes, then releases with the same weight and returns the reading value.
func (*Semaphore) TryAcquire ¶
TryAcquire acquires the semaphore with a weight of n without blocking. On success, returns true. On failure, returns false and leaves the semaphore unchanged.