xylock

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 1, 2022 License: MIT Imports: 3 Imported by: 4

README

Xybor founder Go Reference GitHub Repo stars GitHub top language GitHub go.mod Go version GitHub release (release name instead of tag name) Codacy Badge Codacy Badge Go Report

Introduction

Package xylock defines wrapper types of sync mutex, rwmutex, and semaphore.

Features

Xylock wrapper structs have fully methods of origin structs. For example, Lock is the wrapper struct of sync.Mutex, and it has the following methods:

func (l *Lock) Lock()
func (l *Lock) Unlock()

Methods of wrapper structs have an additional features, that is to do nothing if the receiver pointer is nil. This is helpful when lock is an optional development.

// These following commands will not cause a panic. They just do nothing.
var lock *xylock.Lock = nil
lock.Lock()
lock.Unlock()

Xylock structs allows to run a function in thread-safe area (with Lock and Unlock cover the function).

var lock = xylock.Lock{}
lock.LockFunc(func() {
    // thread-safe area
})

Thread-safe methods of Xylog structs with R as prefix will support to read data.

var foo int
var lock = xylock.Lock{}
var result = lock.RLockFunc(func() any {
    return foo
}).(int)

Example

func Example() {
	var x int
	var lock = xylock.Lock{}
	for i := 0; i < 10000; i++ {
		go lock.LockFunc(func() {
			x = x + 1
		})
	}

	time.Sleep(time.Millisecond)
	fmt.Println(lock.RLockFunc(func() any { return x }))

	// Output:
	// 10000
}

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

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

func (l *Lock) RLockFunc(f func() any) any

RLockFunc blocks to call a function until it completes, then returns the reading value.

func (*Lock) TryLock

func (l *Lock) TryLock() bool

TryLock tries to lock l 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 (*Lock) Unlock

func (l *Lock) Unlock()

Unlock unlocks l. It is a run-time error if l is not locked on entry to Unlock.

A locked Mutex is not associated with a particular goroutine. It is allowed for one goroutine to lock a Mutex and then arrange for another goroutine to unlock it.

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

func (rw *RWLock) RLockFunc(f func() any) any

RLockFunc blocks to call a function for reading until it completes, then returns reading value.

func (*RWLock) RLocker

func (rw *RWLock) RLocker() sync.Locker

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

func (rw *RWLock) RWLockFunc(f func() any) any

RWLockFunc blocks to call a function for reading and writing until it completes, then returns reading value.

func (*RWLock) TryLock

func (rw *RWLock) TryLock() bool

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

func (rw *RWLock) TryRLock() bool

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.

func (*RWLock) WLockFunc

func (rw *RWLock) WLockFunc(f func())

WLockFunc blocks to call a function for writing until it completes.

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

func NewSemaphore(n int64) *Semaphore

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) Acquire

func (s *Semaphore) Acquire(n int64)

Acquire is a shortcut of AcquireCtx with context.TODO().

func (*Semaphore) AcquireCtx

func (s *Semaphore) AcquireCtx(ctx context.Context, n int64)

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

func (s *Semaphore) AcquireFunc(n int64, f func())

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

func (s *Semaphore) RAcquireFunc(n int64, f func() any) any

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) Release

func (s *Semaphore) Release(n int64)

Release releases the semaphore with a weight of n.

func (*Semaphore) TryAcquire

func (s *Semaphore) TryAcquire(n int64) bool

TryAcquire acquires the semaphore with a weight of n without blocking. On success, returns true. On failure, returns false and leaves the semaphore unchanged.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL