README

PkgGoDev Go Report Card

RowLock

RowLock is a Go library. A row lock is a set of locks associated with rows. Instead of locking and unlocking globally, you only operate locks on a row level.

(Example code on pkg.go.dev)

License

BSD 3-Clause.

Expand ▾ Collapse ▴

Documentation

Overview

    Package rowlock provides an implementation of row lock.

    A row lock is a set of locks associated with rows. Instead of locking and unlocking globally, you only operate locks on a row level.

    RowLock provides optional RLock and RUnlock functions to use separated read and write locks. In order to take advantage of them, NewLocker function used in NewRowLock must returns an implementation of RWLocker (for example, RWMutexNewLocker returns a new sync.RWMutex). If the locker returned by NewLocker didn't implement RLocker function defined in RWLocker, RLock will work the same as Lock and RUnlock will work the same as Unlock.

    Example
    Output:
    
    key1 got lock after about 200ms
    key2 got lock after about 300ms
    key1 got lock after about 400ms
    key2 got lock after about 600ms
    

    Index

    Examples

    Constants

    This section is empty.

    Variables

    This section is empty.

    Functions

    func MutexNewLocker

    func MutexNewLocker() sync.Locker

      MutexNewLocker is a NewLocker using sync.Mutex.

      func RWMutexNewLocker

      func RWMutexNewLocker() sync.Locker

        RWMutexNewLocker is a NewLocker using sync.RWMutex.

        Example
        Output:
        
        key1 got lock after about 150ms
        key2 got lock after about 200ms
        key1 got read lock after about 300ms
        key1 got read lock after about 300ms
        key2 got read lock after about 400ms
        key2 got read lock after about 400ms
        key1 got lock after about 550ms
        key2 got lock after about 750ms
        

        Types

        type NewLocker

        type NewLocker func() sync.Locker

          NewLocker defines a type of function that can be used to create a new Locker.

          type RWLocker

          type RWLocker interface {
          	sync.Locker
          
          	RLocker() sync.Locker
          }

            RWLocker is the abstracted interface of sync.RWMutex.

            type Row

            type Row = defaultdict.Comparable

              Row is the type of a row.

              It must be comparable: https://golang.org/ref/spec#Comparison_operators.

              type RowLock

              type RowLock struct {
              	// contains filtered or unexported fields
              }

                RowLock defines a set of locks.

                When you do Lock/Unlock operations, you don't do them on a global scale. Instead, a Lock/Unlock operation is operated on a given row.

                If NewLocker returns an implementation of RWLocker in NewRowLock, the RowLock can be locked separately for read in RLock and RUnlock functions. Otherwise, RLock is the same as Lock and RUnlock is the same as Unlock.

                func NewRowLock

                func NewRowLock(f NewLocker) *RowLock

                  NewRowLock creates a new RowLock with the given NewLocker.

                  func (*RowLock) Lock

                  func (rl *RowLock) Lock(row Row)

                    Lock locks a row.

                    If this is a new row, a new locker will be created using the NewLocker specified in NewRowLock.

                    func (*RowLock) RLock

                    func (rl *RowLock) RLock(row Row)

                      RLock locks a row for read.

                      It only works as expected when NewLocker specified in NewRowLock returns an implementation of RWLocker. Otherwise, it's the same as Lock.

                      func (*RowLock) RUnlock

                      func (rl *RowLock) RUnlock(row Row)

                        RUnlock unlocks a row for read.

                        It only works as expected when NewLocker specified in NewRowLock returns an implementation of RWLocker. Otherwise, it's the same as Unlock.

                        func (*RowLock) Unlock

                        func (rl *RowLock) Unlock(row Row)

                          Unlock unlocks a row.

                          Source Files